This decentralized application (DApp) leverages the power of federated learning and blockchain technology to enable secure and collaborative data analysis for smart grids.
- Federated Learning: Train a shared machine learning model across multiple smart grids without directly sharing raw data.
- Differential Privacy: Enhance data privacy during model training using PySyft's DPAdam optimizer.
- Homomorphic Encryption: Explore secure data aggregation techniques using homomorphic encryption.
- Blockchain Integration: Record model updates and data usage agreements on the Energy Web Chain for transparency and auditability.
- PySyft: A framework for secure and private federated learning.
- PyTorch: A deep learning library for model development.
- Flask: A Python web framework for building the backend API.
- Web3.py: A library for interacting with the Energy Web Chain.
- Solidity: The programming language for writing the smart contract.
- Truffle: A development environment for Ethereum and EVM-compatible blockchains.
- Ganache: A local blockchain emulator for development and testing.
📦 fuzzy-dollop
├─ Dockerfile
├─ LICENSE
├─ app.py
├─ contracts
│ └─ DataSharing.sol
├─ docker-compose.yml
├─ federated_learning.py
├─ migrations
│ ├─ 1_initial_migration.js
│ └─ 2_deploy_contracts.js
├─ models
│ ├─ handler
│ │ └─ custom_handler.py
│ └─ model.py
├─ readme.md
├─ requirements.txt
├─ setup
│ ├─ setup_node.sh
│ ├─ setup_python.sh
│ └─ setup_torchserve.sh
├─ static
│ ├─ css
│ │ └─ styles.css
│ └─ js
│ └─ scripts.js
├─ templates
│ └─ index.html
├─ tests
│ ├─ test_blockchain.py
│ └─ test_prediction.py
└─ utils
├─ blockchain.py
└─ prediction.py
- Node.js: https://nodejs.org/
- Python 3.8+: https://www.python.org/
- Docker: https://www.docker.com/
- Energy Web Chain Account: https://www.energyweb.org/
-
Clone the Repository:
git clone https://github.com/your-username/smart-grid-dapp.git cd smart-grid-dapp
-
Install Dependencies:
bash setup/setup_node.sh
python3 -m venv venv source venv/bin/activate
pip install -r requirements.txt
bash setup/setup_torchserve.sh
- Open
truffle-config.js
. - Configure the
networks
section to connect to the Energy Web Chain. - Provide your EWC account credentials and an API key for an EWC node provider (if necessary).
Important: Replace "YOUR_PRIVATE_KEY"
in app.py
and test_blockchain.py
with a valid private key for interacting with the blockchain. For security reasons, use a test account and a testnet for development.
-
Start Ganache (for local development):
ganache-cli
-
Deploy the Smart Contract:
truffle compile truffle migrate --network ewc
-
Train the Model:
python federated_learning.py
-
Archive and Serve the Model:
torch-model-archiver --model-name smartgridmodel --version 1.0 \ --model-file models/model.py --serialized-file global_model.pth \ --handler models/handler/custom_handler.py --export-path model_store torchserve --start --ncs --model-store model_store --models smartgridmodel=smartgridmodel.mar
-
Run the Flask Application:
python app.py
-
Access the UI: Open the provided URL (from Codespaces or your local server) in your web browser.
-
Run All Tests:
python -m unittest discover
-
Run Specific Tests:
python -m unittest tests/test_blockchain.py python -m unittest tests/test_prediction.py
- Integrate real smart grid data sources.
- Develop a more sophisticated user interface.
- Implement additional blockchain functionalities (e.g., data usage agreements, tokenized rewards).
- Explore more advanced privacy-preserving techniques.
- Deploy to a production environment.
Contributions are welcome! Please fork the repository and submit pull requests.
This project is licensed under the MIT License.