Spaces:
Sleeping
Sleeping
Upload tools/weather_fetcher.py with huggingface_hub
Browse files- tools/weather_fetcher.py +60 -0
tools/weather_fetcher.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# --- User Defined Logic ---
|
| 7 |
+
import os
|
| 8 |
+
import requests
|
| 9 |
+
from typing import Dict, Any
|
| 10 |
+
|
| 11 |
+
def weather_fetcher(city: str) -> str:
|
| 12 |
+
"""Fetch current weather information for a given city using OpenWeatherMap API.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
city: Name of the city to get weather data for.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
A formatted string containing temperature (°C), humidity (%), weather conditions,
|
| 19 |
+
and wind speed (m/s) for the specified city.
|
| 20 |
+
"""
|
| 21 |
+
api_key = os.getenv("OPENWEATHERMAP_API_KEY", "demo_key_for_testing")
|
| 22 |
+
base_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 23 |
+
params = {
|
| 24 |
+
"q": city,
|
| 25 |
+
"appid": api_key,
|
| 26 |
+
"units": "metric"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(base_url, params=params, timeout=10)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
data = response.json()
|
| 33 |
+
|
| 34 |
+
temp = data["main"]["temp"]
|
| 35 |
+
humidity = data["main"]["humidity"]
|
| 36 |
+
conditions = data["weather"][0]["description"]
|
| 37 |
+
wind_speed = data["wind"]["speed"]
|
| 38 |
+
|
| 39 |
+
return (
|
| 40 |
+
f"🌤️ Weather in {city.title()}:\n"
|
| 41 |
+
f"🌡️ Temperature: {temp}°C\n"
|
| 42 |
+
f"💧 Humidity: {humidity}%\n"
|
| 43 |
+
f"☁️ Conditions: {conditions.title()}\n"
|
| 44 |
+
f"💨 Wind Speed: {wind_speed} m/s\n"
|
| 45 |
+
f"📍 Coordinates: {data['coord']['lat']}, {data['coord']['lon']}"
|
| 46 |
+
)
|
| 47 |
+
except requests.exceptions.RequestException as e:
|
| 48 |
+
return f"❌ Error fetching weather data for {city}: {str(e)}\n\n💡 Note: This tool requires a valid OpenWeatherMap API key. You can get one for free at https://openweathermap.org/api"
|
| 49 |
+
except KeyError as e:
|
| 50 |
+
return f"❌ Error parsing weather data: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# --- Interface Factory ---
|
| 53 |
+
def create_interface():
|
| 54 |
+
return gr.Interface(
|
| 55 |
+
fn=weather_fetcher,
|
| 56 |
+
inputs=[gr.Textbox(label=k) for k in ['city']],
|
| 57 |
+
outputs=gr.Textbox(label="A formatted string containing temperature (°C), humidity (%), weather conditions, wind speed (m/s), and coordinates for the specified city."),
|
| 58 |
+
title="weather-fetcher",
|
| 59 |
+
description="Auto-generated tool: weather-fetcher"
|
| 60 |
+
)
|