Reactive Machines

Seamless rendering of Amazon SageMaker Canvas Models

Machine learning (ML) models production can often be a complex and skilled task, especially for customers without deep technical and devops skills. Amazon Sagemaker Canvas simplifies model building by providing a code-based interface, so you can create highly accurate ML models using your existing data sources and without writing a single line of code. But building a model is only the end of the journey; To use it well is to call it bad and important. Amazon SageMaker Server Impncess is designed for workloads with varying traffic patterns and idle times. Provisioning automation and infrastructure scale based on demand, alleviates the need to manage servers or pre-replace capacity.

In this post, we walk through how to create an ML model built on sagemaker canvas and also use sagemaker server for validation. This solution can help you go from model creation to production forecasting quickly, efficiently, and without managing any infrastructure.

Overview

To demonstrate the front-end creation of a trained sagemaker canvas model, let's examine the workflow of an example:

  1. Submit the trained model to the Amazon SageMaker Model Registry.
  2. Create a new sagemaker model with correct configuration.
  3. Create an end-to-end configuration.
  4. Use a flawless finish with the created model and configuration of the fault.

You can use the process, as shown in the following diagram.

In this example, we use a pre-trained recovery model for the complete sagemaker endpoint. In this way, we can apply our model to various fields that do not require real-time trends.

Requirements

As a prerequisite, you must have access to Amazon Simple Cloud Service (Amazon S3) and Amazon Sagemaker AI. If you don't have sagemaker AI Domain configured in your account, and you need permissions to create sagemaker AI Domain.

You should also have a resume or breakdown model that you have studied. You can train your sagemaker canvas model as you normally would. This includes creating an Amazon SageMaker Data Wrangler Flow, performing the necessary data transformations, and selecting model configurations. If you are not making a professional model, you can follow one of the Amazon Sagemaker canvas labs for an immersion day to create one before proceeding. For this example, we use the classification model that was trained on the canvas-Sample-Shipping-Shipping-Shipping.CSV sample.

Save your model to the Sagemaker Model Registry

Complete the following steps to save your model to the Sagemaker Model Registry:

  1. In the sagemaker ai console, select Tudio Introducing Amazon Sagemaker Studio.
  2. In the sagemaker Studio instance, launch sagemaker canvas, which will open a new tab.

Open SageMaker Studio

  1. Find the model and Model Version you want to use on your baseless endpoint.
  2. From the options menu (three vertical dots), select Enter the model manual.

Save to Registry Model

You can now exit sagemaker canvas by logging in. To manage costs and prevent additional labor costs, you can also configure sagemaker canvas to automatically close when idle.

Allow your model to be shipped

After you have entered your model into the Model Registry, complete the following steps:

  1. In the sagemaker studio UI, select Models in the navigation pane.

The model you just exported to sagemaker canvas should be added with the submission status of Pending approval of brochure.

  1. Select the Model Version you want to use and update the status to – Approved by selecting the mode of delivery.

Find the shipping tab

  1. Select the type of model and go to Campaign tab. This is where you will find information related to the model and the corresponding container.
  2. Select the container and the spatial model associated with the trained model. You can identify it by looking at the presence of natural diversity SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT.

ECR and S3 URIS

Create a new model

Complete the following steps to create a new model:

  1. Without closing the sagemaker Studio tab, open a new tab and open the sagemaker ai console.
  2. Choose Models to Punishment section and select Create a model.
  3. Name your model.
  4. Leave the install option for the container as is Give the model creativity and space to decorate the photo and used CompressedModel Type.
  5. Enter the Amazon Elastic Directory Registry (Amazon ECR) URI, the Amazon S3 URI, and the environment variable found in the previous step.

A variable will be displayed as a single line in Sagemaker Studio, in the following format:

SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT: text/csv, SAGEMAKER_INFERENCE_OUTPUT: predicted_label, SAGEMAKER_INFERENCE_SUPPORTED: predicted_label, SAGEMAKER_PROGRAM: tabular_serve.py, SAGEMAKER_SUBMIT_DIRECTORY: /opt/ml/model/code

You may have different variations than those in the previous example. All variables that arise from the evolution of your environment must be added to your model. Make sure that each variation is in its own line when a new model is added.

Environmental variables

  1. Choose Create a model.

Create an Endpoint configuration

Complete the following steps to build the final configuration:

  1. In the sagemaker ai console, select End configuration Creating new configurations that are different from the models.
  2. Set the end type to – We are expensive then set a different model from the model created in the previous step.

Configuration of Model Endpoind

  1. Choose Create the final configuration.

Create an ending

Complete the following steps to create the conclusion:

  1. In the sagemaker ai console, select Certain powers Move the fan and create a new ending.
  2. Say the end.
  3. Select the display configuration made in the previous section and select Select Display Configuration.
  4. Choose Create an ending.

Endpovent Creation Model

The conclusion may take several minutes to be created. When the status is updated to Putting the platform in serviceyou can start calling the end.

The following sample code shows how to call an endpoint from a Jupyter Notebook located in your SageMaker Studio Environment:

import boto3
import csv
from io import StringIO
import time

def invoke_shipping_prediction(features):
    sagemaker_client = boto3.client('sagemaker-runtime')
    
    # Convert to CSV string format
    output = StringIO()
    csv.writer(output).writerow(features)
    payload = output.getvalue()
    
    response = sagemaker_client.invoke_endpoint(
        EndpointName="canvas-shipping-data-model-1-serverless-endpoint",
        ContentType="text/csv",
        Accept="text/csv",
        Body=payload
    )
    
    response_body = response['Body'].read().decode()
    reader = csv.reader(StringIO(response_body))
    result = list(reader)[0]  # Get first row
    
    # Parse the response into a more usable format
    prediction = {
        'predicted_label': result[0],
        'confidence': float(result[1]),
        'class_probabilities': eval(result[2]),  
        'possible_labels': eval(result[3])       
    }
    
    return prediction

# Features for inference
features_set_1 = [
    "Bell",
    "Base",
    14,
    6,
    11,
    11,
    "GlobalFreight",
    "Bulk Order",
    "Atlanta",
    "2020-09-11 00:00:00",
    "Express",
    109.25199890136719
]

features_set_2 = [
    "Bell",
    "Base",
    14,
    6,
    15,
    15,
    "MicroCarrier",
    "Single Order",
    "Seattle",
    "2021-06-22 00:00:00",
    "Standard",
    155.0483856201172
]

# Invoke the SageMaker endpoint for feature set 1
start_time = time.time()
result = invoke_shipping_prediction(features_set_1)

# Print Output and Timing
end_time = time.time()
total_time = end_time - start_time

print(f"Total response time with endpoint cold start: {total_time:.3f} seconds")
print(f"Prediction for feature set 1: {result['predicted_label']}")
print(f"Confidence for feature set 1: {result['confidence']*100:.2f}%")
print("nProbabilities for feature set 1:")
for label, prob in zip(result['possible_labels'], result['class_probabilities']):
    print(f"{label}: {prob*100:.2f}%")


print("---------------------------------------------------------")

# Invoke the SageMaker endpoint for feature set 2
start_time = time.time()
result = invoke_shipping_prediction(features_set_2)

# Print Output and Timing
end_time = time.time()
total_time = end_time - start_time

print(f"Total response time with warm endpoint: {total_time:.3f} seconds")
print(f"Prediction for feature set 2: {result['predicted_label']}")
print(f"Confidence for feature set 2: {result['confidence']*100:.2f}%")
print("nProbabilities for feature set 2:")
for label, prob in zip(result['possible_labels'], result['class_probabilities']):
    print(f"{label}: {prob*100:.2f}%")

Change the process

To automatically create Eservice Endpoints every time a new model is approved, you can use the following Yaml file with AWS Cloudformation. This file will implement the creation of sagemaker endpoints with the configuration you specify.

This Template Cloudfformation is provided for promotional purposes and is not intended for direct production use. Developers should thoroughly test this template according to their organization's security guidelines before deployment.

AWSTemplateFormatVersion: "2010-09-09"
Description: Template for creating Lambda function to handle SageMaker model
  package state changes and create serverless endpoints

Parameters:
  MemorySizeInMB:
    Type: Number
    Default: 1024
    Description: Memory size in MB for the serverless endpoint (between 1024 and 6144)
    MinValue: 1024
    MaxValue: 6144

  MaxConcurrency:
    Type: Number
    Default: 20
    Description: Maximum number of concurrent invocations for the serverless endpoint
    MinValue: 1
    MaxValue: 200

  AllowedRegion:
    Type: String
    Default: "us-east-1"
    Description: AWS region where SageMaker resources can be created

  AllowedDomainId:
    Type: String
    Description: SageMaker Studio domain ID that can trigger deployments
    NoEcho: true

  AllowedDomainIdParameterName:
    Type: String
    Default: "/sagemaker/serverless-deployment/allowed-domain-id"
    Description: SSM Parameter name containing the SageMaker Studio domain ID that can trigger deployments

