Spaces:
Sleeping
Sleeping
Update models/openai_image_to_json.py
Browse files- 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 |
-
#
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
api_key = os.getenv('OPENAI_API_KEY')
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
def openaiprocess_image_to_json(image):
|
26 |
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
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 |
|
|