File size: 1,882 Bytes
854fe03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
import gradio as gr
import numpy as np
import string
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import tensorflow as tf
from tensorflow import keras
from keras import layers
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import joblib

nltk.download('stopwords')
nltk.download('omw-1.4')
nltk.download('wordnet')
nltk.download('punkt')

tokenizer, model = joblib.load("lstm_model.pkl")

def preprocess(text, tokenizer):
    lemmatizer = WordNetLemmatizer()
    vocab = set()
    stop_words = set(stopwords.words('english'))
    tokens = word_tokenize(text)
    tokens = [word for word in tokens if word.lower() not in stop_words and word not in string.punctuation]
    tokens = [lemmatizer.lemmatize(word.lower()) for word in tokens]
    vocab.update(tokens)
    preprocessed_text = ' '.join(tokens)
    X = tokenizer.texts_to_sequences(preprocessed_text)
    max_len = max(len(y) for y in X)
    X = pad_sequences(X, maxlen=max_len)
    return X

def predict(text):
    X = preprocess(text, tokenizer)
    pred = model.predict(X)
    probabilities = np.mean(pred, axis=0)
    final_class = np.argmax(probabilities)
    if final_class == 0:
        prediction = "The string is classified as hate speech."
    else:
        prediction = "The string is classified as normal speech."
    return {"prediction": prediction, "probability": probabilities.tolist()}

iface = gr.Interface(
    fn=predict,
    inputs=gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
    outputs=[gr.outputs.Textbox(label="Prediction"), gr.outputs.Textbox(label="Probabilities")],
    title="Hate Speech Classifier",
    description="A classifier to detect hate speech in a given text.",
)

if __name__ == "__main__":
    iface.launch()