Real-time Models (Postgres)
Last updated
@app.post("/predict")
def predict(request: PredictRequest):
# Preprocess & predict
df = pd.DataFrame(columns=['sepal.length', 'sepal.width', 'petal.length', 'petal.width'],
data=[[request.sepal.length, request.sepal.width, request.petal.length, request.petal.width]])
y, confidence = model.predict(df)
# Insert prediction to DB
prediction = IrisModelPrediction(
id=str(uuid.uuid4()),
timestamp=datetime.now(),
sepal_length=request.sepal.length,
sepal_width=request.sepal.width,
petal_length=request.petal.length,
petal_width=request.petal.width,
prediction=y,
confidence=confidence,
)
db.add(prediction)
db.commit()
return {"prediction": y_pred}