File size: 1,703 Bytes
0dfac59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.")