Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,88 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import joblib
|
| 3 |
-
import nltk
|
| 4 |
-
from nltk.corpus import stopwords
|
| 5 |
-
from nltk.tokenize import word_tokenize
|
| 6 |
-
import string
|
| 7 |
-
import re
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
nltk.download('punkt')
|
| 11 |
-
nltk.download('stopwords')
|
| 12 |
-
|
| 13 |
-
def preprocess_text(text):
|
| 14 |
-
|
| 15 |
-
text = text.lower()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
text = ''.join([char for char in text if char not in string.punctuation])
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
text = re.sub(r'\d+', '', text)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
text = ' '.join(text.split())
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
tokens = word_tokenize(text)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stop_words = set(stopwords.words('english'))
|
| 31 |
-
tokens = [token for token in tokens if token not in stop_words]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return ' '.join(tokens)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
model = joblib.load('spam_detector_model.joblib')
|
| 38 |
-
vectorizer = joblib.load('tfidf_vectorizer.joblib')
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
st.title("📧 Spam Message Detector")
|
| 42 |
-
|
| 43 |
-
st.write("""
|
| 44 |
-
This app detects whether a message is spam or not.
|
| 45 |
-
Enter your message below and click 'Analyze' to check!
|
| 46 |
-
""")
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
st.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
st.write("
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
st.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
-
|
| 88 |
-
|
| 89 |
-
""")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import nltk
|
| 4 |
+
from nltk.corpus import stopwords
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
import string
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
nltk.download('punkt')
|
| 11 |
+
nltk.download('stopwords')
|
| 12 |
+
|
| 13 |
+
def preprocess_text(text):
|
| 14 |
+
|
| 15 |
+
text = text.lower()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
text = ''.join([char for char in text if char not in string.punctuation])
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
text = re.sub(r'\d+', '', text)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
text = ' '.join(text.split())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
tokens = word_tokenize(text)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
stop_words = set(stopwords.words('english'))
|
| 31 |
+
tokens = [token for token in tokens if token not in stop_words]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
return ' '.join(tokens)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
model = joblib.load('spam_detector_model.joblib')
|
| 38 |
+
vectorizer = joblib.load('tfidf_vectorizer.joblib')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
st.title("📧 Spam Message Detector")
|
| 42 |
+
|
| 43 |
+
st.write("""
|
| 44 |
+
This app detects whether a message is spam or not.
|
| 45 |
+
Enter your message below and click 'Analyze' to check!
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
message = st.text_area("Enter your message:", height=100)
|
| 49 |
+
|
| 50 |
+
if st.button("Analyze"):
|
| 51 |
+
if message:
|
| 52 |
+
|
| 53 |
+
processed_text = preprocess_text(message)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
text_vectorized = vectorizer.transform([processed_text])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
prediction = model.predict(text_vectorized)[0]
|
| 60 |
+
probability = model.predict_proba(text_vectorized)[0]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
st.markdown("### Analysis Result")
|
| 64 |
+
|
| 65 |
+
if prediction == 1:
|
| 66 |
+
st.error("🚨 This message is likely SPAM!")
|
| 67 |
+
st.write(f"Confidence: {probability[1]:.2%}")
|
| 68 |
+
else:
|
| 69 |
+
st.success("✅ This message appears to be legitimate.")
|
| 70 |
+
st.write(f"Confidence: {probability[0]:.2%}")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
with st.expander("See preprocessing steps"):
|
| 74 |
+
st.write("Original message:", message)
|
| 75 |
+
st.write("Processed message:", processed_text)
|
| 76 |
+
else:
|
| 77 |
+
st.warning("Please enter a message to analyze.")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
with st.sidebar:
|
| 81 |
+
st.header("About the Model")
|
| 82 |
+
st.write("""
|
| 83 |
+
This spam detector uses an XGBoost classifier trained on a dataset of spam and legitimate messages.
|
| 84 |
+
|
| 85 |
+
Model Performance:
|
| 86 |
+
- Training Accuracy: 99.7%
|
| 87 |
+
- Testing Accuracy: 98.9%
|
| 88 |
+
""")
|
|
|