Risalat commited on
Commit
211e266
·
verified ·
1 Parent(s): d7c374a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -41,38 +41,28 @@ from PIL import Image
41
  def image_generator(arg1: str) -> Image.Image:
42
  """
43
  A tool that generates an image based on a text prompt.
44
-
45
  Args:
46
  arg1: A description or prompt for the image to be generated.
47
-
48
  Returns:
49
- A PIL.Image object which Gradio can display.
50
  """
51
  try:
52
  result = image_generation_tool(prompt=arg1)
53
 
54
- # If result is a PIL image, return it directly
55
  if isinstance(result, Image.Image):
56
- return result
57
-
58
- # If result is a URL string
59
- elif isinstance(result, str) and result.startswith("http"):
60
- response = requests.get(result)
61
- image = Image.open(BytesIO(response.content))
62
- return image
63
-
64
- # If result is a dictionary with an image URL
65
  elif isinstance(result, dict) and "image_url" in result:
66
- image_url = result["image_url"]
67
- response = requests.get(image_url)
68
- image = Image.open(BytesIO(response.content))
69
- return image
70
-
71
- return Image.new("RGB", (256, 256), color="gray") # fallback dummy image
72
-
73
  except Exception as e:
74
- # Show a red dummy image with error in metadata or raise it
75
- return Image.new("RGB", (256, 256), color="red")
76
 
77
 
78
 
 
41
  def image_generator(arg1: str) -> Image.Image:
42
  """
43
  A tool that generates an image based on a text prompt.
44
+
45
  Args:
46
  arg1: A description or prompt for the image to be generated.
47
+
48
  Returns:
49
+ A PIL.Image object which Gradio and final_answer can display properly.
50
  """
51
  try:
52
  result = image_generation_tool(prompt=arg1)
53
 
54
+ # If result is a PIL image, return a fully loaded version
55
  if isinstance(result, Image.Image):
56
+ return result.convert("RGB") # Ensures it's fully loaded and in a common format
57
+ elif isinstance(result, str):
58
+ raise ValueError("Expected an image, but got a string URL. Cannot pass this to final_answer.")
 
 
 
 
 
 
59
  elif isinstance(result, dict) and "image_url" in result:
60
+ raise ValueError("Expected an image, but got a dict with 'image_url'. Cannot pass this to final_answer.")
61
+ else:
62
+ raise ValueError(f"Unexpected result type: {type(result)}")
 
 
 
 
63
  except Exception as e:
64
+ # Raise errors to let the agent or UI handle them
65
+ raise RuntimeError(f"Error generating image from prompt '{arg1}': {str(e)}")
66
 
67
 
68