ARTpet/First_agent_template
1
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import requests7 8from bs4 import BeautifulSoup9from typing import List, Dict10from tools.final_answer import FinalAnswerTool11from Gradio_UI import GradioUI12 13# Below is an example of a tool that does nothing. Amaze us with your creativity !14@tool15def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type16 #Keep this format for the description / args / args description but feel free to modify the tool17 """A tool that does nothing yet 18 Args:19 arg1: the first argument20 arg2: the second argument21 """22 return "What magic will you build ?"23 24 25@tool26 27def get_university_rankings() -> List[Dict[str, str]]:28 """29 Fetches the university rankings in Africa from the 4icu.org website.30 31 Returns:32 list: A list of dictionaries containing the rank, university name, and country.33 """34 url = "https://www.4icu.org/top-universities-africa/"35 response = requests.get(url)36 soup = BeautifulSoup(response.content, 'html.parser')37 38 rankings = []39 table = soup.find('table', {'class': 'table'})40 for row in table.find_all('tr')[1:]:41 columns = row.find_all('td')42 rank = columns[0].text.strip()43 university = columns[1].text.strip()44 country = columns[2].text.strip()45 rankings.append({'rank': rank, 'university': university, 'country': country})46 47 return rankings48 49 50@tool51def get_current_time_in_timezone(timezone: str) -> str:52 """A tool that fetches the current local time in a specified timezone.53 Args:54 timezone: A string representing a valid timezone (e.g., 'America/New_York').55 """56 try:57 # Create timezone object58 tz = pytz.timezone(timezone)59 # Get current time in that timezone60 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")61 return f"The current local time in {timezone} is: {local_time}"62 except Exception as e:63 return f"Error fetching time for timezone '{timezone}': {str(e)}"64 65 66final_answer = FinalAnswerTool()67 68# 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:69# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 70 71model = HfApiModel(72max_tokens=2096,73temperature=0.5,74model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded75custom_role_conversions=None,76)77 78 79# Import tool from Hub80image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)81 82with open("prompts.yaml", 'r') as stream:83 prompt_templates = yaml.safe_load(stream)84 85agent = CodeAgent(86 model=model,87 tools=[final_answer,get_university_rankings, get_current_time_in_timezone], ## add your tools here (don't remove final answer)88 max_steps=6,89 verbosity_level=1,90 grammar=None,91 planning_interval=None,92 name=None,93 description=None,94 prompt_templates=prompt_templates95)96 97 98GradioUI(agent).launch()