sancho10 commited on
Commit
d95c27d
·
verified ·
1 Parent(s): c9a1456

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import pickle
5
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
6
+ from sklearn.preprocessing import LabelEncoder
7
+
8
+ # Load the trained model
9
+ model = tf.keras.models.load_model("cyberbullying_hybrid_model.h5")
10
+
11
+ # Load the tokenizer
12
+ with open("tokenizer.pkl", "rb") as f:
13
+ tokenizer = pickle.load(f)
14
+
15
+ # Load the label encoder
16
+ with open("label_encoder.pkl", "rb") as f:
17
+ label_encoder = pickle.load(f)
18
+
19
+ # Function to preprocess text and make predictions
20
+ def predict_cyberbullying(text):
21
+ if not text.strip():
22
+ return "Please enter a valid text."
23
+
24
+ # Convert text to sequences
25
+ seq = tokenizer.texts_to_sequences([text])
26
+ padded_seq = pad_sequences(seq, maxlen=100) # Ensure same max_length used during training
27
+
28
+ # Make prediction
29
+ prediction = model.predict(padded_seq)
30
+ predicted_label = np.argmax(prediction, axis=1) # Get index of highest probability
31
+
32
+ # Convert index back to class label
33
+ predicted_class = label_encoder.inverse_transform(predicted_label)[0]
34
+
35
+ return f"Predicted Cyberbullying Type: {predicted_class}"
36
+
37
+ # Create Gradio interface
38
+ ui = gr.Interface(
39
+ fn=predict_cyberbullying,
40
+ inputs=gr.Textbox(lines=2, placeholder="Enter a text..."),
41
+ outputs="text",
42
+ title="Cyberbullying Detection",
43
+ description="Enter a text and the model will predict the type of cyberbullying."
44
+ )
45
+
46
+ # Launch the UI
47
+ ui.launch()