Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,19 @@
|
|
| 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 |
-
# Check if the API response was successful
|
| 11 |
if data.get("cod") == 200:
|
| 12 |
main_data = data.get("main", {})
|
| 13 |
weather_data = data.get("weather", [{}])[0]
|
| 14 |
-
temperature = main_data.get("temp",
|
| 15 |
pressure = main_data.get("pressure", "N/A")
|
| 16 |
humidity = main_data.get("humidity", "N/A")
|
| 17 |
weather_description = weather_data.get("description", "N/A")
|
|
@@ -27,24 +29,32 @@ def compare_weather(city1, city2):
|
|
| 27 |
if weather1 and weather2:
|
| 28 |
comparison = f"""
|
| 29 |
{city1}:
|
| 30 |
-
Temperature: {weather1[0]}
|
| 31 |
Pressure: {weather1[1]} hPa
|
| 32 |
Humidity: {weather1[2]}%
|
| 33 |
-
|
| 34 |
|
| 35 |
{city2}:
|
| 36 |
-
Temperature: {weather2[0]}
|
| 37 |
Pressure: {weather2[1]} hPa
|
| 38 |
Humidity: {weather2[2]}%
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
return comparison
|
| 42 |
else:
|
| 43 |
return "Error fetching weather data for one or both cities."
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
def kelvin_to_celsius(temp_kelvin):
|
| 5 |
+
return temp_kelvin - 273.15
|
| 6 |
+
|
| 7 |
def get_weather(city_name, api_key):
|
| 8 |
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
| 9 |
complete_url = base_url + "q=" + city_name + "&appid=" + api_key
|
| 10 |
response = requests.get(complete_url)
|
| 11 |
data = response.json()
|
| 12 |
|
|
|
|
| 13 |
if data.get("cod") == 200:
|
| 14 |
main_data = data.get("main", {})
|
| 15 |
weather_data = data.get("weather", [{}])[0]
|
| 16 |
+
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 17 |
pressure = main_data.get("pressure", "N/A")
|
| 18 |
humidity = main_data.get("humidity", "N/A")
|
| 19 |
weather_description = weather_data.get("description", "N/A")
|
|
|
|
| 29 |
if weather1 and weather2:
|
| 30 |
comparison = f"""
|
| 31 |
{city1}:
|
| 32 |
+
Temperature: {weather1[0]:.2f}°C
|
| 33 |
Pressure: {weather1[1]} hPa
|
| 34 |
Humidity: {weather1[2]}%
|
| 35 |
+
Condition: {weather1[3]}
|
| 36 |
|
| 37 |
{city2}:
|
| 38 |
+
Temperature: {weather2[0]:.2f}°C
|
| 39 |
Pressure: {weather2[1]} hPa
|
| 40 |
Humidity: {weather2[2]}%
|
| 41 |
+
Condition: {weather2[3]}
|
| 42 |
"""
|
| 43 |
return comparison
|
| 44 |
else:
|
| 45 |
return "Error fetching weather data for one or both cities."
|
| 46 |
|
| 47 |
+
# Sample cities for dropdown
|
| 48 |
+
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"]
|
| 49 |
+
|
| 50 |
+
interface = gr.Interface(
|
| 51 |
+
fn=compare_weather,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.inputs.Dropdown(choices=cities, label="City 1"),
|
| 54 |
+
gr.inputs.Dropdown(choices=cities, label="City 2")
|
| 55 |
+
],
|
| 56 |
+
outputs="text"
|
| 57 |
+
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
interface.launch()
|