Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def get_weather(city_name):
|
| 6 |
+
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
| 7 |
+
if not API_Key:
|
| 8 |
+
return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
|
| 9 |
+
|
| 10 |
+
url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
|
| 13 |
+
if response.status_code == 200:
|
| 14 |
+
data = response.json()
|
| 15 |
+
weather = data['weather'][0]['description']
|
| 16 |
+
temp = data['main']['temp']
|
| 17 |
+
humidity = data['main']['humidity']
|
| 18 |
+
|
| 19 |
+
# Adding emojis and creative text
|
| 20 |
+
weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
|
| 21 |
+
return (f"In {city_name}, the weather is currently {weather} {weather_emoji}. "
|
| 22 |
+
f"The temperature is a comfy {temp}°C 🌡️. "
|
| 23 |
+
f"Also, humidity levels are at {humidity}%, so keep hydrated! 💧")
|
| 24 |
+
else:
|
| 25 |
+
return "Failed to retrieve data. Please check the city name and try again."
|
| 26 |
+
|
| 27 |
+
# Define Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=get_weather,
|
| 30 |
+
inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
|
| 31 |
+
outputs=gr.Textbox(label="Weather Update"),
|
| 32 |
+
title="WeatherAssistantApp",
|
| 33 |
+
description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the interface
|
| 37 |
+
iface.launch()
|