Created
May 8, 2019 00:25
-
-
Save memodoring/498ef45bda5b8466b228740da1cedf18 to your computer and use it in GitHub Desktop.
Lambda function to get Inference from SageMaker model based on : https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_applying_machine_learning/breast_cancer_prediction/Breast%20Cancer%20Prediction.ipynb
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 os | |
import io | |
import boto3 | |
import json | |
import csv | |
# grab environment variables | |
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()) | |
print(result) | |
pred = int(result['predictions'][0]['score']) | |
predicted_label = 'M' if pred == 1 else 'B' | |
return predicted_label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment