import streamlit as st import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression import plotly.express as px # ----------------------------- # CONFIGURATION # ----------------------------- st.set_page_config(page_title="Smart Energy AI", layout="wide") st.title("⚡ Smart Energy AI Chatbot") st.subheader("Energy Consumption Analysis, Prediction & Sustainability Recommendations") # ----------------------------- # FILE UPLOAD SAFETY FIX # ----------------------------- uploaded_file = st.file_uploader("Upload your energy consumption CSV", type=["csv"]) try: if uploaded_file is not None: df = pd.read_csv(uploaded_file) else: df = pd.read_csv("energy_data.csv") except Exception as e: st.error("⚠️ Dataset not found or invalid file. Please upload a CSV.") st.stop() # ----------------------------- # DATA PREPROCESSING # ----------------------------- df['date'] = pd.to_datetime(df['date'], errors='coerce') df['usage_kwh'] = pd.to_numeric(df['usage_kwh'], errors='coerce') df.dropna(inplace=True) if df.empty: st.warning("No valid data available.") st.stop() # ----------------------------- # SIDEBAR FILTERS # ----------------------------- st.sidebar.header("Filters") appliance_list = df['appliance'].dropna().unique() appliance_filter = st.sidebar.multiselect( "Select Appliance", options=appliance_list, default=appliance_list ) filtered_df = df[df['appliance'].isin(appliance_filter)] # ----------------------------- # DAILY ANALYSIS # ----------------------------- daily_usage = filtered_df.groupby('date')['usage_kwh'].sum().reset_index() total_consumption = filtered_df['usage_kwh'].sum() avg_consumption = filtered_df['usage_kwh'].mean() if not daily_usage.empty: peak_day = daily_usage.loc[daily_usage['usage_kwh'].idxmax()] else: peak_day = {"date": pd.Timestamp.today(), "usage_kwh": 0} # ----------------------------- # METRICS # ----------------------------- col1, col2, col3 = st.columns(3) col1.metric("Total Consumption (kWh)", f"{total_consumption:.2f}") col2.metric("Average Usage (kWh)", f"{avg_consumption:.2f}") col3.metric("Peak Usage Day", str(peak_day['date'].date())) # ----------------------------- # LINE CHART (SAFE) # ----------------------------- st.subheader("📈 Daily Energy Consumption Trend") if not daily_usage.empty: fig = px.line(daily_usage, x='date', y='usage_kwh', markers=True) st.plotly_chart(fig, use_container_width=True) else: st.info("Not enough data for chart") # ----------------------------- # APPLIANCE CHART # ----------------------------- st.subheader("🔌 Appliance Usage") appliance_usage = filtered_df.groupby('appliance')['usage_kwh'].sum().reset_index() if not appliance_usage.empty: fig2 = px.pie(appliance_usage, names='appliance', values='usage_kwh') st.plotly_chart(fig2, use_container_width=True) # ----------------------------- # SIMPLE PREDICTION # ----------------------------- st.subheader("🔮 Prediction") if len(daily_usage) > 1: daily_usage['day'] = np.arange(len(daily_usage)) X = daily_usage[['day']] y = daily_usage['usage_kwh'] model = LinearRegression() model.fit(X, y) next_day = np.array([[len(daily_usage)]]) prediction = model.predict(next_day)[0] st.success(f"Predicted next usage: {prediction:.2f} kWh") else: st.info("Not enough data for prediction") # ----------------------------- # FOOTER # ----------------------------- st.caption("Built with Streamlit | AI + Sustainability + Data Analytics")