Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,15 +22,26 @@ OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
22 |
def encode_image_to_base64(image):
|
23 |
# If image is a tuple (as sometimes provided by Gradio), take the first element
|
24 |
if isinstance(image, tuple):
|
25 |
-
image
|
|
|
|
|
|
|
26 |
|
27 |
# If image is a numpy array, convert to PIL Image
|
28 |
if isinstance(image, np.ndarray):
|
29 |
image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Ensure image is in PIL Image format
|
32 |
if not isinstance(image, Image.Image):
|
33 |
-
raise ValueError("Input must be a PIL Image, numpy array, or
|
|
|
|
|
|
|
|
|
34 |
|
35 |
buffered = io.BytesIO()
|
36 |
image.save(buffered, format="PNG")
|
@@ -218,32 +229,59 @@ def process_and_analyze(image):
|
|
218 |
return None, "OpenAI API key not found in environment variables."
|
219 |
|
220 |
try:
|
221 |
-
#
|
222 |
if isinstance(image, tuple):
|
223 |
-
image
|
224 |
-
|
|
|
|
|
|
|
225 |
image = Image.fromarray(image)
|
|
|
|
|
|
|
226 |
if not isinstance(image, Image.Image):
|
227 |
-
|
228 |
|
229 |
-
#
|
230 |
-
|
|
|
231 |
|
232 |
# Analyze image
|
233 |
gpt_response = analyze_image(image)
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
else:
|
242 |
return image, "Not Surprising"
|
243 |
|
244 |
except Exception as e:
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
|
248 |
|
249 |
# Create Gradio interface
|
|
|
22 |
def encode_image_to_base64(image):
|
23 |
# If image is a tuple (as sometimes provided by Gradio), take the first element
|
24 |
if isinstance(image, tuple):
|
25 |
+
if len(image) > 0 and image[0] is not None:
|
26 |
+
image = image[0]
|
27 |
+
else:
|
28 |
+
raise ValueError("Invalid image tuple provided")
|
29 |
|
30 |
# If image is a numpy array, convert to PIL Image
|
31 |
if isinstance(image, np.ndarray):
|
32 |
image = Image.fromarray(image)
|
33 |
+
|
34 |
+
# If image is a path string, open it
|
35 |
+
elif isinstance(image, str):
|
36 |
+
image = Image.open(image)
|
37 |
|
38 |
# Ensure image is in PIL Image format
|
39 |
if not isinstance(image, Image.Image):
|
40 |
+
raise ValueError("Input must be a PIL Image, numpy array, or valid image path")
|
41 |
+
|
42 |
+
# Convert image to RGB if it's in RGBA mode
|
43 |
+
if image.mode == 'RGBA':
|
44 |
+
image = image.convert('RGB')
|
45 |
|
46 |
buffered = io.BytesIO()
|
47 |
image.save(buffered, format="PNG")
|
|
|
229 |
return None, "OpenAI API key not found in environment variables."
|
230 |
|
231 |
try:
|
232 |
+
# Convert the image to PIL format if needed
|
233 |
if isinstance(image, tuple):
|
234 |
+
if len(image) > 0 and image[0] is not None:
|
235 |
+
image = Image.fromarray(image[0])
|
236 |
+
else:
|
237 |
+
return None, "Invalid image format provided"
|
238 |
+
elif isinstance(image, np.ndarray):
|
239 |
image = Image.fromarray(image)
|
240 |
+
elif isinstance(image, str):
|
241 |
+
image = Image.open(image)
|
242 |
+
|
243 |
if not isinstance(image, Image.Image):
|
244 |
+
return None, "Invalid image format"
|
245 |
|
246 |
+
# Ensure image is in RGB mode
|
247 |
+
if image.mode != 'RGB':
|
248 |
+
image = image.convert('RGB')
|
249 |
|
250 |
# Analyze image
|
251 |
gpt_response = analyze_image(image)
|
252 |
+
|
253 |
+
try:
|
254 |
+
response_data = json.loads(gpt_response)
|
255 |
+
except json.JSONDecodeError:
|
256 |
+
return None, "Error: Invalid response format from GPT"
|
257 |
+
|
258 |
+
if not all(key in response_data for key in ["label", "element", "rating"]):
|
259 |
+
return None, "Error: Missing required fields in analysis response"
|
260 |
|
261 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
262 |
+
try:
|
263 |
+
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
264 |
+
result_image = Image.open(result_buf)
|
265 |
+
analysis_text = (
|
266 |
+
f"Label: {response_data['label']}\n"
|
267 |
+
f"Element: {response_data['element']}\n"
|
268 |
+
f"Rating: {response_data['rating']}/5"
|
269 |
+
)
|
270 |
+
return result_image, analysis_text
|
271 |
+
except Exception as detection_error:
|
272 |
+
return None, f"Error in image detection processing: {str(detection_error)}"
|
273 |
else:
|
274 |
return image, "Not Surprising"
|
275 |
|
276 |
except Exception as e:
|
277 |
+
error_type = type(e).__name__
|
278 |
+
error_msg = str(e)
|
279 |
+
detailed_error = f"Error ({error_type}): {error_msg}"
|
280 |
+
|
281 |
+
# Log the error (you might want to add proper logging)
|
282 |
+
print(detailed_error)
|
283 |
+
|
284 |
+
return None, f"Error processing image: {error_msg}"
|
285 |
|
286 |
|
287 |
# Create Gradio interface
|