-
Notifications
You must be signed in to change notification settings - Fork 1
/
6_APP_flask_app.py
136 lines (102 loc) · 3.61 KB
/
6_APP_flask_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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Flask,send_from_directory,request
import logging
from pandas.io.json import dumps as jsonify
import os
import random
from IPython.display import Javascript,HTML
# Imports needed for the churn explainer code.
from collections import ChainMap
from explainer.utils import log_environment
from explainer.explainedmodel import ExplainedModel
# This reduces the the output to the
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
em = ExplainedModel(os.getenv('MODEL_NAME', 'test_model'))
sample_size = 20
app= Flask(__name__,static_url_path='')
@app.route('/')
def home():
return "<script> window.location.href = '/flask/table_view.html'</script>"
@app.route('/flask/<path:path>')
def send_file(path):
return send_from_directory('flask', path)
def explainid_non_flask(N):
customer_data = dataid_non_flask(N)[0]
customer_data.pop('id')
customer_data.pop(em.label_name)
data = em.cast_dct(customer_data)
probability, explanation = em.explain_dct(data)
return {'data': dict(data),
'probability': probability,
'explanation': explanation,
'id':int(N)}
def dataid_non_flask(N):
customer_id = em.data.index.dtype.type(N)
customer_df = em.data.loc[[customer_id]].reset_index()
return customer_df.to_dict(orient='records')
@app.route('/sample_table')
def sample_table():
#N = request.args.get('N', sample_size, int)
sample_ids = random.sample(range(1,len(em.data)),sample_size)
sample_table = []
for ids in sample_ids:
sample_table.append(explainid_non_flask(str(ids)))
return jsonify(sample_table)
@app.route("/explain")
def explain():
data = dict(ChainMap(request.args, em.default_data))
data = em.cast_dct(data)
probability, explanation = em.explain_dct(data)
return jsonify({'data': dict(data),
'probability': probability,
'explanation': explanation})
@app.route('/explainid')
def explainid():
customer_data = dataid(request.args['id'])[0]
customer_data.pop('id')
customer_data.pop(em.label_name)
data = em.cast_dct(customer_data)
probability, explanation = em.explain_dct(data)
return {'data': dict(data),
'probability': probability,
'explanation': explanation}
@app.route("/data")
def data():
N = request.args.get('N', sample_size, int)
data = em.data.sample(N)
return data.reset_index().to_json(orient='records')
@app.route("/modelname")
def modelname():
return jsonify({'modelname': em.model_name})
@app.route("/dataset")
def dataset():
return jsonify({'dataset': em.dataset})
@app.route("/dataid")
def dataid():
customer_id = em.data.index.dtype.type(request.args['id'])
customer_df = em.data.loc[[customer_id]].reset_index()
return customer_df.to_json(orient='records')
@app.route("/features")
def features():
response = {
'id': em.data.index.name or 'index',
'label': em.label_name,
'features': list(em.default_data.keys())
}
return jsonify(response)
@app.route("/categories")
def categories():
return jsonify({feat: dict(enumerate(cats))
for feat, cats in em.categories.items()})
@app.route("/stats")
def stats():
return jsonify(em.stats)
@app.route("/size")
def size():
return jsonify({'size': len(em.data)})
@app.route("/default")
def default():
return jsonify(em.default_data)
HTML("<a href='https://{}.{}'>Open Table View</a>".format(os.environ['CDSW_ENGINE_ID'],os.environ['CDSW_DOMAIN']))
if __name__=="__main__":
app.run(host='127.0.0.1', port=int(os.environ['CDSW_READONLY_PORT']))