How to pass data to an endpoint

0

Hello,
I have followed the DeepAR Chicago Traffic violations notebook example. The Model and Endpoint has been created and the forecasting is working.

https://github.com/aws/amazon-sagemaker-examples/blob/master/introduction_to_applying_machine_learning/deepar_chicago_traffic_violations/deepar_chicago_traffic_violations.ipynb

Howevr, I haven't deleted the model nor the endpoint in order to use it externally. I have created a Python script on an EC2 that tries to load the endpoint and passes the data to it to get a prediction, and here is what I am doing:

  1. Loading the CSV exactly the way I did it on the notebook
  2. Parsing the CSV the same way I did on the notebook for the "predictor.predict" command
  3. Instead of using the "predictor.predict", I am using "invoke_endpoint" to load the endpoint and passing the data from the previous point
  4. Instead of getting the same response I got on the notebook, I am getting the following message:
    "type: <class 'list'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object"

Not sure what the issue is, seems that it requires a byte data... I guess I cannot send the data as a list to the endpoint and I need to serialize it or to encode it? convert to to JSON? to Bytes?

Any help will be appreciated.
Regards

asked 3 years ago1474 views
3 Answers
0
Accepted Answer

Hello,

So the issue here is the predictor.predict command converts the data to the format necessary for the endpoint to understand, thus you need to serialize or encode the payload by yourself. To do this you can work with something like json.dumps(payload) or for a byte array json.dumps(payload).encode().

If you want to use the predictor class this is taken care of by the serializer option. The serializer encodes/decodes the data for us and lets you simply call the endpoint through the predictor class. An example of this is the following code snippet:

from sagemaker.serializers import IdentitySerializer
from sagemaker.deserializers import JSONDeserializer
serializer=IdentitySerializer(content_type="application/json")

Hope this helps!

To check out the various serializer options that can work for your different use cases check the following link.
Serializers: https://sagemaker.readthedocs.io/en/stable/api/inference/serializers.html

Edited by: rvegira-aws on Jul 22, 2021 9:22 AM

Edited by: rvegira-aws on Jul 22, 2021 9:24 AM

answered 3 years ago
0

Thanks rvegira-aws,

I changed the approach, instead of using the "invoke_endpoint" method, I have used the predictor class as you suggested and this has fixed the issue.

Regards.

answered 3 years ago
0

I faced the exact problem when building models for my website, thanks for the question

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions