Real-time sentiment classification powered by DistilBERT, deployed as a serverless API on AWS Lambda with API Gateway.
┌──────────────┐ ┌───────────────────────────────────────────┐ ┌──────────────┐
│ Client │────▶│ AWS API Gateway │────▶│ Response │
│ (POST JSON) │ │ │ │ POSITIVE 0.99│
└──────────────┘ └───────────────┬───────────────────────────┘ └──────────────┘
│
┌───────────────▼───────────────┐
│ AWS Lambda │
│ ┌─────────────────────────┐ │
│ │ Mangum (ASGI adapter) │ │
│ └────────────┬────────────┘ │
│ ┌────────────▼────────────┐ │
│ │ FastAPI Application │ │
│ └────────────┬────────────┘ │
│ ┌────────────▼────────────┐ │
│ │ DistilBERT (SST-2) │ │
│ │ Sentiment Pipeline │ │
│ └─────────────────────────┘ │
└────────────────────────────────┘
-
Model — Uses
distilbert-base-uncased-finetuned-sst-2-english, a DistilBERT model fine-tuned on the Stanford Sentiment Treebank (SST-2) dataset for binary sentiment classification (POSITIVE/NEGATIVE) -
Serving — FastAPI handles HTTP routing and request validation. Mangum bridges FastAPI's ASGI interface to AWS Lambda's event-based invocation model
-
Cold Start Optimization — The model weights (~260 MB) are downloaded and baked into the Docker image at build time via
get_model.py, so Lambda doesn't need to download them on cold start -
Infrastructure — AWS SAM (Serverless Application Model) defines the Lambda function and API Gateway as infrastructure-as-code in
template.yaml
Request:
{
"comments": [
"This product is absolutely amazing!",
"Terrible experience, would not recommend.",
"It was okay, nothing special."
]
}Response:
{
"result": [
{"label": "POSITIVE", "score": 0.9998},
{"label": "NEGATIVE", "score": 0.9995},
{"label": "NEGATIVE", "score": 0.9124}
]
}Health check endpoint. Returns {"status": "ok"}.
| Component | Technology |
|---|---|
| ML Model | DistilBERT (Hugging Face Transformers) |
| API Framework | FastAPI |
| Lambda Adapter | Mangum |
| Infrastructure | AWS SAM + API Gateway + Lambda |
| Container | Docker (AWS Lambda Python base) |
- AWS CLI configured with credentials
- AWS SAM CLI
- Docker
# Build the application
sam build
# Deploy (guided first time)
sam deploy --guided
# Subsequent deployments
sam deploy# Install dependencies
pip install -r requirements.txt
# Download the model
python get_model.py
# Run locally with Uvicorn
uvicorn app:app --reload --host 0.0.0.0 --port 8000Interactive docs: http://localhost:8000/docs
# Invoke with SAM
sam local invoke SentimentFunction -e events/event.json
# Start local API Gateway
sam local start-apiteslabot/
├── app.py # FastAPI application + Lambda handler
├── get_model.py # Model download script (runs at build time)
├── events/
│ └── event.json # Sample Lambda test event
├── template.yaml # AWS SAM infrastructure definition
├── Dockerfile # Lambda-compatible container image
├── requirements.txt
├── LICENSE
├── .gitignore
└── README.md
The SAM template (template.yaml) provisions:
| Resource | Configuration |
|---|---|
| Lambda Function | 1024 MB memory, 600s timeout, Docker packaged |
| API Gateway | REST API with catch-all proxy routing |
| IAM Role | Auto-generated execution role |
MIT