Spaces:
Runtime error
Runtime error
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
import datetime | |
import requests | |
import pytz | |
import yaml | |
from tools.final_answer import FinalAnswerTool | |
from Gradio_UI import GradioUI | |
# Custom tools with complete docstrings | |
def currency_converter(amount: float, from_currency: str, to_currency: str) -> str: | |
"""Convert between currencies using real-time exchange rates | |
Args: | |
amount: The amount to convert (positive number) | |
from_currency: Currency code to convert from (3-letter ISO code like USD) | |
to_currency: Currency code to convert to (3-letter ISO code like EUR) | |
""" | |
try: | |
url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}" | |
response = requests.get(url) | |
rates = response.json()["rates"] | |
converted = amount * rates[to_currency] | |
return f"Convertion of {amount} {from_currency} = {converted:.2f} {to_currency}" | |
except Exception as e: | |
return f"Error converting {amount} {from_currency} to {to_currency}" | |
def website_availability(url: str) -> str: | |
"""Check if a website is online and responsive | |
Args: | |
url: The full URL of the website to check (including protocol like https://) | |
""" | |
try: | |
response = requests.get(url, timeout=5) | |
return f"Website {url} is online (Status: {response.status_code})" | |
except requests.exceptions.RequestException: | |
return f"Website {url} is unreachable" | |
def text_summarizer(long_text: str) -> str: | |
"""Summarize long text documents | |
Args: | |
long_text: The text content to summarize (at least 3 sentences) | |
""" | |
try: | |
sentences = long_text.split('.') | |
return '. '.join(sentences[:3]) + '...' | |
except Exception as e: | |
return f"Error summarizing text" | |
def get_current_time_in_timezone(timezone: str) -> str: | |
"""Get current time in a specific timezone | |
Args: | |
timezone: Valid timezone name (e.g. America/New_York or Asia/Tokyo) | |
""" | |
try: | |
tz = pytz.timezone(timezone) | |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
return f"The current local time in {timezone} is: {local_time}" | |
except Exception as e: | |
return f"Error fetching time: {str(e)}" | |
def get_current_weather(city: str) -> str: | |
"""Get current weather for a city | |
Args: | |
city: City name (e.g. Paris, London, Tokyo) | |
""" | |
try: | |
# Using a free weather API (no key required) | |
url = f"https://wttr.in/{city}?format=%C+%t+%w" | |
response = requests.get(url) | |
return f"Weather in {city}: {response.text.strip()}" | |
except Exception as e: | |
return f"Weather lookup failed: {str(e)}" | |
final_answer = FinalAnswerTool() | |
model = HfApiModel( | |
max_tokens=2096, | |
temperature=0.5, | |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
custom_role_conversions=None, | |
) | |
# Load only working tools from Hub | |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
with open("prompts.yaml", 'r') as stream: | |
prompt_templates = yaml.safe_load(stream) | |
# Build tools list | |
all_tools = [ | |
final_answer, | |
DuckDuckGoSearchTool(), # Built-in search | |
image_generation_tool, | |
get_current_weather, # Our custom weather tool | |
get_current_time_in_timezone, | |
currency_converter, | |
website_availability, | |
text_summarizer | |
] | |
agent = CodeAgent( | |
model=model, | |
tools=all_tools, | |
max_steps=12, | |
verbosity_level=1, | |
grammar=None, | |
planning_interval=None, | |
name=None, | |
description=None, | |
prompt_templates=prompt_templates | |
) | |
GradioUI(agent).launch() |