Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,13 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import os
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Ensure the latest smolagents version: `pip install --upgrade smolagents`
|
| 11 |
# Ensure prompts.yaml matches the provided structure with system_prompt, final_answer, planning, and managed_agent
|
|
|
|
| 12 |
# Set your Hugging Face API token as an environment variable if required
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
|
|
@@ -41,6 +43,42 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
final_answer = FinalAnswerTool()
|
| 45 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 46 |
|
|
@@ -48,7 +86,6 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 48 |
prompt_templates = yaml.safe_load(stream)
|
| 49 |
|
| 50 |
# Restructure prompt_templates to match CodeAgent expectations
|
| 51 |
-
# Handle cases where planning and managed_agent are parsed incorrectly
|
| 52 |
restructured_templates = {
|
| 53 |
"system_prompt": prompt_templates.get("system_prompt", ""),
|
| 54 |
"final_answer": prompt_templates.get("final_answer", ""),
|
|
@@ -76,7 +113,7 @@ model = HfApiModel(
|
|
| 76 |
|
| 77 |
agent = CodeAgent(
|
| 78 |
model=model,
|
| 79 |
-
tools=[get_motivational_quote, get_current_time_in_timezone, image_generation_tool, final_answer],
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import os
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Ensure the latest smolagents version: `pip install --upgrade smolagents`
|
| 12 |
# Ensure prompts.yaml matches the provided structure with system_prompt, final_answer, planning, and managed_agent
|
| 13 |
+
# Ensure Pillow is installed: `pip install Pillow`
|
| 14 |
# Set your Hugging Face API token as an environment variable if required
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 45 |
|
| 46 |
+
@tool
|
| 47 |
+
def overlay_quote_on_image(image: object, quote: str) -> str:
|
| 48 |
+
"""A tool that overlays a motivational quote on an image and saves it to a file.
|
| 49 |
+
Args:
|
| 50 |
+
image: An AgentImage object from text-to-image tool.
|
| 51 |
+
quote: A string containing the quote to overlay.
|
| 52 |
+
Returns:
|
| 53 |
+
A string representing the file path of the saved image.
|
| 54 |
+
"""
|
| 55 |
+
try:
|
| 56 |
+
# Convert AgentImage to PIL Image (assuming AgentImage has a to_pil() method or similar)
|
| 57 |
+
pil_image = image.to_pil() if hasattr(image, 'to_pil') else Image.open(image)
|
| 58 |
+
draw = ImageDraw.Draw(pil_image)
|
| 59 |
+
|
| 60 |
+
# Load a default font (or specify a path to a .ttf file if available)
|
| 61 |
+
try:
|
| 62 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
| 63 |
+
except:
|
| 64 |
+
font = ImageFont.load_default()
|
| 65 |
+
|
| 66 |
+
# Calculate text size and position
|
| 67 |
+
text = quote
|
| 68 |
+
text_width, text_height = draw.textsize(text, font=font)
|
| 69 |
+
image_width, image_height = pil_image.size
|
| 70 |
+
text_position = ((image_width - text_width) // 2, image_height - text_height - 50)
|
| 71 |
+
|
| 72 |
+
# Draw text with a black outline and white fill
|
| 73 |
+
draw.text(text_position, text, font=font, fill="white", stroke_width=2, stroke_fill="black")
|
| 74 |
+
|
| 75 |
+
# Save the image to a file
|
| 76 |
+
output_path = "/tmp/output_image_with_quote.png"
|
| 77 |
+
pil_image.save(output_path)
|
| 78 |
+
return output_path
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"Error overlaying quote on image: {str(e)}"
|
| 81 |
+
|
| 82 |
final_answer = FinalAnswerTool()
|
| 83 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 84 |
|
|
|
|
| 86 |
prompt_templates = yaml.safe_load(stream)
|
| 87 |
|
| 88 |
# Restructure prompt_templates to match CodeAgent expectations
|
|
|
|
| 89 |
restructured_templates = {
|
| 90 |
"system_prompt": prompt_templates.get("system_prompt", ""),
|
| 91 |
"final_answer": prompt_templates.get("final_answer", ""),
|
|
|
|
| 113 |
|
| 114 |
agent = CodeAgent(
|
| 115 |
model=model,
|
| 116 |
+
tools=[get_motivational_quote, get_current_time_in_timezone, image_generation_tool, overlay_quote_on_image, final_answer],
|
| 117 |
max_steps=6,
|
| 118 |
verbosity_level=1,
|
| 119 |
grammar=None,
|