Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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") # Fetch the API key from environment variables
|
| 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 = f"Current Temperature: {data['main']['temp']}°C"
|
| 17 |
+
humidity = f"Humidity: {data['main']['humidity']}%"
|
| 18 |
+
return weather, temp, humidity
|
| 19 |
+
else:
|
| 20 |
+
return "Failed to retrieve data", "", ""
|
| 21 |
+
|
| 22 |
+
# Define Gradio interface
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=get_weather,
|
| 25 |
+
inputs=gr.Textbox(label="Enter City Name"),
|
| 26 |
+
outputs=[
|
| 27 |
+
gr.Textbox(label="Weather"),
|
| 28 |
+
gr.Textbox(label="Temperature"),
|
| 29 |
+
gr.Textbox(label="Humidity")
|
| 30 |
+
],
|
| 31 |
+
title="Weather App",
|
| 32 |
+
description="Enter a city name to get the current weather description, temperature, and humidity."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the interface
|
| 36 |
+
iface.launch()
|