Warren-SJ/Agent_Example
2
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10# Below is an example of a tool that does nothing. Amaze us with your creativity !11duckduckgosearchtool = DuckDuckGoSearchTool(12 13)14 15@tool16def python_exec(code:str)-> str: #it's import to specify the return type17 #Keep this format for the description / args / args description but feel free to modify the tool18 """A tool that executes python code in a safe environment 19 Args:20 code: the code to execute21 """22 try:23 exec_globals = {}24 exec(code, {}, exec_globals)25 return str(exec_globals)26 except Exception as e:27 return f"Error: {str(e)}"28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31 """A tool that fetches the current local time in a specified timezone.32 Args:33 timezone: A string representing a valid timezone (e.g., 'America/New_York').34 """35 try:36 # Create timezone object37 tz = pytz.timezone(timezone)38 # Get current time in that timezone39 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")40 return f"The current local time in {timezone} is: {local_time}"41 except Exception as e:42 return f"Error fetching time for timezone '{timezone}': {str(e)}"43 44 45final_answer = FinalAnswerTool()46 47# 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:48# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 49 50model = HfApiModel(51max_tokens=2096,52temperature=0.5,53model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded54custom_role_conversions=None,55)56 57 58# Import tool from Hub59image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)60 61with open("prompts.yaml", 'r') as stream:62 prompt_templates = yaml.safe_load(stream)63 64agent = CodeAgent(65 model=model,66 tools=[image_generation_tool, get_current_time_in_timezone, final_answer, duckduckgosearchtool], ## add your tools here (don't remove final answer)67 max_steps=6,68 verbosity_level=1,69 grammar=None,70 planning_interval=None,71 name=None,72 description=None,73 prompt_templates=prompt_templates74)75 76 77GradioUI(agent).launch()