Skip to content

Commit 52f0a91

Browse files
committed
provide a more complete example of the dockerfile and fastAPI
1 parent 046ed95 commit 52f0a91

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

source/_posts/Deploying-a-Machine-Learning-Model-as-an-API-with-FastAPI-Docker-and-Knative.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,71 @@ In this blog post, we'll address this question by walking through the process of
1313
We'll start by wrapping our machine learning model as an API using FastAPI, a modern web framework for building APIs with Python. FastAPI offers automatic OpenAPI documentation generation and high performance, making it an excellent choice for our use case.
1414

1515
```python
16-
from typing import List, Dict
16+
import pickle
17+
from typing import Dict, List
1718
from fastapi import FastAPI
18-
from pydantic import BaseModel
19+
from pydantic import BaseModel, Field
1920
from util.get_prediction_probabilities import get_prediction_probabilities
2021

22+
def load_model():
23+
with open("model.pkl", "rb") as file:
24+
model = pickle.load(file)
25+
return model
26+
27+
model = load_model()
2128
app = FastAPI()
2229

2330
class PredictionResult(BaseModel):
2431
text: str
2532
prediction: str
2633
probabilities: Dict[str, float]
2734

28-
class PredictionRequest(BaseModel):
29-
texts: List[str]
30-
3135
class PredictionResponse(BaseModel):
32-
results: List[PredictionResult]
36+
results: List[PredictionResult] = Field(
37+
examples=[
38+
{
39+
"text": "netflix訂閱",
40+
"prediction": "subscription",
41+
"probabilities": {
42+
"subscription": 0.916624393150892,
43+
"learning": 0.023890389044000114,
44+
},
45+
},
46+
]
47+
)
48+
49+
class PredictionRequest(BaseModel):
50+
texts: List[str] = Field(..., example=["netflix訂閱"])
3351

34-
@app.post('/predict', response_model=PredictionResponse)
52+
@app.post("/predict", response_model=PredictionResponse)
3553
async def predict(request: PredictionRequest):
3654
texts = request.texts
3755
results = get_prediction_probabilities(model, texts)
38-
39-
response = PredictionResponse(results=results)
40-
return response
56+
return {"results": results}
4157
```
4258

4359
## Building a Docker Image
4460

4561
Next, we'll containerize our FastAPI application using Docker. Docker provides a lightweight and portable way to package applications and their dependencies into containers, ensuring consistency across different environments.
4662

4763
```Dockerfile
48-
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
64+
# Use a base image with Python installed
65+
FROM python:3.9-slim
66+
67+
# Set the working directory inside the container
68+
WORKDIR /app
69+
70+
# Copy the requirements file into the container
71+
COPY requirements.txt .
4972

50-
COPY ./app /app
73+
# Install the Python dependencies
74+
RUN pip install -r requirements.txt
5175

52-
RUN pip install scikit-learn # Install any dependencies
76+
# Copy the application code
77+
COPY . .
5378

54-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
79+
# Set the entrypoint command
80+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "5050"]
5581
```
5682

5783
## Deployment on Knative

0 commit comments

Comments
 (0)