File size: 16,662 Bytes
135d1e4
 
 
45309a1
 
0af9146
45309a1
bd3da01
135d1e4
0af9146
bd3da01
0af9146
 
bd3da01
 
 
 
 
 
 
 
 
 
 
90f73f6
 
 
 
 
 
 
bd3da01
 
 
 
 
 
 
 
 
90f73f6
 
 
 
 
bd3da01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90f73f6
 
 
 
 
bd3da01
 
 
 
 
 
 
 
90f73f6
 
 
 
 
 
 
bd3da01
 
 
 
 
 
 
 
 
 
90f73f6
 
 
 
bd3da01
 
 
 
 
 
0af9146
b407fad
bd3da01
 
b407fad
 
 
0af9146
b407fad
 
 
 
 
bd3da01
b407fad
 
 
0af9146
b407fad
 
 
bd3da01
b407fad
 
 
 
 
 
bd3da01
b407fad
bd3da01
0af9146
b407fad
bd3da01
b407fad
 
bd3da01
 
 
0af9146
bd3da01
 
b407fad
bd3da01
 
b407fad
 
 
0af9146
 
b407fad
 
135d1e4
0af9146
135d1e4
 
 
bd3da01
135d1e4
45309a1
 
 
 
 
0af9146
 
90f73f6
 
45309a1
bd3da01
 
 
 
 
 
 
 
0af9146
 
 
 
bd3da01
0af9146
 
 
 
 
 
 
90f73f6
 
 
 
 
 
 
 
 
 
 
0af9146
 
 
 
 
 
 
 
 
 
135d1e4
0af9146
45309a1
0af9146
bd3da01
 
 
 
 
 
 
 
45309a1
0af9146
45309a1
 
 
0af9146
 
45309a1
bd3da01
 
 
 
 
 
 
 
 
 
 
 
0af9146
135d1e4
 
 
 
 
 
 
 
 
 
0af9146
135d1e4
0af9146
bd3da01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135d1e4
bd3da01
0af9146
bd3da01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135d1e4
0af9146
bd3da01
0af9146
45309a1
0af9146
 
 
 
45309a1
bd3da01
 
90f73f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd3da01
90f73f6
 
bd3da01
90f73f6
 
bd3da01
90f73f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd3da01
 
 
 
90f73f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import streamlit as st
import numpy as np
import time
import threading
import json
import logging
from datetime import datetime
import random

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Simulated Federated Learning System
class SimulatedFederatedSystem:
    def __init__(self):
        self.clients = {}
        self.current_round = 0
        self.total_rounds = 10
        self.training_active = False
        self.global_model_accuracy = 0.75
        self.active_clients = 0
        self.clients_ready = 0
        self.model_weights = [random.random() for _ in range(100)]
        # Kubernetes simulation data
        self.k8s_pods = {
            "fl-server": {"status": "Running", "cpu": "20%", "memory": "256MB"},
            "fl-client-1": {"status": "Running", "cpu": "15%", "memory": "128MB"},
            "fl-client-2": {"status": "Running", "cpu": "15%", "memory": "128MB"},
            "fl-client-3": {"status": "Pending", "cpu": "0%", "memory": "0MB"}
        }
        
    def register_client(self, client_id, client_info):
        self.clients[client_id] = {
            'info': client_info,
            'registered_at': datetime.now(),
            'last_seen': datetime.now(),
            'status': 'active'
        }
        self.active_clients = len(self.clients)
        # Update Kubernetes simulation
        if self.active_clients <= 3:
            self.k8s_pods[f"fl-client-{self.active_clients}"]["status"] = "Running"
            self.k8s_pods[f"fl-client-{self.active_clients}"]["cpu"] = "15%"
            self.k8s_pods[f"fl-client-{self.active_clients}"]["memory"] = "128MB"
        return True
        
    def get_training_status(self):
        return {
            'current_round': self.current_round,
            'total_rounds': self.total_rounds,
            'training_active': self.training_active,
            'active_clients': self.active_clients,
            'clients_ready': self.clients_ready,
            'global_accuracy': self.global_model_accuracy
        }
        
    def start_training(self):
        self.training_active = True
        self.current_round = 1
        # Update Kubernetes simulation
        self.k8s_pods["fl-server"]["cpu"] = "35%"
        for i in range(1, min(4, self.active_clients + 1)):
            if self.k8s_pods[f"fl-client-{i}"]["status"] == "Running":
                self.k8s_pods[f"fl-client-{i}"]["cpu"] = "25%"
        
    def simulate_training_round(self):
        if self.training_active and self.current_round < self.total_rounds:
            # Simulate training progress
            self.current_round += 1
            self.global_model_accuracy += random.uniform(0.01, 0.03)
            self.global_model_accuracy = min(self.global_model_accuracy, 0.95)
            self.clients_ready = random.randint(2, min(5, self.active_clients))
            # Update Kubernetes simulation
            self.k8s_pods["fl-server"]["cpu"] = f"{30 + random.randint(5, 15)}%"
            self.k8s_pods["fl-server"]["memory"] = f"{256 + self.current_round * 10}MB"
            for i in range(1, min(4, self.active_clients + 1)):
                if self.k8s_pods[f"fl-client-{i}"]["status"] == "Running":
                    self.k8s_pods[f"fl-client-{i}"]["cpu"] = f"{20 + random.randint(5, 15)}%"
                    self.k8s_pods[f"fl-client-{i}"]["memory"] = f"{128 + self.current_round * 5}MB"
            
    def predict(self, features):
        # Simulate model prediction
        if len(features) != 32:
            return 500.0
        
        # Simple weighted sum with some randomness
        base_score = sum(f * w for f, w in zip(features, self.model_weights[:32]))
        noise = random.uniform(-50, 50)
        credit_score = max(300, min(850, base_score * 100 + 500 + noise))
        
        # Update Kubernetes simulation for prediction
        self.k8s_pods["fl-server"]["cpu"] = f"{30 + random.randint(5, 10)}%"
        
        return credit_score

