CoolFace
Apppublic

MonsieurMory/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py81 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# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def get_gdp(country_iso_code: str, year: int) -> str:13    """Retrieves the GDP of a given country for a specified year in USD.14    15    Args:16        country_iso_code: The ISO 3166-1 alpha-3 code of the country (e.g., "FRA" for France, "USA" for United States).17        year: The year for which to retrieve GDP data.18 19    Returns:20        The GDP of the country in the given year (in US dollars) or an error message.21    """22    base_url = "https://api.worldbank.org/v2/country/{}/indicator/NY.GDP.MKTP.CD"23    response = requests.get(base_url.format(country_iso_code), params={"date": year, "format": "json"})24 25    if response.status_code == 200:26        data = response.json()27        if data and len(data) > 1 and "value" in data[1][0]:28            return f"GDP of {country_iso_code} in {year}: ${data[1][0]['value']:,} USD"29    30    return "GDP data not available."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()49web_search_tool = DuckDuckGoSearchTool()50 51# 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:52# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 53 54model = HfApiModel(55max_tokens=2096,56temperature=0.5,57model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded58custom_role_conversions=None,59)60 61 62# Import tool from Hub63image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)64 65with open("prompts.yaml", 'r') as stream:66    prompt_templates = yaml.safe_load(stream)67    68agent = CodeAgent(69    model=model,70    tools=[final_answer, get_gdp, image_generation_tool, web_search_tool], ## add your tools here (don't remove final answer)71    max_steps=6,72    verbosity_level=1,73    grammar=None,74    planning_interval=None,75    name=None,76    description=None,77    prompt_templates=prompt_templates78)79 80 81GradioUI(agent).launch()