from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel, tool #import requests import pytz import yaml from datetime import datetime from Gradio_UI import GradioUI @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: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that timezone local_time = 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 write_to_markdown(content: str, filename: str) -> None: """ Write the given content to a markdown file. Args: content: The text content to write to the file filename: The name of the markdown file (will automatically add .md extension if not present) Example: >>> write_to_markdown("# Hello World\\nThis is a test.", "output.md") """ # Add .md extension if not present if not filename.endswith('.md'): filename += '.md' # Write content to file with open(filename, 'w', encoding='utf-8') as f: f.write(content) print(f"Content successfully written to {filename}") # Instantiate the DuckDuckGoSearchTool search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0) # Instantiate the FinalAnswerTool final_answer = FinalAnswerTool() # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' model = InferenceClientModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded #model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', custom_role_conversions=None, ) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[ final_answer, search_tool, get_current_time_in_timezone, write_to_markdown, ], ## add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()