Resources:
  AllowedDomainIdParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Ref AllowedDomainIdParameterName
      Type: String
      Value: !Ref AllowedDomainId
      Description: SageMaker Studio domain ID that can trigger deployments

  SageMakerAccessPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: Managed policy for SageMaker serverless endpoint creation
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - sagemaker:CreateModel
              - sagemaker:CreateEndpointConfig
              - sagemaker:CreateEndpoint
              - sagemaker:DescribeModel
              - sagemaker:DescribeEndpointConfig
              - sagemaker:DescribeEndpoint
              - sagemaker:DeleteModel
              - sagemaker:DeleteEndpointConfig
              - sagemaker:DeleteEndpoint
            Resource: !Sub "arn:aws:sagemaker:${AllowedRegion}:${AWS::AccountId}:*"
          - Effect: Allow
            Action:
              - sagemaker:DescribeModelPackage
            Resource: !Sub "arn:aws:sagemaker:${AllowedRegion}:${AWS::AccountId}:model-package/*/*"
          - Effect: Allow
            Action:
              - iam:PassRole
            Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/service-role/AmazonSageMaker-ExecutionRole-*"
            Condition:
              StringEquals:
                "iam:PassedToService": "sagemaker.amazonaws.com"
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource: !Sub "arn:aws:ssm:${AllowedRegion}:${AWS::AccountId}:parameter${AllowedDomainIdParameterName}"

  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
        - !Ref SageMakerAccessPolicy

  ModelDeploymentFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        ZipFile: |
          import os
          import json
          import boto3

          sagemaker_client = boto3.client('sagemaker')
          ssm_client = boto3.client('ssm')

          def handler(event, context):
              print(f"Received event: {json.dumps(event, indent=2)}")
              try:
                  # Get details directly from the event
                  detail = event['detail']
                  print(f'detail: {detail}')
                  
                  # Get allowed domain ID from SSM Parameter Store
                  parameter_name = os.environ.get('ALLOWED_DOMAIN_ID_PARAMETER_NAME')
                  try:
                      response = ssm_client.get_parameter(Name=parameter_name)
                      allowed_domain = response['Parameter']['Value']
                  except Exception as e:
                      print(f"Error retrieving parameter {parameter_name}: {str(e)}")
                      allowed_domain = '*'  # Default fallback
                  
                  # Check if domain ID is allowed
                  if allowed_domain != '*':
                      created_by_domain = detail.get('CreatedBy', {}).get('DomainId')
                      if created_by_domain != allowed_domain:
                          print(f"Domain {created_by_domain} not allowed. Allowed: {allowed_domain}")
                          return {'statusCode': 403, 'body': 'Domain not authorized'}

                  # Get the model package ARN from the event resources
                  model_package_arn = event['resources'][0]

                  # Get the model package details from SageMaker
                  model_package_response = sagemaker_client.describe_model_package(
                      ModelPackageName=model_package_arn
                  )

                  # Parse model name and version from ModelPackageName
                  model_name, version = detail['ModelPackageName'].split('/')
                  serverless_model_name = f"{model_name}-{version}-serverless"

                  # Get all container details directly from the event
                  container_defs = detail['InferenceSpecification']['Containers']

                  # Get the execution role from the event and convert to proper IAM role ARN format
                  assumed_role_arn = detail['CreatedBy']['IamIdentity']['Arn']
                  execution_role_arn = assumed_role_arn.replace(':sts:', ':iam:')
                                                   .replace('assumed-role', 'role/service-role')
                                                   .rsplit('/', 1)[0]

                  # Prepare containers configuration for the model
                  containers = []
                  for i, container_def in enumerate(container_defs):
                      # Get environment variables from the model package for this container
                      environment_vars = model_package_response['InferenceSpecification']['Containers'][i].get('Environment', {}) or {}
                      
                      containers.append({
                          'Image': container_def['Image'],
                          'ModelDataUrl': container_def['ModelDataUrl'],
                          'Environment': environment_vars
                      })

                  # Create model with all containers
                  if len(containers) == 1:
                      # Use PrimaryContainer if there's only one container
                      create_model_response = sagemaker_client.create_model(
                          ModelName=serverless_model_name,
                          PrimaryContainer=containers[0],
                          ExecutionRoleArn=execution_role_arn
                      )
                  else:
                      # Use Containers parameter for multiple containers
                      create_model_response = sagemaker_client.create_model(
                          ModelName=serverless_model_name,
                          Containers=containers,
                          ExecutionRoleArn=execution_role_arn
                      )

                  # Create endpoint config
                  endpoint_config_name = f"{serverless_model_name}-config"
                  create_endpoint_config_response = sagemaker_client.create_endpoint_config(
                      EndpointConfigName=endpoint_config_name,
                      ProductionVariants=[{
                          'VariantName': 'AllTraffic',
                          'ModelName': serverless_model_name,
                          'ServerlessConfig': {
                              'MemorySizeInMB': int(os.environ.get('MEMORY_SIZE_IN_MB')),
                              'MaxConcurrency': int(os.environ.get('MAX_CONCURRENT_INVOCATIONS'))
                          }
                      }]
                  )

                  # Create endpoint
                  endpoint_name = f"{serverless_model_name}-endpoint"
                  create_endpoint_response = sagemaker_client.create_endpoint(
                      EndpointName=endpoint_name,
                      EndpointConfigName=endpoint_config_name
                  )

                  return {
                      'statusCode': 200,
                      'body': json.dumps({
                          'message': 'Serverless endpoint deployment initiated',
                          'endpointName': endpoint_name
                      })
                  }

              except Exception as e:
                  print(f"Error: {str(e)}")
                  raise
      Runtime: python3.12
      Timeout: 300
      MemorySize: 128
      Environment:
        Variables:
          MEMORY_SIZE_IN_MB: !Ref MemorySizeInMB
          MAX_CONCURRENT_INVOCATIONS: !Ref MaxConcurrency
          ALLOWED_DOMAIN_ID_PARAMETER_NAME: !Ref AllowedDomainIdParameterName

  EventRule:
    Type: AWS::Events::Rule
    Properties:
      Description: Rule to trigger Lambda when SageMaker Model Package state changes
      EventPattern:
        source:
          - aws.sagemaker
        detail-type:
          - SageMaker Model Package State Change
        detail:
          ModelApprovalStatus:
            - Approved
          UpdatedModelPackageFields:
            - ModelApprovalStatus
      State: ENABLED
      Targets:
        - Arn: !GetAtt ModelDeploymentFunction.Arn
          Id: ModelDeploymentFunction

  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref ModelDeploymentFunction
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt EventRule.Arn

