File size: 9,250 Bytes
8220955
 
 
 
 
6ae5e44
8220955
6ae5e44
 
8220955
6ae5e44
 
 
d9f7578
6ae5e44
d9f7578
 
6ae5e44
d9f7578
 
6ae5e44
 
 
8220955
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae5e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8220955
 
 
 
 
 
 
 
 
 
6ae5e44
 
 
8220955
6ae5e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8220955
6ae5e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8220955
 
 
 
 
 
 
 
 
6ae5e44
 
 
 
 
 
 
 
 
47cb9f1
 
 
 
 
 
 
 
 
 
 
 
 
6ae5e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8220955
 
 
 
 
 
 
 
 
 
 
 
 
6ae5e44
8220955
 
 
6ae5e44
 
 
8220955
 
 
 
6ae5e44
8220955
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from dotenv import load_dotenv
import os
import random
import numpy as np
import pandas as pd
from huggingface_hub import login

# ----------------------------
# 1. Streamlit App Configuration
# ----------------------------
st.set_page_config(
    page_title="Email Classifier using NLP",
    page_icon="๐Ÿ“ง",
    layout="wide",
    initial_sidebar_state="expanded"
)

# ----------------------------
# 2. Custom Styling
# ----------------------------
st.markdown("""
    <style>
        body {
            background: linear-gradient(to right, #eef2f3, #8e9eab);
        }
        .main-title {
            text-align: center;
            color: #222;
            font-size: 2.3rem;
            font-weight: 700;
        }
        .sub-title {
            text-align: center;
            color: #444;
            font-size: 1.1rem;
            margin-bottom: 40px;
        }
        .email-list {
            background-color: white;
            border-radius: 10px;
            padding: 10px;
            height: 500px;
            overflow-y: auto;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        }
        .email-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
            cursor: pointer;
        }
        .email-item:hover {
            background-color: #f5f5f5;
        }
        .email-content {
            background-color: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            height: 500px;
            overflow-y: auto;
        }
        .prediction-card {
            background-color: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        }
    </style>
""", unsafe_allow_html=True)

# ----------------------------
# 3. App Header
# ----------------------------
st.markdown("<h1 class='main-title'>๐Ÿ“ง Smart Email Classifier</h1>", unsafe_allow_html=True)
st.markdown("<p class='sub-title'>Smart Email Classification App is an advanced Natural Language Processing (NLP) and Deep Learning project designed to automate email intent classification. The application is capable of categorizing emails into six widely-used categories: Promotions, Spam, Social Media Updates, Forum Updates, Code Verification, and Work Updates.</p>", unsafe_allow_html=True)

# ----------------------------
# 4. Sidebar: Model Selection
# ----------------------------
st.sidebar.header("โš™๏ธ Model Configuration")

# Define model options
model_options = {
    "DistilBERT (Fine-tuned) 1": "kaisarhossain/email-classifier-distilbert-finetuned-kaisar",
    "DistilBERT (Fine-tuned) 2": "kaisarhossain/email_classifier_model"
}

model_choice = st.sidebar.selectbox("Select Model", list(model_options.keys()))
MODEL_REPO = model_options[model_choice]
st.sidebar.info(f"Using model: {MODEL_REPO}")

# ----------------------------
# 5. Environment Variables & Authentication
# ----------------------------
load_dotenv()
HF_TOKEN = os.getenv("HF_TOKEN")

if HF_TOKEN:
    try:
        login(token=HF_TOKEN)
    except Exception as e:
        st.sidebar.warning("โš ๏ธ Unable to authenticate with Hugging Face token.")
        st.sidebar.write(e)

# ----------------------------
# 6. Load Model Dynamically
# ----------------------------
@st.cache_resource(show_spinner=True)
def load_model(model_repo):
    tokenizer = AutoTokenizer.from_pretrained(model_repo)
    model = AutoModelForSequenceClassification.from_pretrained(model_repo)
    return tokenizer, model

try:
    tokenizer, model = load_model(MODEL_REPO)
except Exception as e:
    st.error(f"โŒ Failed to load model from {MODEL_REPO}")
    st.exception(e)
    st.stop()

# ----------------------------
# 7. Labels and Dummy Inbox
# ----------------------------
LABELS = [
    "Promotions",
    "Spam",
    "Social Media Updates",
    "Forum Updates",
    "Code Verification",
    "Work Updates"
]

dummy_subjects = {
    "Promotions": ["50% OFF Today Only!", "Your Exclusive Coupon Awaits", "Flash Sale on Electronics"],
    "Spam": ["Claim your free reward!", "Win an iPhone 15 now!", "Youโ€™ve been selected!"],
    "Social Media Updates": ["New friend request on Facebook", "Someone mentioned you on Twitter", "New followers on Instagram"],
    "Forum Updates": ["Your Stack Overflow answer received upvotes", "New discussion thread in Data Science Forum", "Python 3.12 update discussion"],
    "Code Verification": ["Your verification code is 482915", "Confirm login attempt", "Verify your new device"],
    "Work Updates": ["Meeting rescheduled for 3 PM", "Project deadline extended", "Client feedback received"]
}

