Transcendental-Programmer commited on
Commit
90f73f6
Β·
1 Parent(s): bd3da01

fix: streamlit error

Browse files
Files changed (1) hide show
  1. app.py +118 -36
app.py CHANGED
@@ -22,6 +22,13 @@ class SimulatedFederatedSystem:
22
  self.active_clients = 0
23
  self.clients_ready = 0
24
  self.model_weights = [random.random() for _ in range(100)]
 
 
 
 
 
 
 
25
 
26
  def register_client(self, client_id, client_info):
27
  self.clients[client_id] = {
@@ -31,6 +38,11 @@ class SimulatedFederatedSystem:
31
  'status': 'active'
32
  }
33
  self.active_clients = len(self.clients)
 
 
 
 
 
34
  return True
35
 
36
  def get_training_status(self):
@@ -46,6 +58,11 @@ class SimulatedFederatedSystem:
46
  def start_training(self):
47
  self.training_active = True
48
  self.current_round = 1
 
 
 
 
 
49
 
50
  def simulate_training_round(self):
51
  if self.training_active and self.current_round < self.total_rounds:
@@ -54,6 +71,13 @@ class SimulatedFederatedSystem:
54
  self.global_model_accuracy += random.uniform(0.01, 0.03)
55
  self.global_model_accuracy = min(self.global_model_accuracy, 0.95)
56
  self.clients_ready = random.randint(2, min(5, self.active_clients))
 
 
 
 
 
 
 
57
 
58
  def predict(self, features):
59
  # Simulate model prediction
@@ -64,6 +88,10 @@ class SimulatedFederatedSystem:
64
  base_score = sum(f * w for f, w in zip(features, self.model_weights[:32]))
65
  noise = random.uniform(-50, 50)
66
  credit_score = max(300, min(850, base_score * 100 + 500 + noise))
 
 
 
 
67
  return credit_score
68
 
69
  # Global simulated system
@@ -136,6 +164,8 @@ if 'training_history' not in st.session_state:
136
  st.session_state.training_history = []
137
  if 'debug_messages' not in st.session_state:
138
  st.session_state.debug_messages = []
 
 
139
 
140
  # System Status in sidebar
141
  with st.sidebar.expander("System Status"):
@@ -157,6 +187,17 @@ with st.sidebar.expander("Debug Information"):
157
  if st.button("Clear Debug Logs"):
158
  st.session_state.debug_messages = []
159
 
 
 
 
 
 
 
 
 
 
 
 
160
  # Sidebar educational content
161
  with st.sidebar.expander("About Federated Learning"):
