stable-diffusion-paperspace/StableDiffusionUI_Voldemort...

544 lines
16 KiB
Plaintext
Raw Normal View History

2022-09-08 13:02:54 -06:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
2022-09-11 16:35:36 -06:00
"id": "gfKvWAVnz8OB",
"tags": []
2022-09-08 13:02:54 -06:00
},
"source": [
"# Voldemort's Stable Diffusion WebUI\n",
"\n",
2022-09-11 16:35:36 -06:00
"Adapted from: https://colab.research.google.com/drive/1Iy-xW9t1-OQWhb0hNxueGij8phCyluOh\n",
"\n",
"[Get the newest version of this notebook on the Github](https://github.com/Engineer-of-Stuff/stable-diffusion-paperspace/blob/main/StableDiffusionUI_Voldemort_paperspace.ipynb)\n",
"\n",
"**Guides**\n",
"- [Getting started on Paperspace](https://github.com/Engineer-of-Stuff/stable-diffusion-paperspace/blob/main/Docs/Paperspace%20Guide%20for%20Retards.md)\n",
"- [Using the WebUI](https://rentry.org/voldy)\n",
"- [Using the Inpainter](https://rentry.org/drfar)\n",
"- [Textual Inversion](https://rentry.org/aikgx)\n",
"- [Crowd-Sourced Prompts](https://lexica.art/)\n",
"- [Artist Name Prompts](https://sgreens.notion.site/sgreens/4ca6f4e229e24da6845b6d49e6b08ae7?v=fdf861d1c65d456e98904fe3f3670bd3)"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## Installation and Setup\n",
"\n",
"You must reinstall everything each time you restart the machine. If already downloaded dependencies will be auto-updated."
]
},
2022-09-10 14:24:12 -06:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Where to store the models**\n",
"\n",
"`/storage/models/` is for the free tier.\n",
"\n",
"`/notebooks/models/` is for the pro tier.\n",
"\n",
"_You must run the block below or else the variable won't be accessable in the notebook._"
]
},
2022-09-08 13:02:54 -06:00
{
"cell_type": "code",
"execution_count": null,
2022-09-10 14:24:12 -06:00
"metadata": {},
2022-09-08 13:02:54 -06:00
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"model_storage_dir = '/storage/models' # no trailing slash\n",
"\n",
"# ===============================================================\n",
"# Save the variable to Jupiter's temp storage to access it when the kernel restarts.\n",
"# To reset your storage directory, rerun this cell.\n",
"%store model_storage_dir"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Clone the central repository"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sBbcB4vwj_jm",
"tags": []
2022-09-08 13:02:54 -06:00
},
"outputs": [],
"source": [
"import os.path\n",
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
2022-09-08 13:02:54 -06:00
"if not os.path.exists('/notebooks/stable-diffusion-webui'):\n",
2022-09-08 15:49:49 -06:00
" %cd /notebooks/\n",
2022-09-08 13:02:54 -06:00
" !git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui\n",
"else: # update repo if already exists\n",
" print('stable-diffusion-webui already downloaded, updating')\n",
" %cd /notebooks/stable-diffusion-webui\n",
" !git pull"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C68TUpkq0nj_",
"tags": []
},
"source": [
"### Install requirements and download repositories."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SaAJk33ppFw1",
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"%cd /notebooks/stable-diffusion-webui\n",
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
2022-09-08 13:02:54 -06:00
"!pip install -r requirements.txt\n",
"\n",
"if not os.path.exists('/notebooks/stable-diffusion-webui/repositories'): # download repositories if they don't exist\n",
" !mkdir /notebooks/stable-diffusion-webui/repositories\n",
" !git clone https://github.com/CompVis/stable-diffusion.git /notebooks/stable-diffusion-webui/repositories/stable-diffusion\n",
" !git clone https://github.com/CompVis/taming-transformers.git /notebooks/stable-diffusion-webui/repositories/taming-transformers\n",
" !git clone https://github.com/sczhou/CodeFormer.git /notebooks/stable-diffusion-webui/repositories/CodeFormer\n",
"else: # update repositories if they do exist\n",
" print('Updating dependencies')\n",
" for dir in os.listdir('/notebooks/stable-diffusion-webui/repositories'):\n",
" %cd /notebooks/stable-diffusion-webui/repositories/$dir\n",
" !git pull\n",
" \n",
"!pip install -r /notebooks/stable-diffusion-webui/repositories/CodeFormer/requirements.txt\n",
"\n",
"if not os.path.exists('/notebooks/stable-diffusion-webui/GFPGANv1.3.pth'):\n",
" !wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth\n",
"else:\n",
2022-09-08 16:40:35 -06:00
" print('GFPGANv1.3.pth already downloaded')\n",
" \n",
2022-09-10 14:24:12 -06:00
"!mkdir -p $model_storage_dir\n",
2022-09-09 12:15:51 -06:00
"\n",
"# Get some storage back\n",
"!pip cache purge\n",
"!sudo apt-get clean"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
2022-09-08 15:49:49 -06:00
"id": "F0EINk5M0s-w",
"tags": []
2022-09-08 13:02:54 -06:00
},
"source": [
"### Download the model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I've provided a few different ways of aquiring the models. Try the torrent option first.\n",
"\n",
2022-09-08 22:08:17 -06:00
"You don't need to repeat this step if you've already downloaded the models.\n",
"\n",
2022-09-09 12:15:51 -06:00
"**Filesize and Storage Disclaimer**\n",
"\n",
2022-09-10 14:24:12 -06:00
"Paperspace free tier has only 5GB of storage space. The standard model is 4GB and waifu is 3.5GB/7.2GB. To get around this limited storage capacity we will download the models to `/storage/`, a non-persistent directory. Each time you restart the machine you will have to redownload the models.\n",
"\n",
"If you don't want to have to redownload the models every time you restart your machine you have three options:\n",
2022-09-09 12:25:40 -06:00
"1. Add a payment method to your account. Storage overages are billed at \\$0.29/GB and billing occurs monthly and runs at midnight on the first of each month. With a payment method on file, Paperspace will let you use more storage and if you time it right you shouldn't actually be charged for it.\n",
"2. Upgrade to a Pro account. They'll give you 15GB and you'll get longer runtimes and more powerful free GPUs.\n",
2022-09-10 06:21:31 -06:00
"3. Create an account using my referral code `KQLRH37` or [signup link](https://console.paperspace.com/signup?R=KQLRH37). You'll get \\$10 credit that you should be able to put towards the storage overage charges. If you've already created your account you can still redeem the code at the bottom of the Billing page."
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"#### Standard Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Torrent**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
"!apt update\n",
"!apt install -y aria2\n",
2022-09-10 14:24:12 -06:00
"%cd $model_storage_dir\n",
2022-09-09 12:49:42 -06:00
"!aria2c --seed-time=0 --max-overall-upload-limit=1K \"magnet:?xt=urn:btih:3A4A612D75ED088EA542ACAC52F9F45987488D1C&tr=udp://tracker.opentrackr.org:1337\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Web Download**\n",
"\n",
2022-09-08 13:02:54 -06:00
"Voldy provided an alternative download if you don't want to use HuggingFace.\n",
"\n",
"[https://drive.google.com/file/d/1wHFgl0ivCmIZv88hVZXkb8oy9qCuaBGA/view](https://drive.google.com/file/d/1wHFgl0ivCmIZv88hVZXkb8oy9qCuaBGA/view)\n",
"\n",
2022-09-10 14:24:12 -06:00
"Download it to your computer then upload it to your model storage directory (make sure it's named `sd-v1-4.ckpt`).\n",
2022-09-08 13:02:54 -06:00
"\n",
"HuggingFace is much faster and reliable but you need to get access to the repo and provide your user token."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "CT_J9L7oqLxG"
},
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"user_token = \"<enter your user token here>\"\n",
2022-09-08 13:02:54 -06:00
"\n",
"# ===============================================================================================\n",
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
"!wget --header=\"'Authorization: Bearer {user_token}'\" https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt -O $model_storage_dir/sd-v1-4.ckpt"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
2022-09-10 14:24:12 -06:00
"jp-MarkdownHeadingCollapsed": true,
2022-09-08 13:02:54 -06:00
"tags": []
},
"source": [
"#### Waifu Diffusion"
]
},
2022-09-08 15:49:49 -06:00
{
"cell_type": "markdown",
"metadata": {},
2022-09-08 15:49:49 -06:00
"source": [
"**Torrent**"
2022-09-08 15:49:49 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!apt update\n",
"!apt install -y aria2\n",
2022-09-10 14:24:12 -06:00
"%cd $model_storage_dir\n",
2022-09-09 12:49:42 -06:00
"!aria2c --seed-time=0 --max-overall-upload-limit=1K \"magnet:?xt=urn:btih:F45CECF4E9DE86DA83A78DD2CCCD7F27D5557A52&tr=udp://nyquist.localghost.org:6969\""
2022-09-08 15:49:49 -06:00
]
},
2022-09-08 13:02:54 -06:00
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
2022-09-08 13:02:54 -06:00
"source": [
"**Web Download**\n",
2022-09-08 15:49:49 -06:00
"\n",
"Very slow"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
"!wget https://thisanimedoesnotexist.ai/downloads/wd-v1-2-full-ema.ckpt -O $model_storage_dir/wd-v1-2-full-ema.ckpt"
2022-09-08 13:02:54 -06:00
]
},
2022-09-09 12:40:33 -06:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-09-10 14:24:12 -06:00
"**Half-Size Model**\n",
2022-09-09 12:40:33 -06:00
"\n",
2022-09-09 15:48:47 -06:00
"If you're on free tier try this first, it's 3.5GB."
2022-09-09 12:40:33 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
2022-09-09 12:40:33 -06:00
"!apt update\n",
"!apt install -y aria2\n",
2022-09-10 14:24:12 -06:00
"%cd $model_storage_dir\n",
2022-09-10 06:21:31 -06:00
"!aria2c --seed-time=0 --max-overall-upload-limit=1K \"magnet:?xt=urn:btih:153590FD7E93EE11D8DB951451056C362E3A9150&dn=wd-v1-2-full-ema-pruned.ckpt&tr=udp://glotorrents.pw:6969/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://torrent.gresille.org:80/announce&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://p4p.arenabg.ch:1337&tr=udp://tracker.internetwarriors.net:1337&tr=wss://tracker.openwebtorrent.com\""
2022-09-09 12:40:33 -06:00
]
},
2022-09-08 13:02:54 -06:00
{
"cell_type": "markdown",
2022-09-08 16:40:35 -06:00
"metadata": {
2022-09-09 12:40:33 -06:00
"jp-MarkdownHeadingCollapsed": true,
2022-09-08 16:40:35 -06:00
"tags": []
},
2022-09-08 13:02:54 -06:00
"source": [
2022-09-08 16:40:35 -06:00
"#### Gradient Dataset Import"
2022-09-08 15:49:49 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Paperspace provides a way to mount external storage to notebooks, called *Datasets*. This is an advanced method but it will save you many GB since you can reuse the same model file across different notebooks.\n",
"\n",
"https://docs.paperspace.com/gradient/cli/datasets"
2022-09-08 13:02:54 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-09-10 14:30:13 -06:00
"mounted_dataset_path = '/datasets/<name>'\n",
2022-09-08 15:49:49 -06:00
"\n",
"# ============================================================\n",
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
"import os\n",
2022-09-08 16:40:35 -06:00
"for file in os.listdir(mounted_dataset_path):\n",
" if file.endswith(\"ckpt\"):\n",
2022-09-10 14:30:13 -06:00
" !ln -s $mounted_dataset_path/$file $model_storage_dir/$file\n",
2022-09-10 14:24:12 -06:00
" !ls -la --block-size=GB $model_storage_dir/$file"
2022-09-08 13:02:54 -06:00
]
},
2022-09-09 10:19:44 -06:00
{
"cell_type": "markdown",
2022-09-10 14:24:12 -06:00
"metadata": {
"tags": []
},
2022-09-09 10:19:44 -06:00
"source": [
2022-09-09 12:49:42 -06:00
"### Clean up and restart the kernel"
2022-09-09 10:19:44 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-09-10 14:24:12 -06:00
"%store -r model_storage_dir\n",
"!rm $model_storage_dir/*.aria2\n",
2022-09-09 12:49:42 -06:00
"!apt autoremove --purge -y aria2\n",
2022-09-09 10:19:44 -06:00
"import os\n",
"os.kill(os.getpid(), 9)"
]
},
2022-09-08 13:02:54 -06:00
{
"cell_type": "markdown",
"metadata": {
"id": "xt8lbdmC04ox"
},
"source": [
"# Launch the WebUI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-09-08 16:51:44 -06:00
"First, enter the name of the model you want to use into `model_name` in the cell below.\n",
"\n",
"Then run the model. You will get a link to nnn.gradio.app, that's your WebUI. Follow it.\n",
"\n",
"If you have any issues, restart the kernel."
2022-09-08 13:02:54 -06:00
]
},
2022-09-10 14:24:12 -06:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Choose your Model**\n",
"|Name||\n",
"|-|-|\n",
"|`sd-v1-4.ckpt`|Standard diffusion v1.4|\n",
"|`wd-v1-2-full-ema.ckpt`|waifu-diffusion v1.2|\n",
"|`wd-v1-2-full-ema-pruned.ckpt`|waifu-diffusion v1.2 half-size|\n",
"\n",
"Enter the name of the model you want to use in `model_name` below.\n",
"\n",
"To change models you must restart the kernel for it to take effect."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Start the WebUI**"
]
},
2022-09-08 13:02:54 -06:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2022-09-08 15:49:49 -06:00
"id": "R-xAdMA5wxXd",
"tags": []
2022-09-08 13:02:54 -06:00
},
"outputs": [],
"source": [
2022-09-08 16:51:44 -06:00
"model_name = ''\n",
2022-09-08 13:02:54 -06:00
"\n",
2022-09-10 14:24:12 -06:00
"# ===============================================================================================================\n",
"# Don't change anything in here\n",
"%store -r model_storage_dir\n",
"model_file = f'{model_storage_dir}/{model_name}'\n",
2022-09-08 16:40:35 -06:00
"\n",
"import sys\n",
2022-09-08 15:49:49 -06:00
"import os\n",
2022-09-08 16:40:35 -06:00
"\n",
"if not os.path.exists(model_file):\n",
" print('Cannot find model file:', model_file)\n",
" sys.exit(1)\n",
"\n",
"if model_name == '':\n",
2022-09-10 14:24:12 -06:00
" print('You forgot to fill out the variable model_name above.')\n",
" sys.exit(1)\n",
"\n",
2022-09-10 14:24:12 -06:00
"# ===============================================================================================================\n",
" \n",
"# You can modify the args if nessesary\n",
"!python /notebooks/stable-diffusion-webui/webui.py --share --ckpt $model_file --show-negative-prompt # --no-progressbar-hiding"
2022-09-09 15:48:47 -06:00
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Export Generations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-09-10 14:24:12 -06:00
"This block will rename and compress the outputs with 7zip max compression. It expects you to have `log/` and `outputs/` in `/notebooks/`."
2022-09-09 15:48:47 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
2022-09-10 14:24:12 -06:00
"metadata": {
"scrolled": true,
"tags": []
},
2022-09-09 15:48:47 -06:00
"outputs": [],
"source": [
2022-09-10 06:21:31 -06:00
"!apt update\n",
2022-09-09 15:48:47 -06:00
"!apt install -y p7zip-full\n",
"from datetime import datetime\n",
"import os\n",
"datetime_str = datetime.now().strftime('%m-%d-%Y_%H:%M:%S')\n",
"%cd /notebooks/\n",
"!mkdir $datetime_str\n",
"!mv log $datetime_str\n",
"!mv outputs $datetime_str\n",
2022-09-10 14:24:12 -06:00
"print('Compressing, please wait...')\n",
2022-09-09 15:48:47 -06:00
"os.system(f'7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on {datetime_str}.7z {datetime_str}/')"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Delete old output folder\n",
"\n",
"This block will delete the folder you just compressed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!rm -rf /notebooks/$datetime_str/\n",
"!echo Deleted /notebooks/$datetime_str/"
2022-09-08 15:49:49 -06:00
]
2022-09-10 14:24:12 -06:00
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!nvidia-smi"
]
2022-09-08 13:02:54 -06:00
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"private_outputs": true,
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}