Skip to content

kshah2712/sentiment-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’¬ Sentiment Analyzer

Python HuggingFace BERT FastAPI Docker Status

A production-style NLP pipeline that performs sentiment analysis using a fine-tuned BERT model from HuggingFace. Covers transformer architecture, tokenization, fine-tuning on real review data, and serving predictions via a modern FastAPI REST API β€” all containerized with Docker.

Purpose: Demonstrate NLP fundamentals and modern transformer-based approach β€” tokenization, BERT fine-tuning, HuggingFace ecosystem, and building a production NLP API.


πŸ“Œ What This Project Covers

Concept Implementation
NLP Fundamentals Tokenization, text preprocessing, embeddings
Transformers BERT architecture and attention mechanism
Fine-tuning Transfer learning on sentiment dataset
HuggingFace Transformers, Datasets, Trainer API
Model Serving FastAPI with batch prediction support
Containerization Dockerfile + docker-compose
Testing pytest with FastAPI test client

πŸ—‚οΈ Project Structure

sentiment-analyzer/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/                  # Original dataset
β”‚   └── processed/            # Tokenized data
β”œβ”€β”€ notebooks/
β”‚   └── 01_sentiment_eda.ipynb
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ download_data.py      # Download IMDB dataset
β”‚   β”œβ”€β”€ preprocess.py         # Text cleaning + tokenization
β”‚   └── train.py              # Fine-tune BERT model
β”œβ”€β”€ models/                   # Saved model files
β”œβ”€β”€ api/
β”‚   └── app.py                # FastAPI REST API
β”œβ”€β”€ tests/
β”‚   └── test_api.py
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
└── README.md

βš™οΈ Setup & Installation

1. Clone the repository

git clone https://github.com/kshah2712/sentiment-analyzer.git
cd sentiment-analyzer

2. Create virtual environment

conda create -n sentiment python=3.11 -y
conda activate sentiment
pip install -r requirements.txt

3. Download dataset

python src/download_data.py

4. Train the model

python src/train.py

5. Run FastAPI locally

uvicorn api.app:app --reload --port 8000

6. Run with Docker

docker-compose up --build

πŸš€ API Usage

Health check

GET /health

Single prediction

POST /predict
{
  "text": "This movie was absolutely amazing! I loved every minute of it."
}

Response:

{
  "text": "This movie was absolutely amazing!...",
  "sentiment": "POSITIVE",
  "confidence": 0.9987,
  "scores": {
    "positive": 0.9987,
    "negative": 0.0013
  }
}

Batch prediction

POST /predict/batch
{
  "texts": [
    "Great product, highly recommend!",
    "Terrible experience, waste of money."
  ]
}

πŸ“Š Model Results

Metric Score
Accuracy ~93%
F1 Score ~93%
Model distilbert-base-uncased-finetuned-sst-2-english

🧠 Key Concepts

Why BERT over traditional ML?

Traditional ML (TF-IDF + Logistic Regression) treats words as independent tokens. BERT understands context β€” "not good" vs "good" are treated completely differently because BERT reads the full sentence bidirectionally.

Transfer Learning

Instead of training from scratch (needs millions of examples), we use a BERT model pre-trained on billions of words and fine-tune it on our specific task. This gives state-of-the-art results with minimal data and compute.


πŸ§ͺ Running Tests

pytest tests/ -v

πŸ› οΈ Tech Stack

  • Language: Python 3.11
  • NLP: HuggingFace Transformers, BERT/DistilBERT
  • Deep Learning: PyTorch
  • API: FastAPI + Uvicorn
  • Containerization: Docker, Docker Compose
  • Testing: pytest

πŸ“š Key Learnings

  • Tokenization β€” how BERT converts text to numbers
  • Attention mechanism β€” how transformers understand context
  • Transfer learning β€” fine-tuning pretrained models
  • HuggingFace pipeline β€” industry standard for NLP
  • Batch prediction β€” serving multiple inputs efficiently

πŸ—ΊοΈ Part of ML Learning Roadmap

This is Project 5 of 10 in a progressive ML + GenAI portfolio:

# Project Skills
βœ… 1 Classic ML Pipeline EDA, Sklearn, Flask, Docker
βœ… 2 House Price Predictor Regression, Feature Eng., Streamlit
βœ… 3 Customer Churn Classifier SHAP, FastAPI, Imbalanced data
βœ… 5 Sentiment Analyzer (this project) HuggingFace, BERT, NLP, Transformers
8 RAG Chatbot LangChain, Vector DB, GenAI
... ... ...

πŸ‘€ Author

Kashyap Shah GitHub

Releases

No releases published

Packages

 
 
 

Contributors