Deploy ComfyUI on Google Colab

Introduction

ComfyUI is a powerful and modular GUI and backend for working with diffusion models, particularly Stable Diffusion. It offers a node-based interface for creating complex image generation workflows without coding. This guide will show you how to set up and run ComfyUI on Google Colab.

Setting Up the Environment

First, we need to set up the environment on Google Colab. You can choose to use Google Drive for storage or just rely on Colab's temporary storage.

Optional: Mount Google Drive

If you want to save your models and outputs persistently, you can mount your Google Drive. This is useful for larger models or if you want to access your data later.

# Set USE_GOOGLE_DRIVE to True
USE_GOOGLE_DRIVE = True

if USE_GOOGLE_DRIVE:
    print("Mounting Google Drive...")
    from google.colab import drive
    drive.mount('/content/drive')
    WORKSPACE = "/content/drive/MyDrive/ComfyUI"
    %cd /content/drive/MyDrive
else:
    WORKSPACE = 'ComfyUI'

Clone ComfyUI Repository

Now, clone the ComfyUI repository from GitHub.

![ ! -d $WORKSPACE ] && echo -= Initial setup ComfyUI =- && git clone https://github.com/comfyanonymous/ComfyUI
%cd $WORKSPACE

Update ComfyUI (Optional)

If you want to ensure you have the latest version, pull the latest changes.

# Set UPDATE_COMFY_UI to True
UPDATE_COMFY_UI = True

if UPDATE_COMFY_UI:
  print("-= Updating ComfyUI =-")
  !git pull

Install Dependencies

Install the required Python packages.

print("-= Install dependencies =-")
!pip install -r requirements.txt

Note: Colab may have certain torch versions pre-installed. You can see these versions by running !pip list | grep torch. When installing requirements, you may need to use the --extra-index-url flag to specify a torch version compatible with your system.

For example:

!pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu121 --extra-index-url https://download.pytorch.org/whl/cu118 --extra-index-url https://download.pytorch.org/whl/cu117

Downloading Models and Other Resources

You'll need to download some models, checkpoints, and other resources to get started. Here's how to download a basic set of models, including Stable Diffusion 1.5:

Stable Diffusion 1.5

!wget -c https://huggingface.co/Comfy-Org/stable-diffusion-v1-5-archive/resolve/main/v1-5-pruned-emaonly-fp16.safetensors -P ./models/checkpoints/

VAE

!wget -c https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -P ./models/vae/

You can uncomment other commands in the provided Jupyter notebook to download additional models like SDXL, ControlNet, etc.

Running ComfyUI

There are several ways to run ComfyUI on Colab. We recommend using cloudflared for the best experience.

Using Cloudflared

Cloudflared creates a secure tunnel to your Colab instance, allowing you to access the ComfyUI web interface easily.

  1. Install Cloudflared:

    !wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
    !dpkg -i cloudflared-linux-amd64.deb
    
  2. Run ComfyUI with Cloudflared:

    import subprocess
    import threading
    import time
    import socket
    import urllib.request
    
    def iframe_thread(port):
      while True:
          time.sleep(0.5)
          sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          result = sock.connect_ex(('127.0.0.1', port))
          if result == 0:
            break
          sock.close()
      print("\nComfyUI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\n")
    
      p = subprocess.Popen(["cloudflared", "tunnel", "--url", "http://127.0.0.1:{}".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      for line in p.stderr:
        l = line.decode()
        if "trycloudflare.com " in l:
          print("This is the URL to access ComfyUI:", l[l.find("http"):], end='')
        #print(l, end='')
    
    threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()
    
    !python main.py --dont-print-server
    

    This will output a trycloudflare.com URL that you can use to access the ComfyUI interface in your browser.

Alternative Methods

The Jupyter notebook also provides instructions for running ComfyUI with localtunnel or using Colab's iframe. However, cloudflared is generally the most reliable method.

Conclusion

That's it! You've successfully set up and deployed ComfyUI on Google Colab. You can now start experimenting with different models and creating your own unique image generation workflows. Remember to consult the ComfyUI Examples page for inspiration and guidance on using the various features of ComfyUI.