A production-ready text classification API built with FastAPI, featuring both classical (TF-IDF + Logistic Regression) and modern (DistilBERT) approaches. Classifies news articles into 4 categories: World, Sports, Business, Sci/Tech.
- Dual Models: Baseline (TF-IDF + LogReg) and Transformer (DistilBERT) implementations
- FastAPI Backend: Async, auto-documented API with Pydantic validation
- Docker Ready: Containerized for easy deployment
- MLOps Ready: Linting (Ruff), testing (Pytest), CI/CD (GitHub Actions)
- High Performance: ~91% accuracy on AG News test set
- Python 3.11+
- Docker (for containerized deployment)
-
Clone & Setup:
git clone https://github.com/tracy-ml/m2-text-classifier-api.git cd m2-text-classifier-api conda env create -f environment.yml conda activate m2 -
Run API:
PYTHONPATH=. uvicorn src.api.main:app --reload --host 0.0.0.0 --port 8000
-
Test:
curl -X POST "http://localhost:8000/predict" \ -H "Content-Type: application/json" \ -d '{"text": "Apple announces new iPhone"}'
docker build -t m2-text-classifier-api .
docker run -p 8000:8000 m2-text-classifier-apiGET /health- Health checkPOST /predict- Classify text
{
"text": "Your news article text here"
}{
"label": "Sci/Tech",
"probabilities": {
"World": 0.02,
"Sports": 0.01,
"Business": 0.15,
"Sci/Tech": 0.82
},
"text": "Your news article text here"
}Visit http://localhost:8000/docs for Swagger UI with live testing.
# Baseline model
python src/ml/train_baseline.py
# Transformer model
python src/ml/train_transformer.py# Linting
ruff check src tests
# Unit tests
PYTHONPATH=. pytest tests/m2-text-classifier-api/
├── src/
│ ├── api/
│ │ └── main.py # FastAPI app
│ └── ml/
│ ├── train_baseline.py # TF-IDF + LogReg
│ └── train_transformer.py # DistilBERT
├── models/ # Saved models
├── tests/ # Pytest tests
├── .github/workflows/ # CI/CD
├── Dockerfile # Container config
└── requirements.txt # Dependencies
docker build -t m2-text-classifier-api .
docker run -p 8000:8000 m2-text-classifier-api- Heroku:
git push heroku main - AWS/GCP: Use ECS/Cloud Run with the Docker image
- Railway/Vercel: Connect GitHub repo for auto-deployment
- Fork the repository
- Create a feature branch
- Make changes with tests
- Run
ruff checkandpytest - Submit a PR
MIT License - see LICENSE file for details.
- AG News dataset from Hugging Face
- DistilBERT model from Hugging Face Transformers
- FastAPI framework