Skip to content

artemiorimando/teslabot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless Sentiment Analysis API

Real-time sentiment classification powered by DistilBERT, deployed as a serverless API on AWS Lambda with API Gateway.

Architecture

┌──────────────┐     ┌───────────────────────────────────────────┐     ┌──────────────┐
│   Client      │────▶│            AWS API Gateway                │────▶│  Response     │
│  (POST JSON)  │     │                                           │     │  POSITIVE 0.99│
└──────────────┘     └───────────────┬───────────────────────────┘     └──────────────┘
                                     │
                     ┌───────────────▼───────────────┐
                     │        AWS Lambda              │
                     │   ┌─────────────────────────┐  │
                     │   │  Mangum (ASGI adapter)   │  │
                     │   └────────────┬────────────┘  │
                     │   ┌────────────▼────────────┐  │
                     │   │  FastAPI Application     │  │
                     │   └────────────┬────────────┘  │
                     │   ┌────────────▼────────────┐  │
                     │   │  DistilBERT (SST-2)     │  │
                     │   │  Sentiment Pipeline      │  │
                     │   └─────────────────────────┘  │
                     └────────────────────────────────┘

How It Works

  1. 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)

  2. Serving — FastAPI handles HTTP routing and request validation. Mangum bridges FastAPI's ASGI interface to AWS Lambda's event-based invocation model

  3. 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

  4. Infrastructure — AWS SAM (Serverless Application Model) defines the Lambda function and API Gateway as infrastructure-as-code in template.yaml

API Reference

POST /sentiment

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}
  ]
}

GET /

Health check endpoint. Returns {"status": "ok"}.

Tech Stack

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)

Deployment

Prerequisites

Deploy to AWS

# Build the application
sam build

# Deploy (guided first time)
sam deploy --guided

# Subsequent deployments
sam deploy

Local Development

# 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 8000

Interactive docs: http://localhost:8000/docs

Local Lambda Testing

# Invoke with SAM
sam local invoke SentimentFunction -e events/event.json

# Start local API Gateway
sam local start-api

Project Structure

teslabot/
├── 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

Infrastructure

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

License

MIT

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors