-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
116 lines (87 loc) · 3.02 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
import sys
import os
import shutil
import time
import traceback
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import *
from sklearn.cluster import KMeans
from lightgbm import LGBMClassifier
from flask import Flask, request, jsonify
import pandas as pd
import joblib
app = Flask(__name__)
# inputs
training_data = r'dataset\Webdata_PlayerType.csv'
model_directory = 'model'
model_file_name = '%s/model_player.pickle' % model_directory
model_columns_file_name = '%s/model_columns_player.pickle' % model_directory
# These will be populated at training time
model_columns = None
clf = None
@app.route('/predict', methods=['POST'])
def predict():
try :
model_columns = joblib.load(model_columns_file_name)
clf = joblib.load(model_file_name)
except :
model_columns = None
model_labels = {'Playertype':{'type_achiever': 0, 'type_disruptor': 1, 'type_freeSpirit': 2, 'type_player': 3}}
clf = None
if clf:
try:
model_labels = {'Playertype':{'type_achiever': 0, 'type_disruptor': 1, 'type_freeSpirit': 2, 'type_player': 3}}
json_ = request.json
sample = {}
for i in json_ :
if i in model_columns:
sample[i] = json_[i]
query = pd.DataFrame(sample,index=[0])
prediction = clf.predict(query)[0]
lab = res = dict((v,k) for k,v in model_labels['Playertype'].items())
return ({"prediction":lab[prediction] })
except Exception as e:
return jsonify({'error': str(e), 'trace': traceback.format_exc()})
else:
print('train first')
return 'no model here'
@app.route('/train', methods=['GET'])
def train():
df = pd.read_csv(training_data)
x = df[list(joblib.load(model_columns_file_name))]
y = df['PlayerType_results']
# capture a list of columns that will be used for prediction
global model_columns
model_columns = list(x.columns)
joblib.dump(model_columns, model_columns_file_name)
global clf
clf = LGBMClassifier()
start = time.time()
clf.fit(x, y)
joblib.dump(clf, model_file_name)
message1 = 'Trained in %.5f seconds' % (time.time() - start)
message2 = 'Model training score: %s' % clf.score(x, y)
return_message = 'Success. \n{0}. \n{1}.'.format(message1, message2)
return ({"Message":return_message})
@app.route('/wipe', methods=['GET'])
def wipe():
try:
shutil.rmtree('model')
os.makedirs(model_directory)
return ({"Information":'Model wiped'})
except Exception as e:
print(str(e))
return 'Could not remove and recreate the model directory'
if __name__ == '__main__':
try:
clf = joblib.load(model_file_name)
print('model loaded')
model_columns = joblib.load(model_columns_file_name)
print('model columns loaded')
except Exception as e:
print('No model here')
print('Train first')
print(str(e))
clf = None
app.run(debug=True)