# Global simulated system
if 'federated_system' not in st.session_state:
    st.session_state.federated_system = SimulatedFederatedSystem()

# Client Simulator Class
class ClientSimulator:
    def __init__(self, system):
        self.system = system
        self.client_id = f"web_client_{int(time.time())}"
        self.is_running = False
        self.thread = None
        self.last_error = None
        
    def start(self):
        self.is_running = True
        self.thread = threading.Thread(target=self._run_client, daemon=True)
        self.thread.start()
        logger.info(f"Client simulator started")
        
    def stop(self):
        self.is_running = False
        logger.info("Client simulator stopped")
        
    def _run_client(self):
        try:
            # Register with simulated system
            client_info = {
                'dataset_size': 100,
                'model_params': 10000,
                'capabilities': ['training', 'inference']
            }
            
            success = self.system.register_client(self.client_id, client_info)
            
            if success:
                logger.info(f"Successfully registered client {self.client_id}")
                
                # Simulate training participation
                while self.is_running:
                    try:
                        # Simulate training round
                        if self.system.training_active:
                            time.sleep(3)  # Simulate training time
                        else:
                            time.sleep(5)  # Wait for training to start
                            
                    except Exception as e:
                        logger.error(f"Error in client simulator: {e}")
                        self.last_error = f"Error: {e}"
                        time.sleep(10)
                        
        except Exception as e:
            logger.error(f"Failed to start client simulator: {e}")
            self.last_error = f"Failed to start: {e}"
            self.is_running = False

st.set_page_config(page_title="Federated Credit Scoring Demo", layout="centered")
st.title("Federated Credit Scoring Demo")

# Sidebar configuration
st.sidebar.header("Configuration")
DEMO_MODE = st.sidebar.checkbox("Demo Mode", value=True, disabled=True)

# Initialize session state
if 'client_simulator' not in st.session_state:
    st.session_state.client_simulator = None
if 'training_history' not in st.session_state:
    st.session_state.training_history = []
if 'debug_messages' not in st.session_state:
    st.session_state.debug_messages = []
if 'kubernetes_view' not in st.session_state:
    st.session_state.kubernetes_view = False

# System Status in sidebar
with st.sidebar.expander("System Status"):
    system = st.session_state.federated_system
    st.success("βœ… Federated System Running")
    st.info(f"Active Clients: {system.active_clients}")
    st.info(f"Current Round: {system.current_round}/{system.total_rounds}")
    st.info(f"Global Accuracy: {system.global_model_accuracy:.2%}")

