hp1318's picture
Update app.py
323afad verified
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import re
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Tool to fetch weather data (No API Key Required, Uses wttr.in)
@tool
def get_weather(city: str) -> str:
"""Fetches current weather for a given city using wttr.in.
Args:
city: The name of the city for which weather information is needed.
Returns:
str: A string containing weather details such as temperature, humidity, and wind speed.
"""
try:
url = f"https://wttr.in/{city}?format=%C+%t+%h+%w"
response = requests.get(url)
if response.status_code == 200:
return f"Weather in {city}: {response.text}"
return "Weather data unavailable."
except Exception as e:
return f"Error fetching weather: {e}"
# Tool to fetch top 5 tourist attractions in a city
@tool
def get_top_tourist_attractions(city: str) -> str:
"""Finds the top 5 must-visit places in a specified city using DuckDuckGo search.
Args:
city: The name of the city where attractions are being searched.
Returns:
str: A list of the top 5 tourist attractions (names only).
"""
try:
search_tool = DuckDuckGoSearchTool()
results = search_tool.forward(f"Top tourist attractions in {city}")
if results:
return f"Top 5 Tourist Attractions in {city}:\n{results}"
return f"No tourist attractions found for {city}."
except Exception as e:
return f"Error fetching tourist attractions: {e}"
# Tool to get local time
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that 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 for timezone '{timezone}': {str(e)}"
# Tool to fetch top 5 restaurants using DuckDuckGo Search
@tool
def get_top_restaurants(city: str) -> str:
"""Finds the top 5 restaurants in a specified city using DuckDuckGo search.
Args:
city: The name of the city where restaurants are being searched.
Returns:
str: A list of the top 5 restaurants including names and website links.
"""
try:
search_tool = DuckDuckGoSearchTool()
results = search_tool.forward(f"Top 5 restaurants in {city}")
if results:
return f"Top 5 Restaurants in {city}:\n{results}"
return "No restaurant data found."
except Exception as e:
return f"Error fetching restaurants: {e}"
# Final answer tool
final_answer = FinalAnswerTool()
# AI model setup (using a default model for now, can be replaced)
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# Loading the prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Setting up the CodeAgent with the defined tools
agent = CodeAgent(
model=model,
tools=[final_answer, get_weather, get_top_tourist_attractions, get_current_time_in_timezone, get_top_restaurants],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
# Launch Gradio UI
GradioUI(agent).launch()