Skip to content

satyajitghana/lambda-onnx-fastapi-fasthtml

Repository files navigation

Cat vs Dog Classifier Lambda Deployment w/ ONNX, FastAPI & FastHTML

This project deploys a FastAPI application that classifies images as either cats or dogs using a deep learning model. The application is containerized and deployed to AWS Lambda.

deployment

Prerequisites

AWS Setup

  1. Install AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
  1. Create an IAM User:
    • Go to AWS Console > IAM > Users > Create User
    • Create a new policy with the following permissions:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*",
                "ecr:*",
                "ssm:*",
                "s3:*",
                "iam:*"
            ],
            "Resource": "*"
        }
    ]
}
  • Attach the policy to your user
  • Create access keys for the user
  1. Configure AWS CLI:
aws configure

Enter your AWS access key ID, secret access key, default region, and output format.

Install Node.js and CDK

  1. Install Node.js (if not already installed)
  2. Install AWS CDK:
npm install -g aws-cdk

Project Setup

  1. Install development requirements:
pip install -r requirements-dev.txt
  1. Convert PyTorch model to ONNX:
# Place your PyTorch model (model.pt) in the project root
python export_onnx.py

This will create model.onnx in your project directory.

  1. Install application requirements:
pip install -r requirements.txt

Local Testing

Run the FastAPI application locally:

uvicorn app:app --reload

Visit http://localhost:8000 to test the application.

or use the client to test the application:

python client.py

Docker Build

Build the Docker image locally:

docker build -t cat-dog-classifier .

Test the Docker container:

docker run -p 8000:8000 cat-dog-classifier

AWS Deployment

  1. Bootstrap CDK (first time only):
cdk bootstrap
  1. Deploy the application:
cdk deploy

After deployment, CDK will output the Lambda function URL. You can use this URL to access your application.

Cleanup

To remove the deployed resources:

cdk destroy

Troubleshooting

  1. If cdk bootstrap fails, verify your AWS credentials and permissions
  2. If the Docker build fails, ensure all required files are in place
  3. If the Lambda deployment fails, check CloudWatch logs for details

Notes

  • The Lambda function is configured with 8GB of memory and a 5-minute timeout
  • The endpoint is public (no authentication required)
  • The model expects images to be resized to 160x160 pixels

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published