0

I've deployed a category trained model and hosted the endpoint.. and trying to inference this, but have a issue.. basically we take a short description i.e "laptop screen" this should return a category i.e "Hardware" the problem i'm facing is that i just seem to get this error when inferencing this via postman.

i.e if i send this {"data":"laptop screen"}

i get this error in the body

{
    "errorMessage": "Expecting value: line 1 column 1 (char 0)",
    "errorType": "JSONDecodeError",
    "stackTrace": [
        [
            "/var/task/lambda_function.py",
            21,
            "lambda_handler",
            "result = json.loads(response['Body'].read().decode())"
        ],
        [
            "/var/lang/lib/python3.6/json/__init__.py",
            354,
            "loads",
            "return _default_decoder.decode(s)"
        ],
        [
            "/var/lang/lib/python3.6/json/decoder.py",
            339,
            "decode",
            "obj, end = self.raw_decode(s, idx=_w(s, 0).end())"
        ],
        [
            "/var/lang/lib/python3.6/json/decoder.py",
            357,
            "raw_decode",
            "raise JSONDecodeError(\"Expecting value\", s, err.value) from None"
        ]
    ]
}

this is my lambda function:

import os
import io
import boto3
import json
import csv

ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    
    data = json.loads(json.dumps(event))
    payload = data['data']
    print(payload)
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                        ContentType='text/csv',
                                        Body=payload)
    print(response)
    result = json.loads(response['Body'].read().decode())
    
    return result

Ive added this to admin IAM role too

{
    "Sid": "VisualEditor0",
    "Effect": "Allow",
    "Action": "sagemaker:InvokeEndpoint",
    "Resource": "*"
}

Any assistance would be ace, i feel im pretty close it works fine when it comes to predicting priority, but need help when predicting category string.

8
  • 1
    For the future, please spend a minute to see how to properly format your code snippets (done it for you this time). Commented Jul 21, 2021 at 9:57
  • ace cheers bud appreciate it. Commented Jul 21, 2021 at 9:59
  • In postman, did you try sending {"data":"laptop screen", "content-type": "text/csv"} ? Commented Jul 21, 2021 at 18:11
  • Hi Bud, cheers for the response, yep i tried this and still throwing the same error? :/ Commented Jul 22, 2021 at 8:48
  • can you post the lambda cloudwatch entries? Is there any chance response is empty? Commented Jul 22, 2021 at 14:54

1 Answer 1

1

For your lambda function, I think the error might be with the way you're passing your payload in, take the following code snippet.

    data = json.loads(json.dumps(event))
    payload = json.dumps(data)
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       ContentType='application/json',
                                       Body=payload)
    result = json.loads(response['Body'].read().decode())

Adjust the payload that you are passing in to use json.dumps() to encode your data properly for the endpoint.

I work for AWS & my opinions are my own

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Bud, thanks for the heads up.. I tried this when submitting {"data":"laptop screen"}
data = json.loads(json.dumps(event)) payload = json.dumps(data) response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME, ContentType='text/csv', Body=payload) result = json.loads(response['Body'].read().decode())
and got this error {"errorMessage": "Expecting value: line 1 column 1 (char 0)", "errorType": "JSONDecodeError", "stackTrace": [["/var/task/lambda_function.py", 22, "lambda_handler", "result = json.loads(response['Body'].read().decode())"], ["/var/lang/lib/python3.6/json/__init__.py", 354, "loads", "return _default_decoder.decode(s)"], ["/var/lang/lib/python3.6/json/decoder.py", 339, "decode", "obj, end = self.raw_decode(s, idx=_w(s, 0).end())"], ["/var/lang/lib/python3.6/json/decoder.py", 357, "raw_decode", "raise JSONDecodeError(\"Expecting value\", s, err.value) from None"]]}
interesting, when i invoke {"data":"laptop screen"} json.dumps(data) does work on a different endpoint with a model to predict a number based on urgency i.e '1-7' this is correct, but when i invoke the same data on an endpoint based on a category label i.e should be returning response as 'Hardware' i get this same error thrown? even after adjusting the payload to use json.dumps()? could it be because i'm trying to predict a label and not a integer?
I think that might be an issue with your inference code then in the script you're providing or container you're bringing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.