Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,126 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import os
|
| 4 |
-
from groq import Groq
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return None
|
| 37 |
-
|
| 38 |
-
def generate_weather_report(city_name):
|
| 39 |
-
"""
|
| 40 |
-
This is the main function called when the user clicks the button.
|
| 41 |
-
It combines Geocoding, Weather API, and Groq AI.
|
| 42 |
-
"""
|
| 43 |
if not city_name:
|
| 44 |
-
return "Please enter a city name.", "", "", ""
|
| 45 |
-
|
| 46 |
-
# 1. Get Coordinates
|
| 47 |
-
location = get_coordinates(city_name)
|
| 48 |
-
if not location:
|
| 49 |
-
return f"Could not find city: {city_name}", "-", "-", "-"
|
| 50 |
-
|
| 51 |
-
# 2. Get Weather
|
| 52 |
-
weather = get_weather_data(location['latitude'], location['longitude'])
|
| 53 |
-
if not weather:
|
| 54 |
-
return "Error fetching weather data.", "-", "-", "-"
|
| 55 |
-
|
| 56 |
-
current = weather['current']
|
| 57 |
-
temp = f"{current['temperature_2m']} °C"
|
| 58 |
-
humidity = f"{current['relative_humidity_2m']} %"
|
| 59 |
-
wind = f"{current['wind_speed_10m']} km/h"
|
| 60 |
-
|
| 61 |
-
# 3. Get AI Summary using Groq
|
| 62 |
-
if not client:
|
| 63 |
-
ai_message = "⚠️ Groq API Key is missing. Please add it to Settings > Secrets."
|
| 64 |
-
else:
|
| 65 |
-
try:
|
| 66 |
-
prompt = f"""
|
| 67 |
-
The weather in {city_name} is:
|
| 68 |
-
Temperature: {temp}
|
| 69 |
-
Humidity: {humidity}
|
| 70 |
-
Wind Speed: {wind}
|
| 71 |
-
|
| 72 |
-
Provide a witty, helpful weather summary for a visitor. (Max 50 words).
|
| 73 |
-
"""
|
| 74 |
-
completion = client.chat.completions.create(
|
| 75 |
-
messages=[{"role": "user", "content": prompt}],
|
| 76 |
-
model="mixtral-8x7b-32768",
|
| 77 |
-
)
|
| 78 |
-
ai_message = completion.choices[0].message.content
|
| 79 |
-
except Exception as e:
|
| 80 |
-
ai_message = f"AI Error: {str(e)}"
|
| 81 |
-
|
| 82 |
-
return ai_message, temp, humidity, wind
|
| 83 |
-
|
| 84 |
-
# --- UI Layout (Gradio Blocks) ---
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 87 |
|
| 88 |
-
# 1. Home / Header Section
|
| 89 |
gr.Markdown(
|
| 90 |
"""
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# 2. City Selection Section
|
| 97 |
with gr.Row():
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
-
# Connect the
|
| 118 |
search_btn.click(
|
| 119 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
inputs=city_input,
|
| 121 |
-
outputs=[
|
| 122 |
)
|
| 123 |
|
| 124 |
-
# Launch the app
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Helper function to decode WMO Weather Codes into human-readable text
|
| 5 |
+
def get_weather_description(code):
|
| 6 |
+
# WMO Weather interpretation codes (WW)
|
| 7 |
+
codes = {
|
| 8 |
+
0: ("☀️", "Clear sky"),
|
| 9 |
+
1: ("🌤️", "Mainly clear"),
|
| 10 |
+
2: ("⛅", "Partly cloudy"),
|
| 11 |
+
3: ("☁️", "Overcast"),
|
| 12 |
+
45: ("🌫️", "Fog"),
|
| 13 |
+
48: ("🌫️", "Depositing rime fog"),
|
| 14 |
+
51: ("🌦️", "Light Drizzle"),
|
| 15 |
+
53: ("🌦️", "Moderate Drizzle"),
|
| 16 |
+
55: ("🌧️", "Dense Drizzle"),
|
| 17 |
+
61: ("☔", "Slight Rain"),
|
| 18 |
+
63: ("☔", "Moderate Rain"),
|
| 19 |
+
65: ("⛈️", "Heavy Rain"),
|
| 20 |
+
71: ("🌨️", "Slight Snow"),
|
| 21 |
+
73: ("🌨️", "Moderate Snow"),
|
| 22 |
+
75: ("❄️", "Heavy Snow"),
|
| 23 |
+
80: ("🌧️", "Slight Rain Showers"),
|
| 24 |
+
81: ("🌧️", "Moderate Rain Showers"),
|
| 25 |
+
82: ("⛈️", "Violent Rain Showers"),
|
| 26 |
+
95: ("⚡", "Thunderstorm"),
|
| 27 |
+
96: ("⚡", "Thunderstorm with slight hail"),
|
| 28 |
+
99: ("⚡", "Thunderstorm with heavy hail"),
|
| 29 |
+
}
|
| 30 |
+
return codes.get(code, ("❓", "Unknown"))
|
| 31 |
+
|
| 32 |
+
# 2. Main Function to fetch data
|
| 33 |
+
def get_weather_data(city_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if not city_name:
|
| 35 |
+
return "Please enter a city name.", "", "", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
# Step A: Geocoding (Convert City Name to Latitude/Longitude)
|
| 39 |
+
geo_url = "https://geocoding-api.open-meteo.com/v1/search"
|
| 40 |
+
geo_params = {"name": city_name, "count": 1, "language": "en", "format": "json"}
|
| 41 |
+
|
| 42 |
+
geo_res = requests.get(geo_url, params=geo_params).json()
|
| 43 |
+
|
| 44 |
+
if not geo_res.get("results"):
|
| 45 |
+
return f"❌ Could not find city: '{city_name}'", "-", "-", "-", "-"
|
| 46 |
+
|
| 47 |
+
location = geo_res["results"][0]
|
| 48 |
+
lat = location["latitude"]
|
| 49 |
+
lon = location["longitude"]
|
| 50 |
+
city_display = f"{location['name']}, {location.get('country', '')}"
|
| 51 |
+
|
| 52 |
+
# Step B: Get Weather Data (Current conditions)
|
| 53 |
+
# We request: temp, relative humidity, apparent temp (feels like), weather code, wind speed
|
| 54 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
| 55 |
+
weather_params = {
|
| 56 |
+
"latitude": lat,
|
| 57 |
+
"longitude": lon,
|
| 58 |
+
"current": ["temperature_2m", "relative_humidity_2m", "apparent_temperature", "weather_code", "wind_speed_10m"],
|
| 59 |
+
"wind_speed_unit": "kmh"
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
w_res = requests.get(weather_url, params=weather_params).json()
|
| 63 |
+
current = w_res["current"]
|
| 64 |
+
|
| 65 |
+
# Step C: Format Data
|
| 66 |
+
temp = f"{current['temperature_2m']}°C"
|
| 67 |
+
feels_like = f"{current['apparent_temperature']}°C"
|
| 68 |
+
humidity = f"{current['relative_humidity_2m']}%"
|
| 69 |
+
wind = f"{current['wind_speed_10m']} km/h"
|
| 70 |
+
|
| 71 |
+
icon, description = get_weather_description(current["weather_code"])
|
| 72 |
+
status_text = f"{icon} {description}"
|
| 73 |
+
|
| 74 |
+
# Return all values to fill the UI boxes
|
| 75 |
+
return f"📍 Weather in {city_display}", temp, feels_like, status_text, f"💧 Humidity: {humidity} | 💨 Wind: {wind}"
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return f"⚠️ Error: {str(e)}", "-", "-", "-", "-"
|
| 79 |
+
|
| 80 |
+
# 3. Build the Professional UI using Blocks
|
| 81 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 82 |
|
|
|
|
| 83 |
gr.Markdown(
|
| 84 |
"""
|
| 85 |
+
# 🌍 Global Weather Tracker
|
| 86 |
+
### Real-time weather data powered by Open-Meteo
|
| 87 |
"""
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
city_input = gr.Textbox(
|
| 93 |
+
label="Search City",
|
| 94 |
+
placeholder="Type city name (e.g. Lahore, Tokyo)...",
|
| 95 |
+
lines=1
|
| 96 |
+
)
|
| 97 |
+
search_btn = gr.Button("Get Weather", variant="primary")
|
| 98 |
+
|
| 99 |
+
with gr.Column(scale=2):
|
| 100 |
+
# Header for the result
|
| 101 |
+
location_output = gr.Markdown("### 📍 Enter a city to begin")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
temp_output = gr.Textbox(label="Temperature", value="-")
|
| 105 |
+
feels_output = gr.Textbox(label="Feels Like", value="-")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
condition_output = gr.Textbox(label="Condition", value="-")
|
| 109 |
+
details_output = gr.Textbox(label="Details", value="-")
|
| 110 |
|
| 111 |
+
# Connect the button to the function
|
| 112 |
search_btn.click(
|
| 113 |
+
fn=get_weather_data,
|
| 114 |
+
inputs=city_input,
|
| 115 |
+
outputs=[location_output, temp_output, feels_output, condition_output, details_output]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Allow pressing "Enter" to search
|
| 119 |
+
city_input.submit(
|
| 120 |
+
fn=get_weather_data,
|
| 121 |
inputs=city_input,
|
| 122 |
+
outputs=[location_output, temp_output, feels_output, condition_output, details_output]
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch()
|