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.
| 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 |
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
git clone https://github.com/kshah2712/sentiment-analyzer.git
cd sentiment-analyzerconda create -n sentiment python=3.11 -y
conda activate sentiment
pip install -r requirements.txtpython src/download_data.pypython src/train.pyuvicorn api.app:app --reload --port 8000docker-compose up --buildGET /healthPOST /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
}
}POST /predict/batch
{
"texts": [
"Great product, highly recommend!",
"Terrible experience, waste of money."
]
}| Metric | Score |
|---|---|
| Accuracy | ~93% |
| F1 Score | ~93% |
| Model | distilbert-base-uncased-finetuned-sst-2-english |
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.
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.
pytest tests/ -v- Language: Python 3.11
- NLP: HuggingFace Transformers, BERT/DistilBERT
- Deep Learning: PyTorch
- API: FastAPI + Uvicorn
- Containerization: Docker, Docker Compose
- Testing: pytest
- 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
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 |
| ... | ... | ... |
Kashyap Shah GitHub