-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_http.py
98 lines (75 loc) · 2.31 KB
/
serve_http.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
"""
Script for serving.
"""
import json
import pickle
import numpy as np
from bedrock_client.bedrock.metrics.service import ModelMonitoringService
from flask import Flask, Response, current_app, request
from utils.constants import AREA_CODES, STATES, SUBSCRIBER_FEATURES
OUTPUT_MODEL_NAME = "/artefact/lgb_model.pkl"
def predict_prob(subscriber_features,
model=pickle.load(open(OUTPUT_MODEL_NAME, "rb"))):
"""Predict churn probability given subscriber_features.
Args:
subscriber_features (dict)
model
Returns:
churn_prob (float): churn probability
"""
row_feats = list()
for col in SUBSCRIBER_FEATURES:
row_feats.append(subscriber_features[col])
for area_code in AREA_CODES:
if subscriber_features["Area_Code"] == area_code:
row_feats.append(1)
else:
row_feats.append(0)
for state in STATES:
if subscriber_features["State"] == state:
row_feats.append(1)
else:
row_feats.append(0)
# Score
churn_prob = (
model
.predict_proba(np.array(row_feats).reshape(1, -1))[:, 1]
.item()
)
# Log the prediction
current_app.monitor.log_prediction(
request_body=json.dumps(subscriber_features),
features=row_feats,
output=churn_prob
)
return churn_prob
# pylint: disable=invalid-name
app = Flask(__name__)
@app.route("/", methods=["POST"])
def get_churn():
"""Returns the `churn_prob` given the subscriber features"""
subscriber_features = request.json
result = {
"churn_prob": predict_prob(subscriber_features)
}
return result
@app.before_first_request
def init_background_threads():
"""Global objects with daemon threads will be stopped by gunicorn --preload flag.
So instantiate them here instead.
"""
current_app.monitor = ModelMonitoringService()
@app.route("/metrics", methods=["GET"])
def get_metrics():
"""Returns real time feature values recorded by prometheus
"""
body, content_type = current_app.monitor.export_http(
params=request.args.to_dict(flat=False),
headers=request.headers,
)
return Response(body, content_type=content_type)
def main():
"""Starts the Http server"""
app.run()
if __name__ == "__main__":
main()