How to run Automatic1111 on Amazon SageMaker Notebook

Stable Diffusion Web UI — a browser interface based on Gradio library for Stable Diffusion

David Min
5 min readMay 24, 2023
Stable Diffusion AI Art (Stable Diffusion XL)

👋 Updates

The open source Automatic1111 project (A1111 for short), also known as Stable Diffusion WebUI, is a web interface for Stable Diffusion and provides an accessible way to generate images with Stable Diffusion right in your browser. In this post, we’ll look at how to set up Automatic1111 and the Stable Diffusion XL (SDXL) model on Amazon SageMaker Notebook. This provides a fast, scalable environment to generate AI images in the cloud.

We’ll walk through step-by-step guides on how to run Automatic1111 on Amazon SageMaker Notebook and generate sample images to explore Automatic1111 capabilities. We’ll cover everything you need to get Automatic1111 up and running, and start creating creative contents. By the end, you’ll have hands-on experience with Automatic1111 on Amazon’s fully managed notebook environment. Let’s get started! 🚀

You can follow step-by-step guides below to run A1111 on Amazon SageMaker Notebook. 👇

Step 1 — Create Amazon SageMaker Notebook instance

Amazon SageMaker > Notebook > Notebook instances
Create notebook instance
Notebook instance name: a1111-instance
Notebook instance type: ml.g5.2xlarge
Additional configuration > Volume size in GB: 1024
Create notebook instance — Instance Name & Instance Type
Create notebook instance — Additional Configuration > Volume size in GB
Create notebook instance
Notebook instance: Status — Pending
Notebook instance: Status — InService

Step 2 — Install Automatic1111

2-A. Open JupyterLab

Open JupyterLab
JupyterLab

2-B. Open a terminal

JupyterLab > Other > Terminal
Terminal

2-C. Create a persistent conda environment in the terminal

# Install a separate conda installation via Miniconda
WORKING_DIR=/home/ec2-user/SageMaker
mkdir -p "$WORKING_DIR"
wget https://repo.anaconda.com/miniconda/Miniconda3-py310_23.5.2-0-Linux-x86_64.sh -O "$WORKING_DIR/miniconda.sh"
bash "$WORKING_DIR/miniconda.sh" -b -u -p "$WORKING_DIR/miniconda"
rm -rf "$WORKING_DIR/miniconda.sh"
# Create a custom conda environment
source "$WORKING_DIR/miniconda/bin/activate"
KERNEL_NAME="a1111"
PYTHON="3.10.6"
conda create --yes --name "$KERNEL_NAME" python="$PYTHON"
conda activate "$KERNEL_NAME"
pip install --quiet ipykernel

2-D. Install Automatic1111 in the terminal

cd SageMaker
wget https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh
bash webui.sh
Install Automatic1111 example
Install Automatic1111

📔 [Tip] You can change it to dark mode from Settings > Theme > JupyterLab Dark.

Step 3 — Run Automatic1111 using Ngrok

3-A. Launch Automatic1111 in Terminal 1

source /home/ec2-user/SageMaker/miniconda/bin/activate
conda activate a1111
bash webui.sh --theme dark

3-B. Run Ngrok in Terminal 2

  • Sign up (or log in) to the ngrok dashboard and get your Authtoken. The ngrok agent uses the authtoken to log into your account when you start a tunnel.
  • Open another Terminal.
  • Copy the value and run this commands:
    - Install the ngrok agent
    - Add the authtoken in your terminal
    - Start a tunnel
source /home/ec2-user/SageMaker/miniconda/bin/activate
conda activate a1111
pip install pyngrok
ngrok config add-authtoken your_authtoken
cp /home/ec2-user/.ngrok2/ngrok.yml /home/ec2-user/SageMaker/ngrok.yml
ngrok http http://127.0.0.1:7860 --basic-auth 'username:password'

⚠️ Change username:password accordingly!

You should see something similar to the following console UI in your terminal.

ngrok                                                                       (Ctrl+C to quit)

Session Status online
Account inconshreveable (Plan: Free)
Version 3.0.0
Region United States (us)
Latency 78ms
Web Interface http://127.0.0.1:4040
Forwarding https://84c5df439d74.ngrok-free.dev -> http://localhost:8000

Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00

Now open the Forwarding URL in your browser, you should be prompted for a username and password. Once you sign in, you should see your local Automatic1111 web service.

Automatic1111 — Stable Diffusion Web UI v1.6.0

[Note] You are now using TLS (notice the 🔒 in your browser window) with a valid certificate without making any changes to your local service.

Step 4— Run SDXL 1.0 on Automatic1111

4-A. Download SDXL 1.0 base and refiner models from Hugging Face

cd /home/ec2-user/SageMaker/stable-diffusion-webui/models/Stable-diffusion

wget https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
wget https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/resolve/main/sd_xl_refiner_1.0.safetensors

4-B. Start Generating Images

You can review a few sample images generated from A1111 here. 👇

SDXL 1.0 on Automatic1111 — sample image
SDXL 1.0 on Automatic1111 — sample image
SDXL 1.0 on Automatic1111 — sample image

How to use Gradio share and gradio-auth instead of Ngrok

1) Launch Automatic1111 in Terminal 1 using Gradio share.

source /home/ec2-user/SageMaker/miniconda/bin/activate
conda activate a1111
bash webui.sh --share --xformers --gradio-auth username:password --theme dark
[Note] Change username:password accordingly
Open Public URL

2) Login with your username and password.

Public URL — Login

Amazon SageMaker Notebook Actions

Stop/Start/Delete SageMaker Notebook Instance

Amazon SageMaker Notebook — Actions

⚠️ When you stop a notebook, SageMaker terminates the notebook’s Amazon Elastic Compute Cloud (Amazon EC2) instance. Packages that are installed in the Conda environment don’t persist between sessions by default. The /home/ec2-user/SageMaker directory is the only path that persists between notebook instance sessions. This is the directory for the notebook’s Amazon Elastic Block Store (Amazon EBS) volume.

Automatic1111 Start scripts after Stop and Start using Ngrok

a1111-start.sh

source /home/ec2-user/SageMaker/miniconda/bin/activate
conda activate a1111
bash webui.sh --theme dark

a1111-ngrok-start.sh

source /home/ec2-user/SageMaker/miniconda/bin/activate
conda activate a1111
cp /home/ec2-user/SageMaker/ngrok.yml /home/ec2-user/.ngrok2/ngrok.yml
ngrok http http://localhost:7860/ --basic-auth 'username:password'

References:

Related Articles:

--

--