Outputs:
  LambdaFunctionArn:
    Description: ARN of the Lambda function
    Value: !GetAtt ModelDeploymentFunction.Arn
  EventRuleArn:
    Description: ARN of the EventBridge rule
    Value: !GetAtt EventRule.Arn

This stack will limit the creation of the front end that reads the special region and domain. You can find your domain ID when you access sagemaker studio from the Sagemaker AI Console, or by running the following command: aws sagemaker list-domains —region [your-region]

Clean up

To manage costs and protect against additional labor costs, be sure to log in to sagemaker canvas. If you are testing your endpoint using jupyter notebook, you can close your jupyterlab instance by selecting Wait or to stop the automatic shutdown of Jupyterlab.

Set up a Jupyter lab environment

In this post, we have shown how to use the sagemaker canvas model in an infinite component that uses sagemaker compliance. Using this seamless method, you can quickly and efficiently predict from your sagemaker canvas models without needing to manage the underlying infrastructure.

This seamless deployment application is just one example of how AWS services such as sagemaker canvas and sagemaker server Insible are facilitating the ML journey, helping businesses of various sizes and technical proficiencies unlock the value of AI and ML. As you continue to explore the sagemaker ecosystem, be sure to learn how to manage No-coden dazozone data, and transform the first model using sagemaker canvas and sagemaker studio.


About the writers

Nadhya Polanco Is a solutions developer on AWS based in Brussels, Belgium. In this role, he supports organizations that want to incorporate AI and machine learning into their operations. In her free time, Nadhya enjoys indulging in her passion for coffee and travel.

Brajendra Singh Is the development of the main goals in the services of Amazon Web Services, where he collaborates with business customers to design and implement new solutions. With a strong background in software development, he brings deep expertise in data analytics, machine learning, and generative ai.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button