Manavraj commited on
Commit
56ba493
·
verified ·
1 Parent(s): f553381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
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
- # Call the original tool, which returns a PIL Image
97
  image = original_image_generation_tool(prompt)
98
 
99
- # Save image to disk
100
- filename = f"generated_image_{hash(prompt)}.png"
101
  image.save(filename)
102
 
103
- # Convert to base64 markdown
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 from prompt: '{prompt}'\nSaved as: {filename}\n\n![Generated Image](data:image/png;base64,{img_base64})"
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![Generated Image](data:image/png;base64,{img_base64})"
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: