AIOF / app.py
AhhhhCraaaap's picture
Rename stable_diffusion_1_5_webui_STABLE.ipynb to app.py
3a1722a
raw
history blame
20.4 kB
import gradio as gr
def greet(name):
return "Hello " + name + "!!"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "SKi-oMBwhutR"
},
"source": [
"# AUTOMATIC1111 Stable Diffusion WebUI 1.5 + ChilloutMix Checkpoint\n",
"\n",
"For generating AI Onlyfans\n",
"\n",
"> [@nang](https://github.com/nathan-149) \n",
"> Based off of: [@wibus-wee](https://github.com/wibus-wee)\n",
"> Reference: [camenduru/stable-diffusion-webui-colab](https://github.com/camenduru/stable-diffusion-webui-colab)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oL8eHMWfLzO7"
},
"outputs": [],
"source": [
"#@title 2. Check GPU & Dev Environment\n",
"\n",
"from IPython.display import display\n",
"import ipywidgets as widgets\n",
"import requests \n",
"\n",
"import os, subprocess\n",
"paperspace_m4000 = False\n",
"#@markdown Paperspace platform?\n",
"isPaperspace = False #@param {type:\"boolean\"}\n",
"try:\n",
" subprocess.run(['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'], stdout=subprocess.PIPE)\n",
" if 'M4000' in subprocess.run(['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'], stdout=subprocess.PIPE).stdout.decode('utf-8'):\n",
" print(\"WARNING: You're using Quadro M4000 GPU,xformers won't work。\")\n",
" paperspace_m4000 = True\n",
" isPaperspace = True\n",
" else:\n",
" print(\"Your GPU is suitable - \" + subprocess.run(['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'], stdout=subprocess.PIPE).stdout.decode('utf-8') + \"。\")\n",
" print(\"Platform: Paperspace\" if isPaperspace else \"Platform: Colab\")\n",
"except:\n",
" print(\"No GPU appears to be available. Please check your runtime type\")\n",
" exit()\n",
"\n",
"rootDir = isPaperspace and '/tmp' or '/content'\n",
"stableDiffusionWebUIInstalled = os.path.exists(rootDir + '/stable-diffusion-webui')\n",
"%store rootDir\n",
"%store paperspace_m4000 \n",
"%store isPaperspace\n",
"%store stableDiffusionWebUIInstalled"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SaAJk33ppFw1"
},
"outputs": [],
"source": [
"import requests\n",
"#@title 3. Install dependencies and extensions \n",
"\n",
"#@markdown ## **Extensions**\n",
"\n",
"#@markdown xformer\n",
"xformersInstall = True #@param {type:\"boolean\"}\n",
"#@markdown ControlNet\n",
"controlNetExtension = False #@param {type:\"boolean\"}\n",
"#@markdown OpenPose Editor\n",
"openPoseExtension = False #@param {type:\"boolean\"}\n",
"#@markdown Civitai Browser\n",
"civitaiBrowserExtension = False #@param {type:\"boolean\"}\n",
"#@markdown HuggingFace \n",
"huggingFaceExtension = False #@param {type:\"boolean\"}\n",
"#@markdown Images Browser \n",
"imagesBrowserExtension = False #@param {type:\"boolean\"}\n",
"#@markdown Additional Networks \n",
"additionalNetworksExtension = True #@param {type:\"boolean\"}\n",
"#@markdown Deforum \n",
"deforumExtension = False #@param {type:\"boolean\"}\n",
"#@markdown Kohya sd-scripts \n",
"kohyaExtension = False #@param {type:\"boolean\"}\n",
"#@markdown DreamBooth \n",
"dreamBoothExtension = False #@param {type:\"boolean\"}\n",
"\n",
"\n",
"#@markdown ---\n",
"#@markdown ## **Textual Inversion**\n",
"#@markdown Ulzzang-6500 (Korean doll aesthetic)\n",
"ulzzang6500 = True #@param {type:\"boolean\"}\n",
"#@markdown Pure Eros Face\n",
"pureErosFace = True #@param {type:\"boolean\"}\n",
"\n",
"#@markdown ---\n",
"\n",
"#@markdown ## **Startup Options**\n",
"\n",
"#@markdown API Support\n",
"apiSupport = True #@param {type:\"boolean\"}\n",
"\n",
"textualInversionDownloadIDs = {\n",
" 'ulzzang6500': 8109,\n",
" 'pureErosFace': 4514,\n",
"}\n",
"\n",
"def getLatestModelDownloadURL(id):\n",
" try:\n",
" if type(id) == int:\n",
" res = requests.get(endpoint + '/' + str(id)).json()\n",
" latest = res['modelVersions'][0]\n",
" downloadLink = latest['files'][0]['downloadUrl']\n",
" name = latest['files'][0]['name']\n",
" return {\n",
" 'url': downloadLink,\n",
" 'name': name\n",
" }\n",
" else:\n",
" return {\n",
" 'url': id,\n",
" 'name': id.split('/')[-1]\n",
" }\n",
" except:\n",
" print(\"Lora model \" + str(id) + \" not found. Skip.\")\n",
" return None\n",
"\n",
"def getSpecificModelDownloadURL(id, version):\n",
" try:\n",
" if type(id) == int:\n",
" res = requests.get(endpoint + '/' + str(id)).json()\n",
" for modelVersion in res['modelVersions']:\n",
" if modelVersion['name'] == version:\n",
" # if modelVersion[\"baseModel\"] != \"SD 1.5\":\n",
" # print(\"Lora model \" + str(id) + \" is not SD 1.5, may not work. Skip.\")\n",
" # return None\n",
" downloadLink = modelVersion['files'][0]['downloadUrl']\n",
" name = modelVersion['files'][0]['name']\n",
" return {\n",
" 'url': downloadLink,\n",
" 'name': name\n",
" }\n",
" else:\n",
" return {\n",
" 'url': id,\n",
" 'name': id.split('/')[-1]\n",
" }\n",
" except:\n",
" print(\"Lora model \" + str(id) + \" version \" + version + \" not found. Skip.\")\n",
" return None\n",
"\n",
"def getTextualInversionDownloadURLs():\n",
" downloadURLs = []\n",
" for key in textualInversionDownloadIDs:\n",
" if not eval(key): # skip if not selected\n",
" continue\n",
" if type(textualInversionDownloadIDs[key]) is int:\n",
" downloadURLs.append(getLatestModelDownloadURL(textualInversionDownloadIDs[key]))\n",
" elif type(textualInversionDownloadIDs[key]) is dict: # {'id': 123, 'version': 'v1.0'}\n",
" downloadURLs.append(getSpecificModelDownloadURL(textualInversionDownloadIDs[key]['id'], textualInversionDownloadIDs[key]['version']))\n",
" elif type(textualInversionDownloadIDs[key]) is str: # url\n",
" downloadURLs.append({ 'url': textualInversionDownloadIDs[key], 'name': textualInversionDownloadIDs[key].split('/')[-1] })\n",
" downloadURLs = [x for x in downloadURLs if x is not None]\n",
" return downloadURLs\n",
" \n",
"textualInversionDownloadURLs = getTextualInversionDownloadURLs()\n",
"\n",
"%store -r paperspace_m4000 \n",
"%store -r isPaperspace\n",
"%store -r rootDir \n",
"%store -r checkpoints\n",
"%store -r downloadLinks\n",
"%store -r stableDiffusionWebUIInstalled\n",
"\n",
"import subprocess\n",
"\n",
"!apt-get -y install -qq aria2\n",
"ariaInstalled = False\n",
"\n",
"try:\n",
" subprocess.run(['aria2c', '--version'], stdout=subprocess.PIPE)\n",
" ariaInstalled = True\n",
"except:\n",
" pass\n",
"\n",
"if paperspace_m4000:\n",
" if xformersInstall:\n",
" !pip install ninja\n",
" !pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers\n",
" !pip install -q --pre triton\n",
"else:\n",
" !pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.16/xformers-0.0.16+814314d.d20230118-cp38-cp38-linux_x86_64.whl\n",
" !pip install -q --pre triton\n",
" \n",
"\n",
"!git clone -b v2.0 https://github.com/nathan-149/stable-diffusion-webui {rootDir}/stable-diffusion-webui\n",
"!wget https://raw.githubusercontent.com/nathan-149/stable-diffusion-webui-scripts/main/run_n_times.py -O {rootDir}/stable-diffusion-webui/scripts/run_n_times.py\n",
"if deforumExtension:\n",
" !git clone https://github.com/deforum-art/deforum-for-automatic1111-webui {rootDir}/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui\n",
"if imagesBrowserExtension:\n",
" !git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser {rootDir}/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser\n",
"if huggingFaceExtension:\n",
" !git clone https://github.com/camenduru/stable-diffusion-webui-huggingface {rootDir}/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface\n",
"if civitaiBrowserExtension:\n",
" !git clone -b v2.0 https://github.com/camenduru/sd-civitai-browser {rootDir}/stable-diffusion-webui/extensions/sd-civitai-browser\n",
"if openPoseExtension:\n",
" !git clone https://github.com/camenduru/openpose-editor {rootDir}/stable-diffusion-webui/extensions/openpose-editor\n",
"if controlNetExtension:\n",
" !git clone https://github.com/Mikubill/sd-webui-controlnet {rootDir}/stable-diffusion-webui/extensions/sd-webui-controlnet\n",
"if additionalNetworksExtension:\n",
" !git clone https://github.com/kohya-ss/sd-webui-additional-networks {rootDir}/stable-diffusion-webui/extensions/sd-webui-additional-networks\n",
"if kohyaExtension:\n",
" !git clone https://github.com/ddpn08/kohya-sd-scripts-webui.git {rootDir}/stable-diffusion-webui/extensions/kohya-sd-scripts-webui\n",
"if dreamBoothExtension:\n",
" !git clone https://github.com/d8ahazard/sd_dreambooth_extension {rootDir}/stable-diffusion-webui/extensions/sd_dreambooth_extension\n",
"\n",
"if isPaperspace:\n",
" %cd /stable-diffusion-webui\n",
"else:\n",
" %cd {rootDir}/stable-diffusion-webui\n",
"\n",
"\n",
"webuiControlNetModels = [\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_canny-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_depth-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_hed-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_mlsd-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_normal-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_openpose-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_scribble-fp16.safetensors\",\n",
" \"https://huggingface.co./webui/ControlNet-modules-safetensors/resolve/main/control_seg-fp16.safetensors\",\n",
"]\n",
"annotatorLink = [\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/hand_pose_model.pth\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/body_pose_model.pth\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/dpt_hybrid-midas-501f0c75.pt\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/mlsd_large_512_fp32.pth\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/mlsd_tiny_512_fp32.pth\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/network-bsds500.pth\",\n",
" \"https://huggingface.co./ckpt/ControlNet/resolve/main/upernet_global_small.pth\",\n",
"]\n",
"\n",
"def ariaDownload(downloadLink, checkpoint, path):\n",
" if (type(downloadLink) == list and type(checkpoint) == list):\n",
" for i in downloadLink:\n",
" !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {i} -d {path} -o {checkpoint[downloadLink.index(i)]}\n",
" else:\n",
" !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {downloadLink} -d {path} -o {checkpoint}\n",
"def wgetDownload(downloadLink, checkpoint, path):\n",
" if (type(downloadLink) == list and type(checkpoint) == list):\n",
" for i in downloadLink:\n",
" !wget -c {i} -P {path} -O {checkpoint[downloadLink.index(i)]}\n",
" else:\n",
" !wget -c {downloadLink} -P {path} -O {checkpoint}\n",
"def autoDetectDownload(downloadLink, checkpoint, path):\n",
" if ariaInstalled:\n",
" ariaDownload(downloadLink, checkpoint, path)\n",
" else:\n",
" wgetDownload(downloadLink, checkpoint, path)\n",
"\n",
"if controlNetExtension:\n",
" for model in webuiControlNetModels:\n",
" autoDetectDownload(model, model.split('/')[-1], rootDir + \"/stable-diffusion-webui/extensions/sd-webui-controlnet/models\")\n",
" for model in annotatorLink:\n",
" autoDetectDownload(model, model.split('/')[-1], rootDir + \"/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator\")\n",
"for model in textualInversionDownloadURLs:\n",
" autoDetectDownload(model[\"url\"], model[\"name\"], rootDir + \"/stable-diffusion-webui/embeddings\")\n",
"\n",
"if additionalNetworksExtension:\n",
" !ln -s {rootDir}/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora {rootDir}/stable-diffusion-webui/models/Lora\n",
"\n",
"\n",
"stableDiffusionWebUIInstalled = True\n",
"%store stableDiffusionWebUIInstalled\n",
"\n",
"%cd {rootDir}/stable-diffusion-webui\n",
"!sed -i -e '''/prepare_environment()/a\\ os.system\\(f\\\"\"\"sed -i -e ''\\\"s/self.logvar\\\\[t\\\\]/self.logvar\\\\[t.item()\\\\]/g\\\"'' {rootDir}/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/models/diffusion/ddpm.py\"\"\")''' {rootDir}/stable-diffusion-webui/launch.py\n",
"!sed -i -e '''/prepare_environment()/a\\ os.system\\(f\\\"\"\"sed -i -e ''\\\"s/dict()))/dict())).cuda()/g\\\"'' {rootDir}/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/util.py\"\"\")''' {rootDir}/stable-diffusion-webui/launch.py\n",
"if dreamBoothExtension:\n",
" !export REQS_FILE=\"./extensions/sd_dreambooth_extension/requirements.txt\"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ht3hxbzPFL3G"
},
"outputs": [],
"source": [
"#@title Download Chilloutmix Checkpoint\n",
"\n",
"checkpoint = 'chilloutmix.safetensors' #@param [\"chilloutmix.safetensors\"]\n",
"\n",
"downloadLink = 'https://civitai.com/api/download/models/11745' #@param \n",
"\n",
"\n",
"!wget -c {downloadLink} -O /content/stable-diffusion-webui/models/Stable-diffusion/{checkpoint}\n",
"!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co./ckpt/sd14/resolve/main/sd-v1-4.ckpt -d /content/stable-diffusion-webui/models/Stable-diffusion -o sd-v1-4.ckpt\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "D7tw6cMcMOba"
},
"outputs": [],
"source": [
"#@title Download Loras\n",
"\n",
"loraLinks = dict((\n",
" ('koreanDollLikeness_v15.safetensors', 'https://civitai.com/api/download/models/31284'),\n",
" ('xswltry1.safetensors', 'https://civitai.com/api/download/models/29131'),\n",
" ('liyuuLora_liyuuV1.safetensors', 'https://civitai.com/models/9997/liyuu-lora'),\n",
" ('aiBeautyIthlinni_ithlinniV1.safetensors', 'https://civitai.com/api/download/models/19671'),\n",
" ('Cute_girl_mix4.safetensors', 'https://civitai.com/api/download/models/16677'),\n",
" ('breastinclassBetter_v141.safetensors', 'https://civitai.com/api/download/models/23250'),\n",
" ('chilloutmixss_xss10.safetensors', 'https://huggingface.co./HankChang/chilloutmixss_xss10/resolve/main/chilloutmixss_xss10.safetensors'),\n",
" ('moxin.safetensors', 'https://civitai.com/api/download/models/14856'),\n",
" ('legspread10.safetensors', 'https://civitai.com/api/download/models/29760'),\n",
" ('taiwan.safetensors', 'https://civitai.com/api/download/models/20684')\n",
"))\n",
"\n",
"\n",
"for lora, link in loraLinks.items():\n",
" print('\\nKey: %s' % lora)\n",
" print('Value: %s' % link)\n",
" !wget -c {link} -O /content/stable-diffusion-webui/models/Lora/{lora}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "pddH4PXZB-fZ"
},
"outputs": [],
"source": [
"#@title Run UI!\n",
"%pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchtext==0.14.1 torchaudio==0.13.1 torchdata==0.5.1 --extra-index-url https://download.pytorch.org/whl/cu117\n",
"!cd /content/stable-diffusion-webui/ && python launch.py --enable-insecure-extension-access --share"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "W0k4xg3rLzO8"
},
"outputs": [],
"source": [
"#@title 5. Export Photos to /export\n",
"%store -r rootDir \n",
"\n",
"from pathlib import Path\n",
"import os, subprocess\n",
"\n",
"export_storage_dir = Path(rootDir, 'export')\n",
"export_storage_dir.mkdir(exist_ok=True)\n",
"\n",
"!if [ $(dpkg-query -W -f='${Status}' p7zip-full 2>/dev/null | grep -c \"ok installed\") = 0 ]; then sudo apt update && sudo apt install -y p7zip-full; fi # install 7z if it isn't already installed\n",
"from datetime import datetime\n",
"datetime_str = datetime.now().strftime('%m-%d-%Y_%H-%M-%S')\n",
"%cd \"{export_storage_dir}\"\n",
"!mkdir -p \"{datetime_str}/log\"\n",
"!cd \"{rootDir}/stable-diffusion-webui/log\" && mv * \"{export_storage_dir / datetime_str / 'log'}\"\n",
"!cd \"{rootDir}/stable-diffusion-webui/outputs\" && mv * \"{export_storage_dir / datetime_str}\"\n",
"s = subprocess.run(f'find \"{Path(export_storage_dir, datetime_str)}\" -type d -name .ipynb_checkpoints -exec rm -rv {{}} +', shell=True)\n",
"!7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on \"{datetime_str}.7z\" \"{export_storage_dir / datetime_str}\""
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"machine_shape": "hm",
"private_outputs": true,
"provenance": []
},
"gpuClass": "premium",
"kernelspec": {
"display_name": "Python 3",
"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.6"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
iface.launch()