aws / amazon-sagemaker-examples

Example 📓 Jupyter notebooks that demonstrate how to build, train, and deploy machine learning models using 🧠 Amazon SageMaker.
https://sagemaker-examples.readthedocs.io
Apache License 2.0
10.14k stars 6.78k forks source link

Suggestion: Calling SageMaker Endpoint in Lambda Function URLs #3411

Open temiwale88 opened 2 years ago

temiwale88 commented 2 years ago

Hello team -

I'm having issues with the new Lambda function URL invoking a Sagemaker endpoint. When I test the following, it works, however when I click the 'function url', I get 'Internal Server Error'. Can anyone please assist me with this?

import base64
import logging
import json
import boto3
import pickle

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

print('Loading Lambda function')

runtime=boto3.Session().client('sagemaker-runtime')
endpoint_Name='XX_XX'
s3 = boto3.resource('s3')
bucket = 'XX_XX'
pkl_key = "dogImages/classes/dog_breeds_labels.pkl"
classes = pickle.loads(s3.Bucket(bucket).Object(pkl_key).get()['Body'].read())

def lambda_handler(event, context):

    bs=event
    runtime=boto3.Session().client('sagemaker-runtime')

    response=runtime.invoke_endpoint(EndpointName=endpoint_Name,
                                    ContentType="application/json",
                                    Accept='application/json',
                                    #Body=bytearray(x)
                                    Body=json.dumps(bs))

    result=response['Body'].read().decode('utf-8')
    sss=json.loads(result)[0]
    max_value = max(sss) #let's retrieve highest number
    max_index = sss.index(max_value) #let's retrieve the index of the max number
    predicted_dog = classes[max_index]
    final_response = {"predicted_dog" : predicted_dog}

    return {
        'statusCode': 200,
        'headers' : { 'Content-Type' : 'text/plain', 'Access-Control-Allow-Origin' : '*' },
        'type-result':str(type(result)),
        'Content-Type-In':str(context),
        'body' : json.dumps(final_response)
        }
richielo commented 2 years ago

Have you resolved this? I am facing the same issue

hashim7676 commented 12 months ago

did you somehow resolve this issue? I need help

temiwale88 commented 12 months ago

Hey guys -

So sorry I missed your comment @richielo . I don't remember how I solved it but I know I went with Lambda + API Gateway Check out my .ipynb file for how I tested it (hope it's useful - it contains some useful links that helped me)