Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,17 @@
|
|
1 |
-
|
2 |
-
from pydantic import BaseModel
|
3 |
import joblib
|
|
|
4 |
import nltk
|
5 |
from nltk.corpus import stopwords
|
6 |
from nltk.stem import PorterStemmer
|
7 |
-
import re
|
8 |
|
|
|
9 |
nltk.download('stopwords')
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
# Load the model pipeline
|
14 |
pipeline = joblib.load('spam_classifier_pipeline.joblib')
|
15 |
|
16 |
-
|
17 |
-
subject: str
|
18 |
-
body: str
|
19 |
-
|
20 |
def preprocess_text(text):
|
21 |
text = text.lower()
|
22 |
text = re.sub(r'[^a-zA-Z\s]', '', text)
|
@@ -27,12 +22,45 @@ def preprocess_text(text):
|
|
27 |
words = [stemmer.stem(word) for word in words]
|
28 |
return ' '.join(words)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
prediction = pipeline.predict([
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
return {"message": "Spam Classification API"}
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import joblib
|
3 |
+
import re
|
4 |
import nltk
|
5 |
from nltk.corpus import stopwords
|
6 |
from nltk.stem import PorterStemmer
|
|
|
7 |
|
8 |
+
# Download NLTK stopwords
|
9 |
nltk.download('stopwords')
|
10 |
|
11 |
+
# Load the saved pipeline
|
|
|
|
|
12 |
pipeline = joblib.load('spam_classifier_pipeline.joblib')
|
13 |
|
14 |
+
# Preprocessing function (must match your training preprocessing)
|
|
|
|
|
|
|
15 |
def preprocess_text(text):
|
16 |
text = text.lower()
|
17 |
text = re.sub(r'[^a-zA-Z\s]', '', text)
|
|
|
22 |
words = [stemmer.stem(word) for word in words]
|
23 |
return ' '.join(words)
|
24 |
|
25 |
+
# Prediction function
|
26 |
+
def classify_email(subject, body):
|
27 |
+
combined_text = preprocess_text(f"{subject} {body}")
|
28 |
+
prediction = pipeline.predict([combined_text])[0]
|
29 |
+
labels = ["ham", "not_spam", "spam"]
|
30 |
+
return labels[prediction]
|
31 |
+
|
32 |
+
# Create Gradio interface
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown("# 📧 Spam Email Classifier")
|
35 |
+
gr.Markdown("Classify emails into **ham (personal)**, **not_spam (promotional)**, or **spam (junk)**")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
subject = gr.Textbox(label="Email Subject",
|
40 |
+
placeholder="e.g., 'Win a free prize!'")
|
41 |
+
body = gr.Textbox(label="Email Body",
|
42 |
+
placeholder="e.g., 'Click here to claim...'",
|
43 |
+
lines=5)
|
44 |
+
submit_btn = gr.Button("Classify Email")
|
45 |
+
|
46 |
+
with gr.Column():
|
47 |
+
output = gr.Label(label="Prediction")
|
48 |
+
examples = gr.Examples(
|
49 |
+
examples=[
|
50 |
+
["Meeting tomorrow", "Hi team, let's discuss the project at 10 AM."],
|
51 |
+
["Exclusive offer!", "Get 50% off on our new product. Limited time!"],
|
52 |
+
["You won $1,000,000!", "Claim your prize now by clicking this link!"],
|
53 |
+
["Newsletter", "This month's updates and new features"],
|
54 |
+
["Urgent: Account Suspension", "Your account will be closed unless you verify now"]
|
55 |
+
],
|
56 |
+
inputs=[subject, body]
|
57 |
+
)
|
58 |
+
|
59 |
+
submit_btn.click(
|
60 |
+
fn=classify_email,
|
61 |
+
inputs=[subject, body],
|
62 |
+
outputs=output
|
63 |
+
)
|
64 |
|
65 |
+
# For Hugging Face Spaces deployment
|
66 |
+
demo.launch()
|
|