File size: 4,822 Bytes
e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 135d1e4 e7b58b1 |
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 |
import streamlit as st
import requests
import numpy as np
import time
st.set_page_config(page_title="Federated Credit Scoring Demo", layout="centered")
st.title("Federated Credit Scoring Demo (Federated Learning)")
# Sidebar configuration
st.sidebar.header("Configuration")
SERVER_URL = st.sidebar.text_input("Server URL", value="http://localhost:8080")
DEMO_MODE = st.sidebar.checkbox("Demo Mode (No Server Required)", value=True)
st.markdown("""
This demo shows how multiple banks can collaboratively train a credit scoring model using federated learning, without sharing raw data.
Enter customer features below to get a credit score prediction from the federated model.
""")
# --- Feature Input Form ---
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 ---
if submitted:
if DEMO_MODE:
# Demo mode - simulate prediction
with st.spinner("Processing prediction..."):
time.sleep(1) # Simulate processing time
# Simple demo prediction based on feature values
demo_prediction = sum(features) / len(features) * 100 + 500 # Scale to credit score range
st.success(f"Demo Prediction: Credit Score = {demo_prediction:.2f}")
st.info("π‘ This is a demo prediction. In a real federated system, this would come from the trained model.")
# Show what would happen in real mode
st.markdown("---")
st.markdown("**What happens in real federated learning:**")
st.markdown("1. Your features are sent to the federated server")
st.markdown("2. Server uses the global model (trained by multiple banks)")
st.markdown("3. Prediction is returned without exposing any bank's data")
else:
# Real mode - connect to server
try:
with st.spinner("Connecting to federated server..."):
resp = requests.post(f"{SERVER_URL}/predict", json={"features": features}, timeout=10)
if resp.status_code == 200:
prediction = resp.json().get("prediction")
st.success(f"Predicted Credit Score: {prediction:.2f}")
else:
st.error(f"Prediction failed: {resp.json().get('error', 'Unknown error')}")
except Exception as e:
st.error(f"Error connecting to server: {e}")
st.info("π‘ Try enabling Demo Mode to see the interface without a server.")
# --- Training Progress ---
st.header("Federated Training Progress")
if DEMO_MODE:
# Demo training progress
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Current Round", "3/10")
with col2:
st.metric("Active Clients", "3")
with col3:
st.metric("Model Accuracy", "85.2%")
with col4:
st.metric("Training Status", "Active")
st.info("π‘ Demo mode showing simulated training progress. In real federated learning, multiple banks would be training collaboratively.")
else:
# Real training progress
try:
status = requests.get(f"{SERVER_URL}/training_status", timeout=5)
if status.status_code == 200:
data = status.json()
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Current Round", f"{data.get('current_round', 0)}/{data.get('total_rounds', 10)}")
with col2:
st.metric("Active Clients", data.get('active_clients', 0))
with col3:
st.metric("Clients Ready", data.get('clients_ready', 0))
with col4:
st.metric("Training Status", "Active" if data.get('training_active', False) else "Inactive")
else:
st.warning("Could not fetch training status.")
except Exception as e:
st.warning(f"Could not connect to server for training status: {e}")
# --- How it works ---
st.header("How Federated Learning Works")
st.markdown("""
**Traditional ML:** All banks send their data to a central server β Privacy risk β
**Federated Learning:**
1. Each bank keeps their data locally β
2. Banks train models on their own data β
3. Only model updates (not data) are shared β
4. Server aggregates updates to create global model β
5. Global model is distributed back to all banks β
**Result:** Collaborative learning without data sharing! π―
""")
st.markdown("---")
st.markdown("""
*This is a demonstration of federated learning concepts. For full functionality, run the federated server and clients locally.*
""") |