File size: 3,712 Bytes
9b5b26a 3b9f904 f78a8ef 6dee0c8 6aae614 9b5b26a 8563f1a 3b9f904 72da679 9b5b26a 3b9f904 3151e4d 3b9f904 670b88c 9b5b26a 8c01ffb 3b9f904 6aae614 ae7a494 3b9f904 e121372 8563f1a 38abccb 3b9f904 8563f1a 13d500a 8c01ffb 3b9f904 8563f1a 3b9f904 8c01ffb 8fe992b 3151e4d 6dee0c8 3b9f904 6dee0c8 8c01ffb 3b9f904 8fe992b 3b9f904 07d41e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
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
# 1. Tool: Get Current Time in Any Timezone
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
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)}"
# 2. Tool: Get Current Weather Data (Requires OpenWeatherMap API)
@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').
"""
api_key = "YOUR_OPENWEATHER_API_KEY" # Replace with your OpenWeather API key
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)}"
# 3. Tool: Currency Exchange Converter
@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: The source currency code (e.g., 'USD').
to_currency: The target currency code (e.g., 'INR').
"""
api_key = "YOUR_EXCHANGERATE_API_KEY" # Replace with a valid API key from ExchangeRate API
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 f"Error: Unable to fetch exchange rates."
rate = response["conversion_rates"].get(to_currency, None)
if rate is None:
return f"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)}"
# Load Hugging Face Text-to-Image Tool
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Load Final Answer Tool
final_answer = FinalAnswerTool()
# Initialize Model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
custom_role_conversions=None,
)
# Load Prompt Templates
with open("prompts.yaml", "r") as stream:
prompt_templates = yaml.safe_load(stream)
# Create Agent with Additional Tools
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,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates,
)
# Launch Gradio UI
GradioUI(agent).launch() |