Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 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 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity!
|
| 12 |
@tool
|
|
@@ -32,9 +30,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 32 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 33 |
|
| 34 |
|
|
|
|
| 35 |
final_answer = FinalAnswerTool()
|
| 36 |
|
| 37 |
-
#
|
| 38 |
model = HfApiModel(
|
| 39 |
max_tokens=2096,
|
| 40 |
temperature=0.5,
|
|
@@ -49,35 +48,16 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
|
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
image.save(filename) # Save image as a PNG file
|
| 56 |
-
|
| 57 |
-
return f"Image saved as {filename}"
|
| 58 |
-
|
| 59 |
-
# Define the agent
|
| 60 |
-
agent = CodeAgent(
|
| 61 |
-
model=model,
|
| 62 |
-
tools=[final_answer, get_current_time_in_timezone, my_custom_tool, image_generation_tool],
|
| 63 |
-
max_steps=6,
|
| 64 |
-
verbosity_level=1,
|
| 65 |
-
grammar=None,
|
| 66 |
-
planning_interval=None,
|
| 67 |
-
name=None,
|
| 68 |
-
description=None,
|
| 69 |
-
prompt_templates=prompt_templates
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
# Modify this part to generate an image and save it
|
| 73 |
-
def generate_and_save_image(prompt):
|
| 74 |
-
# Generate image
|
| 75 |
image = image_generation_tool(prompt=prompt)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 3 |
import datetime
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from PIL import Image # 用于处理图像
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Below is an example of a tool that does nothing. Amaze us with your creativity!
|
| 10 |
@tool
|
|
|
|
| 30 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 31 |
|
| 32 |
|
| 33 |
+
# Initialize FinalAnswerTool
|
| 34 |
final_answer = FinalAnswerTool()
|
| 35 |
|
| 36 |
+
# Initialize the model
|
| 37 |
model = HfApiModel(
|
| 38 |
max_tokens=2096,
|
| 39 |
temperature=0.5,
|
|
|
|
| 48 |
with open("prompts.yaml", 'r') as stream:
|
| 49 |
prompt_templates = yaml.safe_load(stream)
|
| 50 |
|
| 51 |
+
# Function to generate and display the image
|
| 52 |
+
def generate_and_show_image(prompt):
|
| 53 |
+
# Generate the image using the tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
image = image_generation_tool(prompt=prompt)
|
| 55 |
|
| 56 |
+
# Save the image to a file (this is optional but useful for download)
|
| 57 |
+
image.save("generated_image.png") # Save as PNG
|
| 58 |
+
|
| 59 |
+
# Return the image file for Gradio to display
|
| 60 |
+
return image # This directly returns the image to be shown in Gradio
|
| 61 |
|
| 62 |
+
# Create a Gradio interface to display the image
|
| 63 |
+
gr.Interface(fn=generate_and_show_image, inputs="text", outputs="image").launch(share=True)
|