File size: 2,355 Bytes
c59c834
677f706
c59c834
677f706
 
 
 
c59c834
677f706
 
c59c834
677f706
 
c59c834
677f706
 
 
 
 
 
 
 
 
 
c59c834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677f706
c59c834
 
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
59
60
61
62
63
64
65
66
import gradio as gr
import joblib
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer

# Download NLTK stopwords
nltk.download('stopwords')

# Load the saved pipeline
pipeline = joblib.load('spam_classifier_pipeline.joblib')

# Preprocessing function (must match your training preprocessing)
def preprocess_text(text):
    text = text.lower()
    text = re.sub(r'[^a-zA-Z\s]', '', text)
    words = text.split()
    stop_words = set(stopwords.words('english'))
    words = [word for word in words if word not in stop_words]
    stemmer = PorterStemmer()
    words = [stemmer.stem(word) for word in words]
    return ' '.join(words)

# Prediction function
def classify_email(subject, body):
    combined_text = preprocess_text(f"{subject} {body}")
    prediction = pipeline.predict([combined_text])[0]
    labels = ["ham", "not_spam", "spam"]
    return labels[prediction]

# Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# 📧 Spam Email Classifier")
    gr.Markdown("Classify emails into **ham (personal)**, **not_spam (promotional)**, or **spam (junk)**")
    
    with gr.Row():
        with gr.Column():
            subject = gr.Textbox(label="Email Subject", 
                                placeholder="e.g., 'Win a free prize!'")
            body = gr.Textbox(label="Email Body", 
                            placeholder="e.g., 'Click here to claim...'",
                            lines=5)
            submit_btn = gr.Button("Classify Email")
        
        with gr.Column():
            output = gr.Label(label="Prediction")
            examples = gr.Examples(
                examples=[
                    ["Meeting tomorrow", "Hi team, let's discuss the project at 10 AM."],
                    ["Exclusive offer!", "Get 50% off on our new product. Limited time!"],
                    ["You won $1,000,000!", "Claim your prize now by clicking this link!"],
                    ["Newsletter", "This month's updates and new features"],
                    ["Urgent: Account Suspension", "Your account will be closed unless you verify now"]
                ],
                inputs=[subject, body]
            )
    
    submit_btn.click(
        fn=classify_email,
        inputs=[subject, body],
        outputs=output
    )

# For Hugging Face Spaces deployment
demo.launch()