Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,46 +36,38 @@ def encode_image_to_base64(image):
|
|
36 |
image.save(buffered, format="PNG")
|
37 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
38 |
|
39 |
-
|
40 |
def analyze_image(image):
|
41 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
42 |
base64_image = encode_image_to_base64(image)
|
43 |
|
44 |
-
# --- MINIMAL FIX START ---
|
45 |
-
# We build a Python list of dicts, then JSON-encode it:
|
46 |
-
prompt_list = [
|
47 |
-
{
|
48 |
-
"type": "text",
|
49 |
-
"text": """Your task is to determine if the image is surprising or not surprising.
|
50 |
-
if the image is surprising, determine which element, figure or object in the image is making the image surprising and write it only in one sentence with no more then 6 words, otherwise, write 'NA'.
|
51 |
-
Also rate how surprising the image is on a scale of 1-5, where 1 is not surprising at all and 5 is highly surprising.
|
52 |
-
Provide the response as a JSON with the following structure:
|
53 |
-
{
|
54 |
-
"label": "[surprising OR not surprising]",
|
55 |
-
"element": "[element]",
|
56 |
-
"rating": [1-5]
|
57 |
-
}"""
|
58 |
-
},
|
59 |
-
{
|
60 |
-
"type": "image_url",
|
61 |
-
"image_url": {
|
62 |
-
"url": f"data:image/jpeg;base64,{base64_image}"
|
63 |
-
}
|
64 |
-
}
|
65 |
-
]
|
66 |
-
|
67 |
-
prompt_json = json.dumps(prompt_list)
|
68 |
-
|
69 |
messages = [
|
70 |
{
|
71 |
"role": "user",
|
72 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
]
|
75 |
-
# --- MINIMAL FIX END ---
|
76 |
|
77 |
response = client.chat.completions.create(
|
78 |
-
model="gpt-4o-mini",
|
79 |
messages=messages,
|
80 |
max_tokens=100,
|
81 |
temperature=0.1,
|
@@ -167,7 +159,7 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
167 |
(box[0], box[1]),
|
168 |
box[2] - box[0],
|
169 |
box[3] - box[1],
|
170 |
-
linewidth=max(2, min(original_size) / 500),
|
171 |
edgecolor='red',
|
172 |
facecolor='none'
|
173 |
)
|
@@ -196,6 +188,7 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
196 |
|
197 |
plt.axis('off')
|
198 |
|
|
|
199 |
buf = io.BytesIO()
|
200 |
plt.savefig(buf,
|
201 |
format='png',
|
@@ -240,11 +233,7 @@ def process_and_analyze(image):
|
|
240 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
241 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
242 |
result_image = Image.open(result_buf)
|
243 |
-
analysis_text =
|
244 |
-
f"Label: {response_data['label']}\n"
|
245 |
-
f"Element: {response_data['element']}\n"
|
246 |
-
f"Rating: {response_data['rating']}/5"
|
247 |
-
)
|
248 |
return result_image, analysis_text
|
249 |
else:
|
250 |
return image, "Not Surprising"
|
@@ -253,6 +242,7 @@ def process_and_analyze(image):
|
|
253 |
return None, f"Error processing image: {str(e)}"
|
254 |
|
255 |
|
|
|
256 |
def create_interface():
|
257 |
with gr.Blocks() as demo:
|
258 |
gr.Markdown("# Image Surprise Analysis")
|
@@ -277,4 +267,4 @@ def create_interface():
|
|
277 |
|
278 |
if __name__ == "__main__":
|
279 |
demo = create_interface()
|
280 |
-
demo.launch()
|
|
|
36 |
image.save(buffered, format="PNG")
|
37 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
38 |
|
|
|
39 |
def analyze_image(image):
|
40 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
41 |
base64_image = encode_image_to_base64(image)
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
messages = [
|
44 |
{
|
45 |
"role": "user",
|
46 |
+
"content": [
|
47 |
+
{
|
48 |
+
"type": "text",
|
49 |
+
"text": """Your task is to determine if the image is surprising or not surprising.
|
50 |
+
if the image is surprising, determine which element, figure or object in the image is making the image surprising and write it only in one sentence with no more then 6 words, otherwise, write 'NA'.
|
51 |
+
Also rate how surprising the image is on a scale of 1-5, where 1 is not surprising at all and 5 is highly surprising.
|
52 |
+
Provide the response as a JSON with the following structure:
|
53 |
+
{
|
54 |
+
"label": "[surprising OR not surprising]",
|
55 |
+
"element": "[element]",
|
56 |
+
"rating": [1-5]
|
57 |
+
}"""
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"type": "image_url",
|
61 |
+
"image_url": {
|
62 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
63 |
+
}
|
64 |
+
}
|
65 |
+
]
|
66 |
}
|
67 |
]
|
|
|
68 |
|
69 |
response = client.chat.completions.create(
|
70 |
+
model="gpt-4o-mini",
|
71 |
messages=messages,
|
72 |
max_tokens=100,
|
73 |
temperature=0.1,
|
|
|
159 |
(box[0], box[1]),
|
160 |
box[2] - box[0],
|
161 |
box[3] - box[1],
|
162 |
+
linewidth=max(2, min(original_size) / 500), # Scale line width with image size
|
163 |
edgecolor='red',
|
164 |
facecolor='none'
|
165 |
)
|
|
|
188 |
|
189 |
plt.axis('off')
|
190 |
|
191 |
+
# Save with high DPI
|
192 |
buf = io.BytesIO()
|
193 |
plt.savefig(buf,
|
194 |
format='png',
|
|
|
233 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
234 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
235 |
result_image = Image.open(result_buf)
|
236 |
+
analysis_text = f"Label: {response_data['label']}\nElement: {response_data['element']}\nRating: {response_data['rating']}/5"
|
|
|
|
|
|
|
|
|
237 |
return result_image, analysis_text
|
238 |
else:
|
239 |
return image, "Not Surprising"
|
|
|
242 |
return None, f"Error processing image: {str(e)}"
|
243 |
|
244 |
|
245 |
+
# Create Gradio interface
|
246 |
def create_interface():
|
247 |
with gr.Blocks() as demo:
|
248 |
gr.Markdown("# Image Surprise Analysis")
|
|
|
267 |
|
268 |
if __name__ == "__main__":
|
269 |
demo = create_interface()
|
270 |
+
demo.launch()
|