All Projects โ†’ aws-samples โ†’ zero-administration-inference-with-aws-lambda-for-hugging-face

aws-samples / zero-administration-inference-with-aws-lambda-for-hugging-face

Licence: other
Zero administration inference with AWS Lambda for ๐Ÿค—

Programming Languages

python
139335 projects - #7 most used programming language
Dockerfile
14818 projects

Projects that are alternatives of or similar to zero-administration-inference-with-aws-lambda-for-hugging-face

TabFormer
Code & Data for "Tabular Transformers for Modeling Multivariate Time Series" (ICASSP, 2021)
Stars: โœญ 209 (+1000%)
Mutual labels:  transformer, huggingface
Neural-Scam-Artist
Web Scraping, Document Deduplication & GPT-2 Fine-tuning with a newly created scam dataset.
Stars: โœญ 18 (-5.26%)
Mutual labels:  transformer, huggingface
Transformer Temporal Tagger
Code and data form the paper BERT Got a Date: Introducing Transformers to Temporal Tagging
Stars: โœญ 55 (+189.47%)
Mutual labels:  transformer, huggingface
terraform-aws-efs-backup
Terraform module designed to easily backup EFS filesystems to S3 using DataPipeline
Stars: โœญ 40 (+110.53%)
Mutual labels:  lambda, efs
lambda-launcher
Application launcher in haskell. Mostly Just For Fun.
Stars: โœญ 67 (+252.63%)
Mutual labels:  lambda
pinboard-backup
This backs up Pinboard bookmarks to DynamoDB.
Stars: โœญ 17 (-10.53%)
Mutual labels:  lambda
super-serverless-sample
Backend serverless que simula o sistema de votaรงรฃo do BBB
Stars: โœญ 30 (+57.89%)
Mutual labels:  lambda
linqjs
use linq and lambda in javascript on es6, can use linq function in an Object or an Array or a String value | ไธ€ไธชๆ–นไพฟๅฏนๆ•ฐ็ป„ใ€ๅญ—ๅ…ธใ€ๆ ‘ๅฝขๆ•ฐๆฎ่ฟ›่กŒๆ“ไฝœใ€็ญ›้€‰็ญ‰ๆ“ไฝœ็š„ๅทฅๅ…ทๅบ“
Stars: โœญ 17 (-10.53%)
Mutual labels:  lambda
herman
Herman is a tool to simplify deployment of AWS Services using ECS and Lambda, and the provisioning of various AWS services.
Stars: โœญ 33 (+73.68%)
Mutual labels:  lambda
twitter
A serverless social network that's under development with some cool stuff, such as Serverless Framework, AppSync, GraphQL, Lambda, DynamoDB, Cognito, Kinesis Firehose, and Algolia โ˜๏ธ
Stars: โœญ 29 (+52.63%)
Mutual labels:  lambda
appsync-resolvers
AWS AppSync Resolvers for GraphQL using AWS Lambda functions in Go.
Stars: โœญ 37 (+94.74%)
Mutual labels:  lambda
serverless-ninja
Code repository of AWS Serverless Ninja Guidebook ๐Ÿ‘น ๐Ÿ‘น ๐Ÿ‘น
Stars: โœญ 40 (+110.53%)
Mutual labels:  lambda
amrlib
A python library that makes AMR parsing, generation and visualization simple.
Stars: โœญ 107 (+463.16%)
Mutual labels:  transformer
terraform-provider-bless
Terraform provider to automate the creation of BLESS deployments
Stars: โœญ 12 (-36.84%)
Mutual labels:  lambda
cfn-encrypt
๐Ÿ”‘๐Ÿ”โ˜๏ธ Cloudformation custom resource that enables creation of KMS encrypted strings and SSM secure parameters
Stars: โœญ 13 (-31.58%)
Mutual labels:  lambda
well-classified-examples-are-underestimated
Code for the AAAI 2022 publication "Well-classified Examples are Underestimated in Classification with Deep Neural Networks"
Stars: โœญ 21 (+10.53%)
Mutual labels:  transformer
zappa-slack-inviter
A server-less Slack inviter
Stars: โœญ 32 (+68.42%)
Mutual labels:  lambda
spotify-lambda
ฮป๐ŸŽตAWS Lambda functions for Spotify tracking
Stars: โœญ 12 (-36.84%)
Mutual labels:  lambda
0x4447 product answering machine
โ˜Ž๏ธ An automated answering machine build on top of Amazon Connect
Stars: โœญ 38 (+100%)
Mutual labels:  lambda
efsu
efsu is for accessing AWS EFS from your machine without a VPN
Stars: โœญ 42 (+121.05%)
Mutual labels:  efs

Zero administration inference with AWS Lambda for ๐Ÿค—

Note: This is not production code and simply meant as a demo

