Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,49 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
API_KEY = "91b23cab82ee530b2052c8757e343b0d"
|
| 5 |
+
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
|
| 6 |
+
|
| 7 |
+
def get_weather(city_name):
|
| 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['cod'] == 200:
|
| 18 |
+
return {
|
| 19 |
+
'City': city_name,
|
| 20 |
+
'Temperature': data['main']['temp'],
|
| 21 |
+
'Weather': data['weather'][0]['description'],
|
| 22 |
+
'Humidity': data['main']['humidity'],
|
| 23 |
+
'Wind Speed': data['wind']['speed']
|
| 24 |
+
}
|
| 25 |
+
else:
|
| 26 |
+
return {
|
| 27 |
+
'City': city_name,
|
| 28 |
+
'Error': data['message']
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def compare_weather(city1, city2):
|
| 32 |
+
"""Compare weather of two cities."""
|
| 33 |
+
weather1 = get_weather(city1)
|
| 34 |
+
weather2 = get_weather(city2)
|
| 35 |
+
|
| 36 |
+
return weather1, weather2
|
| 37 |
+
|
| 38 |
+
# Gradio Interface
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=compare_weather,
|
| 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 |
+
iface.launch()
|