Code Monkey home page Code Monkey logo

mp10's Introduction

Deploying Hugging Face Transformers model on AWS Lambda with Docker containers

Steps for implementation:

Ensure you have Python 3.11 installed.

Install Required Libraries

  1. Create a virtual environment: python -m venv venv
  2. Activate it:
    • On Windows: venv\Scripts\activate
    • On macOS/Linux: source venv/bin/activate
  3. Install libraries with pip: pip install transformers torch

Download Model and Tokenizer

  1. Navigate to the lambda-service folder.
  2. Create a Python script, e.g., download_model.py.
  3. Copy and paste the provided code into the script.
  4. Execute the script: python download_model.py

Why Download Directly into Docker?

To overcome Lambda environment constraints.

Add requirements.txt

Create a file named requirements.txt and list the required libraries:

torch
transformers

Dockerfile

Use a Dockerfile to define the Docker image setup:

# Dockerfile
FROM public.ecr.aws/lambda/python:3.11

COPY requirements.txt ./
RUN python3 -m pip install -r requirements.txt --target ${LAMBDA_TASK_ROOT}

COPY ./ ${LAMBDA_TASK_ROOT}/

CMD [ "handler.handler" ]

Build the Docker Image

Build the Docker image using the following command:

docker build -t sentiment-analyzer:latest .

Run the Docker Image

Run the Docker image using the following command:

docker run -p 9000:8080 sentiment-analyzer:latest

Invoke Lambda Function Locally

Test the function with curl:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"text": "I love AI"}'

mp10's People

Contributors

kvanme01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.