Deploy an NLP classification model with Amazon SageMaker and Lambda

Image for post
Image for post

Introduction

Image for post
Image for post
The finished application

Amazon SageMaker and the ML workflow

Image for post
Image for post

Text Classification with SageMaker BlazingText

sess = sagemaker.Session()
bucket = sess.default_bucket()
role = get_execution_role()
region_name = sess.region_name

Training our classification model

Image for post
Image for post
container = sagemaker.amazon.amazon_estimator.get_image_uri(region_name, 
"blazingtext", "latest")
bt_model = sagemaker.estimator.Estimator(container,
role,
train_instance_count=1,
train_instance_type= 'ml.c4.4xlarge',
train_volume_size = 30,
input_mode= 'File',
output_path=s3_output_location,
sagemaker_session=sess)
train_data = sagemaker.session.s3_input(s3_train_data, 
distribution= 'FullyReplicated',
content_type='text/plain',
s3_data_type='S3Prefix')
validation_data = sagemaker.session.s3_input(s3_validation_data,
distribution='FullyReplicated',
content_type='text/plain',
s3_data_type='S3Prefix')
data_channels = {'train': train_data, 'validation': validation_data}
bt_model.fit(inputs=data_channels, logs=True)
#train_accuracy: 0.9869
Number of train examples: 112000

#validation_accuracy: 0.9723
Number of validation examples: 70000
Image for post
Image for post

Deploy the trained model to a SageMaker endpoint

text_classifier = bt_model.deploy(initial_instance_count = 1,
instance_type = 'ml.t2.medium')
print(text_classifier.endpoint)
>> 'blazingtext-2020-XX-XX-XX-XX-XX-101'

Making use of the deployed model

Image for post
Image for post

How Lambda talks to SageMaker

Image for post
Image for post
Image for post
Image for post
def simple_tokenizer_for_bt(text_input):
words = REPLACE_NO_SPACE.sub("", text_input.lower())
words = REPLACE_WITH_SPACE.sub(" ", words)
return words
{  "instances": ["regina spektor is my favorite musician because she plays the piano", "the burj khalifa is the tallest skyscraper in the world"] }
runtime = boto3.Session().client('sagemaker-runtime')
response = runtime.invoke_endpoint(
EndpointName = 'blazingtext-2020-XX-XX-XX-XX-XX-101',
ContentType = 'application/json',
Body = json.dumps(payload))
output = json.loads(response['Body'].read().decode('utf-8'))
prob = output[0]['prob'][0]*100
label = output[0]['label'][0].split('__label__')[1]
final_output = 'The predicted label is {} with a probability of {:.1f}%'.format(label, prob)
return {'statusCode' : 200,
'headers' : { 'Content-Type' : 'text/plain',
'Access-Control-Allow-Origin' : '*' },
'body' : final_output
}
{"body": "Walter Perry Johnson was a Major League Baseball right-handed pitcher. He played his entire 21-year baseball career for the Washington Senators."}
{
"statusCode": 200,
"headers": {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*"
},
"body": "The predicted label is Athlete with a probability of 94.0%"
}

API Gateway

Image for post
Image for post
Image for post
Image for post

Host the app on an S3 bucket

action="https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/prod"
Image for post
Image for post
The finished application

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store