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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 6 |
+
import pickle
|
| 7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
+
import os
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import plotly.express as px
|
| 12 |
+
|
| 13 |
+
#Load tokenizer
|
| 14 |
+
fp = Path(__file__).with_name('tokenizer.pkl')
|
| 15 |
+
with open(fp,mode="rb") as f:
|
| 16 |
+
tokenizer = pickle.load(f)
|
| 17 |
+
|
| 18 |
+
#Load LSTM
|
| 19 |
+
fp = Path(__file__).with_name('lstm_model.h5')
|
| 20 |
+
LSTM_model = tf.keras.models.load_model(fp, compile=True)
|
| 21 |
+
|
| 22 |
+
#Load GRU
|
| 23 |
+
fp = Path(__file__).with_name('gru_model.h5')
|
| 24 |
+
GRU_model = load_model(fp)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def tokenizer_pad(tokenizer,comment_text,max_length=200):
|
| 28 |
+
|
| 29 |
+
comment_text = [comment_text]
|
| 30 |
+
tokenized_text = tokenizer.texts_to_sequences(comment_text)
|
| 31 |
+
|
| 32 |
+
padded_sequences = pad_sequences(sequences=tokenized_text,maxlen=max_length,padding="post",truncating="post")
|
| 33 |
+
|
| 34 |
+
return padded_sequences
|
| 35 |
+
|
| 36 |
+
def LSTM_predict(x):
|
| 37 |
+
x = tokenizer_pad(tokenizer=tokenizer,comment_text=x)
|
| 38 |
+
|
| 39 |
+
pred_proba = LSTM_model.predict(x)[0]
|
| 40 |
+
|
| 41 |
+
pred_proba = [round(i,2) for i in pred_proba]
|
| 42 |
+
|
| 43 |
+
#print(pred_proba)
|
| 44 |
+
|
| 45 |
+
return pred_proba
|
| 46 |
+
|
| 47 |
+
def GRU_predict(x):
|
| 48 |
+
x = tokenizer_pad(tokenizer=tokenizer,comment_text=x)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
pred_proba = GRU_model.predict(x)[0]
|
| 52 |
+
|
| 53 |
+
pred_proba = [round(i,2) for i in pred_proba]
|
| 54 |
+
|
| 55 |
+
#print(pred_proba)
|
| 56 |
+
|
| 57 |
+
return pred_proba
|
| 58 |
+
|
| 59 |
+
def plot(result):
|
| 60 |
+
label = ['độc hại', 'cực kì độc hại', 'tục tĩu', 'đe dọa', 'xúc phạm', 'thù ghét cá nhân']
|
| 61 |
+
data = pd.DataFrame()
|
| 62 |
+
data['Nhãn'] = label
|
| 63 |
+
data['Điểm'] = result
|
| 64 |
+
|
| 65 |
+
#print(data)
|
| 66 |
+
|
| 67 |
+
p = px.bar(data, x='Nhãn', y='Điểm', color='Nhãn', range_y=[0, 1] )
|
| 68 |
+
return p
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
def judge(x):
|
| 72 |
+
|
| 73 |
+
label = ['độc hại', 'cực kì độc hại', 'tục tĩu', 'đe dọa', 'xúc phạm', 'thù ghét cá nhân']
|
| 74 |
+
result = []
|
| 75 |
+
judge_result = []
|
| 76 |
+
|
| 77 |
+
lstm_pred = LSTM_predict(x)
|
| 78 |
+
gru_pred = GRU_predict(x)
|
| 79 |
+
|
| 80 |
+
#print(result)
|
| 81 |
+
|
| 82 |
+
return_result = 'Result'
|
| 83 |
+
result_lstm = np.round(lstm_pred, 2)
|
| 84 |
+
result_gru = np.round(gru_pred, 2)
|
| 85 |
+
for i in range(6):
|
| 86 |
+
result.append((result_lstm[i]+result_gru[i])/2)
|
| 87 |
+
|
| 88 |
+
final_result = np.round(result, 2)
|
| 89 |
+
|
| 90 |
+
#print(final_result)
|
| 91 |
+
return_result += '\nMô hình LSTM\n'
|
| 92 |
+
return_result += f"{result_lstm}\n"
|
| 93 |
+
# for i in range(6):
|
| 94 |
+
# if result_lstm[i]>=0 and result_lstm[i]<0.1:
|
| 95 |
+
# return_result += "Tính {} là không có\n".format(label[i])
|
| 96 |
+
# if result_lstm[i]>=0.1 and result_lstm[i]<0.5:
|
| 97 |
+
# return_result += "Tính {} ở mức không rõ ràng, không thể xác định chính xác\n".format(label[i])
|
| 98 |
+
# if result_lstm[i]>=0.5 and result_lstm[i]<0.8:
|
| 99 |
+
# return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i])
|
| 100 |
+
# if result_lstm[i]>=0.8:
|
| 101 |
+
# return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i])
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
return_result += '\nMô hình GRU\n'
|
| 105 |
+
return_result += f"{result_gru}\n"
|
| 106 |
+
# for i in range(6):
|
| 107 |
+
# if result_gru[i]>=0 and result_gru[i]<0.1:
|
| 108 |
+
# return_result += "Tính {} là không có\n".format(label[i])
|
| 109 |
+
# if result_gru[i]>=0.1 and result_gru[i]<0.5:
|
| 110 |
+
# return_result += "Tính {} ở mức không rõ ràng, không thể xác định chính xác\n".format(label[i])
|
| 111 |
+
# if result_gru[i]>=0.5 and result_gru[i]<0.8:
|
| 112 |
+
# return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i])
|
| 113 |
+
# if result_gru[i]>=0.8:
|
| 114 |
+
# return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i])
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
another_result = ''
|
| 118 |
+
another_result += "\nTổng quan kết quả trung bình:\n"
|
| 119 |
+
another_result += f"{final_result}\n"
|
| 120 |
+
|
| 121 |
+
for i in range(6):
|
| 122 |
+
if final_result[i]>=0 and final_result[i]<0.1:
|
| 123 |
+
another_result += "Tính {} là không có\n".format(label[i])
|
| 124 |
+
if final_result[i]>=0.1 and final_result[i]<0.5:
|
| 125 |
+
another_result += "Tính {} ở mức không rõ ràng, không thể xác định chính xác\n".format(label[i])
|
| 126 |
+
if final_result[i]>=0.5 and final_result[i]<0.8:
|
| 127 |
+
another_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i])
|
| 128 |
+
if final_result[i]>=0.8:
|
| 129 |
+
another_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i])
|
| 130 |
+
|
| 131 |
+
another_result += "\nKết luận:\n"
|
| 132 |
+
if max(final_result)>=0 and max(final_result)<0.1:
|
| 133 |
+
another_result += "Ngôn ngữ phù hợp mọi lứa tuổi.\n"
|
| 134 |
+
if max(final_result)>=0.1 and max(final_result)<0.5:
|
| 135 |
+
another_result += "Ngôn ngữ cần được kiểm tra lại.\n"
|
| 136 |
+
if max(final_result)>=0.5 and max(final_result)<0.8:
|
| 137 |
+
another_result += "Ngôn ngữ không phù hợp, cần xem xét lại.\n"
|
| 138 |
+
if max(final_result)>=0.8:
|
| 139 |
+
another_result += "Ngôn ngữ vi phạm tiêu chuẩn cộng đồng nghiêm trọng, yêu cầu chấn chỉnh.\n"
|
| 140 |
+
|
| 141 |
+
p = plot(final_result)
|
| 142 |
+
return (return_result)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# if __name__ == "__main__":
|
| 146 |
+
# # print("Loading")
|
| 147 |
+
# # while(True):
|
| 148 |
+
# # string = input("\nMời nhập văn bản: ")
|
| 149 |
+
# # os.system('cls')
|
| 150 |
+
# # print(f"Văn bản đã nhập: {string}")
|
| 151 |
+
# # judge(string)
|
| 152 |
+
# interface = gr.Interface(fn=judge,
|
| 153 |
+
# inputs=gr.Textbox(lines=2, placeholder='Please write something', label="Input Text"),
|
| 154 |
+
# outputs=['text','plot','text'])
|
| 155 |
+
# interface.launch()
|
gru_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d4c0d1c8b2e9e4037b6d3aa0bab048faf76429b0bcee6051a4ed0be6648224a5
|
| 3 |
+
size 242666376
|
lstm_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4c10cd444e92b4f7ced7e780ad83d94dd5eedd7f3831b45e63e52936593deb59
|
| 3 |
+
size 243514304
|
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from flask import Flask
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
| 4 |
|
|
@@ -12,4 +13,10 @@ def test():
|
|
| 12 |
|
| 13 |
@app.route("/test/<text>")
|
| 14 |
def returnText(text):
|
| 15 |
-
return f'The text is {text}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request
|
| 2 |
+
from app import judge
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
| 13 |
|
| 14 |
@app.route("/test/<text>")
|
| 15 |
def returnText(text):
|
| 16 |
+
return f'The text is {text}'
|
| 17 |
+
|
| 18 |
+
@app.route("/check", methods=["POST"])
|
| 19 |
+
def check():
|
| 20 |
+
comment = request.json['comment']
|
| 21 |
+
result = judge(comment)
|
| 22 |
+
return result
|
requirements.txt
CHANGED
|
@@ -1,2 +1,7 @@
|
|
| 1 |
flask
|
| 2 |
-
gunicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
flask
|
| 2 |
+
gunicorn
|
| 3 |
+
tensorflow
|
| 4 |
+
numpy
|
| 5 |
+
pathlib
|
| 6 |
+
plotly
|
| 7 |
+
pandas
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fccd0f5c6d250f40126a6fc9cc3dbd23d5feff60bf3440da1251fa67fdb7c923
|
| 3 |
+
size 6831015
|