CoolFace
Apppublic

Gercho312/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py78 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_city_stats(city:str)-> str: #it's import to specify the return type13    #Keep this format for the description / args / args description but feel free to modify the tool14    15    """A tool that fetches stats about the specified city. 16    Args:17        city: a string representing the name of the city. 18    """19    api_url = 'https://api.api-ninjas.com/v1/city?name={}'.format(city)20    response = requests.get(api_url, headers={'X-Api-Key': 'YOUR API KEY'})21    if response.status_code == requests.codes.ok:22        # Automatically parse the JSON response23        data = response.json()24        country = data[0]["country"]25        population = data[0]["population"]26    else:27        print("Error:", response.status_code)28        country = "Argentina" #default response29        population = "10000000" #default response30 31        32    return f"{city} is part of {country} and has a population of {population}"33 34@tool35def get_current_time_in_timezone(timezone: str) -> str:36    """A tool that fetches the current local time in a specified timezone.37    Args:38        timezone: A string representing a valid timezone (e.g., 'America/New_York').39    """40    try:41        # Create timezone object42        tz = pytz.timezone(timezone)43        # Get current time in that timezone44        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")45        return f"The current local time in {timezone} is: {local_time}"46    except Exception as e:47        return f"Error fetching time for timezone '{timezone}': {str(e)}"48 49 50final_answer = FinalAnswerTool()51model = HfApiModel(52max_tokens=2096,53temperature=0.5,54model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded55custom_role_conversions=None,56)57 58 59# Import tool from Hub60image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)61 62with open("prompts.yaml", 'r') as stream:63    prompt_templates = yaml.safe_load(stream)64    65agent = CodeAgent(66    model=model,67    tools=[final_answer, image_generation_tool, get_current_time_in_timezone,get_city_stats], ## add your tools here (don't remove final answer)68    max_steps=6,69    verbosity_level=1,70    grammar=None,71    planning_interval=None,72    name=None,73    description=None,74    prompt_templates=prompt_templates75)76 77 78GradioUI(agent).launch()