import datetime import pytz import yaml import os from smolagents import CodeAgent, HfApiModel, load_tool, tool from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # 1. Define your custom logic tools @tool 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)}" # 2. Load the Image Generation Tool image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) # 3. Instantiate the Final Answer Tool final_answer = FinalAnswerTool() # 4. Configure the Model using the stable HfApiModel model = HfApiModel( model_id='meta-llama/Llama-3.2-3B-Instruct', max_tokens=2096, temperature=0.5, ) # 5. Load Prompt Templates # Ensure prompts.yaml is in your files! with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) # 6. Initialize the Agent # Initialize the Agent WITHOUT loading the local YAML agent = CodeAgent( model=model, tools=[ final_answer, get_current_time_in_timezone, image_generation_tool ], max_steps=6, verbosity_level=1, # REMOVE the prompt_templates=prompt_templates line ) # 7. Launch the User Interface GradioUI(agent).launch()