Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,12 +51,46 @@ custom_role_conversions=None,
|
|
| 51 |
# Import tool from Hub
|
| 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) -> Image:
|
| 56 |
"""
|
| 57 |
A tool that generates an image based on the given text prompt and displays it.
|
|
|
|
| 58 |
Args:
|
| 59 |
prompt: A string describing the image to be generated.
|
|
|
|
|
|
|
|
|
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
# Load the image generation tool
|
|
@@ -69,16 +103,17 @@ def generate_image_from_text(prompt: str) -> Image:
|
|
| 69 |
response = requests.get(image_url)
|
| 70 |
image = Image.open(io.BytesIO(response.content))
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
display.display(image)
|
| 74 |
image.save("generated_image.png")
|
| 75 |
print("Image saved as generated_image.png. Open the file to view it.")
|
| 76 |
-
|
|
|
|
| 77 |
return image
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error generating image: {str(e)}")
|
| 81 |
-
|
|
|
|
| 82 |
with open("prompts.yaml", 'r') as stream:
|
| 83 |
prompt_templates = yaml.safe_load(stream)
|
| 84 |
|
|
|
|
| 51 |
# Import tool from Hub
|
| 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) -> Image:
|
| 56 |
+
# """
|
| 57 |
+
# A tool that generates an image based on the given text prompt and displays it.
|
| 58 |
+
# Args:
|
| 59 |
+
# prompt: A string describing the image to be generated.
|
| 60 |
+
# """
|
| 61 |
+
# try:
|
| 62 |
+
# # Load the image generation tool
|
| 63 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 64 |
+
|
| 65 |
+
# # Generate the image and get the URL
|
| 66 |
+
# image_url = image_generation_tool(prompt)
|
| 67 |
+
|
| 68 |
+
# # Fetch the image from the URL
|
| 69 |
+
# response = requests.get(image_url)
|
| 70 |
+
# image = Image.open(io.BytesIO(response.content))
|
| 71 |
+
|
| 72 |
+
# # Display the image inline for Jupyter Notebook or applications with inline display support
|
| 73 |
+
# display.display(image)
|
| 74 |
+
# image.save("generated_image.png")
|
| 75 |
+
# print("Image saved as generated_image.png. Open the file to view it.")
|
| 76 |
+
# # Return the image object
|
| 77 |
+
# return image
|
| 78 |
+
|
| 79 |
+
# except Exception as e:
|
| 80 |
+
# print(f"Error generating image: {str(e)}")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
@tool
|
| 85 |
def generate_image_from_text(prompt: str) -> Image:
|
| 86 |
"""
|
| 87 |
A tool that generates an image based on the given text prompt and displays it.
|
| 88 |
+
|
| 89 |
Args:
|
| 90 |
prompt: A string describing the image to be generated.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
Image: The generated image that can be displayed by Gradio.
|
| 94 |
"""
|
| 95 |
try:
|
| 96 |
# Load the image generation tool
|
|
|
|
| 103 |
response = requests.get(image_url)
|
| 104 |
image = Image.open(io.BytesIO(response.content))
|
| 105 |
|
| 106 |
+
# Optionally save the image to a file
|
|
|
|
| 107 |
image.save("generated_image.png")
|
| 108 |
print("Image saved as generated_image.png. Open the file to view it.")
|
| 109 |
+
|
| 110 |
+
# Return the image object for Gradio to display
|
| 111 |
return image
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
print(f"Error generating image: {str(e)}")
|
| 115 |
+
return None
|
| 116 |
+
|
| 117 |
with open("prompts.yaml", 'r') as stream:
|
| 118 |
prompt_templates = yaml.safe_load(stream)
|
| 119 |
|