NovelAI 5ch Wiki - 入門用Notebook
学習は別のでやる流れだけど生成には便利に使えるはず。

Remote UI(1111を含む)の利用が禁止になりました。
以後、ブラウザで操作するタイプのソフトウェアは使わないでください。



最新版はこちら


今後の更新はありません。

最終版のセル(参考)
#@title ↓再生ボタンを押してね

#@markdown 実行は自己責任で。

#@markdown gradioログイン情報(パスワード空白時は自動生成)
user = "user"#@param {type:"string"}
password = ""#@param {type:"string"}



#@markdown 環境調査(2回目以降は飛ばしてもいい)
use_envcheck = True #@param {type:"boolean"}
if use_envcheck:
  # Ubuntu
  !uname -m && cat /etc/*release
  res = !cat /etc/*release | grep 'VERSION_ID' | grep -o '[0-9.]*'
  if not res[0].startswith('20.04'):
    raise RuntimeError("Ubuntuのバージョンが変わった " + res[0])

  # Python
  res = !python -V | grep -o '[0-9.]*'
  if not res[0].startswith('3.9.'):
    raise RuntimeError("Pythonのバージョンが変わった " + res[0])

  # GPU/CUDA
  !nvidia-smi
  res = !nvcc -V | grep 'Cuda compilation tools, release' | grep -o '[0-9.]*'
  if not res[0].startswith('11.8'):
    raise RuntimeError("CUDAのバージョンが変わった " + res[0])

  # RAMの容量取得(GiB)
  res = !free -m | grep 'Mem:' | awk '{print $2}'
  if not res:
    raise ValueError("RAMが取得できない")
  ram = round(float(res[0]) / 1024, 3)
  if ram < 12:
    raise ValueError("RAMが思ったより少ない" + ram)
  print(f"RAM : {ram}")

  # VRAMの容量取得(GiB)
  res = !nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits
  if not res:
    raise ValueError("VRAMが取得できない(GPUに繋がってないかも)")
  vram = round(float(res[0]) / 1024, 3)
  if vram < 14:
    raise ValueError("VRAMが思ったより少ない " + vram)
  print(f"VRAM: {vram}")
  del res

### セットアップ

%cd /content

#@markdown # Pythonインストール

#@markdown miniconda 3.10.9
use_conda = False #@param {type:"boolean"}
#@markdown apt-get 3.10.10 こっちは最近調子悪いみたい
use_apt = False #@param {type:"boolean"}

if use_conda and use_apt:
   raise ValueError("どっちやねん")

if use_conda:
   !wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
   !bash Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local

if use_apt:
  !sudo apt autoremove
  !sudo apt-get -y update
  !sudo apt-get -y install python3.10
  !sudo cp `which python3.10` /usr/local/bin/python

# Pythonのバージョン確認
!python -V

# pipを最新に
!wget https://bootstrap.pypa.io/get-pip.py -O - | sudo python

#@markdown xformersインストール(当面torch1のみ)
use_xformers = False #@param {type:"boolean"}

#@markdown torch2インストール(use_conda必須)
use_torch2 = False #@param {type:"boolean"}
if use_torch2:
  !pip install --upgrade torch torchvision --extra-index-url https://download.pytorch.org/whl/cu118
# torch1をアップデート(Python 3.10の時だけ)
elif use_conda or use_apt:
  !pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117

if use_xformers:
  !pip install -U --pre triton
  !pip install xformers==0.0.16rc425

# これが無いと動かないので
!pip install fastapi==0.90.1

### パスワード関連

# デフォルトパスワード生成
import secrets
import string

def get_password(length: int) -> str:
    # secretsはrandomとかと違って暗号学的に安全な乱数を生成できる。
    pass_chars = string.ascii_letters + string.digits
    password = ''.join(secrets.choice(pass_chars) for i in range(length))
    return password

if not password:
    password = get_password(64)

del secrets
del string
del get_password

### WEBUIのセットアップ

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

#@markdown モデルのURL(複数いるならFilerでダウンロードしましょう)
ckpt_url = 'https://huggingface.co/aka7774/fp16_safetensors/resolve/main/7th_anime_v3_A-fp16-1833-2982-0018.safetensors'#@param {type:"string"}
# ダウンロードにトークンの要らないモデルなのでコメントアウト
##@markdown トークンはここで貰ってきて https://huggingface.co/settings/tokens
#user_token = ''#@param {type:"string"}
#user_header = f"\"Authorization: Bearer {user_token}\""
#!wget -c --header={user_header} "{ckpt_url}" -P /content/stable-diffusion-webui/models/Stable-diffusion/
#del user_header
if ckpt_url:
   !wget -c "{ckpt_url}" -P /content/stable-diffusion-webui/models/Stable-diffusion/

# @markdown aria2cで落としたいモデルのURL(なければ空白)
aria_url = ''#@param {type:"string"}
if aria_url:
    !apt install -y -qq aria2
    !aria2c --summary-interval=10 -x 3 --allow-overwrite=true -Z {aria_url}

import os

#@markdown # Extensions
%cd /content/stable-diffusion-webui/extensions/

#@markdown --enable-insecure-extension-access
use_eiea = False #@param {type:"boolean"}

#@markdown ImagesとFilerはColabで便利なので入れましょう
use_filer = True #@param {type:"boolean"}
if use_filer:
    #!git clone https://github.com/aka7774/sd_images_browser.git
    !git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser.git
    !git clone https://github.com/aka7774/sd_filer.git

#@markdown 型抜き系(Katanuki rembg PBRemTools)
use_katanuki = False #@param {type:"boolean"}
if use_katanuki:
    !git clone https://github.com/mattyamonaca/PBRemTools.git
    !git clone https://github.com/aka7774/sd_katanuki.git
    !git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg.git

#@markdown 学習関連(dreambooth, Hypernetwork-MonkeyPatch)
use_train = False #@param {type:"boolean"}
if use_train:
    !git clone https://github.com/d8ahazard/sd_dreambooth_extension.git
    !git clone https://github.com/aria1th/Hypernetwork-MonkeyPatch-Extension.git

#@markdown マージ関連(supermerger, MBW)とモデル変換(toolkit)
use_merge = False #@param {type:"boolean"}
if use_merge:
    !git clone https://github.com/hako-mikan/sd-webui-supermerger.git
    !git clone https://github.com/bbc-mc/sdweb-merge-block-weighted-gui.git
    !git clone https://github.com/aka7774/sd_merge_numa.git
    !git clone https://github.com/arenatemp/stable-diffusion-webui-model-toolkit.git

#@markdown LoRA関連(additional-networks, locon, two-shot)
use_lora = False #@param {type:"boolean"}
if use_lora:
    !git clone https://github.com/kohya-ss/sd-webui-additional-networks.git
    !git clone https://github.com/KohakuBlueleaf/a1111-sd-webui-locon.git
    !git clone https://github.com/opparco/stable-diffusion-webui-two-shot.git

#@markdown ControlNet(fp16モデルつき)
use_controlnet = False #@param {type:"boolean"}
if use_controlnet:
  !pip install prettytable
  !git clone https://github.com/Mikubill/sd-webui-controlnet.git
  %cd sd-webui-controlnet
  !git clone https://huggingface.co/webui/ControlNet-modules-safetensors.git
  for fi in os.listdir("ControlNet-modules-safetensors"):
    os.rename(f"ControlNet-modules-safetensors/{fi}", f"models/{fi}")
  %cd ..
  !git clone https://github.com/gogodr/sd-webui-stopmotion.git

#@markdown aka: fake_xyplot, curly_rand, copyguard
use_aka = False #@param {type:"boolean"}
if use_aka:
    !git clone https://github.com/aka7774/sd_fake_xyplot.git
    !git clone https://github.com/aka7774/sd_curly_rand.git
    !git clone https://github.com/aka7774/sd_copyguard.git

#@markdown aka: Gimai(ゲームの会話シーン制作)
use_gimai = False #@param {type:"boolean"}
if use_gimai:
  !sudo apt install ffmpeg
  !ffmpeg -version
  !git clone https://github.com/aka7774/sd_gimai.git
  %cd sd_gimai
  !git clone https://huggingface.co/spaces/skytnt/moe-tts.git
  %cd ..

# 一度起動して必要なものをインストールする
%cd /content/stable-diffusion-webui/
!COMMANDLINE_ARGS="--exit" REQS_FILE="requirements.txt" python launch.py

#@markdown ## Google Driveのマウント(任意)
use_drive = False #@param {type:"boolean"}
if use_drive:
  from google.colab import drive
  drive.mount('/content/drive')
  # filerのデフォルトバックアップ先を設定
  !mkdir -m 0777 /content/stable-diffusion-webui/extensions/sd_filer/config
  !echo '{"backup_dir":"/content/drive/MyDrive/sd"}' > /content/stable-diffusion-webui/extensions/sd_filer/config/config.json

# WEBUIの起動
!COMMANDLINE_ARGS="{'--xformers' if use_xformers else ''} {'--opt-sdp-no-mem-attention' if use_torch2 else ''} --opt-channelslast {'--enable-insecure-extension-access' if use_eiea else ''} --share --gradio-auth {user}:{password}" REQS_FILE="requirements.txt" python launch.py

更新について


pipの問題で動かなくなりがちなのでコメント等を見るといいかも。

更新履歴

2023-04-05
  • 本体の動作確認できてないんだけど、気になったExtensionを追加した。
    • PBRemTools AlUlkesh/stable-diffusion-webui-images-browser sd-webui-stopmotion
2023-03-19
  • torch2に対応しました。めちゃ速くなるので楽しい。
  • xformersを入れなくできるようにした(torch2では不要だし)
  • 環境調査をスキップできるようにした
  • Extensionsを更新(LyCoris動かす用など)
  • --enable-insecure-extension-accessを入れられるようにした
2023-03-16
  • Pythonのバージョン指定を次の3つに変更しました
    • Python 3.9.16 Colabデフォルト 時短になるのでおすすめ
    • Python 3.10.9 use_conda 最新版を入れるので将来おかしくなる可能性あり 今3.9だと困る場合にどうぞ
    • Python 3.10.10 use_apt 従来通り。今はうまく動いていない?
  • Python 3.9.16選択時にはデフォルトのtorchを使うようにしました 時短
  • 環境チェックを厳格にしてRuntimeErrorを返すようにしました
2023-02-15
  • ControlNet(fp16)に対応しました
  • 学習はkohya_train_network_simpleで出来るのでExtensionを外しました
  • fastapiのバージョンを0.90.1に固定しました
  • Pythonが3.10.10になってました(悪影響は無いはず)

segがエラーで動かんなあ。prettytableは入れたつもりなんだけど。
ModuleNotFoundError: No module named 'lib2to3'
2023-01-24
  • Python 3.10にきちんと対応(xformersがすんなり入るよ、やったね)
  • モジュールのバージョンをあげました
  • 流行りのExtensionsに入れ替え(面倒なので今後はやらんかも)
  • モデルのURLをVAE内蔵の7th3Aにしました(ダウンロードが早く済むはず)
2023-01-14
  • images_browserを修正版に取り替えた
  • その他こまごまとExtensions追加
2022-12-20
  • condaかpipか選べるようにした。エラー出た時の予備になるかも?
  • 階層マージ遊びが出来るExtensionをインストールできるようにした(たぶん4GBのモデルならいける)
  • モデルのURLを3つまで入れられるようにした(もっと入れたければ改造して)
  • SD2.0系は未対応(需要あれば対応する)

初心者向け手順

  1. Googleアカウントを用意する(普段使いとは分けるのがおすすめ)
  2. 次のページを読んで基礎を理解する
    1. https://www.python.jp/train/experience/colab.html
    2. https://www.python.jp/train/experience/exec-python...
  3. 「1+1」のかわりに 以下の黄色い枠内の内容をコピペして 上のリンク先のnotebook内容をコピーして実行
  4. 実行結果に「Running on public URL: https://xxxxx.gradio.live」と出てくるのでそこに飛んで、usernameとpasswordを入力する
    1. usernameはデフォルト実行ならuser。passwordは自動生成されているので「Launching Web UI with arguments:」の行を参照

注意

Notebookを新規作成できたら、最初に Rumtime - Change runtime type で GPU を選ぶ

もう最初っから選ばれてるかも。


このNotebookではいくつかのExtensionが自動インストールされる
  • 重たいやつはチェックボックスで入れるかどうか選べる
モデルを増やしたい時はFilerでダウンロードしよう

ダウンロードしたいモデルのURLを用意しましょう


Google Driveのマウントはしなくても動かせるのでお好みで
  • マウントしないと全部消えるので後腐れが無い
  • マウントするとデータの保存が楽になる

赤ちゃん用Notebook

グラボもねえ!知識もねえ!金もそれほど持ってねえ!
でもローカル使ってみたいンゴ・・・!という人でも使える(かもしれない)Notebookが公開されている
上記の入門用Notebookですらうまくいかなかった人用
※高頻度で更新されているので、このページの説明と違う箇所もあるかもしれない

入門用Notebook(旧バージョン)