CoolFace
Apppublic

GuillaumeProjects/First_agent_template

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
app.py82 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10@tool11def search_web(query:str)-> str: #it's import to specify the return type12    #Keep this format for the description / args / args description but feel free to modify the tool13    """A tool that search the web14    Args:15        query: the query to search the web16    """  17    try:18        web_search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)19        results = web_search_tool(query)20 21        if not results:22            return "No results found."23 24        # Format results into readable text25        formatted = "\n".join(26            [f"{i+1}. {r.get('title', 'No title')} - {r.get('link', 'No link')}\n{r.get('snippet', '')}"27             for i, r in enumerate(results)]28        )29        return formatted30 31    except Exception as e:32        return f"Error during web search: {str(e)}"33 34 35@tool36def get_current_time_in_timezone(timezone: str) -> str:37    """A tool that fetches the current local time in a specified timezone.38    Args:39        timezone: A string representing a valid timezone (e.g., 'America/New_York').40    """41    try:42        # Create timezone object43        tz = pytz.timezone(timezone)44        # Get current time in that timezone45        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")46        return f"The current local time in {timezone} is: {local_time}"47    except Exception as e:48        return f"Error fetching time for timezone '{timezone}': {str(e)}"49 50 51final_answer = FinalAnswerTool()52 53# 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:54# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 55 56model = HfApiModel(57max_tokens=2096,58temperature=0.5,59model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded60custom_role_conversions=None,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,search_web,get_current_time_in_timezone], ## 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()