1

Below is the code to get the model from Hugging Face Hub and deploy the same model via sagemaker.

from sagemaker.huggingface import HuggingFaceModel
import sagemaker

role = sagemaker.get_execution_role()

# Hub Model configuration. https://huggingface.co/models
hub = {
    'HF_MODEL_ID':'siebert/sentiment-roberta-large-english',
    'HF_TASK':'text-classification'
}

# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
    transformers_version='4.17.0',
    pytorch_version='1.10.2',
    py_version='py38',
    env=hub,
    role=role, 
)

# deploy model to SageMaker Inference
predictor = huggingface_model.deploy(
    initial_instance_count=1, # number of instances
    instance_type='ml.g4dn.xlarge' # ec2 instance type
)

How can I deploy this model via sagemaker pipeline .

How can I include this code in sagemaker pipeline.

Soumya
  • 181
  • 1
  • 6

1 Answers1

1

Prerequisites

SageMaker pipelines offer many different components with lots of functionality. Your question is quite general and needs to be contextualized to a specific problem.

You have to start with putting up a pipeline first. See the complete guide "Defining a pipeline".

Quick response

My answer is to follow this official AWS guide that answers your question exactly:

SageMaker Pipelines: train a Hugging Face model, deploy it with a Lambda step


General explanation

Basically, you need to build your pipeline architecture with the components you need and register the trained model within the Model Registry.

Next, you have two paths you can follow:

  1. Trigger a lambda that automatically deploys the registered model (as the guide does).
  2. Out of the context of the pipeline, do the automatic deployment by retrieving the ARN of the registered model on the Model Registry. You can get it from register_step.properties.ModelPackageArn or in an external script using boto3 (e.g. using list_model_packages)
Giuseppe La Gualano
  • 1,491
  • 1
  • 4
  • 24