File size: 995 Bytes
745c264
 
e748b64
c81d774
745c264
 
 
 
 
 
 
 
 
 
c81d774
e748b64
745c264
 
 
c81d774
 
745c264
 
e748b64
745c264
 
 
 
 
 
85698b8
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
from __future__ import annotations

from fastapi import FastAPI, HTTPException

from schemas import HealthResponse, PredictRequest, PredictResponse
from service import TimesFmService

app = FastAPI(title="TimesFm Space", version="0.1.0")
service = TimesFmService()


@app.get("/health", response_model=HealthResponse)
def health() -> HealthResponse:
    return service.health()


@app.get("/")
def root() -> dict[str, str]:
    return {"service": "timesfm", "status": "ok"}


@app.post("/predict", response_model=PredictResponse)
def predict(payload: PredictRequest) -> PredictResponse:
    try:
        return service.predict(payload)
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    except RuntimeError as exc:
        raise HTTPException(status_code=503, detail=str(exc)) from exc
    except Exception as exc:  # pragma: no cover - API guardrail
        raise HTTPException(status_code=500, detail=f"prediction_failed: {exc}") from exc