> For the complete documentation index, see [llms.txt](https://ludela.gitbook.io/paperspace-gradientdenovelairkaruwo8dorudenikasu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ludela.gitbook.io/paperspace-gradientdenovelairkaruwo8dorudenikasu/noki.md).

# 導入の手引き

<figure><img src="/files/thS1blvieiyQGbgdNURZ" alt=""><figcaption></figcaption></figure>

Paperspace gradientでのNovelAIローカル版のインストールから起動までを解説する。

導入はさして難しく無いが初回はダウンロード等場合によって時間かかるので初回は30分程度かかってしまうかもしれない。2回目以降に使うときは1,2分ですぐ起動して使えるはず。

ざっくりした全体の流れは次の通り。

1. Paperspace gradientに登録する
2. GPUマシン環境を借りる
3. コードのコピペでstable diffusion、NovelAIモデルの準備
4. WebUIを起動！

### 1. Paperspace gradientに登録

これに関しては一般的なサイト登録操作と同じなため省略する。

Paperspace gradientでググるなり下記リンクから登録すればよい。

{% embed url="<https://www.paperspace.com/gradient>" %}

一応我田引水しておくと、下のリンクは最初に10ドル分無料枠が入る紹介クーポン付きリンクになっている。（紹介した私にも15ドル無料枠が入るアフィリエイト的なやつ）

{% embed url="<https://console.paperspace.com/signup?R=WBLNVW6>" %}

一人1アカウントしか持てないのでどんなサインアップ方法だったか確認できないが、paperspaceの中でもgradientというサービスのProプラン（月8ドル）に加入しとけばとりあえず問題ないと思う。ちなみに多分ProプランからGrowthプランにアップグレードしても8ドルは返ってこないので、最初からGrowth使う予定ならGrowthプランで入った方が良いかも。無料プランでも多分問題ないと思うがもしかしたら今回解説する方法ではstable diffusionを動かせないかもしれない。

### 2.  GPUマシン環境を借りる

①ログイン後のトップページから「CREATE PROJECT」を押してプロジェクトを新規作成する。

<figure><img src="/files/dHQ5NElgTtlAkjJ7V3yq" alt=""><figcaption><p>新規登録時点では少し違う画面である可能性がある。</p></figcaption></figure>

②プロジェクト名を入力して「CREATE」ボタンを押す。（こだわりがなければ自動入力された名前をそのまま使っても良い）

<figure><img src="/files/f0gaU22tSEHZiSgsuzOk" alt=""><figcaption></figcaption></figure>

③プロジェクトトップページに飛ぶので「CREATE」を押す。

<figure><img src="/files/I6LZyfiU7r45zHr5BI0J" alt=""><figcaption></figcaption></figure>

④借りるマシンの環境を下画像の通り設定し、マシン（Notebook）を起動する。文字で書くと

(1) 「Select a runtime」の項は「show more」を押して下の方にある「Start from scratch」を選択。

(2)「Select a machine」の項は「Free-OOO」で始まるもののうちで一番下に出てくるやつを選ぶ。（できればProプランならA4000かRTX5000、GrowthプランならA100かA6000を選びたい。出て来なくても何度かページ更新してれば空くかも。別に後ですぐ変更できるので適当でも良いが。）

(3)「Auto-shutdown time」を6 hours（最大）に設定。

(4)「START NOTEBOOK」を押す。

<figure><img src="/files/8RjzerzEaJB3tniymA2w" alt=""><figcaption></figcaption></figure>

（画面遷移が起きない場合はエラーが起きてる。大抵はページを更新すれば直る。GPUがちょうどいっぱいになったせいなこととかの場合はGPUを変更する必要がある。）

### 3. stable diffusion、NovelAIモデルの準備

ソフトを環境にインストールしモデルファイルをダウンロードする。

⑤左のメニューの下にあるオレンジのマークを押す。

<figure><img src="/files/YftrLX15Qu6jLd8LNvWx" alt=""><figcaption><p>*paperspace gradient自体もnotebookUIを提供しているが個人的経験としてjupyterlabのUIを使った方が安定する気がする</p></figcaption></figure>

⑥下画像の通りNotebookのPython3を押す。（この画面になっていなければ、左の青地に白十字のボタンを押してから操作する。）

<figure><img src="/files/PWnVnnDd3TQc2l84mRhs" alt=""><figcaption></figcaption></figure>

⑦新しいファイルが作成され、左のサイドバーから確認できる。そのままでも動作は可能だがわかりやすいようにファイル名を右クリック→「Rename」で「stable-diffusion.ipynb」などに名前を変更。

<figure><img src="/files/nlXxiPhM9XO4xeWgAy91" alt=""><figcaption></figcaption></figure>

⑧下記の文を全部コピーして右の空欄に貼り付ける。

文章の意味はさっぱりわからないかもしれないがこれはstable diffusion webuiをインストールしてNovelAIリーク版のモデルをダウンロードするおまじないである。

他のモデルのファイルを追加する方法についてはTipsにて紹介する。

```bash
# 初回のみのおまじない（stable diffusion webuiのインストール、モデルのダウンロード）
%mkdir -p /storage/atelier
%cd /notebooks
!ln -s /storage

%cd /storage/atelier
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
%cd /storage/atelier/stable-diffusion-webui/models/Stable-diffusion/
!sudo apt update
!sudo apt install -y -qq aria2

# NovelAIリーク版ダウンロード
!aria2c -x 3 --allow-overwrite=true -Z https://cloudflare-ipfs.com/ipfs/bafybeicpamreyp2bsocyk3hpxr7ixb2g2rnrequub3j2ahrkdxbvfbvjc4/model.ckpt
!curl -Lo model.vae.pt https://cloudflare-ipfs.com/ipfs/bafybeiccldswdd3wvg57jhclcq53lvsc6gizasiblwayvhlv6eq4wow7wu/animevae.pt 

```

コピペしたら上の再生ボタン▶️を押してコードを実行。

<figure><img src="/files/tG4ojBVtSIUvtCrEceop" alt=""><figcaption><p>インストールのおまじない。/storageフォルダーがアカウント中での共有ストレージになっており、いちいち再度ダウンロードせずに使いまわせる。参考: <a href="https://docs.paperspace.com/gradient/data/">https://docs.paperspace.com/gradient/data/</a></p></figcaption></figure>

コードの下にごちゃごちゃ文字列が出力されていくと思う。4GB程度のモデルと、vae.ptをダウンロードするので2分-10分程度は時間がかかる。初回にダウンロードしたら2回目以降こちらのコードを実行する必要はない。

コードの左側に\[\*]:がある間は実行が続いている。それが\[数字]:に変わったら実行が終了した合図。

<figure><img src="/files/L8bq4jhMo74FnYzR6ckg" alt=""><figcaption><p>実行中のサイン</p></figcaption></figure>

<figure><img src="/files/RXwJu4wryNVxYyYDjft2" alt=""><figcaption><p>実行完了のサイン</p></figcaption></figure>

実行が終了したとき、下図の通り3.9GiB/3.9GiB、784M 100 784Mというように出力され、ファイルが正常にダウンロード完了していたことを確認する。もし終了時点で1.5GiB/3.9GiBとかになってたらネットワークエラー等でモデルファイルが正しくダウンロードできてない可能性がある。その場合再度コードを実行させる（コードをクリックして左に青いバーがある状態で再生▶️ボタンを押す）必要がある。

<figure><img src="/files/xdDrW3CiYOgVCoVouTkq" alt=""><figcaption></figcaption></figure>

### 3.5. (オプション) xformersのインストール

xformersを使用して高速化を行う場合、以下のコードを先ほどの出力の下の空欄にコピペし、再生ボタンで実行する。これはマシンの起動のたびに行う必要がある。

```bash
%%bash
# https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
MINICONDA_INSTALLER_SCRIPT=Miniconda3-py39_4.12.0-Linux-x86_64.sh
MINICONDA_PREFIX=/usr/local
wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT
chmod +x $MINICONDA_INSTALLER_SCRIPT
./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX
conda update conda -y
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch -y
conda install xformers -c xformers/label/dev -y
```

###

### 4. WebUIの起動

#### 起動のおまじない

先ほどの出力のひとつ下に新しい空欄ができていると思うのでそこに下記コードをコピペで入力し、再生▶️ボタンで実行する。こちらはアプリの起動コードなので毎回実行する必要がある。こちらも初回の起動だけ少し余分に時間がかかるが気長に待ちたい。

なお--shareによる起動方法はセキュリティ上の懸念があるため、ユーザー名・パスワードによる認証を追加している。「--gradio-auth sd:test」の部分を「--gradio-auth ユーザー名:パスワード」という形で任意に変えることで自由に起動認証のユーザー名、パスワードを設定できる。

また、xformersを用いる場合--xformersを末尾につけて使うと良い。

<pre class="language-bash"><code class="lang-bash"># 起動のおまじない
%cd /storage/atelier/stable-diffusion-webui
# webuiを自動更新するおまじない(任意)
# !git pull
!python launch.py --share  --gradio-auth sd:test
# xformersを利用する場合
<strong># !python launch.py --share  --gradio-auth sd:test --xformers
</strong></code></pre>

<figure><img src="/files/8aveDtDJ3sCDTOQ85baR" alt=""><figcaption></figcaption></figure>

正常に実行された場合、待っていれば画像赤下線部のようなリンクが出現する。このリンクをクリックすればいよいよアプリにアクセスできる。起動するとユーザー名とパスワードを聞かれるので先ほど--gradio-authで入力していたもの（デフォルトでユーザー名:sd、パスワード:test）を入力する。

#### 画像生成の初期設定を行う

NovelAIに設定を合わせるため、setting tabを開き、右下のeta noise seed deltaの値を31337にし、**「apply setting」を押す。**<mark style="color:red;">**（押さないと反映されないので注意。）**</mark>

<figure><img src="/files/k0glgWYI1sSrlT0vK1A2" alt=""><figcaption></figcaption></figure>

txt2imgタブに戻り、プロンプトで実際に画像を生成してみる。

このとき、NovelAIの本家と類似の生成をするためには、Promptの最初に「**masterpiece,best quality,** 」、

Negative promptの最初に「**lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry, artist name**」を入力する必要がある。

<https://rentry.org/voldy>のチュートリアルにならってパラメータを入力しアスカの画像を生成すると、次のように正常に画像が生成されているのが確認できる。

**パラメータ**

**Sampler:** `Euler`\
**Seed:** `2870305590`\
**CFG:** `12`\
**Resolution:** `512x512`\
**Prompt:** `masterpiece, best quality, masterpiece, asuka langley sitting cross legged on a chair`\
**Negative Prompt:** `lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry, artist name`

<figure><img src="/files/dp10JpYRN9S9SJDTez9w" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ludela.gitbook.io/paperspace-gradientdenovelairkaruwo8dorudenikasu/noki.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
