Spam Email Detection Project

Github Repo of Project :https://github.com/Scikitlearnalchemists/Project1-Email-Spam-Detction

๐Ÿš€ Features

  • ๐Ÿ” Secure Gmail OAuth Authentication
  • ๐Ÿ“ฅ Fetch latest emails directly from Gmail
  • ๐Ÿ“จ Analyze both Subject and Email Body
  • ๐Ÿค– Machine Learning-based Spam Detection
  • ๐Ÿ“Š Confidence Score for every prediction
  • โœ Manual Email Prediction
  • ๐Ÿ” Search scanned emails by subject
  • ๐ŸŒ™ Dark/Light Theme Support
  • โšก Scan up to 100 recent Gmail emails
  • ๐Ÿ–ฅ User-friendly Flask Dashboard

๐Ÿ›  Tech Stack

Backend

  • Python
  • Flask

Machine Learning

  • Scikit-learn
  • Pandas
  • NumPy
  • NLTK

Frontend

  • HTML
  • CSS
  • JavaScript
  • Bootstrap

APIs

  • Gmail API
  • Google OAuth 2.0

๐Ÿง  Machine Learning Model

The spam detection model uses:

  • TF-IDF Vectorization
  • Random Forest

The model is trained on a labeled Spam/Ham email dataset and predicts whether an email is Spam or Ham along with a confidence score.

1. Clone the Repository

git clone https://github.com/iadityamishra159/Gmail-Spam-Detection.git

cd Gmail-Spam-Detection

2. Create Virtual Environment

Windows

python -m venv venv
venv\Scripts\activate

Linux / macOS

python3 -m venv venv

source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Download NLTK Resources
python download_nltk.py

๐Ÿ”‘ Gmail API Setup

This repository does not include Google OAuth credentials for security reasons.

You must create your own Google Cloud Project.

Step 1

Go to https://console.cloud.google.com/

Step 2

go to select project and create a new project

Step 3

Enable the Gmail API.

APIs & Services
โ†“
Library
โ†“
Search “Gmail API”
โ†“
Enable

Step 4

Configure the OAuth Consent Screen.

Create an OAuth Client ID.

Credentials
โ†“
Create Credentials
โ†“
OAuth Client ID

Step 6

Download the credentials file.

Rename it as

credentials.json

and place it inside the project root.

Step 7

Dataset

The original dataset used to train this project is not included in this repository because of its large size.

Download it from:

Dataset Link: https://www.kaggle.com/datasets/nikhilchaudhary7108/spam-email-datasetenron-trec-and-others?select=email_dataset_classification.csv

After downloading, place the files inside the dataset/ folder:

dataset/
โ””โ”€โ”€ email_dataset_classification.csv

Step 8

Train the Model

Before running the application, train the model by executing:

python train_model.py

This will automatically generate:

You may be got some error in this you have to resolve this by your self its came because of different version or Os system

model/spam_model.pkl model/tfidf_vectorizer.pkl

Step 9

Run the application.

python app.py

The browser will automatically ask for Gmail authorization during the first run.