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()