Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
"""Fetch weather information for a city using OpenWeatherMap API."""
|
| 9 |
-
params = {
|
| 10 |
-
'q': city_name,
|
| 11 |
-
'appid': API_KEY,
|
| 12 |
-
'units': 'metric' # This will return temperature in Celsius
|
| 13 |
-
}
|
| 14 |
-
response = requests.get(BASE_URL, params=params)
|
| 15 |
data = response.json()
|
| 16 |
|
| 17 |
-
if data[
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
else:
|
| 26 |
-
return
|
| 27 |
-
'City': city_name,
|
| 28 |
-
'Error': data['message']
|
| 29 |
-
}
|
| 30 |
|
| 31 |
def compare_weather(city1, city2):
|
| 32 |
-
|
| 33 |
-
weather1 = get_weather(city1)
|
| 34 |
-
weather2 = get_weather(city2)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
inputs=[gr.inputs.Textbox(placeholder="Enter first city..."),
|
| 42 |
-
gr.inputs.Textbox(placeholder="Enter second city...")],
|
| 43 |
-
outputs=[gr.outputs.JSON(label="City 1 Weather"),
|
| 44 |
-
gr.outputs.JSON(label="City 2 Weather")],
|
| 45 |
-
live=True
|
| 46 |
-
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
def get_weather(city_name, api_key):
|
| 5 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
| 6 |
+
complete_url = base_url + "q=" + city_name + "&appid=" + api_key
|
| 7 |
+
response = requests.get(complete_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
data = response.json()
|
| 9 |
|
| 10 |
+
if data["cod"] != "404":
|
| 11 |
+
main_data = data["main"]
|
| 12 |
+
weather_data = data["weather"][0]
|
| 13 |
+
temperature = main_data["temp"]
|
| 14 |
+
pressure = main_data["pressure"]
|
| 15 |
+
humidity = main_data["humidity"]
|
| 16 |
+
weather_description = weather_data["description"]
|
| 17 |
+
return temperature, pressure, humidity, weather_description
|
| 18 |
else:
|
| 19 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def compare_weather(city1, city2):
|
| 22 |
+
api_key = "91b23cab82ee530b2052c8757e343b0d"
|
| 23 |
+
weather1 = get_weather(city1, api_key)
|
| 24 |
+
weather2 = get_weather(city2, api_key)
|
| 25 |
|
| 26 |
+
if weather1 and weather2:
|
| 27 |
+
comparison = f"""
|
| 28 |
+
{city1}:
|
| 29 |
+
Temperature: {weather1[0]} K
|
| 30 |
+
Pressure: {weather1[1]} hPa
|
| 31 |
+
Humidity: {weather1[2]}%
|
| 32 |
+
Description: {weather1[3]}
|
| 33 |
+
|
| 34 |
+
{city2}:
|
| 35 |
+
Temperature: {weather2[0]} K
|
| 36 |
+
Pressure: {weather2[1]} hPa
|
| 37 |
+
Humidity: {weather2[2]}%
|
| 38 |
+
Description: {weather2[3]}
|
| 39 |
+
"""
|
| 40 |
+
return comparison
|
| 41 |
+
else:
|
| 42 |
+
return "Error fetching weather data for one or both cities."
|
| 43 |
|
| 44 |
+
interface = gr.Interface(fn=compare_weather,
|
| 45 |
+
inputs=["text", "text"],
|
| 46 |
+
outputs="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
+
interface.launch()
|