Create handler.py
Browse files- handler.py +35 -0
handler.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import pickle
|
3 |
+
from typing import Dict, List, Any
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
# set device
|
9 |
+
class EndpointHandler():
|
10 |
+
def __init__(self, path=""):
|
11 |
+
# load the optimized model
|
12 |
+
pathb = os.path.join(path,"./churn.pkl")
|
13 |
+
self.pipe = pd.read_pickle(pathb)
|
14 |
+
|
15 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
16 |
+
"""
|
17 |
+
Args:
|
18 |
+
data (:obj:):
|
19 |
+
includes the input data and the parameters for the inference.
|
20 |
+
Return:
|
21 |
+
A :obj:`list`:. A string representing what the label/class is
|
22 |
+
"""
|
23 |
+
inputs = data.pop("inputs", data)
|
24 |
+
parameters = data.pop("parameters", None)
|
25 |
+
df = pd.DataFrame(inputs)
|
26 |
+
|
27 |
+
df["TotalCharges"] = df["TotalCharges"].replace(" ", np.nan, regex=False).astype(float)
|
28 |
+
df = df.drop(columns=["customerID"])
|
29 |
+
df = df.drop(columns=["Churn"])
|
30 |
+
|
31 |
+
# run inference pipeline
|
32 |
+
pred = self.pipe.predict(df)
|
33 |
+
|
34 |
+
# postprocess the prediction
|
35 |
+
return {"pred": pred}
|