File size: 1,146 Bytes
b5f4f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, List

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field

from predictor import get_predictor


class PredictRequest(BaseModel):
    battery_id: str = Field(default="B0005", description="Battery id or 0-based battery index")
    window: List[List[float]] = Field(..., description="15 x 13 feature window")


app = FastAPI(title="Battery Capacity Predictor API", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

predictor = get_predictor()


@app.get("/")
def root() -> dict[str, str]:
    return {"status": "ok", "message": "Battery capacity prediction API is running"}


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "healthy"}


@app.post("/predict")
def predict(request: PredictRequest) -> dict[str, Any]:
    try:
        return predictor.predict(request.window, battery_id=request.battery_id)
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc