Spaces:
Sleeping
Sleeping
Simple Spam Filtering using Naive Bayes
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 7 |
+
from sklearn.pipeline import Pipeline
|
| 8 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 9 |
+
|
| 10 |
+
# Sample dataset (email, label)
|
| 11 |
+
data = {'text': [
|
| 12 |
+
'Congratulations! You have won a free lottery ticket.',
|
| 13 |
+
'Important meeting scheduled for tomorrow.',
|
| 14 |
+
'Limited-time offer! Get a discount now!',
|
| 15 |
+
'Your bank account needs urgent verification.',
|
| 16 |
+
'Lunch meeting at 1 PM.',
|
| 17 |
+
'Win a free trip to the Bahamas!',
|
| 18 |
+
'Project deadline extended to next week.',
|
| 19 |
+
'Exclusive deal just for you! Buy now!',
|
| 20 |
+
'Reminder: Your doctor appointment is at 10 AM tomorrow.',
|
| 21 |
+
'Earn money fast with this simple trick!',
|
| 22 |
+
'Meeting rescheduled to 3 PM.',
|
| 23 |
+
'Verify your email to secure your account.',
|
| 24 |
+
'Huge discount on your favorite products!',
|
| 25 |
+
'Team outing planned for this weekend.',
|
| 26 |
+
'Act now! Limited seats available for the webinar.',
|
| 27 |
+
'Your order has been shipped successfully.',
|
| 28 |
+
'Congratulations! You have been selected for a special reward.',
|
| 29 |
+
'Last chance to claim your exclusive offer!',
|
| 30 |
+
'Monthly budget report attached.',
|
| 31 |
+
'Reminder: Submit your timesheet by Friday.'
|
| 32 |
+
],
|
| 33 |
+
'label': ['spam', 'legit', 'spam', 'spam', 'legit', 'spam', 'legit', 'spam', 'legit', 'spam', 'legit', 'spam', 'spam', 'legit', 'spam', 'legit', 'spam', 'spam', 'legit', 'legit']}
|
| 34 |
+
|
| 35 |
+
df = pd.DataFrame(data)
|
| 36 |
+
|
| 37 |
+
# Splitting data into training and testing sets
|
| 38 |
+
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['label'], test_size=0.2, random_state=42)
|
| 39 |
+
|
| 40 |
+
# Building the spam filter model using a pipeline
|
| 41 |
+
model = Pipeline([
|
| 42 |
+
('vectorizer', CountVectorizer()),
|
| 43 |
+
('tfidf', TfidfTransformer()),
|
| 44 |
+
('classifier', MultinomialNB())
|
| 45 |
+
])
|
| 46 |
+
|
| 47 |
+
# Train the model
|
| 48 |
+
model.fit(X_train, y_train)
|
| 49 |
+
|
| 50 |
+
# Streamlit App
|
| 51 |
+
st.title("Spam Filter Email Classifier")
|
| 52 |
+
|
| 53 |
+
email_input = st.text_area("Enter email content:")
|
| 54 |
+
if st.button("Classify Email"):
|
| 55 |
+
if email_input:
|
| 56 |
+
prediction = model.predict([email_input])[0]
|
| 57 |
+
st.write(f"The email is classified as: {prediction}")
|
| 58 |
+
else:
|
| 59 |
+
st.write("Please enter an email to classify.")
|