Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
API_KEY = "YOUR_OPENWEATHERMAP_API_KEY" # Replace with your own API key
|
| 5 |
+
|
| 6 |
+
def get_weather(city):
|
| 7 |
+
try:
|
| 8 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
data = response.json()
|
| 11 |
+
|
| 12 |
+
if data.get("cod") != 200:
|
| 13 |
+
return f"Error: {data.get('message', 'City not found')}"
|
| 14 |
+
|
| 15 |
+
city_name = data["name"]
|
| 16 |
+
country = data["sys"]["country"]
|
| 17 |
+
temp = data["main"]["temp"]
|
| 18 |
+
description = data["weather"][0]["description"].title()
|
| 19 |
+
humidity = data["main"]["humidity"]
|
| 20 |
+
wind_speed = data["wind"]["speed"]
|
| 21 |
+
|
| 22 |
+
weather_report = (
|
| 23 |
+
f"Weather in {city_name}, {country}:\n"
|
| 24 |
+
f"Temperature: {temp}°C\n"
|
| 25 |
+
f"Condition: {description}\n"
|
| 26 |
+
f"Humidity: {humidity}%\n"
|
| 27 |
+
f"Wind Speed: {wind_speed} m/s"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return weather_report
|
| 31 |
+
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=get_weather,
|
| 38 |
+
inputs=gr.Textbox(label="Enter city name"),
|
| 39 |
+
outputs=gr.Textbox(label="Weather Forecast"),
|
| 40 |
+
title="Weather Forecast App",
|
| 41 |
+
description="Enter a city name to get the current weather forecast."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch()
|