aiBatteryLifeCycle / README.md
NeerajCodz's picture
feat: full project β€” ML simulation, dashboard UI, models on HF Hub
f381be8
---
title: AI Battery Lifecycle Predictor
emoji: πŸ”‹
colorFrom: green
colorTo: blue
sdk: docker
pinned: false
app_port: 7860
license: mit
---
# AI Battery Lifecycle Predictor
**IEEE Research-Grade** machine-learning system for predicting Li-ion battery
**State of Health (SOH)**, **Remaining Useful Life (RUL)**, and **degradation state**,
with an operational **recommendation engine** for lifecycle optimization.
Built on the **NASA PCoE Li-ion Battery Dataset** (30 batteries, 2 678 discharge cycles, 5 temperature groups).
---
## Key Results (v2 β€” Intra-Battery Chronological Split)
| Rank | Model | RΒ² | MAE (%) | Within Β±5% |
|------|-------|----|---------|------------|
| 1 | **ExtraTrees** | **0.975** | **0.84** | **99.3%** βœ“ |
| 2 | **SVR** | **0.974** | **0.87** | **99.3%** βœ“ |
| 3 | **GradientBoosting** | **0.958** | **1.12** | **98.5%** βœ“ |
| 4 | **RandomForest** | **0.952** | **1.34** | **96.7%** βœ“ |
| 5 | **LightGBM** | **0.948** | **1.51** | **96.0%** βœ“ |
**All 5 classical ML models exceed the 95% accuracy gate.** 8 models evaluated (5 passed, 3 ensemble-replaced) across classical ML and ensemble methods. 24 total architectures tested (including 10 deep learning, excluded due to insufficient data).
### v1 β†’ v2 Improvements
- **Split fix:** Cross-battery train-test split (data leakage) β†’ intra-battery chronological 80/20 per-battery split
- **Pass rate:** 41.7% (5/12 models passing) β†’ 100% (5/5 classical ML + 3 replaced ensemble models)
- **Top accuracy:** 94.2% β†’ 99.3% (+5.1 pp)
- **Bug fixes:** Removed avg_temp auto-correction; fixed recommendation baseline (0 cycles β†’ 100-1000 cycles)
- **New models:** ExtraTrees, GradientBoosting, Ensemble voting
- **Versioned API:** `/api/v1/*` (frozen, legacy) and `/api/v2/*` (current, bug-fixed, served in parallel)
---
## Highlights
| Feature | Details |
|---------|---------|
| **Models (24)** | Ridge, Lasso, ElasticNet, KNN Γ—3, SVR, Random Forest, **ExtraTrees**, **GradientBoosting**, XGBoost, LightGBM, LSTM Γ—4, BatteryGPT, TFT, iTransformer Γ—3, VAE-LSTM, Stacking & Weighted Ensemble |
| **Notebooks** | 9 research-grade Jupyter notebooks (EDA β†’ Evaluation), fully executed |
| **Frontend** | React + TypeScript + Three.js (3D battery pack heatmap), **v1/v2 toggle**, **Research Paper tab** |
| **Backend** | FastAPI REST API + Gradio interactive UI, **versioned /api/v1/ & /api/v2/** |
| **Deployment** | Single Docker container for Hugging Face Spaces |
---
## Quick Start
### 1. Clone & Setup
```bash
git clone <repo-url>
cd aiBatteryLifecycle
python -m venv venv
# Windows
.\venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
pip install -r requirements.txt
pip install torch --index-url https://download.pytorch.org/whl/cu124
pip install tensorflow
```
### 2. Run Notebooks
```bash
jupyter lab notebooks/
```
Execute notebooks `01_eda.ipynb` through `09_evaluation.ipynb` in order.
### 3. Start the API
```bash
uvicorn api.main:app --host 0.0.0.0 --port 7860 --reload
```
- **API Docs:** http://localhost:7860/docs
- **Gradio UI:** http://localhost:7860/gradio
- **Health:** http://localhost:7860/health
### 4. Start Frontend (Dev)
```bash
cd frontend
npm install
npm run dev
```
Open http://localhost:5173
### 5. Docker
```bash
# Recommended β€” docker compose
docker compose up --build
# Or low-level
docker build -t battery-predictor .
docker run -p 7860:7860 -e LOG_LEVEL=INFO battery-predictor
```
Add `-v ./artifacts/logs:/app/artifacts/logs` to persist structured JSON logs.
---
## Project Structure
```
aiBatteryLifecycle/
β”œβ”€β”€ cleaned_dataset/ # NASA PCoE dataset (142 CSVs + metadata)
β”œβ”€β”€ src/ # Core ML library
β”‚ β”œβ”€β”€ data/ # loader, features, preprocessing
β”‚ β”œβ”€β”€ models/
β”‚ β”‚ β”œβ”€β”€ classical/ # Ridge, KNN, SVR, RF, XGB, LGBM
β”‚ β”‚ β”œβ”€β”€ deep/ # LSTM, Transformer, iTransformer, VAE-LSTM
β”‚ β”‚ └── ensemble/ # Stacking, Weighted Average
β”‚ β”œβ”€β”€ evaluation/ # metrics, recommendations
β”‚ └── utils/ # config, plotting
β”œβ”€β”€ notebooks/ # 01_eda β†’ 09_evaluation
β”œβ”€β”€ api/ # FastAPI backend + Gradio
β”‚ β”œβ”€β”€ main.py
β”‚ β”œβ”€β”€ schemas.py
β”‚ β”œβ”€β”€ model_registry.py
β”‚ β”œβ”€β”€ gradio_app.py
β”‚ └── routers/
β”œβ”€β”€ frontend/ # Vite + React + Three.js
β”‚ └── src/components/ # Dashboard, 3D viz, Predict, etc.
β”œβ”€β”€ docs/ # Documentation
β”œβ”€β”€ artifacts/ # Generated: models, figures, scalers
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
└── README.md
```
---
## Dataset
**NASA Prognostics Center of Excellence (PCoE) Battery Dataset**
- 30 Li-ion 18650 cells (B0005–B0056, after cleaning)
- 2 678 discharge cycles extracted
- Nominal capacity: 2.0 Ah
- End-of-Life threshold: 1.4 Ah (30% fade)
- Five temperature groups: 4Β°C, 22Β°C, 24Β°C, 43Β°C, 44Β°C
- Cycle types: charge, discharge, impedance
- 12 engineered features per cycle (voltage, current, temperature, impedance, duration)
**Reference:** B. Saha and K. Goebel (2007). *Battery Data Set*, NASA Prognostics Data Repository.
---
## Models
### Classical ML
- **Linear:** Ridge, Lasso, ElasticNet
- **Instance-based:** KNN (3 configs)
- **Kernel:** SVR (RBF)
- **Tree ensemble:** Random Forest, **ExtraTrees** *(v2)*, **GradientBoosting** *(v2)*, XGBoost (Optuna HPO), LightGBM (Optuna HPO)
### Deep Learning
- **LSTM family:** Vanilla, Bidirectional, GRU, Attention LSTM (MC Dropout uncertainty)
- **Transformer:** BatteryGPT (nano decoder-only), TFT (Temporal Fusion)
- **iTransformer:** Vanilla, Physics-Informed (dual-head), Dynamic-Graph
### Generative
- **VAE-LSTM:** Variational autoencoder with LSTM encoder/decoder, health head, anomaly detection
### Ensemble
- **Stacking:** Out-of-fold + Ridge meta-learner
- **Weighted Average:** L-BFGS-B optimized weights
---
## API Endpoints
| Method | Path | Description |
|--------|------|-------------|
| POST | `/api/predict` | Single-cycle SOH prediction (default: v2 models) |
| POST | `/api/v1/predict` | Predict using v1 models (cross-battery split) |
| POST | `/api/v2/predict` | Predict using v2 models (chrono split, bug-fixed) |
| POST | `/api/predict/ensemble` | Always uses BestEnsemble |
| POST | `/api/predict/batch` | Multi-cycle batch prediction |
| POST | `/api/recommend` | Operational recommendations |
| POST | `/api/v2/recommend` | v2 recommendations (fixed baseline) |
| GET | `/api/models` | List all models with version / RΒ² metadata |
| GET | `/api/v1/models` | List v1 models |
| GET | `/api/v2/models` | List v2 models |
| GET | `/api/models/versions` | Group models by generation (v1 / v2) |
| GET | `/api/dashboard` | Full dashboard data |
| GET | `/api/batteries` | List all batteries |
| GET | `/api/battery/{id}/capacity` | Per-battery capacity curve |
| GET | `/api/figures` | List saved figures (PNG only) |
| GET | `/api/figures/{name}` | Serve a figure |
| GET | `/health` | Liveness probe |
All endpoints are documented interactively at **`/docs`** (Swagger UI) and **`/redoc`**.
---
## License
This project is for academic and research purposes.
Dataset: NASA PCoE public domain.