Setting up Stable Diffusion API on a Google Cloud VM instance.

Stable Diffusion Tutorial: Creating an API on GCP VM Instance

Understanding Stable Diffusion: A Revolutionary AI Tool

Stable Diffusion is an advanced deep learning model introduced in 2022, designed to transform textual descriptions into stunning images. This state-of-the-art text-to-image model has gained immense popularity for its ability to generate detailed visuals based on simple text inputs. Beyond image generation, Stable Diffusion serves multiple purposes such as inpainting, outpainting, and generating image-to-image translations guided by text prompts. Stability.ai has played a pivotal role in the model's development, ensuring it remains at the forefront of creative AI technology.

How to Create a Google Cloud Platform (GCP) Account

If you’re looking to harness the power of Stable Diffusion, the first step is creating a GCP account. Follow these steps:

  1. Visit the GCP Free Tier page.
  2. Set up a billing account, as GPU usage requires billing information.
  3. Consider setting a Budget with alerts to manage costs effectively.

Requesting GPU Access in Your GCP Project

After creating your GCP account, you’ll need to enable the Compute Engine API:

  1. Navigate to the APIs & Services page.
  2. Search for the Compute Engine API and click Enable.
  3. Request permission to create virtual machines with GPU by going to the Quotas page. Filter for GPUs and request an increase from 0 to 1, specifying a reason like using an ML model requiring a GPU.

Approval might take a few days, so patience is key.

Creating a Virtual Machine (VM) Instance

Next, you’ll need to create a VM instance:

  1. Go to GCP Instances Page and click Create instance.
  2. Name your instance (e.g., stable-diffusion-instance) and select your desired region.
  3. In the Machine configuration, select GPU. The A100 provides top performance, but the T4 is more budget-friendly and effective for most uses.
  4. Machine Type: Select n1-standard-4 with at least 15GB of memory.
  5. For the Boot Disk, change it to a Deep Learning VM based on Debian 10, and increase disk size if needed.
  6. Under Firewall, check Allow HTTP traffic and Allow HTTPS traffic.
  7. In Networking, add a network tag (e.g., stable-diffusion-tag).

One advantage of GCP instances is that you only pay when they are active.

Creating a Firewall Rule for Your Instance

To ensure your instance is accessible from the internet, create a firewall rule:

  1. Go to the Firewall Rules section and click Create Firewall Rule.
  2. Name it stable-diffusion-rule and select Tags to add your earlier tag.
  3. In Source IP ranges, add 0.0.0.0/0 to allow access from any IP.
  4. In Protocols and ports, specify tcp:5000 and click Create.

Accessing and Configuring Your Compute Instance

To access the instance, use SSH through the console. On first login, you’ll need to install the Nvidia driver by typing Y. Note that you may need to reinstall drivers upon restarting your VM.

Setting Up Stable Diffusion

Once the instance is configured, you’ll clone two repositories and install Cog, as detailed on its GitHub page. After setting up, build your Docker image which will require downloading weights from Hugging Face:

  1. Create an account on Hugging Face.
  2. Generate an Auth token from your settings page.
  3. Utilize your token to download the necessary model weights.

Once everything is configured, you can test if the model runs successfully.

Testing Your Setup

After running the model, an output file named output-1.png should appear. Download it through the terminal to verify successful operation. Additionally, you can retrieve your compute instance's external IP to access the service over the internet.

Decoding API Responses

When testing the API, note that responses are base64 encoded. To decode, use online tools such as Code Beautify for convenience.

Exploring Extra Features

For those interested in video generation, the Deforum model is a great option for creating short videos using similar methods. Make sure to stop current containers when trying out new models!

Conclusion

Thank you for following this tutorial! If you found it helpful, check out more resources on our tutorial page and continue exploring the capabilities of Stable Diffusion.

This guide aims to empower you to harness the potential of Stable Diffusion and GCP for your creative projects. Happy computing!

Back to blog

Leave a comment