File size: 4,641 Bytes
f381be8
 
 
 
 
 
 
 
 
 
 
 
 
 
c9ce7dc
f381be8
c9ce7dc
f381be8
c9ce7dc
 
 
 
 
 
f381be8
c9ce7dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f381be8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# API Documentation

## Base URL

- **Local:** `http://localhost:7860`
- **Docker:** `http://localhost:7860`
- **Hugging Face Spaces:** `https://neerajcodz-aibatterylifecycle.hf.space`

## Interactive Docs

- **Swagger UI:** `/docs`
- **ReDoc:** `/redoc`
- **Gradio UI:** `/gradio`

## API Versioning (v3.0.0)

The API supports three model generations served in parallel, with dynamic on-demand loading:

| Prefix | Models | Features | Default Model | Status |
|--------|--------|:--------:|---------------|--------|
| `/api/v1/*` | v1 classical (12 models) | 12 | Random Forest | On-demand |
| `/api/v2/*` | v2 classical + deep (20+ models) | 12 | ExtraTrees | On-demand |
| `/api/v3/*` | v3 optimized + ensemble (18 models) | 18 | XGBoost | **Loaded at startup** |
| `/api/*` | Default (v3) | 18 | XGBoost | Same as v3 |

### Startup Behavior
- Only **v3** models are loaded at startup to minimize memory and boot time.
- v1 and v2 models can be loaded on-demand via `POST /api/versions/{v}/load`.
- Each version's metadata is read from `artifacts/{version}/models.json`.

### Version Management Endpoints

```http
GET /api/versions
```
Returns all versions with `on_disk`, `loaded`, `model_count`, `catalog_count`, `description`, `features`, `champion`, and `status`.

```http
POST /api/versions/{version}/load
```
Downloads (if needed) and loads a version's models into memory. Returns immediately if already on disk.

```http
GET /api/versions/{version}/models-meta
```
Returns the full `models.json` metadata for a version.

---

## Endpoints

### Health Check

```http
GET /health
```

Response:
```json
{
  "status": "ok",
  "version": "2.0.0",
  "models_loaded": 12,
  "device": "cpu"
}
```

---

### Single Prediction

```http
POST /api/predict
Content-Type: application/json
```

Request:
```json
{
  "battery_id": "B0005",
  "cycle_number": 100,
  "ambient_temperature": 24.0,
  "peak_voltage": 4.2,
  "min_voltage": 2.7,
  "avg_current": 2.0,
  "avg_temp": 25.0,
  "temp_rise": 3.0,
  "cycle_duration": 3600,
  "Re": 0.04,
  "Rct": 0.02,
  "delta_capacity": -0.005
}
```

Optionally include `"model_name"` to select a specific model (leave null to use the registry default):

```json
{
  ...
  "model_name": "random_forest"
}
```

Response:
```json
{
  "battery_id": "B0005",
  "cycle_number": 100,
  "soh_pct": 92.5,
  "rul_cycles": 450,
  "degradation_state": "Healthy",
  "confidence_lower": 90.5,
  "confidence_upper": 94.5,
  "model_used": "random_forest",
  "model_version": "v1.0.0"
}
```

---

### Ensemble Prediction

```http
POST /api/predict/ensemble
Content-Type: application/json
```

Always uses the **BestEnsemble (v3.0.0)** — weighted average of Random Forest, XGBoost, and
LightGBM (weights proportional to R²). Body is identical to single prediction.

Response includes `"model_version": "v3.0.0"`.

---

### Batch Prediction

```http
POST /api/predict/batch
Content-Type: application/json
```

Request:
```json
{
  "battery_id": "B0005",
  "cycles": [
    {"cycle_number": 1, "ambient_temperature": 24, ...},
    {"cycle_number": 2, "ambient_temperature": 24, ...}
  ]
}
```

---

### Recommendations

```http
POST /api/recommend
Content-Type: application/json
```

Request:
```json
{
  "battery_id": "B0005",
  "current_cycle": 100,
  "current_soh": 85.0,
  "ambient_temperature": 24.0,
  "top_k": 5
}
```

Response:
```json
{
  "battery_id": "B0005",
  "current_soh": 85.0,
  "recommendations": [
    {
      "rank": 1,
      "ambient_temperature": 24.0,
      "discharge_current": 0.5,
      "cutoff_voltage": 2.7,
      "predicted_rul": 500,
      "rul_improvement": 50,
      "rul_improvement_pct": 11.1,
      "explanation": "Operate at 24°C, 0.5A, cutoff 2.7V for ~500 cycles RUL"
    }
  ]
}
```

---

### Dashboard Data

```http
GET /api/dashboard
```

Returns full dashboard payload with battery fleet stats, capacity fade curves, and model metrics.

---

### Battery List

```http
GET /api/batteries
```

---

### Battery Capacity

```http
GET /api/battery/{battery_id}/capacity
```

---

### Model List

```http
GET /api/models
```

Returns every registered model with version, family, R², and load status.

---

### Model Versions

```http
GET /api/models/versions
```

Groups models by generation:

```json
{
  "v1_classical":  ["ridge", "lasso", "random_forest", "xgboost", "lightgbm", ...],
  "v2_deep":       ["vanilla_lstm", "bilstm", "gru", "attention_lstm", "tft", ...],
  "v2_ensemble":   ["best_ensemble"],
  "other":         [],
  "default_model": "best_ensemble"
}
```

---

### Figures

```http
GET /api/figures          # List all
GET /api/figures/{name}   # Serve a figure
```