Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,21 @@ def kelvin_to_celsius(temp_kelvin):
|
|
| 8 |
return temp_kelvin - 273.15
|
| 9 |
|
| 10 |
def get_weather(city_name, api_key):
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def summarize_text(text):
|
| 14 |
"""Summarize the provided text using OpenAI."""
|
|
@@ -16,7 +30,28 @@ def summarize_text(text):
|
|
| 16 |
return response.choices[0].text.strip()
|
| 17 |
|
| 18 |
def compare_weather(city1, city2):
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Sample cities for dropdown
|
| 22 |
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"]
|
|
@@ -30,4 +65,5 @@ interface = gr.Interface(
|
|
| 30 |
outputs="text"
|
| 31 |
)
|
| 32 |
|
| 33 |
-
if
|
|
|
|
|
|
| 8 |
return temp_kelvin - 273.15
|
| 9 |
|
| 10 |
def get_weather(city_name, api_key):
|
| 11 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
| 12 |
+
complete_url = base_url + "q=" + city_name + "&appid=" + api_key
|
| 13 |
+
response = requests.get(complete_url)
|
| 14 |
+
data = response.json()
|
| 15 |
+
|
| 16 |
+
if data.get("cod") == 200:
|
| 17 |
+
main_data = data.get("main", {})
|
| 18 |
+
weather_data = data.get("weather", [{}])[0]
|
| 19 |
+
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 20 |
+
pressure = main_data.get("pressure", "N/A")
|
| 21 |
+
humidity = main_data.get("humidity", "N/A")
|
| 22 |
+
weather_description = weather_data.get("description", "N/A")
|
| 23 |
+
return temperature, pressure, humidity, weather_description
|
| 24 |
+
else:
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
def summarize_text(text):
|
| 28 |
"""Summarize the provided text using OpenAI."""
|
|
|
|
| 30 |
return response.choices[0].text.strip()
|
| 31 |
|
| 32 |
def compare_weather(city1, city2):
|
| 33 |
+
api_key = "91b23cab82ee530b2052c8757e343b0d"
|
| 34 |
+
weather1 = get_weather(city1, api_key)
|
| 35 |
+
weather2 = get_weather(city2, api_key)
|
| 36 |
+
|
| 37 |
+
if weather1 and weather2:
|
| 38 |
+
comparison = f"""
|
| 39 |
+
{city1}:
|
| 40 |
+
Temperature: {weather1[0]:.2f}°C
|
| 41 |
+
Pressure: {weather1[1]} hPa
|
| 42 |
+
Humidity: {weather1[2]}%
|
| 43 |
+
Condition: {weather1[3]}
|
| 44 |
+
|
| 45 |
+
{city2}:
|
| 46 |
+
Temperature: {weather2[0]:.2f}°C
|
| 47 |
+
Pressure: {weather2[1]} hPa
|
| 48 |
+
Humidity: {weather2[2]}%
|
| 49 |
+
Condition: {weather2[3]}
|
| 50 |
+
"""
|
| 51 |
+
summary = summarize_text(comparison)
|
| 52 |
+
return summary
|
| 53 |
+
else:
|
| 54 |
+
return "Error fetching weather data for one or both cities."
|
| 55 |
|
| 56 |
# Sample cities for dropdown
|
| 57 |
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"]
|
|
|
|
| 65 |
outputs="text"
|
| 66 |
)
|
| 67 |
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
interface.launch()
|