Skip to content

Commit 135d1e4

Browse files
feat: complete demo
1 parent e7b58b1 commit 135d1e4

File tree

6 files changed

+294
-77
lines changed

6 files changed

+294
-77
lines changed

README.md

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ This project implements a federated learning framework combined with a Retrieval
44

55
## Features
66

7-
- Federated Learning using TensorFlow Federated
7+
- Federated Learning using TensorFlow
88
- Privacy-preserving data generation using VAE/GAN
99
- RAG integration for enhanced data quality
1010
- Secure Multi-Party Computation (SMPC)
1111
- Differential Privacy implementation
1212
- Kubernetes-based deployment
1313
- Comprehensive monitoring and logging
14+
- **NEW: Interactive Web Demo** - Try it out without setup!
1415

15-
## Installation
16-
17-
```bash
18-
pip install -r requirements.txt
19-
```
20-
21-
## Usage
16+
## Quick Demo (No Installation Required)
2217

18+
🚀 **Live Demo**: [Hugging Face Spaces](https://huggingface.co/spaces/ArchCoder/federated-credit-scoring)
2319

24-
## Project Structure
25-
20+
The web demo allows you to:
21+
- Enter customer features and get credit score predictions
22+
- See how federated learning works
23+
- Understand privacy-preserving ML concepts
2624

27-
## License
28-
29-
MIT
25+
## Installation
3026

31-
## Contributing
27+
```bash
28+
# Create virtual environment
29+
python3 -m venv venv
30+
source venv/bin/activate # On Windows: venv\Scripts\activate
3231

32+
# Install dependencies
33+
pip install -r requirements.txt
34+
```
3335

3436
## Federated Credit Scoring Demo (with Web App)
3537

@@ -65,7 +67,37 @@ streamlit run webapp/streamlit_app.py
6567
- Enter 32 features (dummy values are fine for demo)
6668
- Click "Predict Credit Score" to get a prediction from the federated model
6769
- View training progress in the app
70+
- Toggle between Demo Mode (no server required) and Real Mode (connects to server)
6871

6972
*For best results, keep the server and at least two clients running in parallel.*
7073

74+
## Project Structure
75+
76+
```
77+
FinFedRAG-Financial-Federated-RAG/
78+
├── src/
79+
│ ├── api/ # REST API for server and client communication
80+
│ ├── client/ # Federated learning client implementation
81+
│ ├── server/ # Federated learning server and coordinator
82+
│ ├── rag/ # Retrieval-Augmented Generation components
83+
│ ├── models/ # VAE/GAN models for data generation
84+
│ └── utils/ # Privacy, metrics, and utility functions
85+
├── webapp/ # Streamlit web application
86+
├── config/ # Configuration files
87+
├── tests/ # Unit and integration tests
88+
├── docker/ # Docker configurations
89+
├── kubernetes/ # Kubernetes deployment files
90+
└── app.py # Root app.py for Hugging Face Spaces deployment
91+
```
92+
93+
## License
94+
95+
MIT
96+
97+
## Contributing
98+
99+
Please read our contributing guidelines before submitting pull requests.
100+
71101
---
102+
103+
**Demo URL**: https://huggingface.co/spaces/ArchCoder/federated-credit-scoring

app.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
""")

config/client_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ client:
2323
training:
2424
local_epochs: 3
2525
learning_rate: 0.001
26+
batch_size: 32
2627

2728
# Privacy configuration
2829
privacy:

config/server_config.yaml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# server_config.yaml configuration
22

3-
server:
4-
# API server configuration
5-
api:
6-
host: "0.0.0.0"
7-
port: 8080
8-
debug: false
9-
10-
# Federated learning configuration
11-
federated:
12-
min_clients: 2
13-
rounds: 10
14-
sample_fraction: 0.8
15-
16-
# Aggregation configuration
17-
aggregation:
18-
method: "fedavg"
19-
weighted: true
20-
21-
# Monitoring configuration
22-
monitoring:
23-
log_level: "INFO"
3+
# API server configuration
4+
api:
5+
host: "0.0.0.0"
6+
port: 8080
7+
debug: false
8+
9+
# Federated learning configuration
10+
federated:
11+
min_clients: 2
12+
rounds: 10
13+
sample_fraction: 0.8
14+
15+
# Aggregation configuration
16+
aggregation:
17+
method: "fedavg"
18+
weighted: true
19+
20+
# Monitoring configuration
21+
monitoring:
22+
log_level: "INFO"
2423

2524
# Model configuration
2625
model:

requirements.txt

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
# Core ML frameworks
2-
tensorflow
3-
tensorflow-federated
4-
torch
5-
transformers
1+
# Core ML and Deep Learning
2+
tensorflow>=2.8.0
3+
numpy>=1.21.0
4+
pandas>=1.3.0
5+
scikit-learn>=1.0.0
66

7-
# Data processing
8-
pandas
9-
numpy
10-
scikit-learn
7+
# Web Framework and API
8+
flask>=2.0.0
9+
requests>=2.25.0
10+
streamlit
11+
12+
# Configuration and utilities
13+
pyyaml>=6.0
14+
pathlib2>=2.3.0
15+
16+
# Development and testing
17+
pytest>=6.0.0
18+
pytest-cov>=2.0.0
19+
20+
# Logging and monitoring
21+
python-json-logger>=2.0.0
22+
23+
# Optional: For advanced features
24+
# tensorflow-federated>=0.20.0 # Uncomment if using TFF
25+
# torch>=1.10.0 # Uncomment if using PyTorch
1126

1227
# RAG components
1328
elasticsearch
@@ -18,19 +33,8 @@ tensorflow-privacy
1833
pysyft
1934

2035
# API and web
21-
flask
2236
fastapi
2337
uvicorn
24-
requests
25-
streamlit
26-
27-
# Configuration and utilities
28-
pyyaml
29-
# Testing and development
30-
pytest
31-
black
32-
flake8
33-
isort
3438

3539
# Documentation
3640
sphinx

0 commit comments

Comments
 (0)