Spaces:
Sleeping
Sleeping
Upload 5 files
Browse filesThis app helps you easily classify SMS messages as spam or not, using machine learning. Whether it's promotional offers or suspicious links, it determines if your message is safe or unwanted.
➤Accurate Spam Detection: Classifies messages into "spam" or "not spam" (ham) with high accuracy.
➤Simple and User-Friendly: Just enter the message, hit "Predict," and get results instantly.
➤Fast Processing: Quickly analyzes and classifies any SMS or message in real-time.
➤Machine Learning Powered: Uses a trained model to ensure reliable predictions based on patterns in text.
➤Easy to Use: No technical knowledge required; the app does all the work.
➤Safe and Secure: Helps you avoid phishing or scam messages by detecting spam automatically.
- app.py +70 -0
- kingthings_exeter.ttf +0 -0
- model.pkl +3 -0
- requirements.txt +5 -0
- vectorizer.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
import string
|
4 |
+
import nltk
|
5 |
+
from nltk.corpus import stopwords
|
6 |
+
from nltk.stem.porter import PorterStemmer
|
7 |
+
|
8 |
+
nltk.download('stopwords')
|
9 |
+
nltk.download('punkt')
|
10 |
+
|
11 |
+
# Initialize the PorterStemmer
|
12 |
+
ps = PorterStemmer()
|
13 |
+
|
14 |
+
# Load models and resources
|
15 |
+
def load_resources():
|
16 |
+
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
17 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
18 |
+
return tfidf, model
|
19 |
+
|
20 |
+
# Text preprocessing function
|
21 |
+
def transform_text(text):
|
22 |
+
text = text.lower()
|
23 |
+
tokens = nltk.word_tokenize(text)
|
24 |
+
|
25 |
+
# Remove non-alphanumeric tokens and stopwords, and apply stemming
|
26 |
+
filtered_tokens = [ps.stem(word) for word in tokens if word.isalnum() and word not in stopwords.words('english')]
|
27 |
+
return " ".join(filtered_tokens)
|
28 |
+
|
29 |
+
# Predict whether a message is spam or not
|
30 |
+
def predict_spam(input_text, tfidf, model):
|
31 |
+
transformed_text = transform_text(input_text)
|
32 |
+
vector_input = tfidf.transform([transformed_text])
|
33 |
+
result = model.predict(vector_input)[0]
|
34 |
+
return result
|
35 |
+
|
36 |
+
# Display result in Streamlit
|
37 |
+
def display_prediction(result):
|
38 |
+
if result == "spam":
|
39 |
+
st.success("This is spam 🚫")
|
40 |
+
elif result == "ham":
|
41 |
+
st.success("This is not spam 👍")
|
42 |
+
|
43 |
+
# Main Streamlit app function
|
44 |
+
def main():
|
45 |
+
# Load resources
|
46 |
+
tfidf, model = load_resources()
|
47 |
+
|
48 |
+
# Set the app title
|
49 |
+
st.title("Email/SMS Spam Classifier")
|
50 |
+
|
51 |
+
# Input text area for user message
|
52 |
+
input_sms = st.text_area("Enter your message here:")
|
53 |
+
|
54 |
+
# Placeholder for prediction result
|
55 |
+
prediction_placeholder = st.empty()
|
56 |
+
|
57 |
+
# Predict button
|
58 |
+
if st.button('Predict'):
|
59 |
+
if input_sms.strip() == "":
|
60 |
+
prediction_placeholder.markdown(
|
61 |
+
"<h3 style='color: #f24b4b; font-size: 1.75rem;'>Please enter a message first ⚠️</h3>",
|
62 |
+
unsafe_allow_html=True)
|
63 |
+
else:
|
64 |
+
result = predict_spam(input_sms, tfidf, model)
|
65 |
+
with prediction_placeholder:
|
66 |
+
display_prediction(result)
|
67 |
+
|
68 |
+
# Run the app
|
69 |
+
if __name__ == "__main__":
|
70 |
+
main()
|
kingthings_exeter.ttf
ADDED
Binary file (10.5 kB). View file
|
|
model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f90b4ceea8d5f6396e8450dda1a8466064bec9f09e058d049e812d87ab8771ec
|
3 |
+
size 96617
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas==2.2.2
|
2 |
+
numpy==1.26.4
|
3 |
+
streamlit==1.37.1
|
4 |
+
scikit-learn==1.5.1
|
5 |
+
nltk==3.8.1
|
vectorizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:093c46197d8e1cb29f987096a82f913d7459feeb0e62df74cec08c7d4d6e7883
|
3 |
+
size 94142
|