eaglelandsonce commited on
Commit
916a2e9
Β·
verified Β·
1 Parent(s): 1cdd346

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import plotly.express as px
5
+ from datasets import load_dataset
6
+ import folium
7
+ from streamlit_folium import st_folium
8
+ from geopy.geocoders import Nominatim
9
+ from shadcn.ui import Card, CardContent, Button
10
+
11
+ # Initialize geolocator
12
+ geolocator = Nominatim(user_agent="geoapiExercises")
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 Section with Styling
24
+ st.markdown("""
25
+ # 🌍 **Smart Network Infrastructure Planner**
26
+ Effortlessly optimize network infrastructure while accounting for budget, signal strength, terrain challenges, and climate risks.
27
+ """)
28
+ st.sidebar.header("πŸ”§ Input Parameters")
29
+
30
+ # User Inputs from Sidebar
31
+ with st.sidebar:
32
+ budget = st.number_input("πŸ’° Total Budget (in $1000s):", min_value=10, max_value=1000, step=10)
33
+ priority_area = st.selectbox("🌎 Priority Area:", ["Rural", "Urban", "Suburban"])
34
+ signal_threshold = st.slider("πŸ“Ά Signal Strength Threshold (dBm):", min_value=-120, max_value=-30, value=-80)
35
+ terrain_weight = st.slider("πŸŒ„ Terrain Difficulty Weight:", min_value=0.0, max_value=1.0, value=0.5)
36
+ cost_weight = st.slider("πŸ’΅ Cost Weight:", min_value=0.0, max_value=1.0, value=0.5)
37
+ climate_risk_weight = st.slider("🌑️ Climate Risk Weight:", min_value=0.0, max_value=1.0, value=0.5)
38
+ include_human_readable = st.checkbox("πŸ—ΊοΈ Include Human-Readable Info", value=True)
39
+
40
+ # Tabs for Data Display and Analysis
41
+ st.markdown("## πŸš€ Insights & Recommendations")
42
+ tabs = st.tabs(["Terrain Analysis", "Filtered Data", "Geographical Map"])
43
+
44
+ # Simulate Terrain and Climate Risk Data
45
+ def generate_terrain_data():
46
+ np.random.seed(42)
47
+ data = {
48
+ "Region": [f"Region-{i}" for i in range(1, 11)],
49
+ "Latitude": np.random.uniform(30.0, 50.0, size=10),
50
+ "Longitude": np.random.uniform(-120.0, -70.0, size=10),
51
+ "Terrain Difficulty (0-10)": np.random.randint(1, 10, size=10),
52
+ "Signal Strength (dBm)": np.random.randint(-120, -30, size=10),
53
+ "Cost ($1000s)": np.random.randint(50, 200, size=10),
54
+ "Priority Area": np.random.choice(["Rural", "Urban", "Suburban"], size=10),
55
+ "Climate Risk (0-10)": np.random.randint(0, 10, size=10),
56
+ "Description": [
57
+ "Flat area with minimal obstacles",
58
+ "Hilly terrain, moderate construction difficulty",
59
+ "Dense urban area with high costs",
60
+ "Suburban area, balanced terrain",
61
+ "Mountainous region, challenging setup",
62
+ "Remote rural area, sparse population",
63
+ "Coastal area, potential for high signal interference",
64
+ "Industrial zone, requires robust infrastructure",
65
+ "Dense forest region, significant signal attenuation",
66
+ "Open plains, optimal for cost-effective deployment"
67
+ ]
68
+ }
69
+ return pd.DataFrame(data)
70
+
71
+ terrain_data = generate_terrain_data()
72
+
73
+ # Reverse Geocoding Function
74
+ def get_location_name(lat, lon):
75
+ try:
76
+ location = geolocator.reverse((lat, lon), exactly_one=True)
77
+ return location.address if location else "Unknown Location"
78
+ except Exception as e:
79
+ return "Error: Unable to fetch location"
80
+
81
+ # Add Location Name to Filtered Data
82
+ if include_human_readable:
83
+ filtered_data = terrain_data[
84
+ (terrain_data["Signal Strength (dBm)"] >= signal_threshold) &
85
+ (terrain_data["Cost ($1000s)"] <= budget) &
86
+ (terrain_data["Priority Area"] == priority_area)
87
+ ]
88
+ filtered_data["Location Name"] = filtered_data.apply(
89
+ lambda row: get_location_name(row["Latitude"], row["Longitude"]), axis=1
90
+ )
91
+ else:
92
+ filtered_data = terrain_data[
93
+ (terrain_data["Signal Strength (dBm)"] >= signal_threshold) &
94
+ (terrain_data["Cost ($1000s)"] <= budget) &
95
+ (terrain_data["Priority Area"] == priority_area)
96
+ ]
97
+
98
+ # Add Composite Score for Ranking
99
+ filtered_data["Composite Score"] = (
100
+ (1 - terrain_weight) * filtered_data["Signal Strength (dBm)"] +
101
+ (terrain_weight) * (10 - filtered_data["Terrain Difficulty (0-10)"]) -
102
+ (cost_weight) * filtered_data["Cost ($1000s)"] -
103
+ (climate_risk_weight) * filtered_data["Climate Risk (0-10)"]
104
+ )
105
+
106
+ # Display Filtered Data in Tab
107
+ with tabs[1]:
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)", "Climate Risk (0-10)", "Description", "Composite Score"
112
+ ] if include_human_readable else [
113
+ "Region", "Priority Area", "Signal Strength (dBm)", "Cost ($1000s)", "Terrain Difficulty (0-10)", "Climate Risk (0-10)", "Description", "Composite Score"
114
+ ]
115
+ st.dataframe(filtered_data[columns_to_display])
116
+
117
+ # Map Visualization in Tab
118
+ with tabs[2]:
119
+ st.subheader("Geographical Map of Regions")
120
+ if not filtered_data.empty:
121
+ map_center = [filtered_data["Latitude"].mean(), filtered_data["Longitude"].mean()]
122
+ region_map = folium.Map(location=map_center, zoom_start=6)
123
+
124
+ for _, row in filtered_data.iterrows():
125
+ folium.Marker(
126
+ location=[row["Latitude"], row["Longitude"]],
127
+ popup=(
128
+ f"<b>Region:</b> {row['Region']}<br>"
129
+ f"<b>Location:</b> {row.get('Location Name', 'N/A')}<br>"
130
+ f"<b>Description:</b> {row['Description']}<br>"
131
+ f"<b>Signal Strength:</b> {row['Signal Strength (dBm)']} dBm<br>"
132
+ f"<b>Cost:</b> ${row['Cost ($1000s)']}k<br>"
133
+ f"<b>Terrain Difficulty:</b> {row['Terrain Difficulty (0-10)']}<br>"
134
+ f"<b>Climate Risk:</b> {row['Climate Risk (0-10)']}"
135
+ ),
136
+ icon=folium.Icon(color="blue", icon="info-sign")
137
+ ).add_to(region_map)
138
+
139
+ st_folium(region_map, width=700, height=500)
140
+ else:
141
+ st.write("No regions match the selected criteria.")
142
+
143
+ # Visualization Tab
144
+ with tabs[0]:
145
+ st.subheader("Signal Strength vs. Cost")
146
+ fig = px.scatter(
147
+ filtered_data,
148
+ x="Cost ($1000s)",
149
+ y="Signal Strength (dBm)",
150
+ size="Terrain Difficulty (0-10)",
151
+ color="Region",
152
+ title="Signal Strength vs. Cost",
153
+ labels={
154
+ "Cost ($1000s)": "Cost in $1000s",
155
+ "Signal Strength (dBm)": "Signal Strength in dBm",
156
+ },
157
+ )
158
+ st.plotly_chart(fig)
159
+
160
+ # Recommendation Engine
161
+ st.header("✨ Deployment Recommendations")
162
+
163
+ def recommend_deployment(data):
164
+ if data.empty:
165
+ return "No viable deployment regions within the specified parameters."
166
+ best_region = data.loc[data["Composite Score"].idxmax()]
167
+ 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)']}, Climate Risk: {best_region['Climate Risk (0-10)']}, and Estimated Cost: ${best_region['Cost ($1000s)']}k\nDescription: {best_region['Description']}\nLocation Name: {best_region.get('Location Name', 'N/A')}"
168
+
169
+ recommendation = recommend_deployment(filtered_data)
170
+
171
+ # Display Recommendation in a Card
172
+ st.markdown("### 🌟 Final Recommendation")
173
+ with st.container():
174
+ st.markdown(f"**{recommendation}**")
175
+
176
+ # Footer
177
+ st.sidebar.markdown("---")
178
+ st.sidebar.markdown(
179
+ "**Developed for Hackathon using Hugging Face Infinite Dataset Hub**\n\n[Visit Hugging Face](https://huggingface.co)")