162
  st.markdown("""
@@ -291,46 +332,87 @@ if st.session_state.client_simulator:
291
  else:
292
  st.sidebar.warning("Disconnected")
293
 
294
- # System Information
295
- st.header("System Information")
296
- st.markdown("""
297
- ### πŸš€ **Complete Federated Learning System**
298
-
299
- This demo showcases a **fully functional federated learning system** running entirely on Hugging Face Spaces:
300
-
301
- #### **What's Running:**
302
- - βœ… **Federated Server**: Coordinates training across multiple clients
303
- - βœ… **Client Simulator**: Participates in federated learning rounds
304
- - βœ… **Model Aggregation**: FedAvg algorithm for combining model updates
305
- - βœ… **Privacy Protection**: No raw data sharing between participants
306
- - βœ… **Real-time Training**: Live training progress visualization
307
- - βœ… **Credit Scoring**: Predictions from the federated model
308
-
309
- #### **How It Works:**
310
- 1. **Client Registration**: Banks register with the federated server
311
- 2. **Local Training**: Each client trains on their private data
312
- 3. **Model Updates**: Only model weights are shared (not data)
313
- 4. **Aggregation**: Server combines updates using federated averaging
314
- 5. **Global Model**: Updated model is distributed to all clients
315
- 6. **Predictions**: Users get credit scores from the collaborative model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
 
317
- #### **Privacy Benefits:**
318
- - πŸ”’ **Data Never Leaves**: Each bank's data stays local
319
- - πŸ”’ **Model Updates Only**: Only gradients/weights are shared
320
- - πŸ”’ **No Central Database**: No single point of data collection
321
- - πŸ”’ **Collaborative Learning**: Multiple banks improve the model together
322
 
323
- #### **Production Ready Features:**
324
- - πŸ—οΈ **Kubernetes Deployment**: Ready for production scaling
325
- - 🐳 **Docker Containers**: Containerized for easy deployment
326
- - πŸ“Š **Monitoring**: Real-time training metrics and health checks
327
- - πŸ”§ **Configuration**: Flexible config management
328
- - πŸ§ͺ **Testing**: Comprehensive test suite
329
 
330
- **This is a complete, production-ready federated learning system!** 🎯
331
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
 
333
  # Auto-refresh for training simulation
334
  if st.session_state.federated_system.training_active:
335
  time.sleep(2)
336
- st.experimental_rerun()
 
22
  self.active_clients = 0
23
  self.clients_ready = 0
24
  self.model_weights = [random.random() for _ in range(100)]
25
+ # Kubernetes simulation data
26
+ self.k8s_pods = {
27
+ "fl-server": {"status": "Running", "cpu": "20%", "memory": "256MB"},
28
+ "fl-client-1": {"status": "Running", "cpu": "15%", "memory": "128MB"},
29
+ "fl-client-2": {"status": "Running", "cpu": "15%", "memory": "128MB"},
30
+ "fl-client-3": {"status": "Pending", "cpu": "0%", "memory": "0MB"}
31
+ }
32
 
33
  def register_client(self, client_id, client_info):
34
  self.clients[client_id] = {
 
38
  'status': 'active'
39
  }
40
  self.active_clients = len(self.clients)
41
+ # Update Kubernetes simulation
42
+ if self.active_clients <= 3:
43
+ self.k8s_pods[f"fl-client-{self.active_clients}"]["status"] = "Running"
44
+ self.k8s_pods[f"fl-client-{self.active_clients}"]["cpu"] = "15%"
45
+ self.k8s_pods[f"fl-client-{self.active_clients}"]["memory"] = "128MB"
46
  return True
47
 
48
  def get_training_status(self):
 
58
  def start_training(self):
59
  self.training_active = True
60
  self.current_round = 1
61
+ # Update Kubernetes simulation
62
+ self.k8s_pods["fl-server"]["cpu"] = "35%"
63
+ for i in range(1, min(4, self.active_clients + 1)):
64
+ if self.k8s_pods[f"fl-client-{i}"]["status"] == "Running":
65
+ self.k8s_pods[f"fl-client-{i}"]["cpu"] = "25%"
66
 
67
  def simulate_training_round(self):
68
  if self.training_active and self.current_round < self.total_rounds:
 
71
  self.global_model_accuracy += random.uniform(0.01, 0.03)
72
  self.global_model_accuracy = min(self.global_model_accuracy, 0.95)
73
  self.clients_ready = random.randint(2, min(5, self.active_clients))
74
+ # Update Kubernetes simulation
75
+ self.k8s_pods["fl-server"]["cpu"] = f"{30 + random.randint(5, 15)}%"
76
+ self.k8s_pods["fl-server"]["memory"] = f"{256 + self.current_round * 10}MB"
77
+ for i in range(1, min(4, self.active_clients + 1)):
78
+ if self.k8s_pods[f"fl-client-{i}"]["status"] == "Running":
79
+ self.k8s_pods[f"fl-client-{i}"]["cpu"] = f"{20 + random.randint(5, 15)}%"
80
+ self.k8s_pods[f"fl-client-{i}"]["memory"] = f"{128 + self.current_round * 5}MB"
81
 
82
  def predict(self, features):
83
  # Simulate model prediction
 
88
  base_score = sum(f * w for f, w in zip(features, self.model_weights[:32]))
89
  noise = random.uniform(-50, 50)
90
  credit_score = max(300, min(850, base_score * 100 + 500 + noise))
91
+
92
+ # Update Kubernetes simulation for prediction
93
+ self.k8s_pods["fl-server"]["cpu"] = f"{30 + random.randint(5, 10)}%"
94
+
95
  return credit_score
96
 
97
  # Global simulated system
 
164
  st.session_state.training_history = []
165
  if 'debug_messages' not in st.session_state:
166
  st.session_state.debug_messages = []
167
+ if 'kubernetes_view' not in st.session_state:
168
+ st.session_state.kubernetes_view = False
169
 
170
  # System Status in sidebar
171
  with st.sidebar.expander("System Status"):
 
187
  if st.button("Clear Debug Logs"):
188
  st.session_state.debug_messages = []
189
 
190
+ # Kubernetes monitoring in sidebar
191
+ with st.sidebar.expander("Kubernetes Monitoring"):
192
+ st.write("**Simulated Kubernetes Cluster**")
193
+
194
+ if st.button("Toggle Kubernetes View"):
195
+ st.session_state.kubernetes_view = not st.session_state.kubernetes_view
196
+
197
+ st.success("βœ… Kubernetes Cluster Running")
198
+ st.info(f"Namespace: federated-learning")
199
+ st.info(f"Pods Running: {system.active_clients + 1}") # Clients + Server
200
+
201
  # Sidebar educational content
202
  with st.sidebar.expander("About Federated Learning"):
203
  st.markdown("""
 
332
  else:
333
  st.sidebar.warning("Disconnected")
334
 
335
+ # Kubernetes visualization (if enabled)
336
+ if st.session_state.kubernetes_view:
337
+ st.header("Kubernetes Cluster Visualization")
338
+
339
+ # Create a simple visualization of the Kubernetes cluster
340
+ st.markdown("""
341
+ ```
342
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
343
+ β”‚ Kubernetes Cluster (Simulated) β”‚
344
+ β”‚ β”‚
345
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
346
+ β”‚ β”‚ fl-server β”‚ β”‚ fl-client-1 β”‚ β”‚ fl-client-2 β”‚ β”‚
347
+ β”‚ β”‚ Pod β”‚ β”‚ Pod β”‚ β”‚ Pod β”‚ β”‚
348
+ β”‚ β”‚ Running βœ… β”‚ β”‚ Running βœ… β”‚ β”‚ Running βœ… β”‚ β”‚
349
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
350
+ β”‚ β”‚
351
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
352
+ β”‚ β”‚ fl-server-service β”‚ β”‚
353
+ β”‚ β”‚ Type: ClusterIP β”‚ β”‚
354
+ β”‚ β”‚ Port: 8080:8000 β”‚ β”‚
355
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
356
+ β”‚ β”‚
357
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
358
+ ```
359
+ """)
360
+
361
+ # Show simulated Kubernetes metrics
362
+ col1, col2 = st.columns(2)
363
+
364
+ with col1:
365
+ st.subheader("Pod Status")
366
+ pod_status = {}
367
+ for pod_name, pod_data in system.k8s_pods.items():
368
+ pod_status[pod_name] = pod_data["status"]
369
+ st.json(pod_status)
370
+
371
+ with col2:
372
+ st.subheader("Resource Usage")
373
+ resource_usage = {
374
+ pod_name: {
375
+ "CPU": pod_data["cpu"],
376
+ "Memory": pod_data["memory"]
377
+ } for pod_name, pod_data in system.k8s_pods.items()
378
+ }
379
+ st.json(resource_usage)
380
+
381
+ # Kubernetes commands
382
+ st.subheader("Kubernetes Commands")
383
+ with st.expander("Available Commands"):
384
+ st.code("""
385
+ # View all pods
386
+ kubectl get pods -n federated-learning
387
 
388
+ # View all services
389
+ kubectl get services -n federated-learning
 
 
 
390
 
391
+ # View pod logs
392
+ kubectl logs -f deployment/fl-server -n federated-learning
 
 
 
 
393
 
394
+ # Scale deployment
395
+ kubectl scale deployment/fl-client --replicas=5 -n federated-learning
396
+ """)
397
+
398
+ # Kubernetes deployment visualization
399
+ st.subheader("Deployment Architecture")
400
+ st.markdown("""
401
+ ```mermaid
402
+ graph TD
403
+ A[Kubernetes Cluster] --> B[federated-learning Namespace]
404
+ B --> C[fl-server Deployment]
405
+ B --> D[fl-client Deployment]
406
+ B --> E[fl-server-service Service]
407
+ C --> F[fl-server Pod]
408
+ D --> G[fl-client-1 Pod]
409
+ D --> H[fl-client-2 Pod]
410
+ D --> I[fl-client-3 Pod]
411
+ E --> F
412
+ ```
413
+ """)
414
 
415
  # Auto-refresh for training simulation
416
  if st.session_state.federated_system.training_active:
417
  time.sleep(2)
418
+ st.rerun()