Skip to content

Repository files navigation

Federated Learning System - Complete Implementation

A production-ready Federated Learning system with three main components following the "Modern Privacy" stack.

📁 Project Structure

fl-system/
├── fl-server/          # FastAPI backend with Socket.io
│   ├── main.py         # Main server application
│   ├── fedavg_service.py  # Federated Averaging algorithm
│   ├── requirements.txt   # Python dependencies
│   └── Dockerfile      # Docker configuration
├── fl-client/          # React frontend with TensorFlow.js
│   ├── src/
│   │   ├── App.js      # Main React component
│   │   ├── App.css     # Styling
│   │   ├── index.js    # Entry point
│   │   └── trainingService.js  # Training utilities
│   ├── public/
│   │   └── index.html
│   └── package.json
├── fl-dashboard/       # Next.js admin dashboard
│   ├── src/
│   │   ├── pages/
│   │   │   ├── _app.js
│   │   │   └── index.js
│   │   └── styles/
│   └── package.json
└── PLAN.md             # Development plan

🚀 Quick Start

Option 1: Docker (Recommended)

# Start all services
docker-compose up

# Access points:
# - FL Server: http://localhost:8000
# - FL Client: http://localhost:3000
# - Dashboard: http://localhost:5000

Option 2: Manual Setup

1. Start the FL Server

# Install dependencies
cd fl-server
pip install -r requirements.txt

# Start the server
python main.py

Server will be available at http://localhost:8000

Available Endpoints:

  • GET / - API information
  • GET /health - Health check
  • GET /model/global - Get global model weights
  • POST /model/update - Submit weight update
  • GET /stats - Server statistics
  • WS /ws/{client_id} - WebSocket connection

2. Start the FL Client

cd fl-client
npm install
npm start

Client will be available at http://localhost:3000

Features:

  • Connects to server via WebSocket
  • Downloads global model automatically
  • Trains model locally using TensorFlow.js
  • Sends weight updates to server
  • Real-time status updates

3. Start the Dashboard

cd fl-dashboard
npm install
npm run dev

Dashboard will be available at http://localhost:5000

Features:

  • Real-time system health monitoring
  • Model accuracy trends
  • Client participation statistics
  • Federated averaging progress

🧠 How It Works

Federated Learning Flow

  1. Server Initialization: Server creates initial model weights
  2. Client Download: Clients connect and download global model
  3. Local Training: Clients train on their local data (TensorFlow.js)
  4. Weight Update: Clients send only weight updates (not raw data)
  5. Aggregation: Server performs Federated Averaging (FedAvg)
  6. Model Update: New global model is distributed to all clients

Privacy Benefits

  • Raw data never leaves user device
  • Only model weight updates are shared
  • Differential privacy support (client-side)
  • No central storage of sensitive data

🔧 API Reference

FL Server Endpoints

GET /health

{
  "status": "healthy",
  "connected_clients": 5,
  "model_version": 3
}

GET /model/global

{
  "model": {
    "version": 0,
    "layers": [...],
    "metadata": {...}
  },
  "model_version": 0,
  "client_count": 5
}

POST /model/update

{
  "client_id": "client_abc123",
  "weights": {...},
  "accuracy": 0.85,
  "samples_trained": 1000
}

WebSocket Events

Client to Server:

  • weight_update - Send weight updates
  • request_model - Request latest model

Server to Client:

  • model_update - Receive model update
  • update_received - Update acknowledgment
  • training_round_complete - Round completion notification

🛠️ Tech Stack

Layer Technology
Frontend React.js + TensorFlow.js
Backend FastAPI + Socket.io
ML TensorFlow/Keras (server), TensorFlow.js (client)
Database MongoDB (for model metadata)
Real-time WebSockets (Socket.io)
Containerization Docker

📊 Architecture Diagram

                    Federated Learning System
                    
  +-------------+    +-----------------+    +-------------+
  |   Client 1  |    |   FL Server     |    |  Dashboard  |
  | (React.js)  |◄──►|   (FastAPI)     |◄──►|  (Next.js)  |
  |  Training   |    |  Aggregation    |    |  Monitoring |
  +------+------+    +--------+--------+    +-------------+
         |                    |
         |  Weight Updates    |
         |   (WebSocket)      |
         v                    v
  +-------------+    +-----------------+
  |   Client N  |    |  FedAvg Service |
  |  Training   |    |  Model Updates  |
  +-------------+    +-----------------+

🧪 Testing

Test Server Endpoints

# Health check
curl http://localhost:8000/health

# Get model
curl http://localhost:8000/model/global

# Get stats
curl http://localhost:8000/stats

Test with Multiple Clients

  1. Open multiple browser tabs at http://localhost:3000
  2. Each tab gets a unique client ID
  3. Click "Train Local Model" on each
  4. Watch Dashboard for aggregation progress

🔐 Security Features

  • Client Authentication: Unique client IDs for tracking
  • Secure Communication: WebSocket connections
  • Differential Privacy: Optional noise addition (see fl-client/src/differentialPrivacy.js)
  • No Raw Data Exposure: Only model weights transmitted

📈 Performance Metrics

The Dashboard displays:

  • Model accuracy over rounds
  • Training loss trends
  • Client participation rates
  • Round completion times
  • System resource utilization

🚦 Next Steps (Development Phases)

  1. Phase 1: Environment and Backend Setup (Complete)
  2. Phase 2: Client-side training with real data
  3. Phase 3: Advanced Federated Averaging (secure aggregation)
  4. Phase 4: Enhanced Dashboard with more visualizations

📝 License

MIT License - See LICENSE file for details

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Built with for privacy-preserving machine learning

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages