🙌

Stable Diffusion Web UIをDatabricks上で動かす

2023/11/24に公開

Stable Diffusion Web UIをDatabricks上で動かす

Stable DiffusionのUIとしてメジャーな「Stable Diffusion web UI」をDatabricks上で動かします。

  • Stable Diffusion web UI

https://github.com/AUTOMATIC1111/stable-diffusion-webui

Stable Diffusionを快適に動かすにはGPUを使うのが望ましいのですが、ローカルPCにNVIDIA GeForceなどが搭載されていない場合は、クラウドのGPUリソースを使用するというオプションもあります。

Stable Diffusion web UIのGithub上にもインフラとしてして使用できるクラウドサービス一覧が掲載されていますが、Databricksの記載がまだありません。
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Online-Services

ということで、Databricks上のGPUノードで動かしてみましょう。

環境

DB Runtime: 14.1 ML GPU
ノードタイプ:g5.xlarge (NVIDIA A10g × 1)

前提

Unity Catalogが有効化されていることが前提です。
まだという方はこちらの手順を参考に有効化ください。

https://docs.databricks.com/en/data-governance/unity-catalog/get-started.html

1. Unity Calalog VolumeにCheckpointファイルなどをダウンロード

Unity Catalogの当該Metastore内にすでにhiroshiというカタログ、stablediffusionというスキーマが存在しているものとします。その中に、model_repoというボリュームを新規作成します。

USE CATALOG hiroshi
USE SCHEMA stablediffusion
CREATE VOLUME model_repo

次に以下のCheckpoint、VAE、Negative Embeddingをそれぞれダウンロードし、ボリュームに格納していきます。

def file_exists(path: str):
  if path[:5] == "/dbfs":
     import os
     return os.path.exists(path)
  else:
     try:
         dbutils.fs.ls(path)
         return True
     except Exception as e:
         if 'java.io.FileNotFoundException' in str(e):
             return False
         else:
             raise

CATALOG_NAME='hiroshi'
SCHEMA_NAME='stablediffusion'
VOLUME_NAME='model_repo'
VOLUME_PATH=f"/Volumes/{CATALOG_NAME}/{SCHEMA_NAME}/{VOLUME_NAME}"

%cd /tmp

if not file_exists(f"{VOLUME_PATH}/epicphotogasm_lastUnicorn.safetensors"):
  !wget --content-disposition https://civitai.com/api/download/models/223670
  dbutils.fs.mv('file:/tmp/epicphotogasm_lastUnicorn.safetensors', VOLUME_PATH)

if not file_exists(f"{VOLUME_PATH}/vae-ft-mse-840000-ema-pruned.safetensors"):
  !wget https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors
  dbutils.fs.mv('file:/tmp/vae-ft-mse-840000-ema-pruned.safetensors', VOLUME_PATH)

if not file_exists(f"{VOLUME_PATH}/EasyNegative.safetensors"):
  !wget https://huggingface.co/datasets/gsdf/EasyNegative/resolve/main/EasyNegative.safetensors
  dbutils.fs.mv('file:/tmp/EasyNegative.safetensors', VOLUME_PATH)

2. Stable Diffusion Web UIをダウンロード

Stable Diffusion Web UIのリポジトリーをノードのローカルディスクにクローンし、Checkpoint、VAEなどをUnity Catalogから当該ディレクトリーにコピーする。

%cd /tmp
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui

dbutils.fs.mv(VOLUME_PATH+'/epicphotogasm_lastUnicorn.safetensors', 'file:/tmp/stable-diffusion-webui/models/Stable-diffusion/')

dbutils.fs.mv(VOLUME_PATH+'/vae-ft-mse-840000-ema-pruned.safetensors', 'file:/tmp/stable-diffusion-webui/models/VAE/')

dbutils.fs.mv(VOLUME_PATH+'/EasyNegative.safetensors', 'file:/tmp/stable-diffusion-webui/embeddings/')

3. Stable Diffusion Web UIを実行

%cd /tmp/stable-diffusion-webui
!python launch.py --share --xformers --enable-insecure-extension-access --gradio-auth YOUR_ID:YOUR_PASSWORD

起動に成功すると、セルの実行ログに以下のようにGradioのPublic URLが表示されるので、Webブラウザからアクセスください。
なお、Web UI実行中はセルがずっと実行中になります。止めたい場合は「キャンセル」をクリックください。
また、GradioのURLはPublicのため、誰でもアクセスが可能です。従って、セキュリティ面をケアする場合は少なくとも--gradio-auth YOUR_ID:YOUR_PASSWORDオプションによって、ログイン認証を必須としてください。

Running on public URL: https://XXXXXXXXXXXX.gradio.live

参照

BFN!

Discussion