File size: 1,478 Bytes
d40eb53 | 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 | from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from predictor import load_benchmark, load_teacher, load_student
_models: dict = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
_models["benchmark"] = load_benchmark()
_models["teacher"] = load_teacher()
_models["student"] = load_student()
yield
_models.clear()
app = FastAPI(title="AMANE Toxicity API", version="1.0.0", lifespan=lifespan)
class PredictRequest(BaseModel):
text: str
model: str = "student"
class BatchRequest(BaseModel):
texts: list[str]
model: str = "student"
@app.get("/health")
def health():
return {
name: {"loaded": p.loaded, "error": p.error}
for name, p in _models.items()
}
@app.post("/predict")
def predict(req: PredictRequest):
p = _models.get(req.model)
if p is None:
raise HTTPException(status_code=404, detail=f"Unknown model: {req.model}")
if not p.loaded:
raise HTTPException(status_code=503, detail=f"Model not available: {p.error}")
return p.predict(req.text)
@app.post("/predict_batch")
def predict_batch(req: BatchRequest):
p = _models.get(req.model)
if p is None:
raise HTTPException(status_code=404, detail=f"Unknown model: {req.model}")
if not p.loaded:
raise HTTPException(status_code=503, detail=f"Model not available: {p.error}")
return p.predict_batch(req.texts)
|