A comprehensive NLP and Computer Vision project portfolio showcasing multiple machine learning techniques for toxic comment detection and image generation.
This repository contains two main projects:
- Multi-label Toxic Comment Classification: A comprehensive NLP project comparing multiple techniques (TF-IDF+SVC, BiLSTM, DistilBERT) for detecting toxic content in online comments across 6 categories.
- Image Generation with GANs: A generative adversarial network implementation for creating realistic handwritten digits using the MNIST dataset.
A multi-label classification system that identifies toxic comments across six categories:
toxicsevere_toxicobscenethreatinsultidentity_hate
1. EDA & Preprocessing (EDA.ipynb)
- Exploratory data analysis of the Kaggle Jigsaw Toxic Comment Classification dataset
- Text cleaning and normalization
- Label distribution analysis
- Stratified train/validation/test split (80/10/10)
- Feature engineering and statistics
2. Model Implementations
-
BiLSTM Baseline (
BiLSTM Model.ipynb)- Bidirectional LSTM with word-level embeddings
- Mean + max pooling for sentence representation
- Multi-label classification with BCE loss and class weighting
- Per-label threshold tuning for optimal F1 scores
-
DistilBERT (Final Model) (
DistillBERT_ChosenTechnique.ipynb)- Fine-tuned DistilBERT transformer model
- Subword tokenization for robust handling of slang/typos
- Contextual understanding through self-attention mechanisms
- Interactive Gradio interface for end-user demonstration
- Comprehensive evaluation metrics
- Class Imbalance Handling: Uses
pos_weightin BCE loss to handle rare labels - Threshold Optimization: Per-label threshold tuning on validation set
- Comprehensive Metrics: Micro-F1, Macro-F1, PR-AUC, Hamming Accuracy
- Reproducibility: Fixed seeds and consistent train/val/test splits
- Production-Ready Interface: Gradio demo for real-time inference
| Model | Micro-F1 | Macro-F1 | Mean PR-AUC | Hamming Accuracy |
|---|---|---|---|---|
| DistilBERT | 0.789 | 0.694 | 0.741 | 0.984 |
| BiLSTM | 0.674 | 0.567 | 0.555 | 0.976 |
DistilBERT was selected as the final model for its superior performance on rare labels and better contextual understanding.
A Generative Adversarial Network implementation for generating realistic handwritten digits from the MNIST dataset.
-
Generator: Maps random noise (100-dim) → 28×28 grayscale images
- Fully connected layers with ReLU activations
- Tanh output for normalized pixel values [-1, 1]
-
Discriminator: Binary classifier for real vs. generated images
- Deep feedforward network with dropout regularization
- Sigmoid output for probability scores
- Adversarial training with alternating updates
- Binary cross-entropy loss for both networks
- Adam optimizer (lr=0.0001)
- 30 epochs on MNIST training set
- PyTorch: Deep learning framework
- Transformers (Hugging Face): Pre-trained DistilBERT model
- scikit-learn: Data splitting, metrics, and baseline models
- pandas & numpy: Data manipulation and analysis
- matplotlib & seaborn: Visualization
- Gradio: Interactive web interface
- PyTorch: Neural network implementation
- torchvision: MNIST dataset loading and transforms
- matplotlib: Visualization of generated images
.
├── EDA.ipynb # Exploratory data analysis and preprocessing
├── BiLSTM Model.ipynb # BiLSTM baseline implementation
├── DistillBERT_ChosenTechnique.ipynb # DistilBERT final model with Gradio interface
├── ImageGeneration.ipynb # GAN-based image generation
└── README.md # This file
- Successfully handles multi-label classification with 6 toxicity categories
- Achieves 98.4% Hamming accuracy with DistilBERT
- Strong performance on rare labels (threat, identity_hate) through class weighting
- Interactive demo interface for real-time classification
- Generator learns to produce realistic MNIST-like digits
- Adversarial training achieves equilibrium between generator and discriminator
- Generated images demonstrate learned distribution of handwritten digits
pip install torch torchvision transformers
pip install pandas numpy scikit-learn matplotlib seaborn
pip install gradioThe lightweight CI suite exercises the tokenisation, vocabulary, encoding, text-cleaning, sigmoid, and threshold-tuning functions directly from the committed notebooks. It does not download the training dataset or model weights.
python -m pip install -r requirements-ci.txt
ruff check tests
mypy tests/notebook_loader.py
PYTHONPATH=tests python -m unittest discover -s tests
jupyter nbconvert --to html --ClearOutputPreprocessor.enabled=True \
--output-dir /tmp/notebook-build *.ipynbFor the toxic comment classification project:
- Download the Kaggle Jigsaw Toxic Comment Classification Challenge dataset
- Place
train.csvin the working directory
For the GAN project:
- MNIST dataset is automatically downloaded by PyTorch
- Run
EDA.ipynbto preprocess the data and create train/val/test splits - Run
BiLSTM Model.ipynbto train the baseline model - Run
DistillBERT_ChosenTechnique.ipynbto train the final model and launch the Gradio interface
- Run
ImageGeneration.ipynbto train the GAN and generate images - Training takes approximately 30 epochs
- Generated images are displayed in a 4×4 grid
- All notebooks are designed to run on Google Colab with GPU support
- Model artifacts and metrics are saved for reproducibility
- Content warning: The toxic comment dataset contains offensive language
- Seeds are fixed for reproducible results across all experiments
This project is for educational and portfolio purposes.
Author: Luke Nguyen Repository: https://github.com/tuilachit/ToxicComment_NLP_Project