Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -89,28 +89,30 @@ original_image_generation_tool = load_tool("agents-course/text-to-image", trust_
|
|
89 |
@tool
|
90 |
def image_generation_tool(prompt: str) -> str:
|
91 |
"""Generates an image from a prompt and embeds it as base64 in markdown.
|
|
|
92 |
Args:
|
93 |
prompt: The text prompt to generate an image from.
|
94 |
"""
|
95 |
try:
|
96 |
-
#
|
97 |
image = original_image_generation_tool(prompt)
|
98 |
|
99 |
-
# Save image to
|
100 |
-
filename = f"generated_image_{hash(prompt)}.png"
|
101 |
image.save(filename)
|
102 |
|
103 |
-
# Convert to base64
|
104 |
with open(filename, "rb") as f:
|
105 |
img_bytes = f.read()
|
106 |
img_base64 = base64.b64encode(img_bytes).decode("utf-8")
|
107 |
|
108 |
-
return f"Image generated
|
109 |
-
|
110 |
except Exception as e:
|
111 |
return f"Error generating image: {str(e)}"
|
112 |
|
113 |
|
|
|
114 |
search_tool = DuckDuckGoSearchTool()
|
115 |
|
116 |
with open("prompts.yaml", 'r') as stream:
|
|
|
89 |
@tool
|
90 |
def image_generation_tool(prompt: str) -> str:
|
91 |
"""Generates an image from a prompt and embeds it as base64 in markdown.
|
92 |
+
|
93 |
Args:
|
94 |
prompt: The text prompt to generate an image from.
|
95 |
"""
|
96 |
try:
|
97 |
+
# Run the tool and get back a PIL Image object
|
98 |
image = original_image_generation_tool(prompt)
|
99 |
|
100 |
+
# Save the image to a file
|
101 |
+
filename = f"generated_image_{abs(hash(prompt))}.png"
|
102 |
image.save(filename)
|
103 |
|
104 |
+
# Convert image to base64 for embedding
|
105 |
with open(filename, "rb") as f:
|
106 |
img_bytes = f.read()
|
107 |
img_base64 = base64.b64encode(img_bytes).decode("utf-8")
|
108 |
|
109 |
+
return f"Image generated for prompt: '{prompt}'\nSaved as: {filename}\n\n"
|
110 |
+
|
111 |
except Exception as e:
|
112 |
return f"Error generating image: {str(e)}"
|
113 |
|
114 |
|
115 |
+
|
116 |
search_tool = DuckDuckGoSearchTool()
|
117 |
|
118 |
with open("prompts.yaml", 'r') as stream:
|