-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from fastapi import FastAPI
from joblib import load
from pydantic import BaseModel
app = FastAPI()
model = load("model.pkl")
model_classes = {
0: "setosa",
1: "versicolor",
2: "virginica",
}
class Observation(BaseModel):
"""
A Pydantic model for the observation data.
This is our ML model's input data.
"""
sepal_length: float
sepal_width: float
petal_length: float
petal_width: float
@app.get("/")
async def root():
return {
"name": "back4app-deploy-ml-model",
"description": "A FastAPI webapp demonstrating how to deploy a ML model to Back4app Containers.",
"version": "1.0.0",
}
@app.post("/predict")
async def predict(observation: Observation):
predictions = model.predict([[
observation.sepal_length,
observation.sepal_width,
observation.petal_length,
observation.petal_width,
]])
prediction = predictions[0]
prediction_class = model_classes[prediction]
return {
"prediction": int(prediction),
"prediction_class": prediction_class,
}