|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
import datetime |
|
import requests |
|
import pytz |
|
import yaml |
|
import os |
|
from typing import Optional, Dict, Any, List |
|
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 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)}" |
|
|
|
@tool |
|
def list_available_timezones() -> str: |
|
"""Lists all available timezone names grouped by region.""" |
|
all_timezones = pytz.all_timezones |
|
timezone_dict = {} |
|
|
|
for tz in all_timezones: |
|
region = tz.split('/', 1)[0] if '/' in tz else 'Other' |
|
if region not in timezone_dict: |
|
timezone_dict[region] = [] |
|
timezone_dict[region].append(tz) |
|
|
|
result = [] |
|
for region, zones in timezone_dict.items(): |
|
result.append(f"{region}: {', '.join(zones[:5])}" + |
|
(f" and {len(zones)-5} more..." if len(zones) > 5 else "")) |
|
|
|
return "\n".join(result) |
|
|
|
|
|
@tool |
|
def get_weather(location: str) -> str: |
|
"""Fetches current weather information for a specified location. |
|
|
|
Args: |
|
location: A string representing a city or location (e.g., 'New York'). |
|
""" |
|
|
|
|
|
return f"Weather information for {location}: Simulated weather data (add API key and implement actual API call)" |
|
|
|
|
|
try: |
|
with open("prompts.yaml", 'r') as stream: |
|
prompt_templates = yaml.safe_load(stream) |
|
except Exception as e: |
|
print(f"Error loading prompt templates: {e}") |
|
prompt_templates = {} |
|
|
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
|
|
model = HfApiModel( |
|
max_tokens=2096, |
|
temperature=0.5, |
|
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
|
custom_role_conversions=None, |
|
) |
|
|
|
|
|
try: |
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
except Exception as e: |
|
print(f"Failed to load image generation tool: {e}") |
|
image_generation_tool = None |
|
|
|
|
|
tools = [ |
|
final_answer, |
|
get_current_time_in_timezone, |
|
list_available_timezones, |
|
get_weather |
|
] |
|
|
|
|
|
if image_generation_tool: |
|
tools.append(image_generation_tool) |
|
|
|
|
|
agent = CodeAgent( |
|
model=model, |
|
tools=tools, |
|
max_steps=6, |
|
verbosity_level=1, |
|
grammar=None, |
|
planning_interval=None, |
|
name=None, |
|
description=None, |
|
prompt_templates=prompt_templates |
|
) |
|
|
|
|
|
GradioUI(agent).launch() |