Team Ai
Apppublic

BinarySniffer/First_agent_template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py94 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_weather_at_coordinates(latitude:float, longitude:float)-> 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 tool that fetches the current weather conditions for a given latitude and longitude.15    Args:16        latitude: The latitude of the city to get the weather of as a float17        longitude: The longitude of the city to get the weather of as a float18    """19    try:20        # Using Open-Meteo API (free, no API key required)21        url = f"https://api.open-meteo.com/v1/forecast"22        params = {23            'latitude': latitude,24            'longitude': longitude,25            'current': 'temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code,cloud_cover,wind_speed_10m',26            'timezone': 'auto'27        }28        29        response = requests.get(url, params=params)30        response.raise_for_status()31        data = response.json()32        33        current = data['current']34        weather_info = f"""Current weather conditions:35                        - Temperature: {current['temperature_2m']}°C36                        - Feels like: {current['apparent_temperature']}°C37                        - Humidity: {current['relative_humidity_2m']}%38                        - Precipitation: {current['precipitation']}mm39                        - Cloud cover: {current['cloud_cover']}%40                        - Wind speed: {current['wind_speed_10m']} km/h41                        - Weather code: {current['weather_code']}"""42        43        return weather_info44    except Exception as e:45        return f"Error fetching weather data: {str(e)}"46 47@tool48def get_current_time_in_timezone(timezone: str) -> str:49    """A tool that fetches the current local time in a specified timezone.50    Args:51        timezone: A string representing a valid timezone (e.g., 'America/New_York').52    """53    try:54        # Create timezone object55        tz = pytz.timezone(timezone)56        # Get current time in that timezone57        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")58        return f"The current local time in {timezone} is: {local_time}"59    except Exception as e:60        return f"Error fetching time for timezone '{timezone}': {str(e)}"61 62 63final_answer = FinalAnswerTool()64 65# 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:66# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 67 68model = HfApiModel(69    max_tokens=2096,70    temperature=0.5,71    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded, see above72    custom_role_conversions=None,73)74 75 76# Example of importing tool from Hugging Face Hub77#image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)78 79with open("prompts.yaml", 'r') as stream:80    prompt_templates = yaml.safe_load(stream)81    82agent = CodeAgent(83    model=model,84    tools=[final_answer, get_current_time_in_timezone, get_weather_at_coordinates], ## add your tools here (don't remove final answer)85    max_steps=6,86    verbosity_level=1,87    grammar=None,88    planning_interval=None,89    name=None,90    description=None,91    prompt_templates=prompt_templates92)93 94GradioUI(agent).launch()