smokxy commited on
Commit
0b50b5d
·
1 Parent(s): fdca52f
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -11,25 +11,37 @@ from Gradio_UI import GradioUI
11
 
12
 
13
  @tool
14
- def generate_image(prompt: str, style: str) -> str:
15
  """A tool that generates an image based on a prompt and a style.
16
  Args:
17
  prompt: the prompt to generate the image
18
- style: the style of the image
 
 
19
  """
20
  try:
21
  # Add style to the prompt if specified
22
  full_prompt = f"{prompt}, {style} style" if style else prompt
23
 
24
- # Generate the image
25
- image = image_generation_tool(full_prompt)
 
26
 
27
- # Convert the PIL image to AgentImage
 
 
 
 
 
 
 
 
 
 
 
 
28
  from smolagents.agent_types import AgentImage
29
- agent_image = AgentImage(image)
30
-
31
- # Return the AgentImage object
32
- return agent_image
33
  except Exception as e:
34
  return f"Error generating image: {str(e)}"
35
 
 
11
 
12
 
13
  @tool
14
+ def generate_image(prompt: str, style: str = "") -> str:
15
  """A tool that generates an image based on a prompt and a style.
16
  Args:
17
  prompt: the prompt to generate the image
18
+ style: the style of the image (optional)
19
+ Returns:
20
+ An AgentImage object containing the generated image
21
  """
22
  try:
23
  # Add style to the prompt if specified
24
  full_prompt = f"{prompt}, {style} style" if style else prompt
25
 
26
+ # Generate the image using the loaded tool
27
+ # This should return a PIL image object
28
+ pil_image = image_generation_tool(full_prompt)
29
 
30
+ # Create a temporary file path to save the image
31
+ import os
32
+ import tempfile
33
+
34
+ # Create temp directory if it doesn't exist
35
+ temp_dir = os.path.join(tempfile.gettempdir(), "generated_images")
36
+ os.makedirs(temp_dir, exist_ok=True)
37
+
38
+ # Save the image to a file
39
+ img_path = os.path.join(temp_dir, f"generated_{hash(full_prompt)}.png")
40
+ pil_image.save(img_path)
41
+
42
+ # Return the AgentImage object with the file path
43
  from smolagents.agent_types import AgentImage
44
+ return AgentImage(img_path)
 
 
 
45
  except Exception as e:
46
  return f"Error generating image: {str(e)}"
47