Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
# Function to fetch weather data
|
| 5 |
def get_weather(city: str):
|
| 6 |
if not city:
|
| 7 |
return "β Please enter a city name."
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Gradio Interface
|
| 44 |
demo = gr.Interface(
|
|
@@ -46,7 +50,7 @@ demo = gr.Interface(
|
|
| 46 |
inputs=gr.Textbox(label="City Name", placeholder="Enter city (e.g., Karachi)"),
|
| 47 |
outputs="text",
|
| 48 |
title="π¦οΈ Weather Forecast App",
|
| 49 |
-
description="Enter a city name to get the current temperature
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
|
|
|
| 4 |
def get_weather(city: str):
|
| 5 |
if not city:
|
| 6 |
return "β Please enter a city name."
|
| 7 |
|
| 8 |
+
try:
|
| 9 |
+
# Step 1: Geocode city β lat/lon
|
| 10 |
+
geocode_url = "https://nominatim.openstreetmap.org/search"
|
| 11 |
+
params_geo = {"q": city, "format": "json", "limit": 1}
|
| 12 |
+
headers = {"User-Agent": "GradioWeatherApp/1.0"} # required by Nominatim
|
| 13 |
+
resp_geo = requests.get(geocode_url, params=params_geo, headers=headers)
|
| 14 |
+
|
| 15 |
+
if resp_geo.status_code != 200 or not resp_geo.json():
|
| 16 |
+
return f"β Could not find location for '{city}'."
|
| 17 |
+
|
| 18 |
+
loc = resp_geo.json()[0]
|
| 19 |
+
lat, lon = loc["lat"], loc["lon"]
|
| 20 |
+
|
| 21 |
+
# Step 2: Get weather from Open-Meteo
|
| 22 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
| 23 |
+
params_weather = {
|
| 24 |
+
"latitude": lat,
|
| 25 |
+
"longitude": lon,
|
| 26 |
+
"current_weather": True,
|
| 27 |
+
"timezone": "auto"
|
| 28 |
+
}
|
| 29 |
+
resp_weather = requests.get(weather_url, params=params_weather)
|
| 30 |
+
|
| 31 |
+
if resp_weather.status_code != 200 or "current_weather" not in resp_weather.json():
|
| 32 |
+
return "β Failed to retrieve weather data."
|
| 33 |
+
|
| 34 |
+
cw = resp_weather.json()["current_weather"]
|
| 35 |
+
temp = cw.get("temperature")
|
| 36 |
+
wind = cw.get("windspeed")
|
| 37 |
+
|
| 38 |
+
return (
|
| 39 |
+
f"π Weather in {city.title()}:\n"
|
| 40 |
+
f"π‘οΈ Temperature: {temp} Β°C\n"
|
| 41 |
+
f"π¨ Wind Speed: {wind} km/h"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"β οΈ Error: {str(e)}"
|
| 46 |
|
| 47 |
# Gradio Interface
|
| 48 |
demo = gr.Interface(
|
|
|
|
| 50 |
inputs=gr.Textbox(label="City Name", placeholder="Enter city (e.g., Karachi)"),
|
| 51 |
outputs="text",
|
| 52 |
title="π¦οΈ Weather Forecast App",
|
| 53 |
+
description="Enter a city name to get the current weather (temperature + wind speed)."
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|