from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import pytz import yaml from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # Tool to calculate hours difference @tool def get_hours_difference(time_1: str, time_2: str) -> str: """A tool that calculates the number of hours difference between two times. Args: time_1: The first time in format yyyy-mm-dd hh:mm:ss. time_2: The second time in format yyyy-mm-dd hh:mm:ss. """ try: expected_time_format = "%Y-%m-%d %H:%M:%S" time_1_object = datetime.datetime.strptime(time_1, expected_time_format) time_2_object = datetime.datetime.strptime(time_2, expected_time_format) if time_1_object > time_2_object: return f"There is a difference of {(time_1_object - time_2_object).seconds / 3600} hours between the two." return f"There is a difference of {(time_2_object - time_1_object).seconds / 3600} hours between the two." except Exception as e: return "Error working out the number of hour difference between the two times." # Tool to get current time in a timezone @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)}" # Fixed DuckDuckGo search tool @tool def duck_search(query: str) -> str: """A tool that searches DuckDuckGo for a given query. Args: query: The term to search for (e.g., 'latest news New York'). """ try: search_tool = DuckDuckGoSearchTool() results = search_tool(query) # Call it directly # Handle string or list/dict output if isinstance(results, str): return f"Found for '{query}': {results.strip()}" elif isinstance(results, list) and len(results) > 0: return f"Found for '{query}': {results[0].get('snippet', 'No snippet')}" return f"No results for '{query}'." except Exception as e: return f"Error searching '{query}': {str(e)}" # Instantiate tools final_answer = FinalAnswerTool() # Model setup model = HfApiModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None, ) # Load image generation tool image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) # Load prompt templates with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) # Create the agent agent = CodeAgent( model=model, tools=[ final_answer, get_current_time_in_timezone, get_hours_difference, duck_search, image_generation_tool ], max_steps=6, verbosity_level=1, prompt_templates=prompt_templates ) # Launch Gradio UI GradioUI(agent).launch()