-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (57 loc) · 1.92 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
from flask import Flask, jsonify, request, render_template, flash
from wtforms import Form, TextField, TextAreaField, validators, SubmitField
import config
import re
import Model_Classifier as t
import score_cleaning as sc
app = Flask(__name__)
# App config
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '7d441f27d441f27567d44522b6176a'
global vul_comment
vul=['disgust','shame','anger','fear']
class ReusableForm(Form):
comment = TextAreaField('Comment:', validators=[validators.required()])
def check_comment_toxicity(comment):
'''
check comment for toxicity
input: string
output: value between 0 - 1 indicating toxicity
'''
score = sc.toxicity_score(comment)
return score
def set_toxicity_message(comment):
res=''
res = t.classify_dataset(comment)
if res=='joy':
message = 'Attention: Your comment is not toxic.'
else:
message = 'Success: Your comment toxic.'
return message
@app.route('/', methods=['GET', 'POST'])
def hello():
form = ReusableForm(request.form)
print(form.errors)
if request.method == 'POST':
print("Form:")
print(form)
#name = request.form['name']
comment = request.form['comment']
#print(f"Name:{name}")
print(f"Comment:{comment}")
if form.validate():
# Save the comment here.
#flash('Hello ' + name + ' Comment: ' + comment)
toxic_score = check_comment_toxicity(comment)
toxicity_message = set_toxicity_message(comment)
print(f"Toxic Score:{toxic_score}")
print(f"Toxic message:{toxicity_message}")
flash(f"{toxicity_message} Toxicity Score: {toxic_score:0.2f}. " +\
f"Your comment was: {comment}")
else:
flash('Error: All the form fields are required. ')
return render_template('hello.html', form=form)
if __name__ == '__main__':
app.run()