Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
# APPLIANCE DISTRIBUTION
|
| 3 |
+
# -----------------------------
|
| 4 |
+
st.subheader("🔌 Appliance-wise Energy Usage")
|
| 5 |
+
appliance_usage = filtered_df.groupby('appliance')['usage_kwh'].sum().reset_index()
|
| 6 |
+
fig = px.pie(appliance_usage, names='appliance', values='usage_kwh', title='Energy Usage Distribution')
|
| 7 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# MACHINE LEARNING PREDICTION
|
| 11 |
+
# -----------------------------
|
| 12 |
+
st.subheader("🔮 Future Energy Usage Prediction")
|
| 13 |
+
|
| 14 |
+
daily_usage['day_number'] = np.arange(len(daily_usage))
|
| 15 |
+
X = daily_usage[['day_number']]
|
| 16 |
+
y = daily_usage['usage_kwh']
|
| 17 |
+
|
| 18 |
+
model = LinearRegression()
|
| 19 |
+
model.fit(X, y)
|
| 20 |
+
|
| 21 |
+
future_day = len(daily_usage)
|
| 22 |
+
prediction = model.predict([[future_day]])[0]
|
| 23 |
+
|
| 24 |
+
st.success(f"Predicted next day's energy consumption: {prediction:.2f} kWh")
|
| 25 |
+
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# SUSTAINABILITY SCORE
|
| 28 |
+
# -----------------------------
|
| 29 |
+
if avg_consumption < 4:
|
| 30 |
+
score = "Excellent 🌱"
|
| 31 |
+
elif avg_consumption < 6:
|
| 32 |
+
score = "Moderate ⚡"
|
| 33 |
+
else:
|
| 34 |
+
score = "High Consumption ⚠️"
|
| 35 |
+
|
| 36 |
+
st.subheader("🌍 Sustainability Score")
|
| 37 |
+
st.info(score)
|
| 38 |
+
|
| 39 |
+
# -----------------------------
|
| 40 |
+
# CHATBOT SECTION
|
| 41 |
+
# -----------------------------
|
| 42 |
+
st.subheader("🤖 Sustainability Chatbot")
|
| 43 |
+
user_query = st.text_input("Ask about your energy usage:")
|
| 44 |
+
|
| 45 |
+
def chatbot_response(query):
|
| 46 |
+
query = query.lower()
|
| 47 |
+
|
| 48 |
+
if "reduce" in query or "save" in query:
|
| 49 |
+
return "Use LED bulbs, reduce AC usage, unplug idle devices, and shift usage to off-peak hours."
|
| 50 |
+
|
| 51 |
+
elif "bill" in query or "cost" in query:
|
| 52 |
+
estimated_bill = total_consumption * 8 # Example ₹8 per kWh
|
| 53 |
+
return f"Estimated electricity bill: ₹{estimated_bill:.2f}"
|
| 54 |
+
|
| 55 |
+
elif "peak" in query:
|
| 56 |
+
return f"Your highest consumption was on {peak_day['date'].date()} with {peak_day['usage_kwh']:.2f} kWh."
|
| 57 |
+
|
| 58 |
+
elif "prediction" in query or "future" in query:
|
| 59 |
+
return f"Predicted next day usage is {prediction:.2f} kWh."
|
| 60 |
+
|
| 61 |
+
else:
|
| 62 |
+
return "Ask about reducing usage, electricity bill, peak consumption, or future predictions."
|
| 63 |
+
|
| 64 |
+
if user_query:
|
| 65 |
+
response = chatbot_response(user_query)
|
| 66 |
+
st.write("### 💬 Chatbot Response:")
|
| 67 |
+
st.success(response)
|
| 68 |
+
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# RAW DATA VIEW
|
| 71 |
+
# -----------------------------
|
| 72 |
+
with st.expander("📄 View Raw Data"):
|
| 73 |
+
st.dataframe(filtered_df)
|
| 74 |
+
|
| 75 |
+
# -----------------------------
|
| 76 |
+
# FOOTER
|
| 77 |
+
# -----------------------------
|
| 78 |
+
st.markdown("---")
|
| 79 |
+
st.caption("Built with Streamlit | AI + Sustainability + Data Analytics")
|