Spaces:
Sleeping
Sleeping
File size: 1,977 Bytes
99823ec |
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 |
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
import os
# Fix protobuf compatibility issue
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
# Load Dataset
df = pd.read_csv("spam.csv", encoding='latin-1')
df = df[['v1', 'v2']]
df.columns = ['label', 'message']
df['label'] = df['label'].map({'ham': 0, 'spam': 1})
# Tabs Navigation
tabs = st.tabs(["Overview", "Dataset & Training", "Spam Detection"])
with tabs[0]: # Overview
st.title("Spam Email Classifier")
st.write("""
This app classifies emails/messages as **Spam** or **Ham** using a **Naïve Bayes Classifier**.
The dataset used for training consists of labeled SMS messages.
""")
with tabs[1]: # Dataset & Training
st.title("Dataset & Training")
st.write("### Sample Data")
st.dataframe(df.head())
st.write("### Dataset Statistics")
st.write(df.describe())
st.write("### Class Distribution")
fig, ax = plt.subplots()
sns.countplot(x='label', data=df, ax=ax)
st.pyplot(fig)
# Train Model
X = df['message']
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
model.fit(X_train, y_train)
with tabs[2]: # Spam Detection
st.title("Spam Detection")
st.sidebar.header("Enter your message:")
user_input = st.sidebar.text_area("Type your email/message here:")
if st.sidebar.button("Classify"):
prediction = model.predict([user_input])[0]
result = "Spam" if prediction == 1 else "Ham"
st.write("### Classification Result")
st.success(f"The message is classified as: {result}")
|