Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import plotly.express as px
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
# ------------------- CONFIG -------------------
|
| 8 |
-
API_KEY = "
|
| 9 |
MODEL_ID = "duongtruongbinh/smolagents-weather-agent"
|
| 10 |
|
| 11 |
client = InferenceClient(API_KEY)
|
| 12 |
|
|
|
|
| 13 |
st.set_page_config(page_title="Weather Forecast App", page_icon="⛅", layout="wide")
|
| 14 |
|
| 15 |
-
# ------------------- UI -------------------
|
| 16 |
mode = st.sidebar.radio("Choose Theme", ["Light", "Dark"])
|
| 17 |
if mode == "Dark":
|
| 18 |
st.markdown('<style>body {background-color: #0e1117; color: white;}</style>', unsafe_allow_html=True)
|
|
@@ -21,15 +21,17 @@ st.title("⛅ Weather Forecasting App")
|
|
| 21 |
st.markdown("Enter a city name to get current weather and a 5-day forecast")
|
| 22 |
|
| 23 |
city = st.text_input("🌍 Enter city name", "London")
|
|
|
|
| 24 |
if city:
|
| 25 |
try:
|
| 26 |
-
response = client(MODEL_ID, {"location": city}) # Correct
|
| 27 |
except Exception as e:
|
| 28 |
st.error(f"API Error: {e}")
|
| 29 |
else:
|
| 30 |
if "error" in response:
|
| 31 |
st.error("City not found or API issue. Try again!")
|
| 32 |
else:
|
|
|
|
| 33 |
st.subheader(f"Current Weather in {city.title()}")
|
| 34 |
col1, col2, col3, col4 = st.columns(4)
|
| 35 |
with col1:
|
|
@@ -43,15 +45,20 @@ if city:
|
|
| 43 |
|
| 44 |
st.write(f"**Condition:** {response['condition']}")
|
| 45 |
|
|
|
|
| 46 |
forecast = response['forecast']
|
| 47 |
dates = [datetime.fromisoformat(f['datetime']).strftime("%d %b") for f in forecast]
|
| 48 |
temps = [f['temp'] for f in forecast]
|
| 49 |
|
| 50 |
-
fig = px.line(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
st.plotly_chart(fig, use_container_width=True)
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import plotly.express as px
|
| 4 |
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# ------------------- CONFIG -------------------
|
| 8 |
+
API_KEY = os.environ["HF_API_KEY"] # Hugging Face secret
|
| 9 |
MODEL_ID = "duongtruongbinh/smolagents-weather-agent"
|
| 10 |
|
| 11 |
client = InferenceClient(API_KEY)
|
| 12 |
|
| 13 |
+
# ------------------- STREAMLIT PAGE -------------------
|
| 14 |
st.set_page_config(page_title="Weather Forecast App", page_icon="⛅", layout="wide")
|
| 15 |
|
|
|
|
| 16 |
mode = st.sidebar.radio("Choose Theme", ["Light", "Dark"])
|
| 17 |
if mode == "Dark":
|
| 18 |
st.markdown('<style>body {background-color: #0e1117; color: white;}</style>', unsafe_allow_html=True)
|
|
|
|
| 21 |
st.markdown("Enter a city name to get current weather and a 5-day forecast")
|
| 22 |
|
| 23 |
city = st.text_input("🌍 Enter city name", "London")
|
| 24 |
+
|
| 25 |
if city:
|
| 26 |
try:
|
| 27 |
+
response = client(MODEL_ID, {"location": city}) # Correct API call
|
| 28 |
except Exception as e:
|
| 29 |
st.error(f"API Error: {e}")
|
| 30 |
else:
|
| 31 |
if "error" in response:
|
| 32 |
st.error("City not found or API issue. Try again!")
|
| 33 |
else:
|
| 34 |
+
# Current weather metrics
|
| 35 |
st.subheader(f"Current Weather in {city.title()}")
|
| 36 |
col1, col2, col3, col4 = st.columns(4)
|
| 37 |
with col1:
|
|
|
|
| 45 |
|
| 46 |
st.write(f"**Condition:** {response['condition']}")
|
| 47 |
|
| 48 |
+
# 5-day forecast chart
|
| 49 |
forecast = response['forecast']
|
| 50 |
dates = [datetime.fromisoformat(f['datetime']).strftime("%d %b") for f in forecast]
|
| 51 |
temps = [f['temp'] for f in forecast]
|
| 52 |
|
| 53 |
+
fig = px.line(
|
| 54 |
+
x=dates,
|
| 55 |
+
y=temps,
|
| 56 |
+
labels={"x": "Date", "y": "Temperature (°C)"},
|
| 57 |
+
title=f"5-Day Temperature Forecast for {city.title()}"
|
| 58 |
+
)
|
| 59 |
+
fig.update_layout(
|
| 60 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 61 |
+
paper_bgcolor='rgba(0,0,0,0)' if mode=='Dark' else 'white',
|
| 62 |
+
font_color='white' if mode=='Dark' else 'black'
|
| 63 |
+
)
|
| 64 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|