Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import geocoder
|
| 4 |
+
|
| 5 |
+
# π Put your API key here
|
| 6 |
+
API_KEY = "YOUR_API_KEY_HERE"
|
| 7 |
+
|
| 8 |
+
# -------- FUNCTIONS --------
|
| 9 |
+
def get_weather(city):
|
| 10 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
|
| 11 |
+
return requests.get(url).json()
|
| 12 |
+
|
| 13 |
+
def get_forecast(city):
|
| 14 |
+
url = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={API_KEY}&units=metric"
|
| 15 |
+
return requests.get(url).json()
|
| 16 |
+
|
| 17 |
+
def get_location():
|
| 18 |
+
g = geocoder.ip('me')
|
| 19 |
+
return g.city
|
| 20 |
+
|
| 21 |
+
# -------- UI --------
|
| 22 |
+
st.set_page_config(page_title="π¦ Weather App", layout="centered")
|
| 23 |
+
|
| 24 |
+
st.title("π¦ Smart Weather App")
|
| 25 |
+
|
| 26 |
+
# Auto location
|
| 27 |
+
if st.button("π Use My Location"):
|
| 28 |
+
city = get_location()
|
| 29 |
+
st.success(f"Detected City: {city}")
|
| 30 |
+
else:
|
| 31 |
+
city = ""
|
| 32 |
+
|
| 33 |
+
# Manual input (voice removed because HF doesn't support mic well)
|
| 34 |
+
city = st.text_input("Enter City Name", city)
|
| 35 |
+
|
| 36 |
+
# Weather
|
| 37 |
+
if city:
|
| 38 |
+
weather = get_weather(city)
|
| 39 |
+
|
| 40 |
+
if weather.get("cod") != 200:
|
| 41 |
+
st.error("β City not found")
|
| 42 |
+
else:
|
| 43 |
+
st.subheader(f"π Weather in {city}")
|
| 44 |
+
|
| 45 |
+
st.metric("π‘ Temperature", f"{weather['main']['temp']} Β°C")
|
| 46 |
+
st.metric("π§ Humidity", f"{weather['main']['humidity']} %")
|
| 47 |
+
st.write(f"π₯ Condition: {weather['weather'][0]['description']}")
|
| 48 |
+
|
| 49 |
+
# Forecast
|
| 50 |
+
st.subheader("π
5-Day Forecast")
|
| 51 |
+
|
| 52 |
+
forecast = get_forecast(city)
|
| 53 |
+
|
| 54 |
+
for i in range(0, 40, 8):
|
| 55 |
+
data = forecast['list'][i]
|
| 56 |
+
st.write(
|
| 57 |
+
f"π {data['dt_txt']} | π‘ {data['main']['temp']}Β°C | {data['weather'][0]['description']}"
|
| 58 |
+
)
|