Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,18 +6,18 @@ from sklearn.ensemble import RandomForestRegressor
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
|
| 8 |
# ----------------------
|
| 9 |
-
#
|
| 10 |
# ----------------------
|
| 11 |
@st.cache_data
|
| 12 |
def load_giga_school_data():
|
| 13 |
-
"""Load
|
| 14 |
try:
|
| 15 |
-
# Try
|
| 16 |
url = "https://raw.githubusercontent.com/Project-Giga/public-datasets/main/school_connectivity.csv"
|
| 17 |
df = pd.read_csv(url)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
st.warning("Using synthetic data -
|
| 21 |
data = {
|
| 22 |
"school_id": [1, 2, 3, 4, 5],
|
| 23 |
"latitude": [40.7128, 34.0522, 41.8781, 29.7604, 33.7490],
|
|
@@ -29,7 +29,7 @@ def load_giga_school_data():
|
|
| 29 |
|
| 30 |
@st.cache_data
|
| 31 |
def load_ookla_speedtest_data():
|
| 32 |
-
"""
|
| 33 |
return pd.DataFrame({
|
| 34 |
'latitude': [40.7128, 34.0522, 41.8781, 29.7604, 33.7490],
|
| 35 |
'longitude': [-74.0060, -118.2437, -87.6298, -95.3698, -84.3880],
|
|
@@ -42,9 +42,12 @@ def load_ookla_speedtest_data():
|
|
| 42 |
# ----------------------
|
| 43 |
def train_model(df):
|
| 44 |
# Feature engineering
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
X = df[["hour", "avg_latency_ms", "avg_download_mbps", "connectivity_score"]]
|
| 47 |
-
y = df["energy_kwh"]
|
| 48 |
|
| 49 |
# Model training
|
| 50 |
model = RandomForestRegressor(n_estimators=10)
|
|
@@ -59,13 +62,22 @@ def train_model(df):
|
|
| 59 |
st.set_page_config(page_title="Public Sector Energy Optimizer", layout="wide")
|
| 60 |
st.title("🏫 AI for School Network Efficiency")
|
| 61 |
|
| 62 |
-
# Load data
|
| 63 |
giga_df = load_giga_school_data()
|
| 64 |
ookla_df = load_ookla_speedtest_data()
|
| 65 |
-
merged_df = pd.merge(giga_df, ookla_df, on=["latitude", "longitude"], how="
|
| 66 |
|
| 67 |
# Train model
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# ----------------------
|
| 71 |
# Dashboard Sections
|
|
@@ -74,27 +86,49 @@ tab1, tab2, tab3 = st.tabs(["📈 Analysis", "🗺️ Map", "About"])
|
|
| 74 |
|
| 75 |
with tab1:
|
| 76 |
st.subheader("Energy vs. Connectivity Analysis")
|
| 77 |
-
fig = px.scatter(df, x="avg_download_mbps", y="energy_kwh",
|
| 78 |
-
color="savings_kwh", title="Download Speed vs. Energy Use"
|
|
|
|
|
|
|
| 79 |
st.plotly_chart(fig, use_container_width=True)
|
| 80 |
|
| 81 |
with tab2:
|
| 82 |
-
st.subheader("School
|
| 83 |
-
fig = px.scatter_mapbox(
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
st.plotly_chart(fig, use_container_width=True)
|
| 89 |
|
| 90 |
with tab3:
|
| 91 |
st.write("## About")
|
| 92 |
st.markdown("""
|
| 93 |
-
**
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
""")
|
| 99 |
|
| 100 |
-
st.sidebar.markdown("⚠️ Note
|
|
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
|
| 8 |
# ----------------------
|
| 9 |
+
# Data Loading (Robust Version)
|
| 10 |
# ----------------------
|
| 11 |
@st.cache_data
|
| 12 |
def load_giga_school_data():
|
| 13 |
+
"""Load school data with fallback to synthetic data"""
|
| 14 |
try:
|
| 15 |
+
# Try real data URL (replace with valid URL when available)
|
| 16 |
url = "https://raw.githubusercontent.com/Project-Giga/public-datasets/main/school_connectivity.csv"
|
| 17 |
df = pd.read_csv(url)
|
| 18 |
+
st.success("Loaded real Giga school data!")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.warning(f"Using synthetic data - {str(e)}")
|
| 21 |
data = {
|
| 22 |
"school_id": [1, 2, 3, 4, 5],
|
| 23 |
"latitude": [40.7128, 34.0522, 41.8781, 29.7604, 33.7490],
|
|
|
|
| 29 |
|
| 30 |
@st.cache_data
|
| 31 |
def load_ookla_speedtest_data():
|
| 32 |
+
"""Generate synthetic speedtest data"""
|
| 33 |
return pd.DataFrame({
|
| 34 |
'latitude': [40.7128, 34.0522, 41.8781, 29.7604, 33.7490],
|
| 35 |
'longitude': [-74.0060, -118.2437, -87.6298, -95.3698, -84.3880],
|
|
|
|
| 42 |
# ----------------------
|
| 43 |
def train_model(df):
|
| 44 |
# Feature engineering
|
| 45 |
+
np.random.seed(42)
|
| 46 |
+
df["hour"] = np.random.randint(0, 24, len(df))
|
| 47 |
+
df["energy_kwh"] = np.random.normal(200, 50, len(df))
|
| 48 |
+
|
| 49 |
X = df[["hour", "avg_latency_ms", "avg_download_mbps", "connectivity_score"]]
|
| 50 |
+
y = df["energy_kwh"]
|
| 51 |
|
| 52 |
# Model training
|
| 53 |
model = RandomForestRegressor(n_estimators=10)
|
|
|
|
| 62 |
st.set_page_config(page_title="Public Sector Energy Optimizer", layout="wide")
|
| 63 |
st.title("🏫 AI for School Network Efficiency")
|
| 64 |
|
| 65 |
+
# Load and merge data
|
| 66 |
giga_df = load_giga_school_data()
|
| 67 |
ookla_df = load_ookla_speedtest_data()
|
| 68 |
+
merged_df = pd.merge(giga_df, ookla_df, on=["latitude", "longitude"], how="inner")
|
| 69 |
|
| 70 |
# Train model
|
| 71 |
+
if not merged_df.empty:
|
| 72 |
+
df, feature_importances = train_model(merged_df)
|
| 73 |
+
|
| 74 |
+
# Prepare visualization parameters
|
| 75 |
+
df["size"] = np.abs(df["savings_kwh"]).clip(lower=5)
|
| 76 |
+
df["size"] = df["size"].apply(lambda x: min(x, 50))
|
| 77 |
+
df["color"] = np.where(df["savings_kwh"] > 0, "green", "red")
|
| 78 |
+
else:
|
| 79 |
+
st.error("No data available for analysis!")
|
| 80 |
+
st.stop()
|
| 81 |
|
| 82 |
# ----------------------
|
| 83 |
# Dashboard Sections
|
|
|
|
| 86 |
|
| 87 |
with tab1:
|
| 88 |
st.subheader("Energy vs. Connectivity Analysis")
|
| 89 |
+
fig = px.scatter(df, x="avg_download_mbps", y="energy_kwh",
|
| 90 |
+
color="savings_kwh", title="Download Speed vs. Energy Use",
|
| 91 |
+
labels={"avg_download_mbps": "Download Speed (Mbps)",
|
| 92 |
+
"energy_kwh": "Energy Consumption (kWh)"})
|
| 93 |
st.plotly_chart(fig, use_container_width=True)
|
| 94 |
|
| 95 |
with tab2:
|
| 96 |
+
st.subheader("School Energy Efficiency Map")
|
| 97 |
+
fig = px.scatter_mapbox(
|
| 98 |
+
df,
|
| 99 |
+
lat="latitude",
|
| 100 |
+
lon="longitude",
|
| 101 |
+
color="color",
|
| 102 |
+
size="size",
|
| 103 |
+
hover_name="school_id",
|
| 104 |
+
hover_data=["connectivity_score", "savings_kwh"],
|
| 105 |
+
mapbox_style="carto-positron",
|
| 106 |
+
zoom=3,
|
| 107 |
+
color_discrete_map={"green": "#2ecc71", "red": "#e74c3c"},
|
| 108 |
+
title="Energy Savings Potential"
|
| 109 |
+
)
|
| 110 |
+
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
|
| 111 |
st.plotly_chart(fig, use_container_width=True)
|
| 112 |
|
| 113 |
with tab3:
|
| 114 |
st.write("## About")
|
| 115 |
st.markdown("""
|
| 116 |
+
**Public Sector Network Optimization Dashboard**
|
| 117 |
+
*AI for Connectivity Hackathon II Submission*
|
| 118 |
+
|
| 119 |
+
Features:
|
| 120 |
+
- 🎯 Identifies energy inefficiencies in school networks
|
| 121 |
+
- 🌍 Geographic visualization of savings potential
|
| 122 |
+
- 🤖 AI-powered energy consumption predictions
|
| 123 |
+
|
| 124 |
+
Data Sources:
|
| 125 |
+
- School locations: Giga (synthetic data in this demo)
|
| 126 |
+
- Network performance: Ookla Open Data (synthetic)
|
| 127 |
+
|
| 128 |
+
Next Steps:
|
| 129 |
+
1. Replace synthetic data with real school telemetry
|
| 130 |
+
2. Integrate live energy monitoring APIs
|
| 131 |
+
3. Add maintenance scheduling features
|
| 132 |
""")
|
| 133 |
|
| 134 |
+
st.sidebar.markdown("⚠️ **Note**: Demo uses synthetic data - replace with real datasets for production use")
|