# Debug section in sidebar
with st.sidebar.expander("Debug Information"):
    st.write("**Recent Logs:**")
    if st.session_state.debug_messages:
        for msg in st.session_state.debug_messages[-5:]:
            st.text(msg)
    else:
        st.text("No debug messages yet")
    
    if st.button("Clear Debug Logs"):
        st.session_state.debug_messages = []

# Kubernetes monitoring in sidebar
with st.sidebar.expander("Kubernetes Monitoring"):
    st.write("**Simulated Kubernetes Cluster**")
    
    if st.button("Toggle Kubernetes View"):
        st.session_state.kubernetes_view = not st.session_state.kubernetes_view
    
    st.success("βœ… Kubernetes Cluster Running")
    st.info(f"Namespace: federated-learning")
    st.info(f"Pods Running: {system.active_clients + 1}")  # Clients + Server

# Sidebar educational content
with st.sidebar.expander("About Federated Learning"):
    st.markdown("""
    **Traditional ML:** Banks send data to central server β†’ Privacy risk
    
    **Federated Learning:** 
    - Banks keep data locally
    - Only model updates are shared
    - Collaborative learning without data sharing
    """)

# Client Simulator in sidebar
st.sidebar.header("Client Simulator")
if st.sidebar.button("Start Client"):
    try:
        st.session_state.client_simulator = ClientSimulator(st.session_state.federated_system)
        st.session_state.client_simulator.start()
        st.sidebar.success("Client started!")
        st.session_state.debug_messages.append(f"{datetime.now()}: Client simulator started")
    except Exception as e:
        st.sidebar.error(f"Failed to start client: {e}")
        st.session_state.debug_messages.append(f"{datetime.now()}: Failed to start client - {e}")

if st.sidebar.button("Stop Client"):
    if st.session_state.client_simulator:
        st.session_state.client_simulator.stop()
        st.session_state.client_simulator = None
        st.sidebar.success("Client stopped!")
        st.session_state.debug_messages.append(f"{datetime.now()}: Client simulator stopped")

# Training Control
st.sidebar.header("Training Control")
if st.sidebar.button("Start Training"):
    st.session_state.federated_system.start_training()
    st.sidebar.success("Training started!")
    st.session_state.debug_messages.append(f"{datetime.now()}: Training started")

if st.sidebar.button("Simulate Round"):
    st.session_state.federated_system.simulate_training_round()
    st.sidebar.success("Round completed!")
    st.session_state.debug_messages.append(f"{datetime.now()}: Training round completed")

# Main content - focused on core functionality
st.header("Enter Customer Features")
with st.form("feature_form"):
    features = []
    cols = st.columns(4)
    for i in range(32):
        with cols[i % 4]:
            val = st.number_input(f"Feature {i+1}", value=0.0, format="%.4f", key=f"f_{i}")
            features.append(val)
    submitted = st.form_submit_button("Predict Credit Score")

# Prediction results
if submitted:
    logger.info(f"Prediction requested with {len(features)} features")
    with st.spinner("Processing..."):
        time.sleep(1)
    
    # Use simulated federated system for prediction
    prediction = st.session_state.federated_system.predict(features)
    st.success(f"Predicted Credit Score: {prediction:.2f}")
    st.session_state.debug_messages.append(f"{datetime.now()}: Prediction: {prediction:.2f}")
    
    # Show prediction explanation
    st.info("""
    **This prediction comes from the federated model trained by multiple banks!**
    
    - Model trained on data from multiple financial institutions
    - No raw data was shared between banks
    - Only model updates were aggregated
    - Privacy-preserving collaborative learning
    """)

# Training progress
st.header("Training Progress")
system = st.session_state.federated_system
status = system.get_training_status()

col1, col2, col3, col4 = st.columns(4)
with col1:
    st.metric("Round", f"{status['current_round']}/{status['total_rounds']}")
with col2:
    st.metric("Clients", status['active_clients'])
with col3:
    st.metric("Accuracy", f"{status['global_accuracy']:.1%}")
with col4:
    st.metric("Status", "Active" if status['training_active'] else "Inactive")

