-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.py
24 lines (18 loc) · 882 Bytes
/
service.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
import numpy as np
import pandas as pd
from sample import sample_input
import bentoml
from bentoml.io import JSON
from bentoml.io import PandasDataFrame
model_ref = bentoml.xgboost.get("ieee-fraud-detection-lg:latest")
preprocessor = model_ref.custom_objects["preprocessor"]
fraud_model_runner = model_ref.to_runner()
svc = bentoml.Service("fraud_detection", runners=[fraud_model_runner])
input_spec = PandasDataFrame.from_sample(sample_input)
@svc.api(input=input_spec, output=JSON())
async def is_fraud(input_df: pd.DataFrame):
input_df = input_df.astype(sample_input.dtypes)
input_features = preprocessor.transform(input_df)
results = await fraud_model_runner.predict_proba.async_run(input_features)
predictions = np.argmax(results, axis=1) # 0 is not fraud, 1 is fraud
return {"is_fraud": list(map(bool, predictions)), "is_fraud_prob": results[:, 1]}