1+ import streamlit as st
2+ import requests
3+ import numpy as np
4+ import time
5+
6+ st .set_page_config (page_title = "Federated Credit Scoring Demo" , layout = "centered" )
7+ st .title ("Federated Credit Scoring Demo (Federated Learning)" )
8+
9+ # Sidebar configuration
10+ st .sidebar .header ("Configuration" )
11+ SERVER_URL = st .sidebar .text_input ("Server URL" , value = "http://localhost:8080" )
12+ DEMO_MODE = st .sidebar .checkbox ("Demo Mode (No Server Required)" , value = True )
13+
14+ st .markdown ("""
15+ This demo shows how multiple banks can collaboratively train a credit scoring model using federated learning, without sharing raw data.
16+ Enter customer features below to get a credit score prediction from the federated model.
17+ """ )
18+
19+ # --- Feature Input Form ---
20+ st .header ("Enter Customer Features" )
21+ with st .form ("feature_form" ):
22+ features = []
23+ cols = st .columns (4 )
24+ for i in range (32 ):
25+ with cols [i % 4 ]:
26+ val = st .number_input (f"Feature { i + 1 } " , value = 0.0 , format = "%.4f" , key = f"f_{ i } " )
27+ features .append (val )
28+ submitted = st .form_submit_button ("Predict Credit Score" )
29+
30+ # --- Prediction ---
31+ if submitted :
32+ if DEMO_MODE :
33+ # Demo mode - simulate prediction
34+ with st .spinner ("Processing prediction..." ):
35+ time .sleep (1 ) # Simulate processing time
36+
37+ # Simple demo prediction based on feature values
38+ demo_prediction = sum (features ) / len (features ) * 100 + 500 # Scale to credit score range
39+ st .success (f"Demo Prediction: Credit Score = { demo_prediction :.2f} " )
40+ st .info ("💡 This is a demo prediction. In a real federated system, this would come from the trained model." )
41+
42+ # Show what would happen in real mode
43+ st .markdown ("---" )
44+ st .markdown ("**What happens in real federated learning:**" )
45+ st .markdown ("1. Your features are sent to the federated server" )
46+ st .markdown ("2. Server uses the global model (trained by multiple banks)" )
47+ st .markdown ("3. Prediction is returned without exposing any bank's data" )
48+
49+ else :
50+ # Real mode - connect to server
51+ try :
52+ with st .spinner ("Connecting to federated server..." ):
53+ resp = requests .post (f"{ SERVER_URL } /predict" , json = {"features" : features }, timeout = 10 )
54+
55+ if resp .status_code == 200 :
56+ prediction = resp .json ().get ("prediction" )
57+ st .success (f"Predicted Credit Score: { prediction :.2f} " )
58+ else :
59+ st .error (f"Prediction failed: { resp .json ().get ('error' , 'Unknown error' )} " )
60+ except Exception as e :
61+ st .error (f"Error connecting to server: { e } " )
62+ st .info ("💡 Try enabling Demo Mode to see the interface without a server." )
63+
64+ # --- Training Progress ---
65+ st .header ("Federated Training Progress" )
66+
67+ if DEMO_MODE :
68+ # Demo training progress
69+ col1 , col2 , col3 , col4 = st .columns (4 )
70+ with col1 :
71+ st .metric ("Current Round" , "3/10" )
72+ with col2 :
73+ st .metric ("Active Clients" , "3" )
74+ with col3 :
75+ st .metric ("Model Accuracy" , "85.2%" )
76+ with col4 :
77+ st .metric ("Training Status" , "Active" )
78+
79+ st .info ("💡 Demo mode showing simulated training progress. In real federated learning, multiple banks would be training collaboratively." )
80+
81+ else :
82+ # Real training progress
83+ try :
84+ status = requests .get (f"{ SERVER_URL } /training_status" , timeout = 5 )
85+ if status .status_code == 200 :
86+ data = status .json ()
87+ col1 , col2 , col3 , col4 = st .columns (4 )
88+ with col1 :
89+ st .metric ("Current Round" , f"{ data .get ('current_round' , 0 )} /{ data .get ('total_rounds' , 10 )} " )
90+ with col2 :
91+ st .metric ("Active Clients" , data .get ('active_clients' , 0 ))
92+ with col3 :
93+ st .metric ("Clients Ready" , data .get ('clients_ready' , 0 ))
94+ with col4 :
95+ st .metric ("Training Status" , "Active" if data .get ('training_active' , False ) else "Inactive" )
96+ else :
97+ st .warning ("Could not fetch training status." )
98+ except Exception as e :
99+ st .warning (f"Could not connect to server for training status: { e } " )
100+
101+ # --- How it works ---
102+ st .header ("How Federated Learning Works" )
103+ st .markdown ("""
104+ **Traditional ML:** All banks send their data to a central server → Privacy risk ❌
105+
106+ **Federated Learning:**
107+ 1. Each bank keeps their data locally ✅
108+ 2. Banks train models on their own data ✅
109+ 3. Only model updates (not data) are shared ✅
110+ 4. Server aggregates updates to create global model ✅
111+ 5. Global model is distributed back to all banks ✅
112+
113+ **Result:** Collaborative learning without data sharing! 🎯
114+ """ )
115+
116+ st .markdown ("---" )
117+ st .markdown ("""
118+ *This is a demonstration of federated learning concepts. For full functionality, run the federated server and clients locally.*
119+ """ )
0 commit comments