-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toxic_Comment_Identification_System.py
46 lines (41 loc) · 1.71 KB
/
Toxic_Comment_Identification_System.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
import os
from utils import ShowToxicLevel, ShowIsToxic, GetResultbybert, ToxicDectorllama8b
from flask import Flask, render_template, request
# encoding:utf-8
base_dir = os.path.dirname(os.path.abspath(__file__))
def get_result_en(words):
isToxic = ShowIsToxic.getResult(words)
toxicLevel = ShowToxicLevel.getResult(words)
result = {**isToxic, **toxicLevel}
result['isToxic'] = False if result['isToxic'] == 1 else True
return result
app = Flask(__name__)
@app.route("/predict", methods=["GET"])
def predict():
if (request.args.get("apiEndpoint") == "localmodel(LSTM)"):
comment = request.args.get("comment")
result = get_result_en(comment)
print(result)
return render_template('ShowEnResult.html', result=result, comment=comment)
if (request.args.get("apiEndpoint") == "localmodel(BERT)"):
comment = request.args.get("comment")
result = GetResultbybert.getResult(comment)
result['isToxic'] = False if result['isToxic'] == 1 else True
print(result)
return render_template('ShowEnResult1.html', result=result, comment=comment)
if (request.args.get("apiEndpoint") == "llama"):
comment = request.args.get("comment")
result = ToxicDectorllama8b.main(comment)
print(type(result))
return render_template('Resultllama.html', result=result)
else:
return "Error method!!!"
@app.route("/")
def home():
return render_template("Main.html")
if __name__ == '__main__':
if not os.path.exists(os.path.join(base_dir, 'model')):
print('Local models not founded, start raining model...')
ShowToxicLevel.train()
print('Local models trained successfully.')
app.run(debug=True)