Create Your Own Stable Diffusion UI on AWS in Minutes

Create Your Own Stable Diffusion UI on AWS in Minutes

Deploy a text-to-image web app with just one command

Image by author — created with Stable Diffusion

What is this about?

Stable Diffusion (SD) has quickly become one of the most popular text-to-image (a.k.a. “AI Art Generation”) models in 2022. One key factor contributing to its success is that it has been made available as open-source software. This spawned a vibrant community that quickly built tools to make SD more accessible to anyone interested in it, regardless of their technical knowledge.

One of those tools is the simple yet powerful web interface stable-diffusion-webui by Automatic1111. It allows us to use all the features of SD without any coding and it is also open-source, which means anyone can download this Web UI as well as the SD model and deploy them anywhere they want. The challenge, however, is that SD still requires GPU power to run, lest we want to wait minutes for the generation of one image. And many of us don’t own GPUs powerful enough to run the model.

Thanks to Cloud Computing we don’t have to spend a small fortune on a GPU, we can just “rent” one instead. So, in this tutorial we will deploy the Automatic1111 Web UI on an AWS EC2 instance equipped with a GPU powerful enough to run Stable Diffusion. And we will do this with just one command using an AWS CloudFormation template to set up all the infrastructure required.

As always you will find the code for this tutorial in my GitHub account.

Why is it important?

Having the Stable Diffusion model and even Automatic’s Web UI available as open-source is an important step to democratising access to state-of-the-art AI tools. But it’s not sufficient because the GPU requirements to run these models are still prohibitively expensive for most consumers. The GPUs required to run these AI models can easily exceed $2,000.

This tutorial shows how we can get started with just $0.53 per hour, which is the on-demand price for a g4dn.xlarge instance on AWS. It lets us use an NVIDIA T4 GPU with 16 GB of VRAM. That means we can run the app for a few hours to try it out and generate the images we want and then shut down the EC2 instance and not pay any more than what we used it for. And all of this with just one click of a button, no coding or Linux experience required, thanks to the AWS CloudFormation template.

One disclaimer before we get started: I’m obviously biased towards AWS since I work there. But the key message I want you to take away from this tutorial is that state-of-the-art AI is more accessible and affordable than ever before through cloud computing, no matter which provider you end up choosing.

Prerequisites

To follow this tutorial we will need an AWS account, and that’s pretty much the only prerequisite. Having the AWS Command Line Interface (CLI) installed on your local machine makes things even easier, but I will also demonstrate how to follow the tutorial without it and just using the AWS console instead.

Quickstart Guide

As mentioned in the intro we will be using a CloudFormation (CF) template to set everything up with just one comand. After launching the template it will take 15–20 minutes until the app is ready, so here is my tip: Launch the template right now and then come back to this blog post to dive deeper into what is actually happening in the background ?

Launching the app in AWS CLI

Copy the CF template (or, alternatively, clone the entire repo) and run the command below on your local machine. This will create a CF stack called “sd-webui-stack” in your AWS account.

https://medium.com/media/af0e3d2ed8bcf85d1e9b5c7f12d6218f/href

Launching the app in the AWS console

In the AWS console, navigate to the CloudFormation section, and choose “Create Stack -> With new resources”:

Image by author

In the following dialogue, choose “Template is ready” and “Upload a template file”:

Image by author

Choose the CF template file from the repo to upload, name the stack “sd-webui-stack”, leave the default settings in the following dialogues, and click “Submit” in the last dialogue. This will create the stack with all the required resources in your AWS account.

A peek behind the curtain

Now that we have launched the CF template we can pull away the curtain and dive deeper into what is actually happening in the background.

The CloudFormation Template

Let’s first have a look at the CF Template:

https://medium.com/media/eeff80f3e5df216a9bd69de1b1de7f11/href

This template sets up a few resources that are required for the application. First we create a “Security Group” to specify which ports will be open on the EC2 instance. We choose port 22 because we want to be able to connect to the instance via SSH and port 7860 because that’s where our app will be listening.

Next we set up the EC2 instance that will host the application. In this template I chose a Ubuntu Server 22.04 LTS distribution (AMI ID ami-0574da719dca65348), simply because that’s what I’m most familiar with. You can change this to a different distribution if you’d like, but note that you would have to amend the setup script (discussed below) accordingly. We choose a g4dn.xlarge instance, as discussed above. We also provision 300 GB of disk space to make sure that we have enough space to host a number of different models. Finally we run the setup script on the EC2 instance, which we will discuss in the next section.

