Skip to content

Latest commit

Β 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🚫 Spam Message Classifier

Python 3.8+ HuggingFace

A state-of-the-art spam message classifier built with RoBERTa transformer model, fine-tuned on multiple SMS spam datasets. This model achieves exceptional performance with 0.9782 F1-score for spam detection and 99.41% overall accuracy, making it ideal for real-world deployment in messaging platforms and content moderation systems.

🎯 Overview

This project develops an intelligent spam detection system using advanced natural language processing techniques. The classifier is designed to accurately distinguish between legitimate messages (ham) and spam content, with a focus on minimizing both false positives and false negatives.

Key Features

  • πŸ€– Transformer-based Architecture: Built on RoBERTa-base for superior text understanding
  • ⚑ High Performance: 0.9782 F1-score for spam detection, 99.41% overall accuracy
  • πŸ”§ Hyperparameter Optimization: Automated tuning using Optuna framework (25 trials)
  • βš–οΈ Class Imbalance Handling: Weighted loss function for optimal training
  • πŸ”— URL Bias Mitigation: Enhanced with real-world ham messages containing links
  • πŸ“Š Comprehensive Evaluation: Multiple metrics including precision, recall, and confusion matrix
  • πŸš€ Production-Ready: Saved in HuggingFace format for easy deployment

πŸ“Š Model Performance

Final Results on Test Set:

  • Overall Accuracy: 99.41%
  • Weighted F1-Score: 0.9941
  • Spam F1-Score: 0.9782
  • Spam Precision: 96.55%
  • Spam Recall: 99.12%
  • Ham Precision: 99.86%
  • Ham Recall: 99.45%

Acceptance Criteria

βœ… Model Accepted: The F1-score for spam class (0.9782) significantly exceeds our predefined acceptance threshold of 0.95, indicating exceptional performance for real-world deployment.

Generalizability

πŸ“Š Strong Generalization: All performance metrics are evaluated on a completely unseen test set (15% of data) that was never used during training or hyperparameter tuning, ensuring robust real-world performance and preventing overfitting.

πŸ”— Handling <URL> Bias in Dataset

During initial training, the model became overconfident and labeled almost all messages containing <URL> as spam, even if some were legitimate ham. To mitigate this bias, I went through Discord servers and collected additional real ham messages containing links.

This helps the model understand that URLs can appear in non-spam messages and improves generalization for real-world inference, particularly important for Discord bot deployment where legitimate messages often contain links.

πŸ—οΈ Architecture & Methodology

Model Architecture

  • Base Model: FacebookAI/roberta-base
  • Task: Binary sequence classification (ham vs spam)
  • Fine-tuning: Custom classification head with 2 output labels
  • Tokenization: RoBERTa tokenizer with optimal sequence length

Training Strategy

  1. Data Preprocessing: SMS text cleaning and label encoding
  2. Tokenization: Dynamic padding with maximum sequence length of 128 tokens
  3. Class Balancing: Weighted loss function to handle imbalanced dataset
  4. Hyperparameter Optimization: Optuna-based automated tuning
  5. Evaluation: Comprehensive metrics on held-out test set

Hyperparameter Optimization

Used Optuna framework to optimize (25 trials):

  • Dropout rates: Hidden dropout (0.1-0.3), Attention dropout (0.1-0.2)
  • Learning rate: 1e-5 to 5e-5 range
  • Weight decay: 0.0 to 0.1 regularization
  • Batch size: 8, 16, or 32 samples
  • Gradient accumulation steps: 1 to 4
  • Training epochs: 2 to 5 epochs
  • Warmup ratio: 0.05 to 0.1 for learning rate scheduling

Best Parameters Found (Trial 6/25):

  • Hidden dropout: 0.10069482002001506
  • Attention dropout: 0.12460257350587067
  • Learning rate: 4.976184540342024e-05
  • Weight decay: 0.04490021845024478
  • Batch size: 16
  • Gradient accumulation steps: 4
  • Epochs: 4
  • Warmup ratio: 0.07622459860163384

πŸ“ Project Structure

spam-message-classifier/
β”œβ”€β”€ data/
β”‚   └── sms_spam_uci.csv
β”‚   └── url_dataset.csv
β”œβ”€β”€ notebooks/
β”‚   └── spam-message-classifier.ipynb    # Complete development notebook
β”œβ”€β”€ README.md                           # This file
└── .gitignore                         # Git ignore rules

Note: The trained model and tokenizer can be found on HuggingFace at roshana1s/spam-message-classifier

πŸš€ Quick Start

Installation

Install required packages:

pip install transformers torch

Usage

Load and use the trained model from HuggingFace:

from transformers import RobertaTokenizer, RobertaForSequenceClassification
import torch

# Load the trained model and tokenizer from HuggingFace
model = RobertaForSequenceClassification.from_pretrained("roshana1s/spam-message-classifier")
tokenizer = RobertaTokenizer.from_pretrained("roshana1s/spam-message-classifier")

πŸ“– Dataset

Sources:

  1. SMS Spam Collection Dataset from UCI Machine Learning Repository
  2. Discord Text Messages β€” a manually collected dataset of real Discord messages containing both ham and spam samples. (This dataset was created to mitigate <URL> bias.)

Preprocessing Steps:

  1. Label encoding (ham β†’ 0, spam β†’ 1)
  2. Text cleaning and normalization with Discord-specific preprocessing
  3. Train/validation/test split (70/15/15)
  4. Tokenization with RoBERTa tokenizer
  5. Dynamic padding and truncation

πŸ› οΈ Technical Implementation

Key Technologies

  • πŸ€— Transformers: HuggingFace transformers library
  • πŸ”₯ PyTorch: Deep learning framework
  • πŸ“Š Scikit-learn: Evaluation metrics and preprocessing
  • 🎯 Optuna: Hyperparameter optimization
  • πŸ“ˆ Matplotlib/Seaborn: Data visualization
  • 🐼 Pandas: Data manipulation

Custom Features

  • Weighted Loss Function: Handles class imbalance effectively
  • Label Smoothing: 0.1 to prevent overconfidence
  • Custom Metrics: Specialized spam detection metrics
  • Confusion Matrix Analysis: Detailed error analysis
  • Class-specific Performance: Separate metrics for ham and spam

πŸ“Š Detailed Results

Confusion Matrix

Predicted Ham Predicted Spam
Actual Ham 725 4
Actual Spam 1 112

Performance Breakdown

  • True Positives (Spam correctly identified): 112
  • True Negatives (Ham correctly identified): 725
  • False Positives (Ham incorrectly flagged): 4
  • False Negatives (Spam missed): 1

🎯 Use Cases

This spam classifier is ideal for:

πŸ’¬ Messaging Platforms

  • Discord bot moderation (Primary use case)
  • SMS filtering systems
  • Chat application content filtering

πŸ›‘οΈ Content Moderation

  • Social media platforms
  • Comment section filtering
  • User-generated content screening

πŸ”„ Model Deployment

HuggingFace

The trained model is available on HuggingFace: πŸ‘‰ roshana1s/spam-message-classifier

Integration with Amy Discord Bot

This model serves as the core spam detection component for Amy, an intelligent Discord moderation bot that:

  • Detects spam messages in real-time
  • Provides automated content moderation
  • Maintains server quality and user experience

⭐ If you found this project helpful, please consider giving it a star! ⭐

About

A state-of-the-art spam message classifier built with RoBERTa transformer model, fine-tuned on multiple SMS spam datasets.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages