如何从圣杯应用程序调用sagemaker xgboost端点?

时间:2019-01-11 21:44:50

标签: python web-applications chalice amazon-sagemaker

我构建了一个圣杯Web应用程序,该应用程序托管在s3存储桶中,并调用xgboost端点。通过网络应用程序调用模型时,我总是收到错误消息。当我查看Lambda日志文件时,发现输入未正确解码。 input_text = app.current_request.raw_body.decode()从二进制解码输入的正确代码是什么,以便我可以将常规字符串传递给端点?

这是错误:

botocore.errorfactory.ModelError:调用InvokeEndpoint操作时发生错误(ModelError):从模型收到消息为“无法将字符串转换为float:user_input = 1%”的客户端错误(415)。

这是我的index.html文件:

<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>

这是我的app.py文件:

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]

我应该能够传递这样的字符串:

'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,0,0,3,0.3497921158934803,0'

并获得如下输出:

'5'

When I run it in a jupyter notebook it works.

2 个答案:

答案 0 :(得分:0)

这有效:

    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'
                )

答案 1 :(得分:0)

此博客文章显示了如何从Chalice应用程序调用SageMaker端点。它使用内置的图像分类算法,但是将其适应XGBoost不会有任何麻烦。

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