File size: 1,508 Bytes
72e2b6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from contextlib import asynccontextmanager

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware

from api.routes import models, monitoring, predict
from api.schemas import HealthResponse
from src.serving.ab_router import ABRouter
from src.serving.predictor import Predictor


@asynccontextmanager
async def lifespan(app: FastAPI):
    print("loading predictors...")
    app.state.predictors = {
        "classical": Predictor("classical"),
        "svm": Predictor("svm"),
        "transformer": Predictor("transformer"),
    }
    print("loading AB router...")
    app.state.ab_router = ABRouter()
    print("startup complete")

    yield

    app.state.predictors.clear()


app = FastAPI(
    title="Intent Classifier API",
    description="End-to-end intent classification with classical ML, neural networks, and fine-tuned transformers",
    version="1.0.0",
    lifespan=lifespan,
)

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

app.include_router(predict.router, tags=["predict"])
app.include_router(models.router, tags=["models"])
app.include_router(monitoring.router, tags=["monitoring"])


@app.get("/health", response_model=HealthResponse)
def health(request: Request) -> HealthResponse:
    return HealthResponse(
        status="ok",
        models_loaded=list(request.app.state.predictors.keys()),
    )