1

I am trying to invoke an Amazon Sagemaker Endpoint from a local python notebook. This is the code I am using.

import boto3

aws_access_key_id = '...............'
aws_secret_access_key = '................'
tkn = '..........'
region_name = '............'

amz = boto3.client('sagemaker-runtime',
                   aws_access_key_id=aws_access_key_id,
                   aws_secret_access_key=aws_secret_access_key,
                   aws_session_token=tkn,
                   region_name=region_name)


response = amz.invoke_endpoint(
    EndpointName='mymodel',
    Body=b'bytes'
)               

However, this doesn't work. Do I have to specify something else in Body ?

2 Answers 2

2

You can use boto3 session . I assume you already prepared jsons and your aws credentials are already on ~/.aws/credentials.

import boto3, json, sagemaker

sagemaker_session = sagemaker.Session()
role              = "YOUR-SAGEMAKER-EXECUTION-ROLE"
region            = boto3.Session().region_name
endpointName=     'YOUR ENDPOINT NAME'
predictor = sagemaker.predictor.RealTimePredictor(
               endpointName, 
               sagemaker_session=sagemaker_session, 
               content_type="application/json")
d='YOUR JSON LINES- YOU CAN OPEN WITH PYTHON BUILT IN FUNCTIONS'
response=predictor.predict(json.dumps(d))

response has the answer body which formatted in json. You can parse it and use your results.

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

Comments

0

Each endpoint expects different binary data. By specifying Body=b'bytes' you're passing the bytes of the string literal bytes, while you should be passing some actual input data to infer off.

According to the doc, I recommended to include the relevant ContentType, of the input data you're sending.

You said:

However, this doesn't work.

What is the error you're getting back?

Comments

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.