-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoxic_app.py
More file actions
50 lines (39 loc) · 1.55 KB
/
toxic_app.py
File metadata and controls
50 lines (39 loc) · 1.55 KB
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
from flask import Flask, jsonify, request
from keras.preprocessing.sequence import pad_sequences
import numpy as np
from keras.models import load_model
import pickle
from flask_cors import CORS
from toxic_helper import preprocess_text, maxlen
app = Flask(__name__)
CORS(app)
model = load_model('model_toxic.h5')
# loading tokenizer
with open('tokenizer_toxic.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
@app.route('/', methods=['POST'])
def index():
if request.method == 'POST':
userjson = request.get_json()
teststring = [preprocess_text(userjson['text'])]
teststring = tokenizer.texts_to_sequences(teststring)
teststring = pad_sequences(teststring, padding='post', maxlen=maxlen)
res = model.predict(np.expand_dims(teststring[0], 0))
tagNames = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
print(tagNames)
print(res)
predictedTags = []
scores = []
for i, item in enumerate(res[0]):
if item >= 0.5:
predictedTags.append(tagNames[i])
scores.append(str(res[0][i]))
# indexes = sorted(range(len(res[0])), key=lambda i: res[0][i])[-3:] # finds top 3 possible tags
# for i in indexes:
# predictedTags.append(tagNames[i])
# scores.append(str(res[0][i]))
return jsonify({'tags': predictedTags, 'scores': scores})
else:
return 'This is the ML API'
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=False, threaded=True)