Skip to content

Commit b407fad

Browse files
fix : fixed the client simulator
1 parent 45309a1 commit b407fad

File tree

3 files changed

+155
-155
lines changed

3 files changed

+155
-155
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ license: mit
1717

1818
# Federated Learning for Privacy-Preserving Financial Data Generation with RAG Integration
1919

20-
This project implements a **complete federated learning framework** with a Retrieval-Augmented Generation (RAG) system for privacy-preserving synthetic financial data generation. The system includes a working server, multiple clients, and an interactive web application.
20+
This project implements a complete federated learning framework with a Retrieval-Augmented Generation (RAG) system for privacy-preserving synthetic financial data generation. The system includes a working server, multiple clients, and an interactive web application.
2121

22-
## 🚀 Live Demo
22+
## Live Demo
2323

2424
**Try it now**: [Hugging Face Spaces](https://huggingface.co/spaces/ArchCoder/federated-credit-scoring)
2525

26-
## Features
26+
## Features
2727

2828
- **Complete Federated Learning System**: Working server, clients, and web interface
2929
- **Real-time Predictions**: Get credit score predictions from the federated model
@@ -33,9 +33,9 @@ This project implements a **complete federated learning framework** with a Retri
3333
- **Educational**: Learn about federated learning concepts
3434
- **Production Ready**: Docker and Kubernetes deployment support
3535

36-
## 🎯 Quick Start
36+
## Quick Start
3737

38-
### Option 1: Try the Demo (No Setup Required)
38+
### Option 1: Try the Demo
3939
1. Visit the [Live Demo](https://huggingface.co/spaces/ArchCoder/federated-credit-scoring)
4040
2. Enter customer features and get predictions
4141
3. Learn about federated learning
@@ -72,7 +72,7 @@ streamlit run webapp/streamlit_app.py
7272
python test_complete_system.py
7373
```
7474

75-
## 🎮 How to Use
75+
## How to Use
7676

7777
### Web Application Features:
7878
- **Demo Mode**: Works without server (perfect for HF Spaces)
@@ -91,7 +91,7 @@ python test_complete_system.py
9191
6. **Global Model**: Updated model is distributed to all clients
9292
7. **Prediction**: Users can get predictions from the global model
9393

94-
## 🏗️ System Architecture
94+
## System Architecture
9595

9696
```
9797
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
@@ -107,7 +107,7 @@ python test_complete_system.py
107107
└─────────────────┘
108108
```
109109

110-
## 📁 Project Structure
110+
## Project Structure
111111

112112
```
113113
FinFedRAG-Financial-Federated-RAG/
@@ -129,7 +129,7 @@ FinFedRAG-Financial-Federated-RAG/
129129
└── test_complete_system.py # End-to-end system test
130130
```
131131

132-
## 🔧 Configuration
132+
## Configuration
133133

134134
### Server Configuration (`config/server_config.yaml`)
135135
```yaml
@@ -159,20 +159,20 @@ client:
159159
input_dim: 32
160160
```
161161

162-
## 🧪 Testing
162+
## Testing
163163

164164
Run the complete system test:
165165
```bash
166166
python test_complete_system.py
167167
```
168168

169169
This will test:
170-
- Server health
171-
- Client registration
172-
- Training status
173-
- Prediction functionality
170+
- Server health
171+
- Client registration
172+
- Training status
173+
- Prediction functionality
174174

175-
## 🚀 Deployment
175+
## Deployment
176176

177177
### Hugging Face Spaces (Recommended)
178178
1. Fork this repository
@@ -196,26 +196,26 @@ streamlit run webapp/streamlit_app.py
196196
docker-compose up
197197
```
198198

199-
## 📊 Performance
199+
## Performance
200200

201201
- **Model Accuracy**: 85%+ across federated rounds
202202
- **Response Time**: <1 second for predictions
203203
- **Scalability**: Supports 10+ concurrent clients
204204
- **Privacy**: Zero raw data sharing
205205

206-
## 🤝 Contributing
206+
## Contributing
207207

208208
1. Fork the repository
209209
2. Create a feature branch
210210
3. Make your changes
211211
4. Add tests
212212
5. Submit a pull request
213213

214-
## 📄 License
214+
## License
215215

216216
MIT License - see LICENSE file for details.
217217

218-
## 🙏 Acknowledgments
218+
## Acknowledgments
219219

220220
- TensorFlow for the ML framework
221221
- Streamlit for the web interface

app.py

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,73 @@
66
import json
77
from datetime import datetime
88

9+
# Client Simulator Class (moved to top)
10+
class ClientSimulator:
11+
def __init__(self, server_url):
12+
self.server_url = server_url
13+
self.client_id = f"web_client_{int(time.time())}"
14+
self.is_running = False
15+
self.thread = None
16+
self.last_update = "Never"
17+
18+
def start(self):
19+
self.is_running = True
20+
self.thread = threading.Thread(target=self._run_client, daemon=True)
21+
self.thread.start()
22+
23+
def stop(self):
24+
self.is_running = False
25+
26+
def _run_client(self):
27+
try:
28+
# Register with server
29+
client_info = {
30+
'dataset_size': 100,
31+
'model_params': 10000,
32+
'capabilities': ['training', 'inference']
33+
}
34+
35+
resp = requests.post(f"{self.server_url}/register",
36+
json={'client_id': self.client_id, 'client_info': client_info})
37+
38+
if resp.status_code == 200:
39+
st.session_state.training_history.append({
40+
'round': 0,
41+
'active_clients': 1,
42+
'clients_ready': 0,
43+
'timestamp': datetime.now()
44+
})
45+
46+
# Simulate client participation
47+
while self.is_running:
48+
try:
49+
# Get training status
50+
status = requests.get(f"{self.server_url}/training_status")
51+
if status.status_code == 200:
52+
data = status.json()
53+
54+
# Update training history
55+
st.session_state.training_history.append({
56+
'round': data.get('current_round', 0),
57+
'active_clients': data.get('active_clients', 0),
58+
'clients_ready': data.get('clients_ready', 0),
59+
'timestamp': datetime.now()
60+
})
61+
62+
# Keep only last 50 entries
63+
if len(st.session_state.training_history) > 50:
64+
st.session_state.training_history = st.session_state.training_history[-50:]
65+
66+
time.sleep(5) # Check every 5 seconds
67+
68+
except Exception as e:
69+
print(f"Client simulator error: {e}")
70+
time.sleep(10)
71+
72+
except Exception as e:
73+
print(f"Failed to start client simulator: {e}")
74+
self.is_running = False
75+
976
st.set_page_config(page_title="Federated Credit Scoring Demo", layout="centered")
1077
st.title("Federated Credit Scoring Demo (Federated Learning)")
1178

@@ -172,71 +239,4 @@
172239
st.markdown("---")
173240
st.markdown("""
174241
*This is a demonstration of federated learning concepts. For full functionality, run the federated server and clients locally.*
175-
""")
176-
177-
# Client Simulator Class
178-
class ClientSimulator:
179-
def __init__(self, server_url):
180-
self.server_url = server_url
181-
self.client_id = f"web_client_{int(time.time())}"
182-
self.is_running = False
183-
self.thread = None
184-
self.last_update = "Never"
185-
186-
def start(self):
187-
self.is_running = True
188-
self.thread = threading.Thread(target=self._run_client, daemon=True)
189-
self.thread.start()
190-
191-
def stop(self):
192-
self.is_running = False
193-
194-
def _run_client(self):
195-
try:
196-
# Register with server
197-
client_info = {
198-
'dataset_size': 100,
199-
'model_params': 10000,
200-
'capabilities': ['training', 'inference']
201-
}
202-
203-
resp = requests.post(f"{self.server_url}/register",
204-
json={'client_id': self.client_id, 'client_info': client_info})
205-
206-
if resp.status_code == 200:
207-
st.session_state.training_history.append({
208-
'round': 0,
209-
'active_clients': 1,
210-
'clients_ready': 0,
211-
'timestamp': datetime.now()
212-
})
213-
214-
# Simulate client participation
215-
while self.is_running:
216-
try:
217-
# Get training status
218-
status = requests.get(f"{self.server_url}/training_status")
219-
if status.status_code == 200:
220-
data = status.json()
221-
222-
# Update training history
223-
st.session_state.training_history.append({
224-
'round': data.get('current_round', 0),
225-
'active_clients': data.get('active_clients', 0),
226-
'clients_ready': data.get('clients_ready', 0),
227-
'timestamp': datetime.now()
228-
})
229-
230-
# Keep only last 50 entries
231-
if len(st.session_state.training_history) > 50:
232-
st.session_state.training_history = st.session_state.training_history[-50:]
233-
234-
time.sleep(5) # Check every 5 seconds
235-
236-
except Exception as e:
237-
print(f"Client simulator error: {e}")
238-
time.sleep(10)
239-
240-
except Exception as e:
241-
print(f"Failed to start client simulator: {e}")
242-
self.is_running = False
242+
""")

0 commit comments

Comments
 (0)