|
import datetime |
|
import requests |
|
import pytz |
|
import yaml |
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
from tools.final_answer import FinalAnswerTool |
|
from Gradio_UI import GradioUI |
|
|
|
|
|
|
|
|
|
@tool |
|
def get_current_time_in_timezone(timezone: str) -> str: |
|
"""Fetches the current local time in a specified timezone. |
|
Args: |
|
timezone: A valid timezone string (e.g., 'America/New_York'). |
|
Returns: |
|
A string with the current local time in the requested timezone. |
|
""" |
|
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 for timezone '{timezone}': {str(e)}" |
|
|
|
|
|
@tool |
|
def get_weather(city: str) -> str: |
|
"""Fetches current weather information for a specified city. |
|
Args: |
|
city: The name of the city (e.g., 'New York'). |
|
Returns: |
|
A string with the weather description and temperature. |
|
""" |
|
api_key = "ac662b5c2463cfa522e1acd2d4cf5a81" |
|
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" |
|
|
|
try: |
|
response = requests.get(url).json() |
|
if response["cod"] != 200: |
|
return f"Error: {response['message']}" |
|
|
|
temp = response["main"]["temp"] |
|
weather_desc = response["weather"][0]["description"] |
|
return f"The current weather in {city} is {weather_desc} with a temperature of {temp}°C." |
|
except Exception as e: |
|
return f"Error fetching weather data: {str(e)}" |
|
|
|
|
|
@tool |
|
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str: |
|
"""Converts currency from one type to another using real-time exchange rates. |
|
Args: |
|
amount: The amount to convert. |
|
from_currency: Source currency code (e.g., 'USD'). |
|
to_currency: Target currency code (e.g., 'INR'). |
|
Returns: |
|
A string with the converted currency amount. |
|
""" |
|
api_key = "90171124ae953d74d1515263" |
|
url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{from_currency}" |
|
|
|
try: |
|
response = requests.get(url).json() |
|
if "conversion_rates" not in response: |
|
return "Error: Unable to fetch exchange rates." |
|
|
|
rate = response["conversion_rates"].get(to_currency) |
|
if rate is None: |
|
return "Error: Unsupported currency code." |
|
|
|
converted_amount = round(amount * rate, 2) |
|
return f"{amount} {from_currency} is equal to {converted_amount} {to_currency}." |
|
except Exception as e: |
|
return f"Error fetching exchange rates: {str(e)}" |
|
|
|
|
|
|
|
|
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
|
|
model = HfApiModel( |
|
max_tokens=2096, |
|
temperature=0.5, |
|
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", |
|
) |
|
|
|
|
|
with open("prompts.yaml", "r") as stream: |
|
prompt_templates = yaml.safe_load(stream) |
|
|
|
|
|
agent = CodeAgent( |
|
model=model, |
|
tools=[ |
|
final_answer, |
|
get_current_time_in_timezone, |
|
get_weather, |
|
convert_currency, |
|
DuckDuckGoSearchTool(), |
|
image_generation_tool, |
|
], |
|
max_steps=6, |
|
verbosity_level=1, |
|
prompt_templates=prompt_templates, |
|
) |
|
|
|
|
|
|
|
|
|
GradioUI(agent).launch() |