CoolFace
Apppublic

kamimas/First_agent_template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py100 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 my_custom_tool(arg1:int, arg2:int)-> 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    """A calculator that calculates the value of two integer inputs arg1 and arg2. Returns the product as a string output15    Args:16        arg1: the first argument17        arg2: the second argument18    """19    return str(arg1*arg2);20 21 22 23@tool24def get_weather(city_name:str)->str:25    """A weather tool that gives you the weather condition and temperature of a location given the name of the city26    Args:27        city_name: A string representing the city name (e.g. 'Toronto')28    """29    base_url = "https://api.weatherapi.com/v1/current.json"30    params = {31        'q': city_name,32        'key': 'da8fb20768d643eabf4215632252903'33    }34    35    try:36        response = requests.get(base_url, params=params)37        response.raise_for_status()38        data = response.json()39 40        weather_description = data['current']['condition']['text']41        temperature = data['current']['temp_c']42    except requests.exceptions.RequestException as e:43        print(f"HTTP error occurred: {e}")44    except KeyError as e:45        print(f"Missing expected data in response: {e}")46    except Exception as e:47        print(f"An unexpected error occurred: {e}")48 49    return f"Weather in {city_name}: {weather_description} and the temperature is {temperature}"50 51@tool52def get_current_time_in_timezone(timezone: str) -> str:53    """A tool that fetches the current local time in a specified timezone.54    Args:55        timezone: A string representing a valid timezone (e.g., 'America/New_York').56    """57    try:58        # Create timezone object59        tz = pytz.timezone(timezone)60        # Get current time in that timezone61        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")62        return f"The current local time in {timezone} is: {local_time}"63    except Exception as e:64        return f"Error fetching time for timezone '{timezone}': {str(e)}"65 66 67final_answer = FinalAnswerTool()68 69# 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:70# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 71 72model = HfApiModel(73max_tokens=2096,74temperature=0.5,75model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded76custom_role_conversions=None,77)78 79 80# Import tool from Hub81image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)82 83with open("prompts.yaml", 'r') as stream:84    prompt_templates = yaml.safe_load(stream)85    86agent = CodeAgent(87    model=model,88    tools=[final_answer, image_generation_tool,get_current_time_in_timezone,get_weather], ## add your tools here (don't remove final answer)89    max_steps=6,90    verbosity_level=1,91    grammar=None,92    planning_interval=None,93    name=None,94    description=None,95    prompt_templates=prompt_templates96)97 98 99 100GradioUI(agent).launch()