Next we create an Elastic IP address and assign it to our EC2 instance. This allows us to have a permanent IP address, so even when we shut down the EC2 instance and restart it at a later point our app will always be hosted on the same IP address.

The setup script

As mentioned, we are running a setup script on the EC2 instance which will execute a number of commands to set everything up for the Web UI. Let’s go through it step by step.

The first section disables the Ubuntu restart dialogue after installing packages and then installs a number of packages that we will need:

https://medium.com/media/c9379fb37b7c087647014d88d190a74f/href

This next section downloads and installs the CUDA driver so that we can access the GPU of the machine:

https://medium.com/media/f72299518cd58230c0322109f1ebe9fe/href

After that we need to install Git Large File Storage because we will download a Stable Diffusion model which is about 5 GB:

https://medium.com/media/aa3297c4394187cfc666b819b78a4551/href

Now that Git LFS is installed we can download the model from the Hugging Face Model Hub. Note that we enabled the option “skip-smudge” which allows us to just download the specific files we need. In this tutorial we are downloading SD v2.1 (512px version) and move it into the directory that the Web UI expects the model to be.

https://medium.com/media/c731ba2232720abcbcfa834e2f192c24/href

Note that you can change the script to download a differenct version of Stable Diffusion, for example version 1.5. You can also add as many models you want into the UI at a later point by putting them into the odel directory.

In addition to the model we also need a configuration file that is read by the WebUI. We download a config file from the Stable Diffusion Github repo and rename it to match the model name and put it also into the same directory:

https://medium.com/media/1010747a62046cd83042e25a4e1a3a61/href

Lastly, we change the ownership of the WebUI to the user ubuntu and kick off the the server as that user (because user root is not allowed to start the application):

https://medium.com/media/95e6826fa2d038247b920bce531f7ac9/href

Testing the Web UI

After 15–20 minutes the deployment should be completed. We can retrieve the IP address of the EC2 instance by running the following command:

https://medium.com/media/e5f791223db1d3e462068d48bd13969c/href

We can also retrieve it by accessing the EC2 panel in the AWS console:

Image by author

Once we have retrieved the IP address we can open the app by navigating in the browser to <IP address>:7860 (if the request times out the installation hasn’t finished yet). Once the installation is completed we can see that the app is up and running ?

Shutting down the EC2 instance and restart the app

Even with only $0.53 per hour we obviously don’t want to run the instance when we don’t use it. We can stop the instance in the AWS console and restart it when we need it again without losing any of the installed apps. Once we restarted the EC2 instance we can log into it via SSH and restart the app with the following command:

https://medium.com/media/5ee668877e7a53b1b4d12d24566167c2/href

Deleting everything

If we want to delete all the resources we created (i.e. Security Group, EC2 instance, Elastic IP), we can just delete the CF stack with the following command (or delete it in the AWS console):

https://medium.com/media/9611390ed902c6f04a7363cf364b71a5/href

Limitations

I would like to stress that this tutorial is a mere starting point for anyone who wants to try out using Stable Diffusion via a Web UI. There are several limitations in this approach which could be addressed if one wants to use this app in production. In particular I did not address any security concerns (note that the app is running over http), scaling concerns (if this app is supposed to serve many users at the same time), and many other aspects.

If we wanted to use this app in production we could use the AWS Well-Architected Framework as a starting point.

Conclusion

In this tutorial we have set up a Web UI for Stable Diffusion with just one command thanks to the CF template. This allows us to access state-of-the-art AI models without having to commit to buying expensive hardware ourselves.

There are many items in the backlog for this app, I listed them here. Any contributions to the repo are more than welcome ☺️

Heiko Hotz

? Follow me on Medium and LinkedIn to read more about Generative AI, Machine Learning, and Natural Language Processing.

? If you’re based in Lonon join one of our NLP London Meetups.

? And if you’re interested in learning more about how I might potentially help with adopting AI & ML in your organisation, reach out on aiml.consulting.


Create Your Own Stable Diffusion UI on AWS in Minutes was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.

This content was originally published here.