forked from platzi/laboratorio-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
37 lines (30 loc) · 1001 Bytes
/
app.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
import pickle
import json
from flask import Flask, request
import pandas as pd
FEATURES = pickle.load(open("churn/models/features.pk", "rb"))
model = pickle.load(open("churn/models/model.pk", "rb"))
column_equivalence = pickle.load(open("churn/models/column_equivalence.pk", "rb"))
# create the Flask app
app = Flask(__name__)
def convert_numerical(features):
output = []
for i, feat in enumerate(features):
if i in column_equivalence:
output.append(column_equivalence[i][feat])
else:
try:
output.append(pd.to_numeric(feat))
except:
output.append(0)
return output
@app.route('/query')
def query_example():
features = convert_numerical(request.args.get('feats').split(','))
response = {
'response': [int(x) for x in model.predict([features])]
}
return json.dumps(response)
if __name__ == '__main__':
# run app in debug mode on port 3001
app.run(debug=True, port=3001)