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