forked from DataTalksClub/machine-learning-zoomcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchurn_serving.py
More file actions
35 lines (22 loc) · 704 Bytes
/
churn_serving.py
File metadata and controls
35 lines (22 loc) · 704 Bytes
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
import pickle
import numpy as np
from flask import Flask, request, jsonify
def predict_single(customer, dv, model):
X = dv.transform([customer])
y_pred = model.predict_proba(X)[:, 1]
return y_pred[0]
with open('churn-model.bin', 'rb') as f_in:
dv, model = pickle.load(f_in)
app = Flask('churn')
@app.route('/predict', methods=['POST'])
def predict():
customer = request.get_json()
prediction = predict_single(customer, dv, model)
churn = prediction >= 0.5
result = {
'churn_probability': float(prediction),
'churn': bool(churn),
}
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=9696)