-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
145 lines (110 loc) · 3.51 KB
/
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
137
138
139
140
141
142
143
144
import argparse
from flask import Flask, jsonify, request
from flask import render_template, send_from_directory
import os
import re
import joblib
import socket
import json
import numpy as np
import pandas as pd
# import model specific functions and variables
from my_modules.model import model_train, model_load, model_predict, MODEL_VERSION, MODEL_VERSION_NOTE
from my_modules.logger import LOGS_DIR
app = Flask(__name__)
@app.route("/")
def landing():
return render_template('index.html')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route('/running', methods=['POST'])
def running():
return render_template('running.html')
@app.route('/predict', methods=['GET','POST'])
def predict():
"""
basic predict function for the API
"""
## input checking
if not request.json:
print("ERROR: API (predict): did not receive request data")
return jsonify([])
if 'query' not in request.json:
print("ERROR API (predict): received request, but no 'query' found within")
return jsonify([])
if 'type' not in request.json:
print("WARNING API (predict): received request, but no 'type' was found assuming 'numpy'")
query_type = 'numpy'
## set the test flag
test = False
if 'mode' in request.json and request.json['mode'] == 'test':
test = True
## extract the query
query = request.json['query']
if request.json['type'] == 'dict':
pass
else:
print("ERROR API (predict): only dict data types have been implemented")
return jsonify([])
## load model
all_data, all_models = model_load(test=test)
if not all_models:
print("ERROR: all_models is not available")
return jsonify([])
_result = model_predict(query['country'], query['year'], query['month'], query['day'], all_data, all_models, test=test)
result = {}
## convert numpy objects to ensure they are serializable
for key,item in _result.items():
if isinstance(item,np.ndarray):
result[key] = item.tolist()
else:
result[key] = item
return(jsonify(result))
@app.route('/train', methods=['GET','POST'])
def train():
"""
basic predict function for the API
the 'mode' flag provides the ability to toggle between a test version and a
production verion of training
"""
## check for request data
if not request.json:
print("ERROR: API (train): did not receive request data")
return jsonify(False)
## set the test flag
test = False
if 'mode' in request.json and request.json['mode'] == 'test':
test = True
print("... training model")
model = model_train(test=test)
print("... training complete")
return(jsonify(True))
@app.route('/logs/<filename>', methods=['GET'])
def logs(filename):
"""
API endpoint to get logs
"""
if not re.search(".log", filename):
print("ERROR: API (log): file requested was not a log file: {}".format(filename))
return jsonify([])
if not os.path.isdir(LOGS_DIR):
print("ERROR: API (log): cannot find log dir")
return jsonify([])
file_path = os.path.join(LOGS_DIR, filename)
if not os.path.exists(file_path):
print("ERROR: API (log): file requested could not be found: {}".format(filename))
return jsonify([])
return send_from_directory(LOGS_DIR, filename, as_attachment=True)
if __name__ == '__main__':
## parse arguments for debug mode
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--debug", action="store_true", help="debug flask")
args = vars(ap.parse_args())
if args["debug"]:
app.run(debug=True, port=8080)
else:
app.run(host='0.0.0.0', threaded=True, port=8080)