North-Seattle-College / ad440-winter2022-thursday-repo

North Seattle College AD 440 Winter 2022 Cloud Practicum class repoitory
Apache License 2.0
2 stars 7 forks source link

Expose the emotion ML model via API from Sagemaker #90

Open toddysm opened 2 years ago

toddysm commented 2 years ago

This is related to the following two tasks #46 and #16 We need only the part that determines if the sentence has emotion and determines that emotion. This functionality needs to be exposed via API that we can call.

Time estimates: 8-10 hours

eknigge commented 2 years ago

I'm stuck. I was able to create an API gateway and connect a lambda function to it. I tested this test implementation and was able to get the expected response. So far so good. Next, I went to update the lambda function with the nltk library. This proved to be a little more complicated, since on Lambda you need to include the packages with the function you run. To do that I did the following:

  1. Make sure to install the packages while running linux, or if on windows, using WSL. If you install packages under windows they will not run on Lambda since it is a linux environment. The workaround for this is to download the wheel/.whl files and unpackage them. The .whl files are compiled libraries for specific platforms.
  2. Package all the supporting libraries into a single .zip file. AWS has a good tutorial for this. https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
  3. Append the lambda_function.py file, or whatever you called your script to the zip file. This ensures that when it executes it has the necessary libraries in the same directory.
  4. Upload your code to Lambda as a .zip file and then execute.

I keep getting an error with importing the regex library. I've search and tried every workaround I could find. Any help is appreciated.

Error log message

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'regex._regex'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "2bd8bb0d-e6e4-4ec5-9d66-3530024288c8",
  "stackTrace": []
}
eknigge commented 2 years ago

Time estimate: 10-20hrs Time spent as of 2/22: 10 hrs

eknigge commented 2 years ago

AWS Lambda With Dependencies

Documenting so that I remember what I did, and perhaps to help others.

Deploying a function to AWS Lambda is a little more involved than you may expect, especially if you require non-standard libraries. To ensure that the code that runs locally will work on AWS Lambda, it can be necessary to download the compiled binaries for linux.

Download dependencies

Create a new folder for the binaries and navigate to that directory

mkdir packages
cd packages/

Download the nltk binaries

pip download --platform manylinux1_x86_64 --only-binary=:all: --no-binary=:none: nltk

Unzip all the files

unzip '*.whl'

Remove the .whl files

rm *.whl

Build the .zip file

To run on AWS Lambda all of the package files and script need to be in a single .zip file. Build this as follows.

Package all the dependencies.

zip -r ../[archive_name.zip] .

Navigate back to the project root directory and append the function to the archive

cd ..
zip -g [archive_name.zip] [function_name]

Now the [archive_name.zip] is ready to upload as a Lambda function.