sairaishaq commited on
Commit
ff8da80
·
verified ·
1 Parent(s): 1ea1970

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ def get_weather(city: str):
5
+ """Fetch weather forecast for a given city using Open-Meteo API"""
6
+ try:
7
+ # Step 1: Get latitude & longitude from Open-Meteo geocoding API
8
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
9
+ geo_response = requests.get(geo_url).json()
10
+
11
+ if "results" not in geo_response:
12
+ return f"❌ Could not find location: {city}"
13
+
14
+ lat = geo_response["results"][0]["latitude"]
15
+ lon = geo_response["results"][0]["longitude"]
16
+
17
+ # Step 2: Get weather forecast
18
+ weather_url = (
19
+ f"https://api.open-meteo.com/v1/forecast"
20
+ f"?latitude={lat}&longitude={lon}"
21
+ f"&current_weather=true"
22
+ )
23
+ weather_response = requests.get(weather_url).json()
24
+
25
+ if "current_weather" not in weather_response:
26
+ return "❌ Weather data not available."
27
+
28
+ weather = weather_response["current_weather"]
29
+ temp = weather["temperature"]
30
+ wind = weather["windspeed"]
31
+ desc = f"🌡 Temperature: {temp}°C\n💨 Windspeed: {wind} km/h"
32
+
33
+ return f"📍 City: {city}\n{desc}"
34
+
35
+ except Exception as e:
36
+ return f"⚠️ Error: {str(e)}"
37
+
38
+ # Gradio Interface
39
+ iface = gr.Interface(
40
+ fn=get_weather,
41
+ inputs=gr.Textbox(label="Enter City Name"),
42
+ outputs=gr.Textbox(label="Weather Forecast"),
43
+ title="🌤 Weather Forecast App",
44
+ description="Enter a city name to get the current weather forecast (powered by Open-Meteo API).",
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ iface.launch()