manaskhan commited on
Commit
136dd77
Β·
verified Β·
1 Parent(s): fc5eb77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -35
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
- # Step 1: Get latitude & longitude using OpenStreetMap (Nominatim)
10
- geocode_url = "https://nominatim.openstreetmap.org/search"
11
- params_geo = {"q": city, "format": "json", "limit": 1}
12
- resp_geo = requests.get(geocode_url, params=params_geo)
13
-
14
- if resp_geo.status_code != 200 or not resp_geo.json():
15
- return f"❌ Could not find location for '{city}'."
16
-
17
- loc = resp_geo.json()[0]
18
- lat, lon = loc["lat"], loc["lon"]
19
-
20
- # Step 2: Fetch current weather from Open-Meteo
21
- weather_url = "https://api.open-meteo.com/v1/forecast"
22
- params_weather = {
23
- "latitude": lat,
24
- "longitude": lon,
25
- "current_weather": True,
26
- "timezone": "auto"
27
- }
28
- resp_weather = requests.get(weather_url, params=params_weather)
29
-
30
- if resp_weather.status_code != 200 or "current_weather" not in resp_weather.json():
31
- return "❌ Failed to retrieve weather data."
32
-
33
- cw = resp_weather.json()["current_weather"]
34
- temp = cw.get("temperature")
35
- wind = cw.get("windspeed")
36
- desc = (
37
- f"πŸ“ Weather in {city.title()}:\n"
38
- f"🌑️ Temperature: {temp} °C\n"
39
- f"πŸ’¨ Wind Speed: {wind} km/h"
40
- )
41
- return desc
 
 
 
 
 
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 and wind speed using Open-Meteo API."
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__":