nabeel857's picture
Upload 5 files
d9cedb1 verified
import streamlit as st
import pickle
import string
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
nltk.download('stopwords')
nltk.download('punkt')
# Initialize the PorterStemmer
ps = PorterStemmer()
# Load models and resources
def load_resources():
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
return tfidf, model
# Text preprocessing function
def transform_text(text):
text = text.lower()
tokens = nltk.word_tokenize(text)
# Remove non-alphanumeric tokens and stopwords, and apply stemming
filtered_tokens = [ps.stem(word) for word in tokens if word.isalnum() and word not in stopwords.words('english')]
return " ".join(filtered_tokens)
# Predict whether a message is spam or not
def predict_spam(input_text, tfidf, model):
transformed_text = transform_text(input_text)
vector_input = tfidf.transform([transformed_text])
result = model.predict(vector_input)[0]
return result
# Display result in Streamlit
def display_prediction(result):
if result == "spam":
st.success("This is spam 🚫")
elif result == "ham":
st.success("This is not spam πŸ‘")
# Main Streamlit app function
def main():
# Load resources
tfidf, model = load_resources()
# Set the app title
st.title("Email/SMS Spam Classifier")
# Input text area for user message
input_sms = st.text_area("Enter your message here:")
# Placeholder for prediction result
prediction_placeholder = st.empty()
# Predict button
if st.button('Predict'):
if input_sms.strip() == "":
prediction_placeholder.markdown(
"<h3 style='color: #f24b4b; font-size: 1.75rem;'>Please enter a message first ⚠️</h3>",
unsafe_allow_html=True)
else:
result = predict_spam(input_sms, tfidf, model)
with prediction_placeholder:
display_prediction(result)
# Run the app
if __name__ == "__main__":
main()