reab5555 commited on
Commit
e674f2c
·
verified ·
1 Parent(s): 76cd7ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -28
app.py CHANGED
@@ -20,32 +20,45 @@ OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
20
 
21
 
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")
48
- return base64.b64encode(buffered.getvalue()).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def analyze_image(image):
51
  client = OpenAI(api_key=OPENAI_API_KEY)
@@ -244,14 +257,21 @@ def process_and_analyze(image):
244
  if image is None:
245
  return None, "Please upload an image first."
246
 
 
 
247
  if OPENAI_API_KEY is None:
248
  return None, "OpenAI API key not found in environment variables."
249
 
250
  try:
251
  # Convert the image to PIL format if needed
252
  if isinstance(image, tuple):
 
253
  if len(image) > 0 and image[0] is not None:
254
- image = Image.fromarray(image[0])
 
 
 
 
255
  else:
256
  return None, "Invalid image format provided"
257
  elif isinstance(image, np.ndarray):
@@ -259,15 +279,19 @@ def process_and_analyze(image):
259
  elif isinstance(image, str):
260
  image = Image.open(image)
261
 
 
 
262
  if not isinstance(image, Image.Image):
263
- return None, "Invalid image format"
264
 
265
  # Ensure image is in RGB mode
266
  if image.mode != 'RGB':
267
  image = image.convert('RGB')
268
 
269
  # Analyze image
 
270
  gpt_response = analyze_image(image)
 
271
 
272
  try:
273
  response_data = json.loads(gpt_response)
@@ -277,8 +301,11 @@ def process_and_analyze(image):
277
  if not all(key in response_data for key in ["label", "element", "rating"]):
278
  return None, "Error: Missing required fields in analysis response"
279
 
 
 
280
  if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
281
  try:
 
282
  result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
283
  result_image = Image.open(result_buf)
284
  analysis_text = (
@@ -288,6 +315,7 @@ def process_and_analyze(image):
288
  )
289
  return result_image, analysis_text
290
  except Exception as detection_error:
 
291
  return None, f"Error in image detection processing: {str(detection_error)}"
292
  else:
293
  return image, "Not Surprising"
@@ -296,10 +324,7 @@ def process_and_analyze(image):
296
  error_type = type(e).__name__
297
  error_msg = str(e)
298
  detailed_error = f"Error ({error_type}): {error_msg}"
299
-
300
- # Log the error (you might want to add proper logging)
301
- print(detailed_error)
302
-
303
  return None, f"Error processing image: {error_msg}"
304
 
305
 
 
20
 
21
 
22
  def encode_image_to_base64(image):
23
+ print(f"Encode image type: {type(image)}") # Debug print
 
 
 
 
 
 
 
 
 
24
 
25
+ try:
26
+ # If image is a tuple (as sometimes provided by Gradio), take the first element
27
+ if isinstance(image, tuple):
28
+ print(f"Image is tuple with length: {len(image)}") # Debug print
29
+ if len(image) > 0 and image[0] is not None:
30
+ if isinstance(image[0], np.ndarray):
31
+ image = Image.fromarray(image[0])
32
+ else:
33
+ image = image[0]
34
+ else:
35
+ raise ValueError("Invalid image tuple provided")
36
 
37
+ # If image is a numpy array, convert to PIL Image
38
+ if isinstance(image, np.ndarray):
39
+ image = Image.fromarray(image)
40
+
41
+ # If image is a path string, open it
42
+ elif isinstance(image, str):
43
+ image = Image.open(image)
44
 
45
+ print(f"Image type after conversion: {type(image)}") # Debug print
 
 
46
 
47
+ # Ensure image is in PIL Image format
48
+ if not isinstance(image, Image.Image):
49
+ raise ValueError(f"Input must be a PIL Image, numpy array, or valid image path. Got {type(image)}")
50
+
51
+ # Convert image to RGB if it's in RGBA mode
52
+ if image.mode == 'RGBA':
53
+ image = image.convert('RGB')
54
+
55
+ buffered = io.BytesIO()
56
+ image.save(buffered, format="PNG")
57
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
58
+ except Exception as e:
59
+ print(f"Encode error details: {str(e)}") # Debug print
60
+ raise
61
+
62
 
63
  def analyze_image(image):
64
  client = OpenAI(api_key=OPENAI_API_KEY)
 
257
  if image is None:
258
  return None, "Please upload an image first."
259
 
260
+ print(f"Initial image type: {type(image)}") # Debug print
261
+
262
  if OPENAI_API_KEY is None:
263
  return None, "OpenAI API key not found in environment variables."
264
 
265
  try:
266
  # Convert the image to PIL format if needed
267
  if isinstance(image, tuple):
268
+ print(f"Image is tuple, length: {len(image)}") # Debug print
269
  if len(image) > 0 and image[0] is not None:
270
+ if isinstance(image[0], np.ndarray):
271
+ image = Image.fromarray(image[0])
272
+ else:
273
+ print(f"First element type: {type(image[0])}") # Debug print
274
+ image = image[0]
275
  else:
276
  return None, "Invalid image format provided"
277
  elif isinstance(image, np.ndarray):
 
279
  elif isinstance(image, str):
280
  image = Image.open(image)
281
 
282
+ print(f"Image type after conversion: {type(image)}") # Debug print
283
+
284
  if not isinstance(image, Image.Image):
285
+ return None, f"Invalid image format: {type(image)}"
286
 
287
  # Ensure image is in RGB mode
288
  if image.mode != 'RGB':
289
  image = image.convert('RGB')
290
 
291
  # Analyze image
292
+ print("Starting GPT analysis...") # Debug print
293
  gpt_response = analyze_image(image)
294
+ print(f"GPT response: {gpt_response}") # Debug print
295
 
296
  try:
297
  response_data = json.loads(gpt_response)
 
301
  if not all(key in response_data for key in ["label", "element", "rating"]):
302
  return None, "Error: Missing required fields in analysis response"
303
 
304
+ print(f"Response data: {response_data}") # Debug print
305
+
306
  if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
307
  try:
308
+ print("Starting image detection...") # Debug print
309
  result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
310
  result_image = Image.open(result_buf)
311
  analysis_text = (
 
315
  )
316
  return result_image, analysis_text
317
  except Exception as detection_error:
318
+ print(f"Detection error details: {str(detection_error)}") # Debug print
319
  return None, f"Error in image detection processing: {str(detection_error)}"
320
  else:
321
  return image, "Not Surprising"
 
324
  error_type = type(e).__name__
325
  error_msg = str(e)
326
  detailed_error = f"Error ({error_type}): {error_msg}"
327
+ print(detailed_error) # Debug print
 
 
 
328
  return None, f"Error processing image: {error_msg}"
329
 
330