Om099 commited on
Commit
8a338f9
·
verified ·
1 Parent(s): 7913127

Create app.py

Browse files

Email Spam Detector using Naive Bayes and Gradio UI.

Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn.naive_bayes import MultinomialNB
4
+ from sklearn.feature_extraction.text import CountVectorizer
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.metrics import classification_report
7
+ import gradio as gr
8
+
9
+ # -------------------------------
10
+ # Load and preprocess the data
11
+ # -------------------------------
12
+ def load_data():
13
+ df = pd.read_csv("spam.csv", encoding="latin-1")
14
+ df = df[['v1', 'v2']] # Keep only the required columns
15
+ df.columns = ['label', 'message']
16
+ df['spam'] = df['label'].apply(lambda x: 1 if x == 'spam' else 0)
17
+ return df
18
+
19
+ # -------------------------------
20
+ # Train the spam classifier
21
+ # -------------------------------
22
+ def train_model(df):
23
+ X_train, X_test, y_train, y_test = train_test_split(df.message, df.spam, test_size=0.2, random_state=42)
24
+
25
+ vectorizer = CountVectorizer()
26
+ X_train_cv = vectorizer.fit_transform(X_train)
27
+ X_test_cv = vectorizer.transform(X_test)
28
+
29
+ model = MultinomialNB()
30
+ model.fit(X_train_cv, y_train)
31
+
32
+ y_pred = model.predict(X_test_cv)
33
+ print("Model performance on test set:")
34
+ print(classification_report(y_test, y_pred))
35
+
36
+ return model, vectorizer
37
+
38
+ # -------------------------------
39
+ # Predict function for Gradio
40
+ # -------------------------------
41
+ def predict_spam(email_text):
42
+ email_count = vectorizer.transform([email_text])
43
+ prediction = model.predict(email_count)[0]
44
+ return "Spam ❌" if prediction == 1 else "Not Spam"
45
+
46
+ # -------------------------------
47
+ # Main Execution
48
+ # -------------------------------
49
+ df = load_data()
50
+ model, vectorizer = train_model(df)
51
+
52
+ # -------------------------------
53
+ # Gradio Interface
54
+ # -------------------------------
55
+ interface = gr.Interface(
56
+ fn=predict_spam,
57
+ inputs=gr.Textbox(lines=5, placeholder="Paste your email text here..."),
58
+ outputs="text",
59
+ title="Email Spam Detector",
60
+ description="A machine learning model using Naive Bayes to detect whether an email is spam or not. Type or paste an email to test it!"
61
+ )
62
+
63
+ # Shareable link
64
+ interface.launch()