from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import requests import pytz import yaml from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # 🔥 CUSTOM TOOL 1: Calculator @tool def calculator(expression: str) -> str: """Evaluates a mathematical expression. Args: expression: A math expression like '2+3*5' """ try: result = eval(expression) return f"Result is {result}" except Exception as e: return f"Error: {str(e)}" # 🔥 CUSTOM TOOL 2: Time Tool @tool def get_current_time_in_timezone(timezone: str) -> str: """Fetches the current local time in a specified timezone. Args: timezone: A valid timezone (e.g., 'Asia/Kolkata') """ try: tz = pytz.timezone(timezone) local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"Current time in {timezone}: {local_time}" except Exception as e: return f"Error: {str(e)}" # 🔥 CUSTOM TOOL 3: Weather Tool @tool def get_weather(city: str) -> str: """Fetches current weather for a city. Args: city: Name of the city """ try: url = f"https://wttr.in/{city}?format=3" response = requests.get(url) return response.text except: return "Error fetching weather" # 🔥 FINAL ANSWER TOOL (MANDATORY) final_answer = FinalAnswerTool() # 🤖 MODEL (LLM) model = HfApiModel( max_tokens=2048, temperature=0.5, model_id='Qwen/Qwen2.5-7B-Instruct', # smaller & stable model custom_role_conversions=None, ) # 🖼️ IMAGE GENERATION TOOL (from Hugging Face Hub) image_generation_tool = load_tool( "agents-course/text-to-image", trust_remote_code=True ) # 📄 LOAD PROMPTS with open("prompts.yaml", "r") as stream: prompt_templates = yaml.safe_load(stream) # 🧠 CREATE AGENT agent = CodeAgent( model=model, tools=[ final_answer, calculator, get_current_time_in_timezone, get_weather, DuckDuckGoSearchTool(), image_generation_tool ], max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name="Smart AI Agent", description="An AI agent that can search, calculate, fetch time, weather, and generate images.", prompt_templates=prompt_templates ) # 🖥️ LAUNCH UI GradioUI(agent).launch()