Spaces:
Sleeping
Sleeping
Update app.py
#1
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.linear_model import LinearRegression
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# CONFIGURATION
|
| 10 |
+
# -----------------------------
|
| 11 |
+
st.set_page_config(page_title="Smart Energy AI", layout="wide")
|
| 12 |
+
|
| 13 |
+
st.title("⚡ Smart Energy AI Chatbot")
|
| 14 |
+
st.subheader("Energy Consumption Analysis, Prediction & Sustainability Recommendations")
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# FILE UPLOAD SAFETY FIX
|
| 18 |
+
# -----------------------------
|
| 19 |
+
uploaded_file = st.file_uploader("Upload your energy consumption CSV", type=["csv"])
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
if uploaded_file is not None:
|
| 23 |
+
df = pd.read_csv(uploaded_file)
|
| 24 |
+
else:
|
| 25 |
+
df = pd.read_csv("energy_data.csv")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error("⚠️ Dataset not found or invalid file. Please upload a CSV.")
|
| 28 |
+
st.stop()
|
| 29 |
+
|
| 30 |
+
# -----------------------------
|
| 31 |
+
# DATA PREPROCESSING
|
| 32 |
+
# -----------------------------
|
| 33 |
+
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
| 34 |
+
df['usage_kwh'] = pd.to_numeric(df['usage_kwh'], errors='coerce')
|
| 35 |
+
df.dropna(inplace=True)
|
| 36 |
+
|
| 37 |
+
if df.empty:
|
| 38 |
+
st.warning("No valid data available.")
|
| 39 |
+
st.stop()
|
| 40 |
+
|
| 41 |
+
# -----------------------------
|
| 42 |
+
# SIDEBAR FILTERS
|
| 43 |
+
# -----------------------------
|
| 44 |
+
st.sidebar.header("Filters")
|
| 45 |
+
|
| 46 |
+
appliance_list = df['appliance'].dropna().unique()
|
| 47 |
+
|
| 48 |
+
appliance_filter = st.sidebar.multiselect(
|
| 49 |
+
"Select Appliance",
|
| 50 |
+
options=appliance_list,
|
| 51 |
+
default=appliance_list
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
filtered_df = df[df['appliance'].isin(appliance_filter)]
|
| 55 |
+
|
| 56 |
+
# -----------------------------
|
| 57 |
+
# DAILY ANALYSIS
|
| 58 |
+
# -----------------------------
|
| 59 |
+
daily_usage = filtered_df.groupby('date')['usage_kwh'].sum().reset_index()
|
| 60 |
+
|
| 61 |
+
total_consumption = filtered_df['usage_kwh'].sum()
|
| 62 |
+
avg_consumption = filtered_df['usage_kwh'].mean()
|
| 63 |
+
|
| 64 |
+
if not daily_usage.empty:
|
| 65 |
+
peak_day = daily_usage.loc[daily_usage['usage_kwh'].idxmax()]
|
| 66 |
+
else:
|
| 67 |
+
peak_day = {"date": pd.Timestamp.today(), "usage_kwh": 0}
|
| 68 |
+
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# METRICS
|
| 71 |
+
# -----------------------------
|
| 72 |
+
col1, col2, col3 = st.columns(3)
|
| 73 |
+
col1.metric("Total Consumption (kWh)", f"{total_consumption:.2f}")
|
| 74 |
+
col2.metric("Average Usage (kWh)", f"{avg_consumption:.2f}")
|
| 75 |
+
col3.metric("Peak Usage Day", str(peak_day['date'].date()))
|
| 76 |
+
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# LINE CHART (SAFE)
|
| 79 |
+
# -----------------------------
|
| 80 |
+
st.subheader("📈 Daily Energy Consumption Trend")
|
| 81 |
+
|
| 82 |
+
if not daily_usage.empty:
|
| 83 |
+
fig = px.line(daily_usage, x='date', y='usage_kwh', markers=True)
|
| 84 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 85 |
+
else:
|
| 86 |
+
st.info("Not enough data for chart")
|
| 87 |
+
|
| 88 |
+
# -----------------------------
|
| 89 |
+
# APPLIANCE CHART
|
| 90 |
+
# -----------------------------
|
| 91 |
+
st.subheader("🔌 Appliance Usage")
|
| 92 |
+
|
| 93 |
+
appliance_usage = filtered_df.groupby('appliance')['usage_kwh'].sum().reset_index()
|
| 94 |
+
|
| 95 |
+
if not appliance_usage.empty:
|
| 96 |
+
fig2 = px.pie(appliance_usage, names='appliance', values='usage_kwh')
|
| 97 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 98 |
+
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# SIMPLE PREDICTION
|
| 101 |
+
# -----------------------------
|
| 102 |
+
st.subheader("🔮 Prediction")
|
| 103 |
+
|
| 104 |
+
if len(daily_usage) > 1:
|
| 105 |
+
daily_usage['day'] = np.arange(len(daily_usage))
|
| 106 |
+
|
| 107 |
+
X = daily_usage[['day']]
|
| 108 |
+
y = daily_usage['usage_kwh']
|
| 109 |
+
|
| 110 |
+
model = LinearRegression()
|
| 111 |
+
model.fit(X, y)
|
| 112 |
+
|
| 113 |
+
next_day = np.array([[len(daily_usage)]])
|
| 114 |
+
prediction = model.predict(next_day)[0]
|
| 115 |
+
|
| 116 |
+
st.success(f"Predicted next usage: {prediction:.2f} kWh")
|
| 117 |
+
else:
|
| 118 |
+
st.info("Not enough data for prediction")
|
| 119 |
+
|
| 120 |
+
# -----------------------------
|
| 121 |
+
# FOOTER
|
| 122 |
+
# -----------------------------
|
| 123 |
+
st.caption("Built with Streamlit | AI + Sustainability + Data Analytics")
|