File size: 3,883 Bytes
9b5b26a 3b9f904 f78a8ef 6dee0c8 6aae614 9b5b26a 8563f1a 07f84c5 72da679 9b5b26a 3b9f904 3151e4d 07f84c5 670b88c 9b5b26a 8c01ffb 07f84c5 3b9f904 07f84c5 3b9f904 6d5d4b9 3b9f904 07f84c5 3b9f904 07f84c5 3b9f904 6d5d4b9 3b9f904 07f84c5 3b9f904 07f84c5 3b9f904 07f84c5 3b9f904 07f84c5 3b9f904 6aae614 ae7a494 3b9f904 e121372 43c0041 fd70d43 faa1225 13d500a 8c01ffb 3b9f904 8563f1a 07f84c5 8c01ffb 8fe992b 3151e4d 6dee0c8 3b9f904 6dee0c8 8c01ffb 3b9f904 8fe992b 07f84c5 3b9f904 3ba6132 |
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 110 111 112 113 114 115 116 |
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
# ---------------------------- TOOLS ----------------------------
# 1. 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 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)}"
# 2. Get Current Weather Data
@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)}"
# 3. 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: 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)}"
# ---------------------------- MODEL & AGENT SETUP ----------------------------
# 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",
)
# Load Prompt Templates
with open("prompts.yaml", "r") as stream:
prompt_templates = yaml.safe_load(stream)
# Create AI Agent with Defined 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,
prompt_templates=prompt_templates,
)
# ---------------------------- LAUNCH APPLICATION ----------------------------
# Launch Gradio UI
GradioUI(agent).launch() |