Spaces:
Sleeping
Sleeping
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() |