Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
|
| 8 |
+
# ----------------------
|
| 9 |
+
# Data Generation (Synthetic)
|
| 10 |
+
# ----------------------
|
| 11 |
+
def generate_data(num_devices=100):
|
| 12 |
+
np.random.seed(42)
|
| 13 |
+
timestamps = pd.date_range("2024-01-01", periods=30, freq="D")
|
| 14 |
+
data = []
|
| 15 |
+
for device_id in range(num_devices):
|
| 16 |
+
for timestamp in timestamps:
|
| 17 |
+
energy_use = np.random.normal(200, 50) # Base energy (kWh)
|
| 18 |
+
latency = np.random.normal(100, 20) # Latency (ms)
|
| 19 |
+
traffic = np.random.randint(0, 100) # Network traffic (%)
|
| 20 |
+
# Simulate "inefficient" devices (20% of data)
|
| 21 |
+
if np.random.random() < 0.2:
|
| 22 |
+
energy_use *= 1.5
|
| 23 |
+
data.append([device_id, timestamp, energy_use, latency, traffic])
|
| 24 |
+
df = pd.DataFrame(data, columns=["device_id", "timestamp", "energy_kwh", "latency_ms", "traffic_pct"])
|
| 25 |
+
return df
|
| 26 |
+
|
| 27 |
+
# ----------------------
|
| 28 |
+
# AI Model (Energy Optimization)
|
| 29 |
+
# ----------------------
|
| 30 |
+
def train_model(df):
|
| 31 |
+
# Feature engineering
|
| 32 |
+
df["hour"] = df["timestamp"].dt.hour
|
| 33 |
+
df["day_of_week"] = df["timestamp"].dt.dayofweek
|
| 34 |
+
X = df[["hour", "day_of_week", "latency_ms", "traffic_pct"]]
|
| 35 |
+
y = df["energy_kwh"]
|
| 36 |
+
|
| 37 |
+
# Train/test split
|
| 38 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
| 39 |
+
|
| 40 |
+
# Model training
|
| 41 |
+
model = RandomForestRegressor(n_estimators=10)
|
| 42 |
+
model.fit(X_train, y_train)
|
| 43 |
+
df["predicted_energy"] = model.predict(X)
|
| 44 |
+
|
| 45 |
+
# Calculate savings potential
|
| 46 |
+
df["savings_kwh"] = df["energy_kwh"] - df["predicted_energy"]
|
| 47 |
+
return df, model.feature_importances_
|
| 48 |
+
|
| 49 |
+
# ----------------------
|
| 50 |
+
# Streamlit App
|
| 51 |
+
# ----------------------
|
| 52 |
+
st.set_page_config(page_title="Energy Optimizer", layout="wide")
|
| 53 |
+
st.title("🌱 AI for Energy-Efficient Public Networks")
|
| 54 |
+
|
| 55 |
+
# Load data
|
| 56 |
+
df = generate_data()
|
| 57 |
+
df, feature_importances = train_model(df)
|
| 58 |
+
|
| 59 |
+
# ----------------------
|
| 60 |
+
# Dashboard Sections
|
| 61 |
+
# ----------------------
|
| 62 |
+
tab1, tab2, tab3 = st.tabs(["📈 Analysis", "🔧 Recommendations", "About"])
|
| 63 |
+
|
| 64 |
+
with tab1:
|
| 65 |
+
st.subheader("Energy Consumption Trends")
|
| 66 |
+
selected_device = st.selectbox("Select Device", df["device_id"].unique())
|
| 67 |
+
device_df = df[df["device_id"] == selected_device]
|
| 68 |
+
|
| 69 |
+
# Plot energy vs. predictions
|
| 70 |
+
fig = px.line(device_df, x="timestamp", y=["energy_kwh", "predicted_energy"],
|
| 71 |
+
labels={"value": "Energy (kWh)"}, title="Actual vs. Optimal Energy Use")
|
| 72 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 73 |
+
|
| 74 |
+
# Feature importance
|
| 75 |
+
st.subheader("What Impacts Energy Use?")
|
| 76 |
+
features = ["Hour", "Day of Week", "Latency", "Traffic"]
|
| 77 |
+
fig = px.bar(x=features, y=feature_importances, labels={"x": "Factor", "y": "Importance"})
|
| 78 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 79 |
+
|
| 80 |
+
with tab2:
|
| 81 |
+
st.subheader("Optimization Recommendations")
|
| 82 |
+
|
| 83 |
+
# Top inefficient devices
|
| 84 |
+
st.write("### Top Energy-Wasting Devices")
|
| 85 |
+
inefficient_devices = df.groupby("device_id")["savings_kwh"].mean().nlargest(5).reset_index()
|
| 86 |
+
st.dataframe(inefficient_devices, hide_index=True)
|
| 87 |
+
|
| 88 |
+
# Savings calculator
|
| 89 |
+
st.write("### Potential Savings")
|
| 90 |
+
total_savings = df["savings_kwh"].sum()
|
| 91 |
+
co2_reduction = total_savings * 0.5 # Assume 0.5 kg CO2 per kWh
|
| 92 |
+
st.metric("Total Monthly Energy Savings", f"{total_savings:,.0f} kWh")
|
| 93 |
+
st.metric("Estimated CO₂ Reduction", f"{co2_reduction:,.0f} kg")
|
| 94 |
+
|
| 95 |
+
# Recommendations
|
| 96 |
+
st.write("### Actionable Steps")
|
| 97 |
+
st.markdown("""
|
| 98 |
+
- **Schedule Off-Peak Maintenance**: Reduce energy use during low-traffic hours.
|
| 99 |
+
- **Upgrade Inefficient Devices**: Prioritize devices with the highest savings potential.
|
| 100 |
+
- **Adjust Traffic Routing**: Use latency data to balance load efficiently.
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
with tab3:
|
| 104 |
+
st.write("## About")
|
| 105 |
+
st.markdown("""
|
| 106 |
+
**Open-Source Energy Optimizer for Public Sector Networks**
|
| 107 |
+
Built for the AI for Connectivity Hackathon II.
|
| 108 |
+
- Code: [GitHub](https://github.com/)
|
| 109 |
+
- Data: Synthetic dataset (replace with real telemetry).
|
| 110 |
+
""")
|