-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpredict.py
44 lines (35 loc) · 948 Bytes
/
predict.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
import json
import pickle
import numpy as np
from celery import Celery
MODEL_PATH = "model.pkl"
papp = Celery(
"predict",
broker="redis://redis:6379/0",
backend="redis://redis:6379/0"
)
def load_model(path: str):
with open(path,"rb") as f:
return pickle.load(f)
def transform_data(input_data: dict):
# Mean value for each column
defaults = {
'sepal_length': 5.84,
'sepal_width': 3.06,
'petal_length': 3.76,
'petal_width': 1.20
}
data = {**defaults,**input_data}
return np.array([[
data[k] for k in defaults.keys()
]])
def get_predictions(model,data):
TARGETS = ['setosa','versicolor','virginica']
preds = model.predict_proba(data)
return dict(zip(TARGETS,preds[0]))
@papp.task
def predict(input_data: dict):
model = load_model(MODEL_PATH)
data = transform_data(input_data)
preds = get_predictions(model,data)
return preds