This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.py
136 lines (101 loc) · 4.05 KB
/
API.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
import traceback
import logging
import numpy as np
from flask import Flask, jsonify, request
from joblib import load
from APIHelperFunctions import choose_model_by_name, get_data, rmse
from datasetreading import motor_imaginary
app = Flask(__name__)
MODELS = ['KNN', 'RF', 'MLP', 'SVM']
SUBJECTS = ['A', 'B', 'C', 'D', 'E', 'F']
logging.basicConfig(filename='predictions.log', level=logging.INFO)
@app.route("/")
def home():
return jsonify({
'Models': MODELS,
'Subjects': SUBJECTS,
'Predict': {
'Description': 'returns predictions for certain subject using the selected model on input data',
'Steps': ['1. Write subject name then a hyphen and then the model name to choose model. Ex: A-KNN',
'2. Write data path after that. Ex: test-data',
'3. Final path should look like Ex: /predict?model=A-KNN&data=data_featuresA']
},
'Make Models': {
'Description': 'Makes selected model for certain subject',
'Steps': ['1. Write model name as \'model\' attribute',
'2. Write subject name as \'subject\' attribute',
'3. Final path should look like Ex: /make_models?model=KNN&subject=A']
},
'View Model': {
'Description': 'Views selected model accuracy and loss on subject data (whole)',
'Steps': ['1. Write model name as \'model\' attribute',
'2. Write subject name as \'subject\' attribute',
'3. Final path should look like Ex: /view_model?model=KNN&subject=A']
},
'Preprocessing': {
'Description': 'Makes features for training',
'Steps': 'path must be /preprocessing'
}
})
@app.route('/predict')
def predict():
model = request.args.get('model', default=None, type=str)
data = request.args.get('data', default=None, type=str)
try:
if model is None or data is None:
return jsonify({
'Error': 'Please enter params for data and model to proceed',
'Example': '/predict?model=MODEL_NAME&data=DATA_NAME'
})
model = load('models.cla/' + model + '.joblib')
data = np.load('features.motor_dataset/' + data + '.npy')
prediction = model.predict(data)
prediction = str(prediction.tolist())
app.logger.info(prediction)
return jsonify({
'Prediction': prediction
})
except:
return jsonify({'trace': traceback.format_exc()})
@app.route('/make_model')
def make_model():
subject = request.args.get('subject', default=None, type=str)
model_name = request.args.get('model', default=None, type=str)
if subject is None or model_name is None:
return jsonify({
'Error': 'Please enter a valid path with all the attributes'
})
return choose_model_by_name(model_name, subject)
@app.route('/preprocessing')
def preprocessing():
motor_imaginary()
return jsonify({
'Finished preprocessing': 'Made features for CLA data',
'Models': MODELS,
'Subjects': SUBJECTS
})
@app.route('/view_model')
def view_model():
subject = request.args.get('subject', default=None, type=str)
model_name = request.args.get('model', default=None, type=str)
if subject is None or model_name is None or subject not in SUBJECTS or model_name not in MODELS:
return jsonify({
'Error': 'Please enter a valid path with all the attributes'
})
x, y, _ = get_data(subject)
model = load('models.cla/' + subject + '-' + model_name + '.joblib')
y_hat = model.predict(x)
percentage, loss = rmse(y_hat, y)
return jsonify({
'Accuracy': percentage,
'Loss': loss
})
@app.route('/direct_chair')
def direct_chair():
prediction = request.args.get('prediction', default=None, type=str)
if prediction is None:
return jsonify({
'Error': 'Please enter a valid path with all the attributes'
})
if __name__ == '__main__':
app.run()