Last active
January 27, 2022 22:09
-
-
Save mvsusp/b9de070b310af6876de7b59beda0fb4a to your computer and use it in GitHub Desktop.
How to make predictions against a SageMaker endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.basicapp; | |
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntime; | |
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntimeClientBuilder; | |
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointRequest; | |
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointResult; | |
import java.nio.ByteBuffer; | |
public class App | |
{ | |
public static void main( String[] args ) | |
{ | |
AmazonSageMakerRuntime runtime = AmazonSageMakerRuntimeClientBuilder.defaultClient(); | |
String body = "{\"instances\": [1.0,2.0,5.0]}"; | |
ByteBuffer bodyBuffer = ByteBuffer.wrap(body.getBytes()); | |
InvokeEndpointRequest request = new InvokeEndpointRequest() | |
.withEndpointName("half-plus-three") | |
.withBody(bodyBuffer); | |
InvokeEndpointResult invokeEndpointResult = runtime.invokeEndpoint(request); | |
String bodyResponse = new String(invokeEndpointResult.getBody().array()); | |
System.out.println(bodyResponse); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
var sageMakerRuntime = new AWS.SageMakerRuntime({region: 'us-west-2',}); | |
var params = { | |
Body: new Buffer('{"instances": [1.0,2.0,5.0]}'), | |
EndpointName: 'half-plus-three' | |
}; | |
sageMakerRuntime.invokeEndpoint(params, function(err, data) { | |
responseData = JSON.parse(Buffer.from(data.Body).toString('utf8')) | |
console.log(responseData); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
client = boto3.client('runtime.sagemaker') | |
data = {"instances": [1.0,2.0,5.0]} | |
response = client.invoke_endpoint(EndpointName='half-plus-three', | |
Body=json.dumps(data)) | |
response_body = response['Body'] | |
print(response_body.read()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
ENDPOINT_NAME=half-plus-three | |
RESPONSE_FILE=prediction_response.json | |
aws sagemaker-runtime invoke-endpoint --endpoint-name ${ENDPOINT_NAME} \ | |
--body '{"instances": [1.0,2.0,5.0]}' prediction_response.json | |
cat ${RESPONSE_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @mvsusp , I have similar requirement to make call to sagemaker endpoint. The above example using default client. Can you please share on how should I make a call to a specific endpoint with authentication. I have access key and secure key. Highly appreciate your help.
Thanks