itskavya/First_agent_template
0
1from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool 7from tools.visit_webpage import VisitWebpageTool8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def days_until_new_year()-> str: #it's import to specify the return type14 #Keep this format for the description / args / args description but feel free to modify the tool15 """A tool that returns how many days are left until new year.16 Args:17 No arguments.18 """19 try: 20 import requests21 from bs4 import BeautifulSoup22 response = requests.get("https://www.timeanddate.com/counters/newyear.html")23 response.raise_for_status() 24 parsed_content = BeautifulSoup(response.content, 'lxml')25 element = parsed_content.find(id='el_d2')26 return element.text27 28 except Exception as e:29 return f"An exception occured: {str(e)}"30 31 32@tool33def get_current_time_in_timezone(timezone: str) -> str:34 """A tool that fetches the current local time in a specified timezone.35 Args:36 timezone: A string representing a valid timezone (e.g., 'America/New_York').37 """38 try:39 # Create timezone object40 tz = pytz.timezone(timezone)41 # Get current time in that timezone42 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")43 return f"The current local time in {timezone} is: {local_time}"44 except Exception as e:45 return f"Error fetching time for timezone '{timezone}': {str(e)}"46 47 48final_answer = FinalAnswerTool()49visit_page = VisitWebpageTool()50web_search = DuckDuckGoSearchTool()51 52# 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:53# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 54 55model = HfApiModel(56max_tokens=2096,57temperature=0.5,58model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded59custom_role_conversions=None, # use default roles like user/assistant/etc60)61 62 63# Import tool from Hub64image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)65 66with open("prompts.yaml", 'r') as stream:67 prompt_templates = yaml.safe_load(stream)68 69agent = CodeAgent(70 model=model,71 tools=[final_answer, image_generation_tool, get_current_time_in_timezone, visit_page, web_search, days_until_new_year], ## add your tools here (don't remove final answer)72 max_steps=6,73 verbosity_level=1,74 grammar=None,75 planning_interval=None,76 name=None,77 description=None,78 prompt_templates=prompt_templates79)80 81 82GradioUI(agent).launch()