A privacy-preserving federated learning platform that enables secure collaboration between energy prosumers while maintaining data privacy through blockchain technology. The system leverages Ethereum smart contracts deployed on the Sepolia testnet for transparent and immutable model weight management.
FedGrid AI Innovation is a comprehensive blockchain-based solution that combines federated learning with energy grid optimization. The platform allows prosumers (producers + consumers) to collaboratively train machine learning models without sharing sensitive data, while maintaining complete transparency and auditability through Ethereum smart contracts.
- Blockchain Integration: Smart contracts deployed on Ethereum Sepolia testnet for transparent model weight management
- MetaMask Wallet Authentication: Secure Web3 authentication using MetaMask wallet
- Privacy-Preserving Federated Learning: Train models locally without sharing raw data
- Decentralized Model Aggregation: Smart contract-based weight aggregation on-chain
- Real-time Dashboard: Live updates showing system status and model performance
- Role-Based Access Control: User and Operator access levels
- Immutable Audit Trail: All model updates recorded on blockchain
- Ethereum Sepolia Testnet - Production blockchain network for deployment
- Ganache - Local blockchain for development and testing
- Web3.py - Python library for blockchain interaction
- Ethers.js - JavaScript library for wallet integration
- Smart Contracts - Solidity contracts for model weight management
- MetaMask - Web3 wallet for authentication
- React - Modern UI framework
- Vite - Fast build tool and dev server
- TailwindCSS - Utility-first CSS framework
- Chart.js - Data visualization
- Leaflet - Interactive maps
- Python - ML operations and blockchain interaction
- TensorFlow - Deep learning framework
- Flask - API server
- NumPy - Numerical computations
The system uses Ethereum smart contracts to manage federated learning:
-
Development & Testing: Ganache local blockchain
- Fast iteration and testing
- No gas costs
- Complete control over network
-
Production Deployment: Sepolia Testnet
- Public Ethereum test network
- Real blockchain environment
- Free test ETH from faucets
postLocalWeights()- Prosumers submit locally trained model weightsgetGlobalModel()- Retrieve current global model weightsupdateGlobalModel()- Aggregate and update global model (owner only)getParticipants()- List all participating prosumers
- Sepolia Testnet:
0x8eaa1ceea2629d42765cbf9032981cef419a2a39
Important: The frontend is fully API-driven with NO static/hardcoded data. All information is fetched dynamically from the Flask backend API.
| Endpoint | Method | Description |
|---|---|---|
/get-global-model |
GET | Fetch global model weights from blockchain |
/get-prediction |
GET | Get personalized energy predictions (24h/7d/30d) |
/get-regional-data |
GET | Fetch real-time regional grid metrics |
/get-bill |
GET | Generate electricity bill for user |
User Browser → React Frontend → Flask API → Blockchain/ML Models
↓
Dynamic Data Display
All components fetch live data:
- BillPayment.jsx: Fetches bills from
/get-billAPI - ModelDashboard.jsx: Fetches model weights from
/get-global-modelAPI - Operator Dashboard: Fetches regional data from
/get-regional-dataAPI - User Predictions: Fetches predictions from
/get-predictionAPI
- Local Training: Each prosumer trains a model on their private energy data
- Weight Submission: Model weights are submitted to the smart contract via
submit_weights.py - Blockchain Storage: Weights are stored immutably on Sepolia testnet
- Aggregation: Operator runs
aggregate.pyto compute federated average - Global Update: Aggregated weights are pushed back to the smart contract
- Distribution: All prosumers retrieve updated global model from blockchain via API
- Raw energy consumption data never leaves local devices
- Only model weights (mathematical parameters) are shared
- Blockchain provides transparency without compromising privacy
- Each transaction is cryptographically signed and verified
- API provides personalized predictions without exposing raw data
- Node.js (v14 or higher)
- Python (v3.8 or higher)
- MetaMask browser extension
- Ethereum Wallet with Sepolia testnet ETH
git clone https://github.com/YourUsername/MajorProject.git
cd MajorProjectnpm installpip install -r requirements.txtCreate a .env file with your blockchain configuration:
SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_API_KEY
CONTRACT_ADDRESS=0x8eaa1ceea2629d42765cbf9032981cef419a2a39
PRIVATE_KEY=your_private_key_hereVisit a Sepolia faucet to get free test ETH:
npm run devThe application will be available at http://localhost:5173
python api.pyAPI server runs on http://localhost:5000
Train models for each prosumer:
python train_local_model.pyThis generates local model weights stored in .npz files.
Submit trained weights to the Sepolia smart contract:
python submit_weights.pyThis creates blockchain transactions for each prosumer's weights.
Run the aggregation script (requires contract owner privileges):
python aggregate.pyThis computes the federated average and updates the global model on-chain.
For local development and testing:
- Install Ganache: Download from https://trufflesuite.com/ganache/
- Start Ganache: Launch the application and create a new workspace
- Update Configuration: Point
SEPOLIA_RPC_URLtohttp://127.0.0.1:7545 - Deploy Contract: Deploy your smart contract to the local network
- Test Locally: Run all scripts against Ganache for fast iteration
FedGrid-AIINNOVATION/
├── src/ # React frontend source
│ ├── App.jsx # Main application component
│ ├── WalletConnect.jsx # MetaMask wallet integration
│ ├── ModelDashboard.jsx # ML model dashboard
│ └── BillPayment.jsx # Energy billing interface
├── public/ # Static assets
├── train_local_model.py # Local model training script
├── submit_weights.py # Blockchain weight submission
├── aggregate.py # Federated aggregation script
├── api.py # Flask API server
├── household_*_dataset.csv # Sample energy datasets
├── package.json # Node.js dependencies
├── requirements.txt # Python dependencies
└── README.md # This file
- Install MetaMask: Download from https://metamask.io/
- Add Sepolia Network: Configure MetaMask for Sepolia testnet
- Get Test ETH: Use a faucet to get free Sepolia ETH
- Connect: Click "Connect MetaMask Wallet" in the application
- Select Role: Choose User or Operator access level
- User: View personal energy data, participate in federated learning
- Operator: Manage grid operations, run aggregation, monitor system
- Never commit private keys to version control
- Use environment variables for sensitive configuration
- Keep MetaMask wallet secure with strong password
- Verify contract addresses before transactions
- Test thoroughly on Ganache before Sepolia deployment
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch
- Test on Ganache local network
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Ethereum Foundation for Sepolia testnet
- Truffle Suite for Ganache
- MetaMask for Web3 wallet integration
- OpenAI for federated learning research
For questions or support, please open an issue on GitHub.
Built with ❤️ using Blockchain & Federated Learning