Ensure you have Python 3.11 installed.
- Create a virtual environment:
python -m venv venv
- Activate it:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
- Install libraries with pip:
pip install transformers torch
- Navigate to the
lambda-service
folder. - Create a Python script, e.g.,
download_model.py
. - Copy and paste the provided code into the script.
- Execute the script:
python download_model.py
To overcome Lambda environment constraints.
Create a file named requirements.txt
and list the required libraries:
torch
transformers
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 using the following command:
docker build -t sentiment-analyzer:latest .
Run the Docker image using the following command:
docker run -p 9000:8080 sentiment-analyzer:latest
Test the function with curl:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"text": "I love AI"}'