# Training visualization
if status['training_active']:
    st.subheader("Training Progress Visualization")
    
    # Simulate training history
    if 'training_history' not in st.session_state:
        st.session_state.training_history = []
    
    # Add current status to history
    st.session_state.training_history.append({
        'round': status['current_round'],
        'accuracy': status['global_accuracy'],
        'clients': status['active_clients'],
        'timestamp': datetime.now()
    })
    
    # Keep only last 20 entries
    if len(st.session_state.training_history) > 20:
        st.session_state.training_history = st.session_state.training_history[-20:]
    
    # Create visualization
    if len(st.session_state.training_history) > 1:
        import pandas as pd
        df = pd.DataFrame(st.session_state.training_history)
        
        col1, col2 = st.columns(2)
        with col1:
            st.line_chart(df.set_index('round')['accuracy'])
            st.caption("Model Accuracy Over Rounds")
        
        with col2:
            st.line_chart(df.set_index('round')['clients'])
            st.caption("Active Clients Over Rounds")

# Client status in sidebar
if st.session_state.client_simulator:
    st.sidebar.header("Client Status")
    if st.session_state.client_simulator.is_running:
        st.sidebar.success("Connected")
        st.sidebar.info(f"ID: {st.session_state.client_simulator.client_id}")
        if st.session_state.client_simulator.last_error:
            st.sidebar.error(f"Last Error: {st.session_state.client_simulator.last_error}")
    else:
        st.sidebar.warning("Disconnected")

# Kubernetes visualization (if enabled)
if st.session_state.kubernetes_view:
    st.header("Kubernetes Cluster Visualization")
    
    # Create a simple visualization of the Kubernetes cluster
    st.markdown("""
    ```
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                Kubernetes Cluster (Simulated)               β”‚
    β”‚                                                             β”‚
    β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
    β”‚  β”‚ fl-server   β”‚  β”‚ fl-client-1 β”‚  β”‚ fl-client-2 β”‚         β”‚
    β”‚  β”‚ Pod         β”‚  β”‚ Pod         β”‚  β”‚ Pod         β”‚         β”‚
    β”‚  β”‚ Running βœ…  β”‚  β”‚ Running βœ…  β”‚  β”‚ Running βœ…  β”‚         β”‚
    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
    β”‚                                                             β”‚
    β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
    β”‚  β”‚ fl-server-service                               β”‚       β”‚
    β”‚  β”‚ Type: ClusterIP                                 β”‚       β”‚
    β”‚  β”‚ Port: 8080:8000                                 β”‚       β”‚
    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
    β”‚                                                             β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ```
    """)
    
    # Show simulated Kubernetes metrics
    col1, col2 = st.columns(2)
    
    with col1:
        st.subheader("Pod Status")
        pod_status = {}
        for pod_name, pod_data in system.k8s_pods.items():
            pod_status[pod_name] = pod_data["status"]
        st.json(pod_status)
    
    with col2:
        st.subheader("Resource Usage")
        resource_usage = {
            pod_name: {
                "CPU": pod_data["cpu"],
                "Memory": pod_data["memory"]
            } for pod_name, pod_data in system.k8s_pods.items()
        }
        st.json(resource_usage)
    
    # Kubernetes commands
    st.subheader("Kubernetes Commands")
    with st.expander("Available Commands"):
        st.code("""
# View all pods
kubectl get pods -n federated-learning

# View all services
kubectl get services -n federated-learning

# View pod logs
kubectl logs -f deployment/fl-server -n federated-learning

# Scale deployment
kubectl scale deployment/fl-client --replicas=5 -n federated-learning
        """)
    
    # Kubernetes deployment visualization
    st.subheader("Deployment Architecture")
    st.markdown("""
    ```mermaid
    graph TD
        A[Kubernetes Cluster] --> B[federated-learning Namespace]
        B --> C[fl-server Deployment]
        B --> D[fl-client Deployment]
        B --> E[fl-server-service Service]
        C --> F[fl-server Pod]
        D --> G[fl-client-1 Pod]
        D --> H[fl-client-2 Pod]
        D --> I[fl-client-3 Pod]
        E --> F
    ```
    """)

# Auto-refresh for training simulation
if st.session_state.federated_system.training_active:
    time.sleep(2)
    st.rerun()