-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
215 lines (185 loc) · 8.37 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from scripts.classify_deep import classify_deep
from scripts.classify_essay import classify_essay
from scripts.classify_shallow import classify_gb, classify_svm
from scripts.encode_labels import load_all_les, get_json_lab_cls
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS, cross_origin
from scripts.get_sample import serve_random
import tensorflow as tf
tf.get_logger().setLevel('INFO')
app = Flask(__name__, static_url_path='/static')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
le_dict = load_all_les()
@app.route("/")
def root():
return app.send_static_file("index.html")
@app.route("/classify_essay", methods=["POST"])
@cross_origin()
def classify_text():
essay = request.form["essay"]
cls, label, prob = classify_essay(essay)
return jsonify({"class": cls, "label": label, "prob": prob})
@app.route("/get_all_les")
@cross_origin()
def get_all_les():
return jsonify(get_json_lab_cls(le_dict))
@app.route("/classify_deep", methods=["POST"])
@cross_origin()
def classify_deep_func():
if request.form["mode"] == "text_label":
sex = int(le_dict["le_sex"].transform([[request.form["sex"]]])[0])
orientation = int(le_dict["le_orientation"].transform([[request.form["orientation"]]])[0])
body_type = int(le_dict["le_body_type"].transform([[request.form["body_type"]]])[0])
diet = int(le_dict["le_diet"].transform([[request.form["diet"]]])[0])
drinks = int(le_dict["le_drinks"].transform([[request.form["drinks"]]])[0])
drugs = int(le_dict["le_drugs"].transform([[request.form["drugs"]]])[0])
ethnicity = int(le_dict["le_ethnicity"].transform([[request.form["ethnicity"]]])[0])
offspring = int(le_dict["le_offspring"].transform([[request.form["offspring"]]])[0])
pets = int(le_dict["le_pets"].transform([[request.form["pets"]]])[0])
sign = int(le_dict["le_sign"].transform([[request.form["sign"]]])[0])
smokes = int(le_dict["le_smokes"].transform([[request.form["smokes"]]])[0])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
elif request.form["mode"] == "int_label":
sex = int(request.form["sex"])
orientation = int(request.form["orientation"])
body_type = int(request.form["body_type"])
diet = int(request.form["diet"])
drinks = int(request.form["drinks"])
drugs = int(request.form["drugs"])
ethnicity = int(request.form["ethnicity"])
offspring = int(request.form["offspring"])
pets = int(request.form["pets"])
sign = int(request.form["sign"])
smokes = int(request.form["smokes"])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
input_features = [
sex,
orientation,
body_type,
diet,
drinks,
drugs,
ethnicity,
offspring,
pets,
sign,
smokes,
age,
height,
income
]
cls, label, prob = classify_deep(input_features)
return jsonify({"class": cls, "label": label, "prob": prob})
@app.route("/classify_svm", methods=["POST"])
@cross_origin()
def classify_svm_func():
if request.form["mode"] == "text_label":
sex = int(le_dict["le_sex"].transform([[request.form["sex"]]])[0])
orientation = int(le_dict["le_orientation"].transform([[request.form["orientation"]]])[0])
body_type = int(le_dict["le_body_type"].transform([[request.form["body_type"]]])[0])
diet = int(le_dict["le_diet"].transform([[request.form["diet"]]])[0])
drinks = int(le_dict["le_drinks"].transform([[request.form["drinks"]]])[0])
drugs = int(le_dict["le_drugs"].transform([[request.form["drugs"]]])[0])
ethnicity = int(le_dict["le_ethnicity"].transform([[request.form["ethnicity"]]])[0])
offspring = int(le_dict["le_offspring"].transform([[request.form["offspring"]]])[0])
pets = int(le_dict["le_pets"].transform([[request.form["pets"]]])[0])
sign = int(le_dict["le_sign"].transform([[request.form["sign"]]])[0])
smokes = int(le_dict["le_smokes"].transform([[request.form["smokes"]]])[0])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
elif request.form["mode"] == "int_label":
sex = int(request.form["sex"])
orientation = int(request.form["orientation"])
body_type = int(request.form["body_type"])
diet = int(request.form["diet"])
drinks = int(request.form["drinks"])
drugs = int(request.form["drugs"])
ethnicity = int(request.form["ethnicity"])
offspring = int(request.form["offspring"])
pets = int(request.form["pets"])
sign = int(request.form["sign"])
smokes = int(request.form["smokes"])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
input_features = [
sex,
orientation,
body_type,
diet,
drinks,
drugs,
ethnicity,
offspring,
pets,
sign,
smokes,
age,
height,
income
]
cls, label = classify_svm(input_features)
return jsonify({"class": cls, "label": label})
@app.route("/serve_random")
@cross_origin()
def random_serve():
return jsonify({"res_rand": serve_random()})
@app.route("/classify_gb", methods=["POST"])
@cross_origin()
def classify_gb_func():
if request.form["mode"] == "text_label":
sex = int(le_dict["le_sex"].transform([[request.form["sex"]]])[0])
orientation = int(le_dict["le_orientation"].transform([[request.form["orientation"]]])[0])
body_type = int(le_dict["le_body_type"].transform([[request.form["body_type"]]])[0])
diet = int(le_dict["le_diet"].transform([[request.form["diet"]]])[0])
drinks = int(le_dict["le_drinks"].transform([[request.form["drinks"]]])[0])
drugs = int(le_dict["le_drugs"].transform([[request.form["drugs"]]])[0])
ethnicity = int(le_dict["le_ethnicity"].transform([[request.form["ethnicity"]]])[0])
offspring = int(le_dict["le_offspring"].transform([[request.form["offspring"]]])[0])
pets = int(le_dict["le_pets"].transform([[request.form["pets"]]])[0])
sign = int(le_dict["le_sign"].transform([[request.form["sign"]]])[0])
smokes = int(le_dict["le_smokes"].transform([[request.form["smokes"]]])[0])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
elif request.form["mode"] == "int_label":
sex = int(request.form["sex"])
orientation = int(request.form["orientation"])
body_type = int(request.form["body_type"])
diet = int(request.form["diet"])
drinks = int(request.form["drinks"])
drugs = int(request.form["drugs"])
ethnicity = int(request.form["ethnicity"])
offspring = int(request.form["offspring"])
pets = int(request.form["pets"])
sign = int(request.form["sign"])
smokes = int(request.form["smokes"])
age = int(le_dict["sc_age"].transform([[request.form["age"]]])[0])
height = int(le_dict["sc_height"].transform([[request.form["height"]]])[0])
income = int(le_dict["sc_income"].transform([[request.form["income"]]])[0])
input_features = [
sex,
orientation,
body_type,
diet,
drinks,
drugs,
ethnicity,
offspring,
pets,
sign,
smokes,
age,
height,
income
]
cls, label = classify_gb(input_features)
return jsonify({"class": cls, "label": label})
if __name__ == "__main__":
app.run()