简体   繁体   中英

Python Notebook Invoke Endpoint Sagemaker from Local

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 ?

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?

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.

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