Deploying AI into Production with FastAPI
Matt Eckerle
Software and Data Engineering Leader
from pydantic import BaseModel
class PredictionRequest(BaseModel):
text: str
class PredictionResponse(BaseModel):
text: str
sentiment: str
confidence: float
@app.post("/predict") def predict_sentiment(request: PredictionRequest): if sentiment_model is None: raise HTTPException( status_code=503, detail="Model not loaded" )
result = sentiment_model(request.text)
return PredictionResponse( text=request.text, sentiment=result[0]["label"], confidence=result[0]["score"] )
Input JSON:
{"text": "This movie was fantastic!"}
Response:
{
"text": "This movie was fantastic!",
"sentiment": "POSITIVE",
"confidence": 0.95
}
try:
result = sentiment_model(request.text)
return PredictionResponse(
text=request.text,
sentiment=result[0]["label"],
confidence=result[0]["score"]
)
except Exception:
raise HTTPException(
status_code=500,
detail="Prediction failed"
)
Response when model fails to predict
{
"detail": "Prediction failed",
"status_code": 500
}
# Example request
import requests
response = requests.post(
"http://localhost:8000/predict",
json={"text": "Great product!"}
)
print(response.json())
{
"text": "Great product!",
"sentiment": "POSITIVE",
"confidence": 0.998
}
Deploying AI into Production with FastAPI