Zaherrr commited on
Commit
726a002
·
verified ·
1 Parent(s): 57beb6a

Update models/openai_image_to_json.py

Browse files
Files changed (1) hide show
  1. models/openai_image_to_json.py +80 -74
models/openai_image_to_json.py CHANGED
@@ -6,90 +6,96 @@ import os
6
  from dotenv import load_dotenv
7
  import json
8
 
9
- # Load the .env file
10
- load_dotenv()
11
 
12
- # Get the API key from the environment
13
- api_key = os.getenv('OPENAI_API_KEY')
14
 
15
- # Function to encode the image
16
- def encode_image(image):
17
- # Convert the image to RGB if it has an alpha channel
18
- if image.mode == 'RGBA':
19
- image = image.convert('RGB')
 
 
 
 
 
 
20
 
21
- buffered = BytesIO()
22
- image.save(buffered, format="JPEG")
23
- return base64.b64encode(buffered.getvalue()).decode('utf-8')
24
 
25
- def openaiprocess_image_to_json(image):
26
 
27
- print(f'fetching openai response')
28
 
29
- # Encode the image
30
- base64_image = encode_image(image)
31
 
32
- headers = {
33
- "Content-Type": "application/json",
34
- "Authorization": f"Bearer {api_key}"
35
- }
36
 
37
- PROMPT = '''
38
- You are responsible for extracting the entities (nodes) and relationships (edges) from the images of mind maps. The mind maps are for Object Oriented Programming.
39
- Don't make up facts, just extracts them. Do not create new entity types that aren't mentioned in the image, and at the same time don't miss anything.
40
- Give the output in JSON format as follows:
41
- {
42
- "nodes": [
43
- {"id": "1", "label": string},
44
- {"id": "2", "label": string},...
45
- ],
46
- "edges": [
47
- {"source": SOURCE_ID, "target": TARGET_ID, "type": "->"},
48
- {"source": SOURCE_ID, "target": TARGET_ID, "type": "->"},...
49
- ]
50
- }
51
- Only return valid python dictionary, dont include (line jump)n in it, dont include spaces, only a dictionary. Do not include any other text outside the Dictionary structure. Make sure that i will get a valid Python dictionary.
52
- make sure that what you return as json_string i can use it in python in this function: json.loads(json_string)
53
- Now extract the entities and relationships from this image:
54
- '''
55
 
56
- payload = {
57
- "model": "gpt-4o",
58
- "messages": [
59
- {
60
- "role": "user",
61
- "content": [
62
- {
63
- "type": "text",
64
- "text": PROMPT
65
- },
66
- {
67
- "type": "image_url",
68
- "image_url": {
69
- "url": f"data:image/jpeg;base64,{base64_image}"
70
- }
71
- }
72
- ]
73
- }
74
- ]
75
- }
76
 
77
- # Send the request to the OpenAI API
78
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
79
 
80
- # Parse the response
81
- response_data = response.json()
82
- print(response_data)
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- # Extract the JSON graph data from the response
85
- if "choices" in response_data and response_data["choices"]:
86
- content = response_data["choices"][0]["message"]["content"]
87
- try:
88
- graph_data = content
89
- except json.JSONDecodeError as e:
90
- print("Failed:", e)
91
- graph_data = None
92
- else:
93
- raise ValueError("No valid response from OpenAI API")
94
 
95
- return graph_data
 
6
  from dotenv import load_dotenv
7
  import json
8
 
9
+ # Using Gemini instead of OpenAI for this deployment only
10
+ from gemini_image_to_json import fetch_gemini_response
11
 
12
+ openaiprocess_image_to_json = fetch_gemini_response
 
13
 
14
+ # # Load the .env file
15
+ # load_dotenv()
16
+
17
+ # # Get the API key from the environment
18
+ # api_key = os.getenv('OPENAI_API_KEY')
19
+
20
+ # # Function to encode the image
21
+ # def encode_image(image):
22
+ # # Convert the image to RGB if it has an alpha channel
23
+ # if image.mode == 'RGBA':
24
+ # image = image.convert('RGB')
25
 
26
+ # buffered = BytesIO()
27
+ # image.save(buffered, format="JPEG")
28
+ # return base64.b64encode(buffered.getvalue()).decode('utf-8')
29
 
30
+ # def openaiprocess_image_to_json(image):
31
 
32
+ # print(f'fetching openai response')
33
 
34
+ # # Encode the image
35
+ # base64_image = encode_image(image)
36
 
37
+ # headers = {
38
+ # "Content-Type": "application/json",
39
+ # "Authorization": f"Bearer {api_key}"
40
+ # }
41
 
42
+ # PROMPT = '''
43
+ # You are responsible for extracting the entities (nodes) and relationships (edges) from the images of mind maps. The mind maps are for Object Oriented Programming.
44
+ # Don't make up facts, just extracts them. Do not create new entity types that aren't mentioned in the image, and at the same time don't miss anything.
45
+ # Give the output in JSON format as follows:
46
+ # {
47
+ # "nodes": [
48
+ # {"id": "1", "label": string},
49
+ # {"id": "2", "label": string},...
50
+ # ],
51
+ # "edges": [
52
+ # {"source": SOURCE_ID, "target": TARGET_ID, "type": "->"},
53
+ # {"source": SOURCE_ID, "target": TARGET_ID, "type": "->"},...
54
+ # ]
55
+ # }
56
+ # Only return valid python dictionary, dont include (line jump)n in it, dont include spaces, only a dictionary. Do not include any other text outside the Dictionary structure. Make sure that i will get a valid Python dictionary.
57
+ # make sure that what you return as json_string i can use it in python in this function: json.loads(json_string)
58
+ # Now extract the entities and relationships from this image:
59
+ # '''
60
 
61
+ # payload = {
62
+ # "model": "gpt-4o",
63
+ # "messages": [
64
+ # {
65
+ # "role": "user",
66
+ # "content": [
67
+ # {
68
+ # "type": "text",
69
+ # "text": PROMPT
70
+ # },
71
+ # {
72
+ # "type": "image_url",
73
+ # "image_url": {
74
+ # "url": f"data:image/jpeg;base64,{base64_image}"
75
+ # }
76
+ # }
77
+ # ]
78
+ # }
79
+ # ]
80
+ # }
81
 
82
+ # # Send the request to the OpenAI API
83
+ # response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
84
 
85
+ # # Parse the response
86
+ # response_data = response.json()
87
+ # print(response_data)
88
+
89
+ # # Extract the JSON graph data from the response
90
+ # if "choices" in response_data and response_data["choices"]:
91
+ # content = response_data["choices"][0]["message"]["content"]
92
+ # try:
93
+ # graph_data = content
94
+ # except json.JSONDecodeError as e:
95
+ # print("Failed:", e)
96
+ # graph_data = None
97
+ # else:
98
+ # raise ValueError("No valid response from OpenAI API")
99
 
100
+ # return graph_data
 
 
 
 
 
 
 
 
 
101