Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy-model-with-fastapi/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pandas==2.2.3
scikit-learn==1.6.1
uvicorn-worker==0.3.0
uvicorn[standard]==0.34.3
pydantic
16 changes: 11 additions & 5 deletions deploy-model-with-fastapi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel


def _get_model_dir():
Expand Down Expand Up @@ -34,18 +35,23 @@ async def lifespan(app: FastAPI):
async def health() -> Dict[str, bool]:
return {"healthy": True}

class RequestBody(BaseModel):
sepal_length: float
sepal_width: float
petal_length: float
petal_width: float

@app.post("/predict")
def predict(
sepal_length: float, sepal_width: float, petal_length: float, petal_width: float
request_body: RequestBody
):
global model
class_names = ["setosa", "versicolor", "virginica"]
data = dict(
sepal_length=sepal_length,
sepal_width=sepal_width,
petal_length=petal_length,
petal_width=petal_width,
sepal_length=request_body.sepal_length,
sepal_width=request_body.sepal_width,
petal_length=request_body.petal_length,
petal_width=request_body.petal_width,
)
prediction = model.predict_proba(pd.DataFrame([data]))[0]
predictions = []
Expand Down