|
import streamlit as st
|
|
import pickle
|
|
import string
|
|
from nltk.corpus import stopwords
|
|
import nltk
|
|
from nltk.stem.porter import PorterStemmer
|
|
ps = PorterStemmer()
|
|
|
|
|
|
|
|
def transform_text(text):
|
|
text = text.lower()
|
|
text = nltk.word_tokenize(text)
|
|
y = []
|
|
|
|
|
|
for i in text:
|
|
if i.isalnum():
|
|
y.append(i)
|
|
|
|
text = y[:]
|
|
y.clear()
|
|
|
|
|
|
for i in text:
|
|
if i not in stopwords.words('english') and i not in string.punctuation:
|
|
y.append(i)
|
|
|
|
text = y[:]
|
|
y.clear()
|
|
|
|
|
|
for i in text:
|
|
y.append(ps.stem(i))
|
|
|
|
return " ".join(y)
|
|
|
|
|
|
|
|
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
|
model = pickle.load(open('model.pkl', 'rb'))
|
|
|
|
|
|
st.title("π§ Email/SMS Spam Classifier")
|
|
st.write("""
|
|
### Enter a message to determine whether it's Spam or Not Spam.
|
|
This classifier uses **natural language processing (NLP)** techniques to preprocess and predict based on your input.
|
|
""")
|
|
|
|
|
|
st.write("#### Message Input:")
|
|
input_sms = st.text_area("Type or paste your message here", height=150)
|
|
|
|
|
|
if st.button("π Classify Message"):
|
|
if input_sms.strip():
|
|
|
|
with st.spinner('Processing...'):
|
|
transformed_sms = transform_text(input_sms)
|
|
|
|
|
|
vector_input = tfidf.transform([transformed_sms])
|
|
|
|
|
|
result = model.predict(vector_input)[0]
|
|
|
|
|
|
if result == 1:
|
|
st.success("π΄ This message is classified as **Spam**.")
|
|
else:
|
|
st.success("π’ This message is classified as **Not Spam**.")
|
|
else:
|
|
st.warning("Please enter a valid message to classify.")
|
|
|
|
|
|
st.markdown("""
|
|
---
|
|
Developed using **Streamlit** and **NLP techniques**.<br>
|
|
**Author**: **Aditya Yadav**
|
|
""", unsafe_allow_html=True) |