Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install necessary libraries before running
|
| 2 |
+
# !pip install geopandas pycaret prophet matplotlib streamlit folium requests streamlit-folium
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
import geopandas as gpd
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
from prophet import Prophet
|
| 10 |
+
from streamlit_folium import folium_static
|
| 11 |
+
import folium
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
# Function to download the naturalearth_lowres dataset
|
| 15 |
+
def download_naturalearth_lowres():
|
| 16 |
+
url = "https://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_0_countries.zip"
|
| 17 |
+
zip_path = "ne_110m_admin_0_countries.zip"
|
| 18 |
+
extracted_folder = "ne_110m_admin_0_countries"
|
| 19 |
+
shapefile_path = os.path.join(extracted_folder, "ne_110m_admin_0_countries.shp")
|
| 20 |
+
|
| 21 |
+
if not os.path.exists(shapefile_path):
|
| 22 |
+
import requests
|
| 23 |
+
import zipfile
|
| 24 |
+
|
| 25 |
+
# Download the zip file
|
| 26 |
+
r = requests.get(url)
|
| 27 |
+
with open(zip_path, "wb") as f:
|
| 28 |
+
f.write(r.content)
|
| 29 |
+
|
| 30 |
+
# Extract the zip file
|
| 31 |
+
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 32 |
+
zip_ref.extractall(extracted_folder)
|
| 33 |
+
|
| 34 |
+
return shapefile_path
|
| 35 |
+
|
| 36 |
+
# Load Geospatial Data
|
| 37 |
+
@st.cache_data
|
| 38 |
+
def load_geospatial_data():
|
| 39 |
+
shapefile_path = download_naturalearth_lowres()
|
| 40 |
+
world = gpd.read_file(shapefile_path)
|
| 41 |
+
underserved_region = world[world['CONTINENT'] == 'Africa'] # Updated column name for compatibility
|
| 42 |
+
underserved_locations = gpd.GeoDataFrame({
|
| 43 |
+
"Location": ["Region A", "Region B", "Region C"],
|
| 44 |
+
"Latitude": [-1.2921, 1.2921, -3.2921],
|
| 45 |
+
"Longitude": [36.8219, 37.8219, 38.8219]
|
| 46 |
+
}, geometry=gpd.points_from_xy([36.8219, 37.8219, 38.8219], [-1.2921, 1.2921, -3.2921]))
|
| 47 |
+
return underserved_region, underserved_locations
|
| 48 |
+
|
| 49 |
+
@st.cache_data
|
| 50 |
+
def load_demand_data():
|
| 51 |
+
data = {
|
| 52 |
+
"date": pd.date_range(start="2022-01-01", periods=24, freq="M"),
|
| 53 |
+
"demand": np.random.randint(50, 200, size=24)
|
| 54 |
+
}
|
| 55 |
+
demand_df = pd.DataFrame(data)
|
| 56 |
+
return demand_df
|
| 57 |
+
|
| 58 |
+
underserved_region, underserved_locations = load_geospatial_data()
|
| 59 |
+
demand_df = load_demand_data()
|
| 60 |
+
|
| 61 |
+
# Streamlit App Title
|
| 62 |
+
st.title("AI-Driven Network Management for Underserved Regions")
|
| 63 |
+
|
| 64 |
+
# Sidebar for Navigation
|
| 65 |
+
st.sidebar.title("Navigation")
|
| 66 |
+
module = st.sidebar.radio("Choose a Module:", ["Overview", "Demand Forecasting", "Underserved Regions"])
|
| 67 |
+
|
| 68 |
+
# Overview
|
| 69 |
+
if module == "Overview":
|
| 70 |
+
st.subheader("Welcome to the AI-Driven Network Management App!")
|
| 71 |
+
st.write("""
|
| 72 |
+
This app provides AI-powered tools to optimize network planning and management for underserved regions.
|
| 73 |
+
|
| 74 |
+
Modules:
|
| 75 |
+
- **Demand Forecasting:** Predict demand for internet services over time.
|
| 76 |
+
- **Underserved Regions:** Identify underserved regions and visualize their locations.
|
| 77 |
+
""")
|
| 78 |
+
|
| 79 |
+
# Demand Forecasting Module
|
| 80 |
+
elif module == "Demand Forecasting":
|
| 81 |
+
st.subheader("Demand Forecasting")
|
| 82 |
+
|
| 83 |
+
# Prepare data for Prophet
|
| 84 |
+
demand_df.rename(columns={"date": "ds", "demand": "y"}, inplace=True)
|
| 85 |
+
|
| 86 |
+
# Initialize and fit Prophet model
|
| 87 |
+
model = Prophet()
|
| 88 |
+
model.fit(demand_df)
|
| 89 |
+
|
| 90 |
+
# Forecast future demand
|
| 91 |
+
future = model.make_future_dataframe(periods=12, freq='M')
|
| 92 |
+
forecast = model.predict(future)
|
| 93 |
+
|
| 94 |
+
# Plot forecast
|
| 95 |
+
st.write("### Historical and Forecasted Demand")
|
| 96 |
+
fig = model.plot(forecast)
|
| 97 |
+
st.pyplot(fig)
|
| 98 |
+
|
| 99 |
+
st.write("### Forecast Data")
|
| 100 |
+
st.dataframe(forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail(12))
|
| 101 |
+
|
| 102 |
+
# Underserved Regions Module
|
| 103 |
+
elif module == "Underserved Regions":
|
| 104 |
+
st.subheader("Underserved Regions")
|
| 105 |
+
|
| 106 |
+
# Show map with underserved regions
|
| 107 |
+
m = folium.Map(location=[0, 20], zoom_start=3)
|
| 108 |
+
|
| 109 |
+
# Add underserved regions
|
| 110 |
+
for _, row in underserved_locations.iterrows():
|
| 111 |
+
folium.Marker(
|
| 112 |
+
location=[row["Latitude"], row["Longitude"]],
|
| 113 |
+
popup=f"Location: {row['Location']}",
|
| 114 |
+
icon=folium.Icon(color="red")
|
| 115 |
+
).add_to(m)
|
| 116 |
+
|
| 117 |
+
st.write("### Geospatial Visualization")
|
| 118 |
+
folium_static(m, width=700, height=500)
|
| 119 |
+
|
| 120 |
+
# Show region data
|
| 121 |
+
st.write("### Region Details")
|
| 122 |
+
st.dataframe(underserved_locations.drop(columns="geometry"))
|