File size: 1,413 Bytes
a3ae041
 
 
 
 
 
 
7b02514
a3ae041
 
 
 
 
 
7b02514
a3ae041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b02514
 
a3ae041
 
 
7b02514
a3ae041
 
7b02514
 
 
a3ae041
 
 
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
import gradio as gr
from official.nlp.optimization import AdamWeightDecay, WarmUp
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
import numpy as np
np.set_printoptions(suppress=True)

labels = [
    "hate speech",
    "offensive language",
    "neither"
]


with tf.keras.utils.custom_object_scope({'AdamWeightDecay': AdamWeightDecay(), 'WarmUp': WarmUp}):
    classifier_model = tf.keras.models.load_model('classifier_model.h5',
                                                  custom_objects={'KerasLayer': hub.KerasLayer})


def run_model(text):
    prediction = classifier_model.predict([text])[0]
    confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
    return confidences


examples = [
    ["This is wonderful!"],
]

short_description = "This application classifies text into three categories: hate speech, offensive language, and neither, using a deep learning model trained on the Hate Speech and Offensive Language Dataset. Enter a sentence and the model will predict its category."

hate_speech = gr.Interface(
    fn=run_model,
    inputs=gr.Textbox(lines=5,
                      placeholder="Enter a sentence here...",
                      label="Input Text"),
    outputs=gr.outputs.Label(),
    examples=examples,
    title="Hate Speech and Offensive Language Classifier",
    description=short_description
)

hate_speech.launch()