Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +134 -0
- gru_model.h5 +3 -0
- lstm_model.h5 +3 -0
- tokenizer.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
#Load tokenizer
|
12 |
+
fp = Path(__file__).with_name('tokenizer.pkl')
|
13 |
+
with open(fp,mode="rb") as f:
|
14 |
+
tokenizer = pickle.load(f)
|
15 |
+
|
16 |
+
#Load LSTM
|
17 |
+
fp = Path(__file__).with_name('lstm_model.h5')
|
18 |
+
LSTM_model = tf.keras.models.load_model(fp, compile=True)
|
19 |
+
|
20 |
+
#Load GRU
|
21 |
+
fp = Path(__file__).with_name('gru_model.h5')
|
22 |
+
GRU_model = load_model(fp)
|
23 |
+
|
24 |
+
|
25 |
+
def tokenizer_pad(tokenizer,comment_text,max_length=200):
|
26 |
+
|
27 |
+
# converting text into integer sequences
|
28 |
+
comment_text = [comment_text]
|
29 |
+
tokenized_text = tokenizer.texts_to_sequences(comment_text)
|
30 |
+
|
31 |
+
# padding based on max length
|
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 |
+
|
40 |
+
#print(x)
|
41 |
+
|
42 |
+
# processing before mapping
|
43 |
+
|
44 |
+
# predicting using best model
|
45 |
+
pred_proba = LSTM_model.predict(x)[0]
|
46 |
+
|
47 |
+
# making predictions readable
|
48 |
+
pred_proba = [round(i,2) for i in pred_proba]
|
49 |
+
|
50 |
+
print(pred_proba)
|
51 |
+
|
52 |
+
return pred_proba
|
53 |
+
|
54 |
+
def GRU_predict(x):
|
55 |
+
x = tokenizer_pad(tokenizer=tokenizer,comment_text=x)
|
56 |
+
|
57 |
+
|
58 |
+
print(x)
|
59 |
+
# processing before mapping
|
60 |
+
|
61 |
+
# predicting using best model
|
62 |
+
pred_proba = GRU_model.predict(x)[0]
|
63 |
+
|
64 |
+
# making predictions readable
|
65 |
+
pred_proba = [round(i,2) for i in pred_proba]
|
66 |
+
|
67 |
+
print(pred_proba)
|
68 |
+
|
69 |
+
return pred_proba
|
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 |
+
|
81 |
+
return_result = 'Result'
|
82 |
+
result_lstm = np.round(lstm_pred, 2)
|
83 |
+
result_gru = np.round(gru_pred, 2)
|
84 |
+
sensitive_result = max(max(result_lstm),max(result_gru))
|
85 |
+
print(sensitive_result)
|
86 |
+
return_result += '\nMô hình LSTM\n'
|
87 |
+
return_result += f"{result_lstm}\n"
|
88 |
+
for i in range(6):
|
89 |
+
if result_lstm[i]>=0 and result_lstm[i]<0.1:
|
90 |
+
return_result += "Tính {} là không có\n".format(label[i])
|
91 |
+
if result_lstm[i]>=0.1 and result_lstm[i]<0.5:
|
92 |
+
return_result += "Tính {} ở mức nhận thấy được, từ ngữ có thể chưa phù hợp\n".format(label[i])
|
93 |
+
if result_lstm[i]>=0.5 and result_lstm[i]<0.8:
|
94 |
+
return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i])
|
95 |
+
if result_lstm[i]>=0.8:
|
96 |
+
return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i])
|
97 |
+
|
98 |
+
|
99 |
+
return_result += '\nMô hình GRU\n'
|
100 |
+
return_result += f"{result_gru}\n"
|
101 |
+
for i in range(6):
|
102 |
+
if result_gru[i]>=0 and result_gru[i]<0.1:
|
103 |
+
return_result += "Tính {} là không có\n".format(label[i])
|
104 |
+
if result_gru[i]>=0.1 and result_gru[i]<0.5:
|
105 |
+
return_result += "Tính {} ở mức nhận thấy được, từ ngữ có thể chưa phù hợp\n".format(label[i])
|
106 |
+
if result_gru[i]>=0.5 and result_gru[i]<0.8:
|
107 |
+
return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i])
|
108 |
+
if result_gru[i]>=0.8:
|
109 |
+
return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i])
|
110 |
+
|
111 |
+
return_result += "\nTổng quan:\n"
|
112 |
+
if sensitive_result>=0 and sensitive_result<0.1:
|
113 |
+
return_result += "Ngôn ngữ phù hợp mọi lứa tuổi.\n"
|
114 |
+
if sensitive_result>=0.1 and sensitive_result<0.5:
|
115 |
+
return_result += "Ngôn ngữ có thể còn chứa từ ngữ chưa phù hợp.\n"
|
116 |
+
if sensitive_result>=0.5 and sensitive_result<0.8:
|
117 |
+
return_result += "Ngôn ngữ không phù hợp, cần xem xét lại.\n"
|
118 |
+
if sensitive_result>=0.8:
|
119 |
+
return_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"
|
120 |
+
|
121 |
+
return return_result
|
122 |
+
|
123 |
+
|
124 |
+
if __name__ == "__main__":
|
125 |
+
# print("Loading")
|
126 |
+
# while(True):
|
127 |
+
# string = input("\nMời nhập văn bản: ")
|
128 |
+
# os.system('cls')
|
129 |
+
# print(f"Văn bản đã nhập: {string}")
|
130 |
+
# judge(string)
|
131 |
+
interface = gr.Interface(fn=judge,
|
132 |
+
inputs=gr.Textbox(lines=2, placeholder='Please write something', label="Input Text"),
|
133 |
+
outputs='text')
|
134 |
+
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
|
tokenizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fccd0f5c6d250f40126a6fc9cc3dbd23d5feff60bf3440da1251fa67fdb7c923
|
3 |
+
size 6831015
|