|
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 |
|
|
|
|
|
model = tf.keras.models.load_model("cyberbullying_hybrid_model.h5") |
|
|
|
|
|
with open("tokenizer.pkl", "rb") as f: |
|
tokenizer = pickle.load(f) |
|
|
|
|
|
with open("label_encoder.pkl", "rb") as f: |
|
label_encoder = pickle.load(f) |
|
|
|
|
|
def predict_cyberbullying(text): |
|
if not text.strip(): |
|
return "Please enter a valid text." |
|
|
|
|
|
seq = tokenizer.texts_to_sequences([text]) |
|
padded_seq = pad_sequences(seq, maxlen=100) |
|
|
|
|
|
prediction = model.predict(padded_seq) |
|
predicted_label = np.argmax(prediction, axis=1) |
|
|
|
|
|
predicted_class = label_encoder.inverse_transform(predicted_label)[0] |
|
|
|
return f"Predicted Cyberbullying Type: {predicted_class}" |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
ui.launch() |
|
|