mherlie commited on
Commit
99823ec
·
1 Parent(s): 2562b51

added code

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. app.py +63 -0
  3. requirements.txt +4 -0
  4. spam.csv +0 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ venv/
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import seaborn as sns
4
+ import matplotlib.pyplot as plt
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.feature_extraction.text import TfidfVectorizer
7
+ from sklearn.naive_bayes import MultinomialNB
8
+ from sklearn.pipeline import make_pipeline
9
+ import os
10
+
11
+ # Fix protobuf compatibility issue
12
+ os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
13
+
14
+ # Load Dataset
15
+ df = pd.read_csv("spam.csv", encoding='latin-1')
16
+ df = df[['v1', 'v2']]
17
+ df.columns = ['label', 'message']
18
+
19
+ df['label'] = df['label'].map({'ham': 0, 'spam': 1})
20
+
21
+ # Tabs Navigation
22
+ tabs = st.tabs(["Overview", "Dataset & Training", "Spam Detection"])
23
+
24
+ with tabs[0]: # Overview
25
+ st.title("Spam Email Classifier")
26
+ st.write("""
27
+ This app classifies emails/messages as **Spam** or **Ham** using a **Naïve Bayes Classifier**.
28
+
29
+ The dataset used for training consists of labeled SMS messages.
30
+ """)
31
+
32
+ with tabs[1]: # Dataset & Training
33
+ st.title("Dataset & Training")
34
+ st.write("### Sample Data")
35
+ st.dataframe(df.head())
36
+
37
+ st.write("### Dataset Statistics")
38
+ st.write(df.describe())
39
+
40
+ st.write("### Class Distribution")
41
+ fig, ax = plt.subplots()
42
+ sns.countplot(x='label', data=df, ax=ax)
43
+ st.pyplot(fig)
44
+
45
+ # Train Model
46
+ X = df['message']
47
+ y = df['label']
48
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
49
+
50
+ model = make_pipeline(TfidfVectorizer(), MultinomialNB())
51
+ model.fit(X_train, y_train)
52
+
53
+ with tabs[2]: # Spam Detection
54
+ st.title("Spam Detection")
55
+
56
+ st.sidebar.header("Enter your message:")
57
+ user_input = st.sidebar.text_area("Type your email/message here:")
58
+
59
+ if st.sidebar.button("Classify"):
60
+ prediction = model.predict([user_input])[0]
61
+ result = "Spam" if prediction == 1 else "Ham"
62
+ st.write("### Classification Result")
63
+ st.success(f"The message is classified as: {result}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ joblib
4
+ scikit-learn
spam.csv ADDED
The diff for this file is too large to render. See raw diff