Skip to content

Fine-tuned a pretrained BERT model on IMDb reviews to classify movie sentiment with 90%+ accuracy; built using Hugging Face Transformers and PyTorch.

License

Notifications You must be signed in to change notification settings

hurkanugur/BERT-Sentiment-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Sentiment Classification using Transformers (DistilBERT)

📖 Overview

This project performs sentiment analysis, classifying IMDB movie reviews as positive or negative using transfer learning with a pretrained DistilBERT model from the Hugging Face Transformers library.

  • ⚙️ Pretrained DistilBERT backbone — a lightweight version of BERT for faster inference
  • 🎯 Binary classification head for sentiment prediction
  • 📉 Cross-Entropy Loss and AdamW optimizer with learning rate scheduling
  • 🧠 Configurable fine-tuning — control how much of DistilBERT to train
  • 🧱 Modular architecture — clean separation of model, training, and data logic
  • 📊 Real-time training & validation loss visualization
  • 🌐 Gradio-powered web interface for live inference demos

🧩 Libraries

  • PyTorch – model, training, and inference
  • Transformers – pretrained BERT models and tokenizer
  • pandas, numpy – data handling
  • torch.utils.data – dataset and dataloader
  • matplotlib – training/validation loss visualization
  • Gradio — interactive web interface for real-time model demos

🖼️ Application Screenshot

Below is a preview of the Gradio Interface used for real-time classification:

Application Screenshot


⚙️ Requirements

  • Python 3.13+
  • Recommended editor: VS Code

📦 Installation

  • Clone the repository
git clone https://github.com/hurkanugur/BERT-Sentiment-Classifier.git
  • Navigate to the BERT-Sentiment-Classifier directory
cd BERT-Sentiment-Classifier
  • Install dependencies
pip install -r requirements.txt

🔧 Setup Python Environment in VS Code

  1. View → Command Palette → Python: Create Environment
  2. Choose Venv and your Python version
  3. Select requirements.txt to install dependencies
  4. Click OK

📂 Project Structure

assets/
└── app_screenshot.png                # Screenshot of the application

data/
└── huggingface_datasets              # HuggingFace datasets (IMDB)

model/
└── imdb_sentiment_classifier.pth     # Trained model

src/
├── config.py                         # Paths, hyperparameters, split ratios
├── dataset.py                        # Data loading & preprocessing
├── device_manager.py                 # Selects and manages compute device
├── train.py                          # Training pipeline
├── inference.py                      # Inference pipeline
├── model.py                          # Neural network definition
└── visualize.py                      # Training/validation plots

main/
├── main_train.py                     # Entry point for training
└── main_inference.py                 # Entry point for inference

requirements.txt                      # Python dependencies

📂 Model Architecture

Pretrained Transformer (distilbert-base-uncased)
  ↓
[CLS] Token Representation
  ↓
Classification Head (added automatically)
  → Linear Layer (hidden_size → 2)
  → Softmax (for binary classification)

📂 Train the Model

Navigate to the project directory:

cd BERT-Sentiment-Classifier

Run the training script:

python -m main.main_train

or

python3 -m main.main_train

📂 Run Inference / Make Predictions

Navigate to the project directory:

cd BERT-Sentiment-Classifier

Run the app:

python -m main.main_inference

or

python3 -m main.main_inference

Releases

No releases published

Packages

No packages published

Languages