简体   繁体   中英

How do you invoke a sagemaker xgboost endpoint from a chalice app?

I built a chalice web-app that is hosted in an s3 bucket and calls an xgboost endpoint. I keep getting an error when I invoke the model through the web-app. When I looked into the Lambda log files I discovered my input is not properly decoding. input_text = app.current_request.raw_body.decode() What would be the correct code to decode the input from binary so I can pass in a regular string to my endpoint?

Here is the error:

botocore.errorfactory.ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (415) from model with message "could not convert string to float: user_input=1%".

Here is my index.html file:

<html>
<head></head>
<body>
<form method="post" action="<chalice_deployed_http>">

<input type="text" name="user_input"><br>

<input type="submit" value="Submit">
</form>
</body>
</html>

Here is my app.py file:

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from io import BytesIO
import csv
import sys, os, base64, datetime, hashlib, hmac
from chalice import Chalice, NotFoundError, BadRequestError
import boto3


app = Chalice(app_name='<name_of_chalice_app>')
app.debug = True

sagemaker = boto3.client('sagemaker-runtime')

@app.route('/', methods=['POST'], content_types=['application/x-www-form-urlencoded'])
def handle_data():
    input_text = app.current_request.raw_body.decode()

    res = sagemaker.invoke_endpoint(
                    EndpointName='<endpoint_name>',
                    Body=input_text,
                    ContentType='text/csv',
                    Accept='Accept'
                )
    return res['Body'].read().decode()[0]

I should be able to pass in a string like this:

'1,4,26,0.076923077,2,3,1,0.611940299,0.7818181820000001,0.40376569,0.571611506,0.12,12,1,0.0,2,1.0,1,2,6,3,1,1,1,1,1,3,1,0.000666667,1,1,2,2,-1.0,0.490196078,-1.0,0.633928571,6.0,145,2,2,1,3,2,2,1,3,2,3,3,-1.0,1,3,1,1,2,1,2,3,1,3,3,1,3,2,3,-1.0,3,3,1,2,2,1,3,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0.3497921158934803,0'

and get output like this:

'5'

When I run it in a jupyter notebook it works.

This worked:

    input_text = app.current_request.raw_body
    d = parse_qs(input_text)
    lst = d[b'user_input'][0].decode()
    res = sagemaker.invoke_endpoint(
                    EndpointName='<name-of-SageMaker-Endpoint>',
                    Body=lst,
                    ContentType='text/csv',
                    Accept='Accept'
                )

This blog post shows you how to invoke a SageMaker endpoint from a Chalice app. It uses the built-in image classification algo, but you shouldn't have any trouble adapting it to XGBoost.

https://medium.com/@julsimon/using-chalice-to-serve-sagemaker-predictions-a2015c02b033

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM