Spaces:
Sleeping
Sleeping
File size: 2,634 Bytes
0debb06 f5deb0a 6aa8e64 0debb06 dea9f96 0debb06 f5deb0a 0debb06 326cc34 0debb06 f5deb0a 0debb06 4a43ed5 04c11ec dea9f96 4a43ed5 dea9f96 4a43ed5 dea9f96 4a43ed5 dea9f96 4a43ed5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import streamlit as st
from huggingface_hub import InferenceClient
import plotly.express as px
from datetime import datetime
import os
pip install --upgrade huggingface_hub
# ------------------- CONFIG -------------------
API_KEY = os.environ["HF_API_KEY"]
MODEL_ID = "duongtruongbinh/smolagents-weather-agent"
client = InferenceClient(API_KEY)
# ------------------- STREAMLIT PAGE -------------------
st.set_page_config(page_title="Weather Forecast App", page_icon="⛅", layout="wide")
mode = st.sidebar.radio("Choose Theme", ["Light", "Dark"])
if mode == "Dark":
st.markdown('<style>body {background-color: #0e1117; color: white;}</style>', unsafe_allow_html=True)
st.title("⛅ Weather Forecasting App")
st.markdown("Enter a city name to get current weather and a 5-day forecast")
city = st.text_input("🌍 Enter city name", "London")
if city:
with st.spinner("Fetching weather data..."):
try:
# ✅ Use text2text instead of client.model()
response = client.text2text(model=MODEL_ID, inputs={"location": city})
if "error" in response:
st.error("City not found or API issue. Try again!")
else:
st.subheader(f"Current Weather in {city.title()}")
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("🌡 Temperature", f"{response['temperature']} °C")
with col2:
st.metric("💧 Humidity", f"{response['humidity']} %")
with col3:
st.metric("🌬 Wind Speed", f"{response['wind_speed']} m/s")
with col4:
st.image(response['icon_url'])
st.write(f"**Condition:** {response['condition']}")
forecast = response['forecast']
dates = [datetime.fromisoformat(f['datetime']).strftime("%d %b") for f in forecast]
temps = [f['temp'] for f in forecast]
fig = px.line(
x=dates,
y=temps,
labels={"x": "Date", "y": "Temperature (°C)"},
title=f"5-Day Temperature Forecast for {city.title()}"
)
fig.update_layout(
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)' if mode=='Dark' else 'white',
font_color='white' if mode=='Dark' else 'black'
)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"API Error: {e}")
|