Spaces:
Sleeping
Sleeping
| from smolagents import Tool | |
| class GetWeather(Tool): | |
| name: str = "get_weather" | |
| description: str = "Get the weather of a given location." | |
| inputs: dict = {'location': {'type': 'string', 'description': 'The location to get the weather of.'}} | |
| output_type: str = "string" | |
| def forward(self, location: str) -> str: | |
| import requests | |
| if not location: | |
| return "No location provided" | |
| lat, long = self.get_lat_long(location) | |
| if lat == "Location not found" or long == "Location not found": | |
| return "Location not found" | |
| timezone = self.get_timezone(lat, long) | |
| if timezone == "Timezone not found": | |
| return "Timezone not found" | |
| api_call = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={long}¤t=temperature_2m,apparent_temperature,precipitation,wind_speed_10m&timezone={timezone}&past_days=1&forecast_days=1" | |
| try: | |
| response = requests.get(api_call) | |
| data = response.json() | |
| return f"Temperature: {data['current']['temperature_2m']}{data['current_units']['temperature_2m']}\n" + f"Feels like: {data['current']['apparent_temperature']}{data['current_units']['apparent_temperature']}\n" + f"Precipitation: {data['current']['precipitation']}{data['current_units']['precipitation']}\n" + f"Wind speed: {data['current']['wind_speed_10m']}{data['current_units']['wind_speed_10m']}" | |
| except Exception as e: | |
| return "Error fetching weather data" | |
| def get_lat_long(self, address: str) -> tuple | str: | |
| """ | |
| Get the latitude and longitude of a given address | |
| Args: | |
| address: A string representing a valid address (e.g., '1600 Amphitheatre Parkway, Mountain View, CA'). | |
| """ | |
| try: | |
| from geopy.geocoders import Nominatim | |
| except ImportError as e: | |
| raise ImportError( | |
| "You must install the `geopy` package to run this tool: for instance run `pip install geopy`." | |
| ) | |
| if not address: | |
| return "No address provided", "No address provided" | |
| geolocator = Nominatim(user_agent="myGeocoder", timeout=10) | |
| try: | |
| location = geolocator.geocode(address) | |
| return location.latitude, location.longitude | |
| except: | |
| return "Location not found", "Location not found" | |
| def get_timezone(self, lat: float, long: float) -> str: | |
| """ | |
| Get the timezone of a given latitude and longitude | |
| Args: | |
| lat: A float representing the latitude of a location. | |
| long: A float representing the longitude of a location. | |
| """ | |
| try: | |
| from timezonefinder import TimezoneFinder | |
| except ImportError as e: | |
| raise ImportError( | |
| "You must install the `timezonefinder` package to run this tool: for instance run `pip install timezonefinder`." | |
| ) | |
| if not lat or not long: | |
| return "No latitude or longitude provided" | |
| tf = TimezoneFinder() | |
| try: | |
| timezone_str = tf.timezone_at(lng=long, lat=lat) | |
| return timezone_str | |
| except: | |
| return "Timezone not found" | |
| def __init__(self, *args, **kwargs): | |
| self.is_initialized = False | |