-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_session.py
130 lines (109 loc) · 3.88 KB
/
app_session.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
from flask import Flask, request, jsonify, session, redirect, url_for
#import pandas as pd
import numpy as np
# import pickle
# import create_prediction_model
# import diagnosis
# import predict_exited_from_saved_model
import json
# import os
import subprocess
import commons_proj as cproj
from diagnostics import model_predictions
from diagnostics import numeric_inputs_summary
from diagnostics import na_percent_summary
from diagnostics import execution_time
from diagnostics import outdated_packages_list
#%% Set up variables for this script
app = Flask(__name__)
app.secret_key = '1652d576-484a-49fd-913a-6879acfa6ba4'
#%% http://127.0.0.1:8000/
@app.route('/')
def index():
if 'user' in session:
return f'Logged in as {session["user"]}\n', True
return 'You are not logged in', False
# @app.route('/login', methods=['GET', 'POST'])
# def login():
# if request.method == 'POST':
# session['user'] = request.form['user']
# return redirect(url_for('index'))
# return '''
# <form method="post">
# <p><input type=text name=user>
# <p><input type=submit value=Login>
# </form>
# '''
#
#%% login - logout
#http://127.0.0.1:8000/login?user=esm
@app.route('/login')
def login():
session['user'] = request.args.get('user', default='*', type=str)
return redirect(url_for('index'))
#http://127.0.0.1:8000/logout
@app.route('/logout')
def logout():
# remove the user from the session if it's there
session.pop('user', None)
return redirect(url_for('index'))
#%% Prediction Endpoint
# http://127.0.0.1:8000/prediction?dirtype=test_data_path&filename=testdata.csv
# curl "http://127.0.0.1:8000/prediction?dirtype=test_data_path&filename=testdata.csv"
#@app.route("/prediction", methods=['POST','OPTIONS'])
@app.route("/prediction")
def predict():
#call the prediction function you created in Step 3
if 'user' not in session:
return redirect(url_for("index"))
dirtype = request.args.get('dirtype', default='*', type=str)
filename = request.args.get('filename', default='*', type=str)
test_data = cproj.load_dataframe(dirtype, filename)
predicted, _ = model_predictions(test_data)
return jsonify(predicted.tolist())
#%% Scoring Endpoint
#http://127.0.0.1:8000/scoring
#@app.route("/scoring", methods=['GET','OPTIONS'])
@app.route("/scoring")
def scoring():
#check the score of the deployed model
if 'user' not in session:
return redirect(url_for("index"))
subprocess.run(['python', 'scoring.py'])
f1_score = cproj.load_txt_file('output_model_path', 'latestscore.txt')
f1_score = f1_score.split()
return jsonify(list(np.float_(f1_score)))
#%% Summary Statistics Endpoint
#http://127.0.0.1:8000/summarystats
# @app.route("/summarystats", methods=['GET','OPTIONS'])
@app.route("/summarystats")
def stats_num_inputs():
#check the score of the deployed model
if 'user' not in session:
return redirect(url_for("index"))
numeric_inputs_stats = numeric_inputs_summary()
json_var = numeric_inputs_stats.to_json() #(orient="split")
parsed = json.loads(json_var)
return parsed
#%% Diagnostics Endpoint
#http://127.0.0.1:8000/diagnostics
#@app.route("/diagnostics", methods=['GET','OPTIONS'])
@app.route("/diagnostics")
def diags_summary():
if 'user' not in session:
return redirect(url_for("index"))
#check timing and percent NA values
na_percent = na_percent_summary()
exec_time = execution_time()
outdated_df = outdated_packages_list()
outdated_dict = outdated_df.to_dict()
json_var1 = na_percent.to_json()
dict_var2 = {'execution_times': exec_time}
dict_var3 = {'outdated_packages': outdated_dict}
json_all = json.loads(json_var1)
json_all.update(dict_var2)
json_all.update(dict_var3)
return json_all
#%%
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000, debug=True, threaded=True)