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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from geopy.geocoders import OpenCageGeocode
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
import folium
|
| 8 |
+
from streamlit_folium import st_folium
|
| 9 |
+
|
| 10 |
+
# Initialize geolocator with OpenCage API
|
| 11 |
+
API_KEY = "YOUR_OPENCAGE_API_KEY" # Replace with your OpenCage API key
|
| 12 |
+
geolocator = OpenCageGeocode(API_KEY)
|
| 13 |
+
|
| 14 |
+
# Hugging Face Datasets
|
| 15 |
+
@st.cache_data
|
| 16 |
+
def load_data():
|
| 17 |
+
network_insights = load_dataset("infinite-dataset-hub/5GNetworkOptimization", split="train")
|
| 18 |
+
return network_insights.to_pandas()
|
| 19 |
+
|
| 20 |
+
# Load Datasets
|
| 21 |
+
network_insights = load_data()
|
| 22 |
+
|
| 23 |
+
# Title
|
| 24 |
+
st.title("Smart Network Infrastructure Planner")
|
| 25 |
+
st.sidebar.header("Input Parameters")
|
| 26 |
+
|
| 27 |
+
# User Inputs from Sidebar
|
| 28 |
+
budget = st.sidebar.number_input("Total Budget (in $1000s):", min_value=10, max_value=1000, step=10)
|
| 29 |
+
priority_area = st.sidebar.selectbox("Priority Area:", ["Rural", "Urban", "Suburban"])
|
| 30 |
+
signal_threshold = st.sidebar.slider("Signal Strength Threshold (dBm):", min_value=-120, max_value=-30, value=-80)
|
| 31 |
+
terrain_weight = st.sidebar.slider("Terrain Difficulty Weight:", min_value=0.0, max_value=1.0, value=0.5)
|
| 32 |
+
cost_weight = st.sidebar.slider("Cost Weight:", min_value=0.0, max_value=1.0, value=0.5)
|
| 33 |
+
include_human_readable = st.sidebar.checkbox("Include Human-Readable Info", value=True)
|
| 34 |
+
|
| 35 |
+
# Display Dataset Options
|
| 36 |
+
data_to_view = st.sidebar.selectbox("Select Dataset to View:", ["Network Insights", "Filtered Terrain Data"])
|
| 37 |
+
|
| 38 |
+
# Terrain and Connectivity Analysis Section
|
| 39 |
+
st.header("Terrain and Connectivity Analysis")
|
| 40 |
+
|
| 41 |
+
# Simulate Terrain Data
|
| 42 |
+
def generate_terrain_data():
|
| 43 |
+
np.random.seed(42)
|
| 44 |
+
data = {
|
| 45 |
+
"Region": [f"Region-{i}" for i in range(1, 11)],
|
| 46 |
+
"Latitude": np.random.uniform(30.0, 50.0, size=10),
|
| 47 |
+
"Longitude": np.random.uniform(-120.0, -70.0, size=10),
|
| 48 |
+
"Terrain Difficulty (0-10)": np.random.randint(1, 10, size=10),
|
| 49 |
+
"Signal Strength (dBm)": np.random.randint(-120, -30, size=10),
|
| 50 |
+
"Cost ($1000s)": np.random.randint(50, 200, size=10),
|
| 51 |
+
"Priority Area": np.random.choice(["Rural", "Urban", "Suburban"], size=10),
|
| 52 |
+
"Description": [
|
| 53 |
+
"Flat area with minimal obstacles",
|
| 54 |
+
"Hilly terrain, moderate construction difficulty",
|
| 55 |
+
"Dense urban area with high costs",
|
| 56 |
+
"Suburban area, balanced terrain",
|
| 57 |
+
"Mountainous region, challenging setup",
|
| 58 |
+
"Remote rural area, sparse population",
|
| 59 |
+
"Coastal area, potential for high signal interference",
|
| 60 |
+
"Industrial zone, requires robust infrastructure",
|
| 61 |
+
"Dense forest region, significant signal attenuation",
|
| 62 |
+
"Open plains, optimal for cost-effective deployment"
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
return pd.DataFrame(data)
|
| 66 |
+
|
| 67 |
+
terrain_data = generate_terrain_data()
|
| 68 |
+
|
| 69 |
+
# Reverse Geocoding Function
|
| 70 |
+
def get_location_name(lat, lon):
|
| 71 |
+
try:
|
| 72 |
+
location = geolocator.reverse((lat, lon), language="en")
|
| 73 |
+
if location and "formatted" in location:
|
| 74 |
+
return location["formatted"]
|
| 75 |
+
return "Location not found"
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error: {str(e)}"
|
| 78 |
+
|
| 79 |
+
# Add Location Name to Filtered Data
|
| 80 |
+
if include_human_readable:
|
| 81 |
+
filtered_data = terrain_data[
|
| 82 |
+
(terrain_data["Signal Strength (dBm)"] >= signal_threshold) &
|
| 83 |
+
(terrain_data["Cost ($1000s)"] <= budget) &
|
| 84 |
+
(terrain_data["Priority Area"] == priority_area)
|
| 85 |
+
]
|
| 86 |
+
filtered_data["Location Name"] = filtered_data.apply(
|
| 87 |
+
lambda row: get_location_name(row["Latitude"], row["Longitude"]), axis=1
|
| 88 |
+
)
|
| 89 |
+
else:
|
| 90 |
+
filtered_data = terrain_data[
|
| 91 |
+
(terrain_data["Signal Strength (dBm)"] >= signal_threshold) &
|
| 92 |
+
(terrain_data["Cost ($1000s)"] <= budget) &
|
| 93 |
+
(terrain_data["Priority Area"] == priority_area)
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
# Add Composite Score for Ranking
|
| 97 |
+
filtered_data["Composite Score"] = (
|
| 98 |
+
(1 - terrain_weight) * filtered_data["Signal Strength (dBm)"] +
|
| 99 |
+
(terrain_weight) * (10 - filtered_data["Terrain Difficulty (0-10)"]) -
|
| 100 |
+
(cost_weight) * filtered_data["Cost ($1000s)"]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Display Selected Dataset
|
| 104 |
+
if data_to_view == "Network Insights":
|
| 105 |
+
st.subheader("Network Insights Dataset")
|
| 106 |
+
st.dataframe(network_insights)
|
| 107 |
+
elif data_to_view == "Filtered Terrain Data":
|
| 108 |
+
st.subheader("Filtered Terrain Data")
|
| 109 |
+
columns_to_display = [
|
| 110 |
+
"Region", "Location Name", "Priority Area", "Signal Strength (dBm)",
|
| 111 |
+
"Cost ($1000s)", "Terrain Difficulty (0-10)", "Description", "Composite Score"
|
| 112 |
+
] if include_human_readable else [
|
| 113 |
+
"Region", "Priority Area", "Signal Strength (dBm)", "Cost ($1000s)", "Terrain Difficulty (0-10)", "Description", "Composite Score"
|
| 114 |
+
]
|
| 115 |
+
st.dataframe(filtered_data[columns_to_display])
|
| 116 |
+
|
| 117 |
+
# Map Visualization
|
| 118 |
+
st.header("Geographical Map of Regions")
|
| 119 |
+
if not filtered_data.empty:
|
| 120 |
+
map_center = [filtered_data["Latitude"].mean(), filtered_data["Longitude"].mean()]
|
| 121 |
+
region_map = folium.Map(location=map_center, zoom_start=6)
|
| 122 |
+
|
| 123 |
+
for _, row in filtered_data.iterrows():
|
| 124 |
+
folium.Marker(
|
| 125 |
+
location=[row["Latitude"], row["Longitude"]],
|
| 126 |
+
popup=(f"<b>Region:</b> {row['Region']}<br>"
|
| 127 |
+
f"<b>Location:</b> {row.get('Location Name', 'N/A')}<br>"
|
| 128 |
+
f"<b>Description:</b> {row['Description']}<br>"
|
| 129 |
+
f"<b>Signal Strength:</b> {row['Signal Strength (dBm)']} dBm<br>"
|
| 130 |
+
f"<b>Cost:</b> ${row['Cost ($1000s)']}k<br>"
|
| 131 |
+
f"<b>Terrain Difficulty:</b> {row['Terrain Difficulty (0-10)']}"),
|
| 132 |
+
icon=folium.Icon(color="blue", icon="info-sign")
|
| 133 |
+
).add_to(region_map)
|
| 134 |
+
|
| 135 |
+
st_folium(region_map, width=700, height=500)
|
| 136 |
+
else:
|
| 137 |
+
st.write("No regions match the selected criteria.")
|
| 138 |
+
|
| 139 |
+
# Visualization
|
| 140 |
+
fig = px.scatter(
|
| 141 |
+
filtered_data,
|
| 142 |
+
x="Cost ($1000s)",
|
| 143 |
+
y="Signal Strength (dBm)",
|
| 144 |
+
size="Terrain Difficulty (0-10)",
|
| 145 |
+
color="Region",
|
| 146 |
+
title="Signal Strength vs. Cost",
|
| 147 |
+
labels={
|
| 148 |
+
"Cost ($1000s)": "Cost in $1000s",
|
| 149 |
+
"Signal Strength (dBm)": "Signal Strength in dBm",
|
| 150 |
+
},
|
| 151 |
+
)
|
| 152 |
+
st.plotly_chart(fig)
|
| 153 |
+
|
| 154 |
+
# Recommendation Engine
|
| 155 |
+
st.header("Deployment Recommendations")
|
| 156 |
+
|
| 157 |
+
def recommend_deployment(data):
|
| 158 |
+
if data.empty:
|
| 159 |
+
return "No viable deployment regions within the specified parameters."
|
| 160 |
+
best_region = data.loc[data["Composite Score"].idxmax()]
|
| 161 |
+
return f"Recommended Region: {best_region['Region']} with Composite Score: {best_region['Composite Score']:.2f}, Signal Strength: {best_region['Signal Strength (dBm)']} dBm, Terrain Difficulty: {best_region['Terrain Difficulty (0-10)']}, and Estimated Cost: ${best_region['Cost ($1000s)']}k\nDescription: {best_region['Description']}\nLocation Name: {best_region.get('Location Name', 'N/A')}"
|
| 162 |
+
|
| 163 |
+
recommendation = recommend_deployment(filtered_data)
|
| 164 |
+
st.subheader(recommendation)
|
| 165 |
+
|
| 166 |
+
# Footer
|
| 167 |
+
st.sidebar.markdown("---")
|
| 168 |
+
st.sidebar.markdown(
|
| 169 |
+
"**Developed for Hackathon using Hugging Face Infinite Dataset Hub**\n\n[Visit Hugging Face](https://huggingface.co)"
|
| 170 |
+
)
|