wracell
first commit
0dfac59
import streamlit as st
import pickle
import string
import re
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
def preprocess_text(text):
text = text.lower()
text = re.sub(f"[{string.punctuation}]", "", text)
return text
# Load the trained model (or train a simple one if not available)
try:
with open("spam_model.pkl", "rb") as f:
model = pickle.load(f)
except FileNotFoundError:
# Sample training data
spam_data = [
("Win a free iPhone now", 1),
("Urgent! You won a lottery", 1),
("Call me when you are free", 0),
("Meeting at 5pm", 0),
]
texts, labels = zip(*spam_data)
pipeline = Pipeline([
("vectorizer", CountVectorizer(preprocessor=preprocess_text)),
("classifier", MultinomialNB())
])
model = pipeline.fit(texts, labels)
with open("spam_model.pkl", "wb") as f:
pickle.dump(model, f)
# Streamlit UI
st.title("📧 Spam Email Detector")
# Instructions for using the app
st.write("### Instructions:")
st.write("1. Enter the email text in the text box below.")
st.write("2. Click the 'Check Spam' button.")
st.write("3. The app will analyze the text and determine if it's Spam or Not Spam.")
st.write("4. If no text is entered, a warning will be displayed.")
email_text = st.text_area("Email Content", "")
if st.button("Check Spam"):
if email_text:
prediction = model.predict([email_text])[0]
result = "Spam" if prediction == 1 else "Not Spam"
st.success(f"Result: {result}")
else:
st.warning("Please enter an email content.")