arunprasadh commited on
Commit
6fc8e14
·
verified ·
1 Parent(s): d9a3af9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -51,21 +52,31 @@ custom_role_conversions=None,
51
  # @tool
52
  # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
53
  @tool
54
- def generate_image_from_text(prompt: str) -> str:
55
- """Generates an image based on the given text prompt.
56
 
57
  Args:
58
  prompt: A string describing the image to be generated.
59
 
60
  Returns:
61
- A string indicating the success or failure of the image generation process.
62
  """
63
  try:
 
64
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
65
  image_url = image_generation_tool(prompt)
66
- return f"Image successfully generated: {image_url}"
 
 
 
 
 
 
 
67
  except Exception as e:
68
- return f"Error generating image: {str(e)}"
69
 
70
  with open("prompts.yaml", 'r') as stream:
71
  prompt_templates = yaml.safe_load(stream)
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from PIL import Image
8
 
9
  from Gradio_UI import GradioUI
10
 
 
52
  # @tool
53
  # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
54
  @tool
55
+ def generate_image_from_text(prompt: str):
56
+ """Generates and displays an image based on the given text prompt.
57
 
58
  Args:
59
  prompt: A string describing the image to be generated.
60
 
61
  Returns:
62
+ Displays the generated image.
63
  """
64
  try:
65
+ # Load the image generation tool
66
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
+
68
+ # Generate the image and get the URL (if applicable)
69
  image_url = image_generation_tool(prompt)
70
+
71
+ # Fetch the image from the URL
72
+ response = requests.get(image_url)
73
+ image = Image.open(io.BytesIO(response.content))
74
+
75
+ # Display the image
76
+ image.show()
77
+
78
  except Exception as e:
79
+ print(f"Error generating image: {str(e)}")
80
 
81
  with open("prompts.yaml", 'r') as stream:
82
  prompt_templates = yaml.safe_load(stream)