File size: 1,163 Bytes
5ce506c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
import pickle

import gradio as gr
import numpy as np
import tensorflow as tf

from utils import clean_text, tokenize_and_pad

# Load pre-trained TensorFlow model
model = tf.keras.models.load_model('model.h5')

# Load tokenizer
with open('tokenizer.pickle', 'rb') as handle:
    tokenizer = pickle.load(handle)
    print(type(tokenizer))

# Constants
MAX_LEN = 300


def predict_hate_speech(text):
    # Clean the text
    cleaned_text = clean_text(text)


    # Tokenize and pad the text
    preprocessed_text = tokenize_and_pad([cleaned_text], tokenizer, MAX_LEN)

    # Make a prediction
    prediction = model.predict(preprocessed_text)

    # Assuming you have two classes: "Hate" and "Not Hate"
    if prediction > 0.5:
        result = "Hate"
    else:
        result = "Not Hate"

    return result


# Create a Gradio interface
iface = gr.Interface(
    fn=predict_hate_speech,
    inputs=gr.Textbox(label="Input Text"),
    outputs=gr.Textbox(label="Output Prediction"),
    title="Hate Speech Classification",
    description="A simple hate speech classifier. Enter a text and click submit to make a prediction."
)

# Run the Gradio app
iface.launch()