-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
99 lines (69 loc) · 2.82 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
from flask import Flask, render_template, url_for, request, jsonify
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import pickle
import numpy as np
app = Flask(__name__)
# Load the TF-IDF vocabulary specific to the category
with open(r"toxic_vect-Copy1.pkl", "rb") as f:
tox = pickle.load(f)
with open(r"severe_toxic_vect-Copy1.pkl", "rb") as f:
sev = pickle.load(f)
with open(r"obscene_vect-Copy1.pkl", "rb") as f:
obs = pickle.load(f)
with open(r"insult_vect-Copy1.pkl", "rb") as f:
ins = pickle.load(f)
with open(r"threat_vect-Copy1.pkl", "rb") as f:
thr = pickle.load(f)
with open(r"identity_hate_vect-Copy1.pkl", "rb") as f:
ide = pickle.load(f)
# Load the pickled RDF models
with open(r"toxic_mod-Copy1.pkl", "rb") as f:
tox_model = pickle.load(f)
with open(r"severe_toxic_mod-Copy1.pkl", "rb") as f:
sev_model = pickle.load(f)
with open(r"obscene_mod-Copy1.pkl", "rb") as f:
obs_model = pickle.load(f)
with open(r"insult_mod-Copy1.pkl", "rb") as f:
ins_model = pickle.load(f)
with open(r"threat_mod-Copy1.pkl", "rb") as f:
thr_model = pickle.load(f)
with open(r"identity_hate_mod-Copy1.pkl", "rb") as f:
ide_model = pickle.load(f)
@app.route("/")
def home():
return render_template('index.html')
@app.route("/predict", methods=['POST'])
def predict():
# Take a string input from user
user_input = request.form['text']
data = [user_input]
vect = tox.transform(data)
pred_tox = tox_model.predict_proba(vect)[:,1]
vect = sev.transform(data)
pred_sev = sev_model.predict_proba(vect)[:,1]
vect = obs.transform(data)
pred_obs = obs_model.predict_proba(vect)[:,1]
vect = thr.transform(data)
pred_thr = thr_model.predict_proba(vect)[:,1]
vect = ins.transform(data)
pred_ins = ins_model.predict_proba(vect)[:,1]
vect = ide.transform(data)
pred_ide = ide_model.predict_proba(vect)[:,1]
out_tox = round(pred_tox[0], 2)
out_sev = round(pred_sev[0], 2)
out_obs = round(pred_obs[0], 2)
out_ins = round(pred_ins[0], 2)
out_thr = round(pred_thr[0], 2)
out_ide = round(pred_ide[0], 2)
print(out_tox)
return render_template('index.html',
pred_tox = 'Prob (Toxic): {}'.format(out_tox),
pred_sev = 'Prob (Severe Toxic): {}'.format(out_sev),
pred_obs = 'Prob (Obscene): {}'.format(out_obs),
pred_ins = 'Prob (Insult): {}'.format(out_ins),
pred_thr = 'Prob (Threat): {}'.format(out_thr),
pred_ide = 'Prob (Identity Hate): {}'.format(out_ide)
)
# Server reloads itself if code changes so no need to keep restarting:
if __name__=="main":
app.run(debug=True)