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