Spaces:
No application file
No application file
Create web_app.py
Browse files- web_app.py +71 -0
web_app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__author__ = "Baishali Dutta"
|
| 2 |
+
__copyright__ = "Copyright (C) 2022 Baishali Dutta"
|
| 3 |
+
__license__ = "Apache License 2.0"
|
| 4 |
+
__version__ = "0.1"
|
| 5 |
+
|
| 6 |
+
# -------------------------------------------------------------------------
|
| 7 |
+
# Import Libraries
|
| 8 |
+
# -------------------------------------------------------------------------
|
| 9 |
+
import pickle
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from keras.models import load_model
|
| 13 |
+
from keras.preprocessing.sequence import pad_sequences
|
| 14 |
+
|
| 15 |
+
from source.config import *
|
| 16 |
+
from source.data_cleaning import clean_text
|
| 17 |
+
|
| 18 |
+
# -------------------------------------------------------------------------
|
| 19 |
+
# Load Existing Model and Tokenizer
|
| 20 |
+
# -------------------------------------------------------------------------
|
| 21 |
+
|
| 22 |
+
# load the trained model
|
| 23 |
+
rnn_model = load_model(MODEL_LOC)
|
| 24 |
+
|
| 25 |
+
# load the tokenizer
|
| 26 |
+
with open(TOKENIZER_LOC, 'rb') as handle:
|
| 27 |
+
tokenizer = pickle.load(handle)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# -------------------------------------------------------------------------
|
| 31 |
+
# Main Application
|
| 32 |
+
# -------------------------------------------------------------------------
|
| 33 |
+
|
| 34 |
+
def make_prediction(input_comment):
|
| 35 |
+
"""
|
| 36 |
+
Predicts the toxicity of the specified comment
|
| 37 |
+
:param input_comment: the comment to be verified
|
| 38 |
+
"""
|
| 39 |
+
input_comment = clean_text(input_comment)
|
| 40 |
+
input_comment = input_comment.split(" ")
|
| 41 |
+
|
| 42 |
+
sequences = tokenizer.texts_to_sequences(input_comment)
|
| 43 |
+
sequences = [[item for sublist in sequences for item in sublist]]
|
| 44 |
+
|
| 45 |
+
padded_data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
|
| 46 |
+
result = rnn_model.predict(padded_data, len(padded_data), verbose=1)
|
| 47 |
+
|
| 48 |
+
return \
|
| 49 |
+
{
|
| 50 |
+
"Toxic": str(result[0][0]),
|
| 51 |
+
"Very Toxic": str(result[0][1]),
|
| 52 |
+
"Obscene": str(result[0][2]),
|
| 53 |
+
"Threat": str(result[0][3]),
|
| 54 |
+
"Insult": str(result[0][4]),
|
| 55 |
+
"Hate": str(result[0][5]),
|
| 56 |
+
"Neutral": str(result[0][6])
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
comment = gr.inputs.Textbox(lines=17, placeholder="Enter your comment here")
|
| 61 |
+
|
| 62 |
+
title = "Comments Toxicity Detection"
|
| 63 |
+
description = "This application uses a Bidirectional Long Short-Term Memory (LSTM) Recurrent Neural Network (RNN) " \
|
| 64 |
+
"model to predict the inappropriateness of a comment"
|
| 65 |
+
|
| 66 |
+
gr.Interface(fn=make_prediction,
|
| 67 |
+
inputs=comment,
|
| 68 |
+
outputs="label",
|
| 69 |
+
title=title,
|
| 70 |
+
description=description) \
|
| 71 |
+
.launch()
|