Comprehensive Guide: Deploying Prebuilt Models using Amazon SageMaker
Tags: aws, machinelearning, stablediffusion, tutorial
Published: 2023-07-13 10:13:53

## Objective
The objective of this article is to provide a step-by-step guide on deploying an end-to-end machine learning model using a prebuilt AWS Jump Start model. The focus will be on leveraging low-code and no-code options provided by AWS. The article covers various aspects of the deployment process, including the creation of a SageMaker domain, deploying Jump Start models to SageMaker Inference Endpoint, and interacting with the deployed model in an open SageMaker notebook.

## Introduction
Overview:

![Image description: Architecture diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34eq9ljulii8osi0g447.png)

[Source](https://catalog.workshops.aws/building-gen-ai-apps-with-found-models/en-US)

This article is part of a three-part series that aims to demonstrate how to deploy machine learning models using AWS's low-code and no-code options. Specifically, this article focuses on deploying a prebuilt AWS Jump Start model and hosting it on a Streamlit server.

In this part of the article series, you will learn the following based on SageMaker:

1. How to create a SageMaker domain.
2. How to deploy Jump Start models to the SageMaker Inference Endpoint.
3. How to interact with the deployed model in an open SageMaker notebook (optional).

Assumptions: You are familiar with navigating the AWS console and have an AWS account with SageMaker Full Access. If you don't have an account, you can sign up for one using the following [link](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-set-up.html)

**Cost:**
Please note that the labs from the workshop page costs around $10 if completed within 2 hours.

**How to Get Free Access SageMaker (Optional):**
1. Utilize the AWS Free Tier.
2. For students, consider joining the AWS DeepRacer Challenge or AWS Educate.
3. Participate in AWS events to receive free credits.
4. Attend AWS workshops to gain access to temporary accounts.

## Jump Start
SageMaker Jump Start is a repository of pretrained end-to-end models that serve as baseline models for building upon. This low-code, no-code solution allows you to quickly leverage ready-to-use models for various use cases, including tabular regression, classification, and state-of-the-art models from Amazon and its partners like Stability AI, Cohere, AI21, and AlexTM. You can also enhance these models by performing additional training on them. Examples of JumpStart models can be found on the JumpStart landing page, which showcases available end-to-end machine learning solutions, pretrained models, and example notebooks.
![Image description: Jump Start model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b310as55ltyjna80exqt.png)

## Domain
In SageMaker, a domain refers to a logical grouping of files and resources within a workspace. It provides a structured environment for managing machine learning deployments. Each domain in SageMaker consists of a collection of resources specifically tailored to facilitate machine learning workflows.

## Model Endpoint
A SageMaker Endpoint is an API that allows you to access a model for inference. It serves as the interface for making predictions using your trained model. When deploying a model, SageMaker provides the necessary infrastructure based on your chosen deployment type. For prebuilt models, the infrastructure specifications are already defined. However, if you are creating your own model, you will need to specify the infrastructure type required to host your model. SageMaker leverages Docker containers for hosting the models, ensuring scalability and flexibility.

## Model Parsing
Model parsing is the process of interpreting and analyzing the structure and content of a model. It involves understanding the input and output formats, data transformations, and any pre- or post-processing steps required. Accurate model parsing is essential for seamless integration and efficient utilization of the model.

## Stable Diffusion
Stable Diffusion is a text-to-image model that utilizes generative modeling techniques to generate photorealistic images based on text prompts. It offers a powerful solution for transforming textual descriptions into visually compelling images. To learn more about Stable Diffusion and its capabilities, refer to this [blog](https://aws.amazon.com/blogs/machine-learning/generate-images-from-text-with-the-stable-diffusion-model-on-amazon-sagemaker-jumpstart/)

## Creating a SageMaker Domain
1. Open the AWS Management Console and enter "Sagemaker" in the search bar. Click on "Amazon SageMaker" to access the SageMaker console.

2. On the left navigation pane, click on "Domains" to go to the SageMaker Domain configuration page.
![Image description: Navigating to the Domains page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z29j630puus72bna5hjo.png)

3. Create a new domain by clicking on the "Create domain" button. Then, click on "Quick setup" (default) for easier domain configuration.
![Image description: Creating a new domain](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qqlseznlqhbedf506x1.png)

4. In the "Quick Setup" section, leave the default settings as they are.

5. Enter a name for your domain in the "Domain name" text box.

6. Under "User profile," you can enter a friendly user name or use the default name provided.

7. In the "Execution role" section, click on the "Create new role" option.
![Image description: Creating a new execution role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozoipc21w5zri3j3695c.png)

8. In the "Create an Execution role" dialog, select "Any S3 bucket" and leave the defaults as they are. Then, click the "Create role" button.
![Image description: Creating a new execution role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6rbmsjs3s4h2411gxva.png)

9. You'll see a message confirming the successful creation of the IAM role.

10. Scroll to the bottom of the page and click the "Submit" button to create the domain.
![Image description: Submitting the domain creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hj6qezehqpntyyu1sl7d.png)

11. Wait for the domain creation process to complete. Once the domain is created, click on the domain you just created.

By following these steps, you will be able to create a SageMaker domain and proceed with configuring and utilizing it for your machine learning workflows.


## Launching SageMaker Studio
1. On the SageMaker Domain page, click on the newly created domain. This will redirect you to the domain details page.
Image description: Domain details page
![Image description domain details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/balwume91l7mlh1wsd4u.png)


2. Look for the "User profiles" section on the page.

Under "User profiles," click on the "Launch" button located on the right side of the page.

3. From the dropdown list that appears, select "Studio." This will launch a SageMaker Studio notebook.

![Image description: Launching SageMaker Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7zdj369zxsyhz78nf9u.png)

Please note that launching the SageMaker Studio may take some time to complete.

Once SageMaker Studio has launched successfully, you will be redirected to the SageMaker Studio landing page, as shown below.
![Image description: SageMaker Studio landing page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw9c9c787yeggbu2us7a.png)

## Deploying JumpStart Models

1. On the SageMaker Studio page, navigate to the left navigation pane and click on "SageMaker JumpStart" to expand the menu.

2. Click on "Models, Notebooks, Solutions" to access the JumpStart models.
![Image description: Selecting JumpStart Models](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq102nfn8igbx18dxrfp.png)

3. On the next screen, select "Stable Diffusion 2.1 base" from the available models, as shown in the image below.
![Image description: Selecting Stable Diffusion model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0878z74qps9xg52j70li.png)

4. On the Stable Diffusion details page, click the "Deploy" button to deploy the model. This will create an endpoint that can be used to interact with the model. The deployment process will also launch an instance to host the endpoint.
![Image description: Deploying the model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7o0e4a8qsa51opvoblkh.png)

5. Click "Deploy" to initiate the deployment process. Please note that the deployment may take some time to complete.

By following these steps, you will be able to deploy the JumpStart model, specifically the Stable Diffusion 2.1 base model. Once deployed, you will have an endpoint that can be used to interact with the model for inference tasks.

## Setting Up the Model's Notebook

1. Once the endpoint is deployed and in service. You can click "Open Notebook" button to interact with the model.

2. After starting the model's new notebook, please allow some time for the notebook to initialize.

3. Scroll down to the setup section and run the first cells as indicated in the notebook.
![Image description: run cells](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihwdbj3yxuqe2kymymu7.png)

Please note: Running the first cells and testing the model on a notebook are optional and can be done if desired.

## Deploying the Flan-T5 XL Model

To deploy the Flan model, follow these steps:

1. Search for "Flan-t5 xl" in the available models.

2. Click on the "Flan-t5 xl" model to access the Flan-t5 xl model details page.

3. On the Flan model details page, click on the "Deploy" button to initiate the deployment process.

4. The flan-t5 xl model is deployed just as the stable diffusion model was deployed.
![Image description: Deploying the Flan model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fym3yhyihma89by8pqt0.png)

By following these steps, you will be able to set up the model's notebook, run the necessary setup cells, and deploy the Flan model for further usage.

## Viewing Endpoints

1. On the SageMaker homepage, navigate to the "Inference" section and select "Endpoints". This will take you to the page displaying all the endpoints.
![Image description: Selecting Endpoints](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bj143jgk1gzhnawmrx4k.png)

2. The endpoints page provides an overview of the endpoints that have been created. This will be useful in Part 2 of this tutorial.
![Image description: endpoints](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ha8u5r1g8ofxj8srf8dx.png)

By following these steps, you will be able to navigate to the endpoints page on the SageMaker homepage, which will be useful for the [subsequent parts](https://dev.to/clouddev/comprehensive-tutorial-deploying-sagemaker-endpoint-on-lambda-for-ai-powered-text-summarization-and-image-generation-29f9-temp-slug-4083371?preview=8ee8d72f11d82a9b5a1c0c93baae8c62f89dd578bd54cd3695e010eb20d43b198c4def7c7bc795e4faa3cc15da1306eb7b12f0f0a480476475ff0b3f) of this tutorial.