-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_consensus_model_serving.py
139 lines (120 loc) · 4.18 KB
/
03_consensus_model_serving.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import asyncio
import numpy as np
import os
import pandas as pd
import pickle
import ray
from ray import serve
import requests
from sklearn.preprocessing import StandardScaler
# normalize prediction example
df_input = pd.read_csv("data/prediction_example.csv")
df_norm = StandardScaler().fit_transform(df_input)
# models locations
RANDOM_FOREST_MODEL_PATH = os.path.join("models/wine-red-quality_random_forest.pkl")
XGBOOST_MODEL_PATH = os.path.join("models/wine-red-quality_xgboost.pkl")
# start Ray
ray.init()
# start Serve
serve.start()
#define deployments
@serve.deployment(route_prefix="/random_forest/red_wines")
class RandomForestModel:
def __init__(self, path):
with open(path, "rb") as f:
self.model = pickle.load(f)
async def __call__(self, request):
payload = await request.json()
return self.serve(payload)
def serve(self, request):
input_vector = [
request["fixed acidity"],
request["volatile acidity"],
request["citric acid"],
request["residual sugar"],
request["chlorides"],
request["free sulfur dioxide"],
request["total sulfur dioxide"],
request["density"],
request["pH"],
request["sulphates"],
request["alcohol"],
]
prediction = self.model.predict([input_vector])[0]
return {"result": str(prediction)}
@serve.deployment(route_prefix="/xgboost/red_wines")
class XGBoostModel:
def __init__(self, path):
with open(path, "rb") as f:
self.model = pickle.load(f)
async def __call__(self, request):
payload = await request.json()
return self.serve(payload)
def serve(self, request):
input_vector = np.array([
request["fixed acidity"],
request["volatile acidity"],
request["citric acid"],
request["residual sugar"],
request["chlorides"],
request["free sulfur dioxide"],
request["total sulfur dioxide"],
request["density"],
request["pH"],
request["sulphates"],
request["alcohol"],
])
prediction = self.model.predict(input_vector.reshape(1,11))[0]
return {"result": str(prediction)}
RandomForestModel.deploy(RANDOM_FOREST_MODEL_PATH)
XGBoostModel.deploy(XGBOOST_MODEL_PATH)
@serve.deployment(route_prefix="/consensus")
class Speculative:
def __init__(self):
self.rfhandle = RandomForestModel.get_handle(sync=False)
self.xgboosthandle = XGBoostModel.get_handle(sync=False)
async def __call__(self, request):
payload = await request.json()
f1, f2 = await asyncio.gather(self.rfhandle.serve.remote(payload),
self.xgboosthandle.serve.remote(payload))
rfresurlt = ray.get(f1)['result']
xgresurlt = ray.get(f2)['result']
ones = []
zeros = []
if rfresurlt == "1":
ones.append("Random forest")
else:
zeros.append("Random forest")
if xgresurlt == "1":
ones.append("XGBoost")
else:
zeros.append("XGBoost")
if len(ones) >= 2:
return {"result": "1", "methods": ones}
else:
return {"result": "0", "methods": zeros}
Speculative.deploy()
sample_request_input_red = {
"fixed acidity": df_norm[0][0],
"volatile acidity": df_norm[0][1],
"citric acid": df_norm[0][2],
"residual sugar": df_norm[0][3],
"chlorides": df_norm[0][4],
"free sulfur dioxide": df_norm[0][5],
"total sulfur dioxide": df_norm[0][6],
"density": df_norm[0][7],
"pH": df_norm[0][8],
"sulphates": df_norm[0][9],
"alcohol": df_norm[0][10],
}
print(":: Random Forrest Classifier - Red Wines ::")
print("")
print(requests.get("http://localhost:8000/random_forest/red_wines", json=sample_request_input_red).text)
print("")
print(":: XGBoost Classifier - Red Wines ::")
print("")
print(requests.get("http://localhost:8000/xgboost/red_wines", json=sample_request_input_red).text)
print("")
print(":: Consensus Results ::")
print("")
print(requests.get("http://localhost:8000/consensus", json=sample_request_input_red).text)