Middle+
from src import load_model @app.post("/classify") async def classify(request: dict): text = request["text"] model = load_model("classifier_v1") prediction = model.predict([text]) return {"label": prediction}
sobes.tech AI
Answer from AI
The provided code has several issues:
-
Loading the model inside the handler —
load_model("classifier_v1")is called on every request, which is very inefficient and slows down the operation. -
Asynchronous and blocking operations — if
model.predictblocks, it can slow down the asynchronous server. -
Lack of input validation — it is advisable to check for the presence and type of the "text" field.
The correct approach is to load the model once at application startup and reuse it:
from src import load_model
from fastapi import FastAPI, HTTPException
app = FastAPI()
model = load_model("classifier_v1") # load at startup
@app.post("/classify")
async def classify(request: dict):
text = request.get("text")
if not text or not isinstance(text, str):
raise HTTPException(status_code=400, detail="Invalid or missing 'text' field")
prediction = model.predict([text])
return {"label": prediction[0]}
If model.predict is a blocking operation, it can be moved to a thread pool using run_in_executor to maintain asynchronicity.