dummy_bodies = {
    "Promotions": "Get up to 70% off on your favorite brands. Offer valid for a limited time only!",
    "Spam": "Click this link to win cash prizes. Limited slots available!",
    "Social Media Updates": "You have new notifications and updates from your social media network.",
    "Forum Updates": "A new reply has been posted to a thread you are following.",
    "Code Verification": "Enter this code in the app to verify your login session.",
    "Work Updates": "Please find attached the meeting notes and next steps for the team."
}

# Generate dummy Gmail inbox
random.seed(42)
inbox_data = []
for _ in range(100):
    label = random.choice(LABELS)
    inbox_data.append({
        "Category": label,
        "Subject": random.choice(dummy_subjects[label]),
        "Body": dummy_bodies[label]
    })
inbox_df = pd.DataFrame(inbox_data)

# ----------------------------
# 8. Classification Function
# ----------------------------
def classify_email(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=1)
        predicted_idx = torch.argmax(probs, dim=1).item()
    confidence = probs[0][predicted_idx].item()
    return LABELS[predicted_idx], confidence, probs[0].numpy()

# ----------------------------
# 9. Gmail-like Layout
# ----------------------------
st.markdown("## ๐Ÿ“ฅ Inbox")
st.markdown("---")
col1, col2, col3 = st.columns([2, 3, 2])

with col1:
    st.markdown("#### ๐Ÿ“ฉ Inbox")
    if not inbox_df.empty:
        # Ensure first email is selected by default
        selected_email = st.radio(
            "Choose an email to view:",
            range(len(inbox_df)),
            format_func=lambda i: inbox_df.iloc[i]["Subject"],
            label_visibility="collapsed",
            index=0  # default to first email
        )
    else:
        st.info("No emails available in the inbox.")
        selected_email = None


with col2:
    st.markdown("#### โœ‰๏ธ Email Details")
    if selected_email is not None:
        selected_row = inbox_df.iloc[selected_email]
        st.markdown(f"**Subject:** {selected_row['Subject']}")
        st.markdown(f"**Body:** {selected_row['Body']}")
    else:
        st.info("๐Ÿ“ฉ Select an email from the inbox to view details.")

with col3:
    st.markdown("#### ๐Ÿ“Š Classification Result")
    if selected_email is not None:
        text = inbox_df.iloc[selected_email]["Subject"] + " " + inbox_df.iloc[selected_email]["Body"]
        predicted_label, confidence, all_probs = classify_email(text)
        st.markdown(f"**Predicted Category:** {predicted_label}")
        st.markdown(f"**Confidence:** {confidence * 100:.2f}%")
        prob_dict = {LABELS[i]: float(all_probs[i]) for i in range(len(LABELS))}
        st.bar_chart(prob_dict)
    else:
        st.warning("Select an email to see classification results.")

# ----------------------------
# 10. Manual Custom Email Input
# ----------------------------
st.markdown("---")
st.subheader("โœ‰๏ธ Enter email text (subject/body) for classification:")
email_text = st.text_area(
    "Enter Email Text Below:",
    placeholder="Example: Your code for verification is 123456 or Meeting scheduled for 3 PM today.",
    height=150
)

if st.button("๐Ÿ” Classify Email"):
    if not email_text.strip():
        st.warning("โš ๏ธ Please enter email text before classifying.")
    else:
        with st.spinner("Classifying..."):
            predicted_label, confidence, all_probs = classify_email(email_text)

        st.markdown("<div class='prediction-card'>", unsafe_allow_html=True)
        st.markdown(f"### ๐Ÿง  Predicted Category: **{predicted_label}**")
        st.markdown(f"**Confidence:** {confidence * 100:.2f}%")
        st.progress(confidence)

        prob_dict = {LABELS[i]: float(all_probs[i]) for i in range(len(LABELS))}
        st.markdown("#### ๐Ÿ“Š Category Probabilities:")
        st.bar_chart(prob_dict)
        st.markdown("</div>", unsafe_allow_html=True)

# ----------------------------
# 11. Footer
# ----------------------------
st.markdown("---")
st.markdown("""
    <p style='text-align: left; color: gray; font-size: 0.9rem'>
    Built for CSC-546: Natural Language Processing (Smart Email Classification Project) | 
    Developed by: Mohammed Golam Kaisar Hossain Bhuyan (hossainbhuyan@cua.edu)
    </p>
""", unsafe_allow_html=True)