First_agent_template / tools /get_weather.py
Ark4u's picture
Deploy ArkAssistant smolagent (Hugging Face Agents Course Unit 1)
2acf84f verified
Raw History Blame Contribute Delete
898 Bytes
from urllib.parse import quote
import requests
from smolagents import tool
@tool
def get_weather(location: str) -> str:
"""Fetches the current live weather report and temperature for a given city or location.
Args:
location: The name of the city or location (e.g., 'Paris', 'Tokyo', 'San Francisco').
"""
try:
encoded_loc = quote(location)
url = f"https://wttr.in/{encoded_loc}?format=%C+%t+(feels+like+%f)+wind:+%w"
headers = {"User-Agent": "HF-Agents-Course-ArkAssistant/1.0"}
resp = requests.get(url, headers=headers, timeout=10)
if resp.status_code == 200 and resp.text:
return f"Weather for {location}: {resp.text.strip()}"
return f"Could not retrieve weather for {location} (status code {resp.status_code})."
except Exception as e:
return f"Error fetching weather for '{location}': {str(e)}"