Spaces:
Sleeping
Sleeping
Download app.py from sperera/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/sperera/First_agent_template/resolve/main/app.py
- Command line
-
hf download hf://spaces/sperera/First_agent_template/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sperera/First_agent_template/resolve/main/app.py
2.72 kB
| from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| from tools.final_answer import FinalAnswerTool | |
| from duckduckgo_search import DDGS | |
| from Gradio_UI import GradioUI | |
| # Below is an example of a tool that does nothing. Amaze us with your creativity ! | |
| def my_custom_tool(arg1: str, arg2: int) -> str: | |
| """A tool that does nothing yet | |
| Args: | |
| arg1: the first argument | |
| arg2: the second argument | |
| """ | |
| return "What magic will you build ?" | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """A tool that fetches the current local time in a specified timezone. | |
| Args: | |
| timezone: A string representing a valid timezone (e.g., 'America/New_York'). | |
| """ | |
| try: | |
| tz = pytz.timezone(timezone) | |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| return f"The current local time in {timezone} is: {local_time}" | |
| except Exception as e: | |
| return f"Error fetching time for timezone '{timezone}': {str(e)}" | |
| def get_current_weather(city: str) -> str: | |
| """Get the current weather conditions and temperature for a given city. | |
| Args: | |
| city: The name of the city to look up (e.g., 'San Francisco', 'Tokyo'). | |
| """ | |
| geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1" | |
| geo_res = requests.get(geo_url).json() | |
| if not geo_res.get("results"): | |
| return f"Could not find coordinates for city: {city}" | |
| lat = geo_res["results"][0]["latitude"] | |
| lon = geo_res["results"][0]["longitude"] | |
| weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m,weather_code" | |
| data = requests.get(weather_url).json() | |
| current = data.get("current", {}) | |
| temp = current.get("temperature_2m") | |
| return f"The current temperature in {city} is {temp}°C." | |
| final_answer = FinalAnswerTool() | |
| search_tool = DuckDuckGoSearchTool() | |
| # --- UNIVERSALLY ROUTED MODEL CONFIGURATION --- | |
| model = InferenceClientModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='meta-llama/Llama-3.3-70B-Instruct', # Universally supported on serverless routers | |
| provider="auto", # Automatically selects an available provider endpoint | |
| custom_role_conversions=None, | |
| ) | |
| # Initialize your agent with the updated model | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer, get_current_time_in_timezone, get_current_weather], | |
| max_steps=6, | |
| verbosity_level=1, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| ) | |
| GradioUI(agent).launch() |