Spaces:
Sleeping
Sleeping
| 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}") | |