Hugging Face Transformers is a popular open-source project that provides pre-trained, natural language processing (NLP) models for a wide variety of use cases. Customers with minimal machine learning experience can use pre-trained models to enhance their applications quickly using NLP. This includes tasks such as text classification, language translation, summarization, and question answering - to name a few.

Overview

Our solution consists of an AWS Cloud Development Kit (AWS CDK) script that automatically provisions container image-based Lambda functions that perform ML inference using pre-trained Hugging Face models. This solution also includes Amazon Elastic File System (EFS) storage that is attached to the Lambda functions to cache the pre-trained models and reduce inference latency.

Architecture diagram In this architectural diagram:

  1. Serverless inference is achieved by using Lambda functions that are based on container image
  2. The container image is stored in an Amazon Elastic Container Registry (ECR) repository within your account
  3. Pre-trained models are automatically downloaded from Hugging Face the first time the function is invoked
  4. Pre-trained models are cached within Amazon Elastic File System storage in order to improve inference latency

The solution includes Python scripts for two common NLP use cases:

  • Sentiment analysis: Identifying if a sentence indicates positive or negative sentiment. It uses a fine-tuned model on sst2, which is a GLUE task.
  • Summarization: Summarizing a body of text into a shorter, representative text. It uses a Bart model that was fine-tuned on the CNN / Daily Mail dataset. For simplicity, both of these use cases are implemented using Hugging Face pipelines.

Prerequisites

The following is required to run this example:

Deploying the example application

  1. Clone the project to your development environment:
git clone <https://github.com/aws-samples/zero-administration-inference-with-aws-lambda-for-hugging-face.git>
  1. Install the required dependencies:
pip install -r requirements.txt
  1. Bootstrap the CDK. This command provisions the initial resources needed by the CDK to perform deployments:
cdk bootstrap
  1. This command deploys the CDK application to its environment. During the deployment, the toolkit outputs progress indications:
cdk deploy

Understanding the code structure

The code is organized using the following structure:

โ”œโ”€โ”€ inference
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ”œโ”€โ”€ sentiment.py
โ”‚   โ””โ”€โ”€ summarization.py
โ”œโ”€โ”€ app.py
โ””โ”€โ”€ ...

The inference directory contains:

  • The Dockerfile used to build a custom image to be able to run PyTorch Hugging Face inference using Lambda functions
  • The Python scripts that perform the actual ML inference

The sentiment.py script shows how to use a Hugging Face Transformers model:

import json
from transformers import pipeline

nlp = pipeline("sentiment-analysis")

def handler(event, context):
response = {
    "statusCode": 200,
    "body": nlp(event['text'])[0]
}
return response

For each Python script in the inference directory, the CDK generates a Lambda function backed by a container image and a Python inference script.

CDK script

The CDK script is named app.py in the solution's repository. The beginning of the script creates a virtual private cloud (VPC).

vpc = ec2.Vpc(self, 'Vpc', max_azs=2)

Next, it creates the EFS file system and an access point in EFS for the cached model:

fs = efs.FileSystem(self, 'FileSystem',
vpc=vpc,
removal_policy=RemovalPolicy.DESTROY)
access_point = fs.add_access_point('MLAccessPoint',
create_acl=efs.Acl(
owner_gid='1001', owner_uid='1001', permissions='750'),
path="/export/models",
posix_user=efs.PosixUser(gid="1001", uid="1001"))

It iterates through the Python files in the inference directory:

docker_folder = os.path.dirname(os.path.realpath(__file__)) + "/inference"
pathlist = Path(docker_folder).rglob('*.py')
for path in pathlist:

And then creates the Lambda function that serves the inference requests:

base = os.path.basename(path)
filename = os.path.splitext(base)[0]
# Lambda Function from docker image
function = lambda_.DockerImageFunction(
    self, filename,
    code=lambda_.DockerImageCode.from_image_asset(docker_folder,
    cmd=[filename+".handler"]),
    memory_size=8096,
    timeout=Duration.seconds(600),
    vpc=vpc,
    filesystem=lambda_.FileSystem.from_efs_access_point(
    access_point, '/mnt/hf_models_cache'),
    environment={
        "TRANSFORMERS_CACHE": "/mnt/hf_models_cache"},
    )

Adding a translator

Optionally, you can add more models by adding Python scripts in the inference directory. For example, add the following code in a file called translate-en2fr.py:

import json
from transformers
import pipeline

en_fr_translator = pipeline('translation_en_to_fr')

def handler(event, context):
    response = {
        "statusCode": 200,
        "body": en_fr_translator(event['text'])[0]
    }
    return response

Then run:

$ cdk synth
$ cdk deploy

This creates a new endpoint to perform English to French translation.

Cleaning up

After you are finished experimenting with this project, run cdk destroy to remove all of the associated infrastructure.

License

This library is licensed under the MIT No Attribution License. See the LICENSE file. Disclaimer: Deploying the demo applications contained in this repository will potentially cause your AWS Account to be billed for services.

Links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].