import gradio as gr import tensorflow as tf import numpy as np from keras.models import load_model from tensorflow.keras.preprocessing.text import Tokenizer import pickle from tensorflow.keras.preprocessing.sequence import pad_sequences import os print(tf.__version__) #Load tokenizer fp = "resources/tokenizer.pkl" with open(fp,mode="rb") as f: tokenizer = pickle.load(f) #Load LSTM fp = 'resources/lstm_model.h5' LSTM_model = tf.keras.models.load_model(fp, compile=True) #Load GRU fp = 'resources/gru_model.h5' GRU_model = load_model(fp) def tokenizer_pad(tokenizer,comment_text,max_length=200): # converting text into integer sequences comment_text = [comment_text] tokenized_text = tokenizer.texts_to_sequences(comment_text) # padding based on max length padded_sequences = pad_sequences(sequences=tokenized_text,maxlen=max_length,padding="post",truncating="post") return padded_sequences def LSTM_predict(x): x = tokenizer_pad(tokenizer=tokenizer,comment_text=x) #print(x) # processing before mapping # predicting using best model pred_proba = LSTM_model.predict(x)[0] # making predictions readable pred_proba = [round(i,2) for i in pred_proba] print(pred_proba) return pred_proba def GRU_predict(x): x = tokenizer_pad(tokenizer=tokenizer,comment_text=x) print(x) # processing before mapping # predicting using best model pred_proba = GRU_model.predict(x)[0] # making predictions readable pred_proba = [round(i,2) for i in pred_proba] print(pred_proba) return pred_proba def judge(x): 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'] result = [] judge_result = [] lstm_pred = LSTM_predict(x) gru_pred = GRU_predict(x) return_result = 'Result' result_lstm = np.round(lstm_pred, 2) result_gru = np.round(gru_pred, 2) sensitive_result = max(max(result_lstm),max(result_gru)) print(sensitive_result) return_result += '\nMô hình LSTM\n' return_result += f"{result_lstm}\n" for i in range(6): if result_lstm[i]>=0 and result_lstm[i]<0.1: return_result += "Tính {} là không có\n".format(label[i]) if result_lstm[i]>=0.1 and result_lstm[i]<0.5: return_result += "Tính {} ở mức nhận thấy được, từ ngữ có thể chưa phù hợp\n".format(label[i]) if result_lstm[i]>=0.5 and result_lstm[i]<0.8: return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i]) if result_lstm[i]>=0.8: return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i]) return_result += '\nMô hình GRU\n' return_result += f"{result_gru}\n" for i in range(6): if result_gru[i]>=0 and result_gru[i]<0.1: return_result += "Tính {} là không có\n".format(label[i]) if result_gru[i]>=0.1 and result_gru[i]<0.5: return_result += "Tính {} ở mức nhận thấy được, từ ngữ có thể chưa phù hợp\n".format(label[i]) if result_gru[i]>=0.5 and result_gru[i]<0.8: return_result += "Tính {} ở mức rõ ràng, cần xem xét\n".format(label[i]) if result_gru[i]>=0.8: return_result += "Tính {} ở mức nghiêm trọng, yêu cầu chấn chỉnh\n".format(label[i]) return_result += "\nTổng quan:\n" if sensitive_result>=0 and sensitive_result<0.1: return_result += "Ngôn ngữ phù hợp mọi lứa tuổi.\n" if sensitive_result>=0.1 and sensitive_result<0.5: return_result += "Ngôn ngữ có thể còn chứa từ ngữ chưa phù hợp.\n" if sensitive_result>=0.5 and sensitive_result<0.8: return_result += "Ngôn ngữ không phù hợp, cần xem xét lại.\n" if sensitive_result>=0.8: 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" return return_result if __name__ == "__main__": # print("Loading") # while(True): # string = input("\nMời nhập văn bản: ") # os.system('cls') # print(f"Văn bản đã nhập: {string}") # judge(string) interface = gr.Interface(fn=judge, inputs=gr.Textbox(lines=2, placeholder='Please write something', label="Input Text"), outputs='text') interface.launch()