Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
from smolagents.agent_types import AgentImage
|
| 3 |
import datetime
|
| 4 |
import requests
|
|
@@ -6,13 +6,11 @@ import pytz
|
|
| 6 |
import yaml
|
| 7 |
from PIL import Image
|
| 8 |
import os
|
|
|
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
-
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
@tool
|
| 17 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 18 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -20,17 +18,13 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 20 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
-
# Create timezone object
|
| 24 |
tz = pytz.timezone(timezone)
|
| 25 |
-
# Get current time in that timezone
|
| 26 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 27 |
return f"The current local time in {timezone} is: {local_time}"
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
@tool
|
| 35 |
def travel_recommendations_for_city(city: str) -> str:
|
| 36 |
"""A tool that provides tourist recommendations for a given city.
|
|
@@ -38,53 +32,84 @@ def travel_recommendations_for_city(city: str) -> str:
|
|
| 38 |
city: A string representing a specific city (e.g., 'Nairobi/Johannesburg').
|
| 39 |
"""
|
| 40 |
search = DuckDuckGoSearchTool()
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
@tool
|
| 54 |
def generate_travel_brochure_image(description: str) -> AgentImage:
|
| 55 |
"""A tool that generates a travel brochure image based on a description.
|
| 56 |
Args:
|
| 57 |
description: A string representing text to be converted into a brochure.
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
final_answer = FinalAnswerTool()
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 76 |
-
|
| 77 |
model = HfApiModel(
|
| 78 |
-
max_tokens=2096,
|
| 79 |
-
temperature=0.5,
|
| 80 |
-
model_id='
|
| 81 |
-
custom_role_conversions=None,
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
with open("prompts.yaml", 'r') as stream:
|
| 86 |
-
prompt_templates = yaml.safe_load(stream)
|
| 87 |
-
|
| 88 |
agent = CodeAgent(
|
| 89 |
model=model,
|
| 90 |
tools=[
|
|
@@ -92,7 +117,7 @@ agent = CodeAgent(
|
|
| 92 |
travel_recommendations_for_city,
|
| 93 |
generate_travel_brochure_image,
|
| 94 |
final_answer
|
| 95 |
-
],
|
| 96 |
max_steps=6,
|
| 97 |
verbosity_level=1,
|
| 98 |
grammar=None,
|
|
@@ -102,5 +127,4 @@ agent = CodeAgent(
|
|
| 102 |
prompt_templates=prompt_templates
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
| 106 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
from smolagents.agent_types import AgentImage
|
| 3 |
import datetime
|
| 4 |
import requests
|
|
|
|
| 6 |
import yaml
|
| 7 |
from PIL import Image
|
| 8 |
import os
|
| 9 |
+
import io
|
| 10 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
+
# Preserving original definition with added robustness
|
|
|
|
|
|
|
| 14 |
@tool
|
| 15 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 16 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 18 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 19 |
"""
|
| 20 |
try:
|
|
|
|
| 21 |
tz = pytz.timezone(timezone)
|
|
|
|
| 22 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 23 |
return f"The current local time in {timezone} is: {local_time}"
|
| 24 |
except Exception as e:
|
| 25 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 26 |
|
| 27 |
+
# Preserving original description with fixed search handling
|
|
|
|
|
|
|
| 28 |
@tool
|
| 29 |
def travel_recommendations_for_city(city: str) -> str:
|
| 30 |
"""A tool that provides tourist recommendations for a given city.
|
|
|
|
| 32 |
city: A string representing a specific city (e.g., 'Nairobi/Johannesburg').
|
| 33 |
"""
|
| 34 |
search = DuckDuckGoSearchTool()
|
| 35 |
+
try:
|
| 36 |
+
# Handle compound city names
|
| 37 |
+
city = city.split('/')[0].strip()
|
| 38 |
+
results = search(f"Top things to do in {city}")
|
| 39 |
+
|
| 40 |
+
if not results:
|
| 41 |
+
return f"Couldn't find any recommendations for {city}."
|
| 42 |
+
|
| 43 |
+
# Extract content from different possible fields
|
| 44 |
+
recommendations = []
|
| 45 |
+
for r in results[:5]:
|
| 46 |
+
content = r.get('body') or r.get('snippet') or r.get('title') or "No description available"
|
| 47 |
+
recommendations.append(f"- {content}")
|
| 48 |
+
|
| 49 |
+
return f"Here are some things to do in {city}:\n" + "\n".join(recommendations)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error fetching recommendations: {str(e)}"
|
| 52 |
|
| 53 |
+
# Preserving original description with enhanced image handling
|
| 54 |
@tool
|
| 55 |
def generate_travel_brochure_image(description: str) -> AgentImage:
|
| 56 |
"""A tool that generates a travel brochure image based on a description.
|
| 57 |
Args:
|
| 58 |
description: A string representing text to be converted into a brochure.
|
| 59 |
"""
|
| 60 |
+
try:
|
| 61 |
+
image_path = image_generation_tool(description)
|
| 62 |
+
print(f"[DEBUG] image_generation_tool returned: {image_path}")
|
| 63 |
+
|
| 64 |
+
# Handle different return types from image tool
|
| 65 |
+
if isinstance(image_path, Image.Image):
|
| 66 |
+
return AgentImage(image_path)
|
| 67 |
+
|
| 68 |
+
elif isinstance(image_path, str):
|
| 69 |
+
if image_path.startswith(('http://', 'https://')):
|
| 70 |
+
# Download from URL
|
| 71 |
+
response = requests.get(image_path, timeout=10)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
return AgentImage(Image.open(io.BytesIO(response.content)))
|
| 74 |
+
|
| 75 |
+
elif os.path.exists(image_path):
|
| 76 |
+
return AgentImage(Image.open(image_path))
|
| 77 |
+
|
| 78 |
+
raise ValueError("Image generation failed - invalid return format")
|
| 79 |
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Image generation error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
# Load image tool with error handling
|
| 84 |
+
try:
|
| 85 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"Error loading image tool: {str(e)}")
|
| 88 |
+
# Fallback to stable diffusion if available
|
| 89 |
+
try:
|
| 90 |
+
image_generation_tool = load_tool("stabilityai/stable-diffusion")
|
| 91 |
+
print("Using stable-diffusion fallback")
|
| 92 |
+
except:
|
| 93 |
+
raise RuntimeError("No working image generation tool available")
|
| 94 |
|
| 95 |
final_answer = FinalAnswerTool()
|
| 96 |
|
| 97 |
+
# Using reliable endpoint as suggested
|
|
|
|
|
|
|
| 98 |
model = HfApiModel(
|
| 99 |
+
max_tokens=2096,
|
| 100 |
+
temperature=0.5,
|
| 101 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 102 |
+
custom_role_conversions=None,
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# Load prompts with fallback
|
| 106 |
+
try:
|
| 107 |
+
with open("prompts.yaml", 'r') as stream:
|
| 108 |
+
prompt_templates = yaml.safe_load(stream)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"Error loading prompts: {str(e)}")
|
| 111 |
+
prompt_templates = {}
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
agent = CodeAgent(
|
| 114 |
model=model,
|
| 115 |
tools=[
|
|
|
|
| 117 |
travel_recommendations_for_city,
|
| 118 |
generate_travel_brochure_image,
|
| 119 |
final_answer
|
| 120 |
+
],
|
| 121 |
max_steps=6,
|
| 122 |
verbosity_level=1,
|
| 123 |
grammar=None,
|
|
|
|
| 127 |
prompt_templates=prompt_templates
|
| 128 |
)
|
| 129 |
|
|
|
|
| 130 |
GradioUI(agent).launch()
|