-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
84 lines (73 loc) · 2.81 KB
/
app.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import streamlit as st
import numpy as np
import re
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model
import pickle
import os
# Download NLTK resources
nltk.download('stopwords')
nltk.download('punkt')
# Load the saved model, tokenizer, and label encoder
model_path = "cyberbullying_detection.h5"
tokenizer_path = 'tokenizer.pickle'
label_encoder_path = 'label_encoder.pickle'
# Ensure files exist
if not os.path.exists(model_path):
st.error(f"Model file '{model_path}' not found.")
else:
model = load_model(model_path)
if not os.path.exists(tokenizer_path):
st.error(f"Tokenizer file '{tokenizer_path}' not found.")
else:
with open(tokenizer_path, 'rb') as handle:
tokenizer = pickle.load(handle)
if not os.path.exists(label_encoder_path):
st.error(f"Label encoder file '{label_encoder_path}' not found.")
else:
with open(label_encoder_path, 'rb') as handle:
label_encoder = pickle.load(handle)
# Define preprocessing function
def preprocess_text(text):
# Lowercasing
text = text.lower()
# Removing Punctuation
text = re.sub(r'[^\w\s]', '', text)
# Tokenization
tokens = word_tokenize(text)
# Removing Stopwords
stop_words = set(stopwords.words('english'))
filtered_tokens = [word for word in tokens if word not in stop_words]
# Stemming
stemmer = PorterStemmer()
stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]
# Combine tokens back into a string
preprocessed_text = ' '.join(stemmed_tokens)
return preprocessed_text
# Streamlit app
st.title("Cyberbullying Detection")
text_input = st.text_area("Enter a text to classify")
if st.button("Classify"):
if text_input:
# Check if model, tokenizer, and label encoder are loaded
if 'model' not in globals():
st.error("Model is not loaded.")
elif 'tokenizer' not in globals():
st.error("Tokenizer is not loaded.")
elif 'label_encoder' not in globals():
st.error("Label encoder is not loaded.")
else:
# Preprocess the input text
preprocessed_text = preprocess_text(text_input)
sequence = tokenizer.texts_to_sequences([preprocessed_text])
sequence_pad = pad_sequences(sequence, maxlen=100)
# Make prediction
prediction = model.predict(sequence_pad)
predicted_label = label_encoder.inverse_transform([np.argmax(prediction)])
st.write(f"Prediction: {predicted_label[0]}")
else:
st.write("Please enter a text to classify.")