bonosa commited on
Commit
3115702
·
1 Parent(s): 9f1b596
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ def get_weather(city, units):
6
+ API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
7
+
8
+ response = requests.get(
9
+ "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units={}".format(city, API_KEY, units)
10
+ )
11
+
12
+ if response.status_code != 200:
13
+ raise Exception("Error getting weather data: {}".format(response.status_code))
14
+
15
+ weather_data = json.loads(response.content.decode("utf-8"))
16
+
17
+ weather_description = weather_data["weather"][0]["description"]
18
+ temperature = weather_data["main"]["temp"]
19
+ pressure = weather_data["main"]["pressure"]
20
+ humidity = weather_data["main"]["humidity"]
21
+
22
+ return weather_description, temperature, pressure, humidity
23
+
24
+ def app(city: str, units: str, show_pressure_humidity: bool):
25
+ weather_description, temperature, pressure, humidity = get_weather(city, units)
26
+
27
+ temperature_units = "Imperial"
28
+ if units == "metric":
29
+ temperature_units = "Celsius"
30
+ elif units == "imperial":
31
+ temperature_units = "Fahrenheit"
32
+
33
+ weather_info = f"The weather in {city} is {weather_description} and the temperature is {temperature} {temperature_units}."
34
+
35
+ if show_pressure_humidity:
36
+ weather_info += f" The pressure is {pressure} hPa and the humidity is {humidity}%."
37
+
38
+ return weather_info
39
+
40
+ iface = gr.Interface(fn=app,
41
+ inputs=["text",
42
+ gr.inputs.Radio(["metric", "imperial"], label="Temperature Units"),
43
+ gr.inputs.Checkbox(label="Show Pressure and Humidity")],
44
+ outputs="text",
45
+ title="Weather Forecast",
46
+ description="Enter a city and get the current weather forecast.")
47
+
48
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ altair<5