Risalat commited on
Commit
86b7ac8
·
verified ·
1 Parent(s): 6d087c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -36,7 +36,8 @@ from smolagents import tool
36
  # Import tool from Hub
37
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
38
  @tool
39
- def image_generator(arg1: str) -> str:
 
40
  """
41
  A tool that generates an image based on a text prompt.
42
 
@@ -44,20 +45,23 @@ def image_generator(arg1: str) -> str:
44
  arg1: A description or prompt for the image to be generated.
45
 
46
  Returns:
47
- A string containing the result or URL of the generated image.
48
  """
49
  try:
50
  result = image_generation_tool(prompt=arg1)
51
 
52
- # Handle different possible return types from the tool
53
- if isinstance(result, str):
54
- return f"Generated image: {result}"
 
 
55
  elif isinstance(result, dict) and "image_url" in result:
56
  return f"Generated image URL: {result['image_url']}"
57
  else:
58
- return f"Image generated successfully: {result}"
59
  except Exception as e:
60
- return f"Error generating image from prompt '{arg1}': {str(e)}"
 
61
 
62
 
63
  @tool
 
36
  # Import tool from Hub
37
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
38
  @tool
39
+ @tool
40
+ def image_generator(arg1: str):
41
  """
42
  A tool that generates an image based on a text prompt.
43
 
 
45
  arg1: A description or prompt for the image to be generated.
46
 
47
  Returns:
48
+ A PIL.Image object which Gradio can display.
49
  """
50
  try:
51
  result = image_generation_tool(prompt=arg1)
52
 
53
+ # If result is a PIL image, return it directly
54
+ if hasattr(result, 'show') and callable(result.show):
55
+ return result # Gradio will display this automatically
56
+ elif isinstance(result, str):
57
+ return f"Generated image URL: {result}"
58
  elif isinstance(result, dict) and "image_url" in result:
59
  return f"Generated image URL: {result['image_url']}"
60
  else:
61
+ return f"Image generated: {result}"
62
  except Exception as e:
63
+ return f"Error generating image: {str(e)}"
64
+
65
 
66
 
67
  @tool