File size: 1,453 Bytes
d95c27d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
import pickle
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder

# Load the trained model
model = tf.keras.models.load_model("cyberbullying_hybrid_model.h5")

# Load the tokenizer
with open("tokenizer.pkl", "rb") as f:
    tokenizer = pickle.load(f)

# Load the label encoder
with open("label_encoder.pkl", "rb") as f:
    label_encoder = pickle.load(f)

# Function to preprocess text and make predictions
def predict_cyberbullying(text):
    if not text.strip():
        return "Please enter a valid text."

    # Convert text to sequences
    seq = tokenizer.texts_to_sequences([text])
    padded_seq = pad_sequences(seq, maxlen=100)  # Ensure same max_length used during training

    # Make prediction
    prediction = model.predict(padded_seq)
    predicted_label = np.argmax(prediction, axis=1)  # Get index of highest probability

    # Convert index back to class label
    predicted_class = label_encoder.inverse_transform(predicted_label)[0]

    return f"Predicted Cyberbullying Type: {predicted_class}"

# Create Gradio interface
ui = gr.Interface(
    fn=predict_cyberbullying,
    inputs=gr.Textbox(lines=2, placeholder="Enter a text..."),
    outputs="text",
    title="Cyberbullying Detection",
    description="Enter a text and the model will predict the type of cyberbullying."
)

# Launch the UI
ui.launch()