safiaa02 commited on
Commit
584490e
·
verified ·
1 Parent(s): bf7c79a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -36
app.py CHANGED
@@ -2,56 +2,75 @@ import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
  import plotly.express as px
 
5
  from prophet import Prophet
 
6
 
7
- # Simulated school network traffic data
8
- st.title("📡 Smart Network Resource Allocation")
9
-
10
- # Load or generate synthetic data
11
- @st.cache_data
12
- def load_data():
13
- schools = [f"School_{i}" for i in range(1, 6)]
14
- time_range = pd.date_range(start="2024-02-01", periods=24, freq="H")
15
  data = []
16
-
17
  for school in schools:
18
  for time in time_range:
19
- bandwidth_usage = np.random.randint(1, 100) # Random usage in Mbps
20
  data.append([school, time, bandwidth_usage])
21
-
22
  df = pd.DataFrame(data, columns=["School", "Timestamp", "Bandwidth_Usage"])
 
23
  return df
24
 
25
- df = load_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Display data
28
- st.subheader("📊 Network Traffic Data")
29
- st.write(df.tail())
 
30
 
31
- # Prophet Model for Bandwidth Prediction
32
- st.subheader("📈 Bandwidth Usage Forecasting")
33
- df_prophet = df.groupby("Timestamp").mean().reset_index()
34
- df_prophet.columns = ["ds", "y"]
 
 
35
 
36
- model = Prophet()
37
- model.fit(df_prophet)
38
- future = model.make_future_dataframe(periods=5, freq="H")
39
- forecast = model.predict(future)
40
 
41
- fig = px.line(forecast, x="ds", y="yhat", title="Predicted Bandwidth Demand")
42
- st.plotly_chart(fig)
 
 
 
43
 
44
- # Dynamic Bandwidth Allocation
45
- st.subheader("⚖️ Smart Bandwidth Allocation")
 
 
 
46
 
47
- def allocate_bandwidth(predictions, total_bandwidth=500):
48
- total_demand = sum(predictions.values())
49
- allocation = {school: (predictions[school] / total_demand) * total_bandwidth for school in predictions}
50
- return allocation
51
 
52
- # Simulated allocation
53
- predictions = {f"School_{i}": np.random.randint(30, 100) for i in range(1, 6)}
54
- allocations = allocate_bandwidth(predictions)
55
 
56
- st.write("📌 **Predicted Bandwidth Allocation (Mbps):**")
57
- st.json(allocations)
 
2
  import pandas as pd
3
  import numpy as np
4
  import plotly.express as px
5
+ from datetime import datetime, timedelta
6
  from prophet import Prophet
7
+ import networkx as nx
8
 
9
+ # Generate synthetic data
10
+ def generate_data():
11
+ schools = [f"School_{i}" for i in range(1, 11)]
12
+ time_range = [datetime.now() - timedelta(hours=i) for i in range(24)]
13
+
 
 
 
14
  data = []
 
15
  for school in schools:
16
  for time in time_range:
17
+ bandwidth_usage = np.random.randint(1, 100) # Random bandwidth usage in Mbps
18
  data.append([school, time, bandwidth_usage])
19
+
20
  df = pd.DataFrame(data, columns=["School", "Timestamp", "Bandwidth_Usage"])
21
+ df.to_csv("network_data.csv", index=False)
22
  return df
23
 
24
+ # Load or generate data
25
+ df = generate_data()
26
+
27
+ def train_prophet(df):
28
+ df["Timestamp"] = pd.to_datetime(df["Timestamp"])
29
+ df_prophet = df.groupby("Timestamp").mean().reset_index()
30
+ df_prophet.columns = ["ds", "y"]
31
+
32
+ model = Prophet()
33
+ model.fit(df_prophet)
34
+
35
+ future = model.make_future_dataframe(periods=5, freq="H") # Predict next 5 hours
36
+ forecast = model.predict(future)
37
+
38
+ return forecast
39
 
40
+ def allocate_bandwidth(demand_predictions, total_bandwidth=500):
41
+ total_demand = sum(demand_predictions.values())
42
+ allocation = {school: (demand / total_demand) * total_bandwidth for school, demand in demand_predictions.items()}
43
+ return allocation
44
 
45
+ def sdn_load_balancer(network_graph, demand_predictions):
46
+ # Simulating a simple SDN-based routing decision
47
+ shortest_paths = {}
48
+ for school in demand_predictions.keys():
49
+ shortest_paths[school] = nx.shortest_path(network_graph, source='Central_Node', target=school)
50
+ return shortest_paths
51
 
52
+ # Create a simple network topology
53
+ graph = nx.Graph()
 
 
54
 
55
+ graph.add_edges_from([
56
+ ('Central_Node', 'School_1'), ('Central_Node', 'School_2'),
57
+ ('Central_Node', 'School_3'), ('Central_Node', 'School_4'),
58
+ ('Central_Node', 'School_5')
59
+ ])
60
 
61
+ # Train model and make predictions
62
+ forecast = train_prophet(df)
63
+ demand_predictions = {f"School_{i}": np.random.randint(20, 100) for i in range(1, 6)}
64
+ bandwidth_allocation = allocate_bandwidth(demand_predictions)
65
+ network_routes = sdn_load_balancer(graph, demand_predictions)
66
 
67
+ # Streamlit UI
68
+ st.title("Smart Network Resource Allocation with SDN Load Balancing")
69
+ fig = px.line(df, x="Timestamp", y="Bandwidth_Usage", color="School", title="Bandwidth Usage Over Time")
70
+ st.plotly_chart(fig)
71
 
72
+ st.write("Predicted Bandwidth Allocations:")
73
+ st.json(bandwidth_allocation)
 
74
 
75
+ st.write("SDN-based Load Balancing Routes:")
76
+ st.json(network_routes)