from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool import datetime import requests import pytz import yaml from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # ───────────────────────────────────────── # TOOL 1: Get current time in any timezone # ───────────────────────────────────────── 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: 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 2: Get weather for any city (free API) # ───────────────────────────────────────── def get_weather(city: str) -> str: """Gets the current weather for a given city using wttr.in. Args: city: The name of the city to get weather for (e.g., 'Bengaluru'). """ try: url = f"https://wttr.in/{city}?format=3" response = requests.get(url, timeout=5) return response.text.strip() except Exception as e: return f"Could not fetch weather for {city}: {str(e)}" # ───────────────────────────────────────── # TOOL 3: Simple calculator # ───────────────────────────────────────── def calculator(expression: str) -> str: """Evaluates a basic math expression and returns the result. Args: expression: A math expression as a string, e.g. '25 * 4 + 10'. """ try: result = eval(expression, {"__builtins__": {}}) return f"Result of '{expression}' = {result}" except Exception as e: return f"Could not evaluate '{expression}': {str(e)}" # ───────────────────────────────────────── # TOOL 4: Get a fun fact about any number # ───────────────────────────────────────── def number_fact(number: int) -> str: """Returns an interesting fact about a given number. Args: number: An integer to get a fun fact about. """ try: response = requests.get(f"http://numbersapi.com/{number}", timeout=5) return response.text except Exception as e: return f"Could not fetch fact for {number}: {str(e)}" # ───────────────────────────────────────── # AGENT SETUP (don't change this much) # ───────────────────────────────────────── final_answer = FinalAnswerTool() model = InferenceClientModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None, ) # Load image generation tool from HF Hub image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) # Load system prompt with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) # ← THIS is where you register all your tools agent = CodeAgent( model=model, tools=[ final_answer, DuckDuckGoSearchTool(), # web search get_current_time_in_timezone, # time zones get_weather, # weather calculator, # math number_fact, # fun facts image_generation_tool, # generate images ], max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()