nekohacker591 commited on
Commit
db34c0a
·
1 Parent(s): db1e5f8

Upload fast_stable_diffusion_AUTOMATIC1111.ipynb

Browse files
fast_stable_diffusion_AUTOMATIC1111.ipynb ADDED
@@ -0,0 +1,684 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "47kV9o1Ni8GH"
7
+ },
8
+ "source": [
9
+ "# **Colab Pro notebook from https://github.com/TheLastBen/fast-stable-diffusion** *Alternatives : [RunPod](https://www.runpod.io/console/gpu-browse?template=runpod-stable-unified) | [Paperspace](https://console.paperspace.com/github/TheLastBen/PPS?machine=Free-GPU)*\n",
10
+ "##**[Support](https://ko-fi.com/thelastben)**"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "metadata": {
17
+ "cellView": "form",
18
+ "id": "Y9EBc437WDOs"
19
+ },
20
+ "outputs": [],
21
+ "source": [
22
+ "#@markdown # Connect Google Drive\n",
23
+ "from google.colab import drive\n",
24
+ "from IPython.display import clear_output\n",
25
+ "import ipywidgets as widgets\n",
26
+ "import os\n",
27
+ "\n",
28
+ "def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)\n",
29
+ "Shared_Drive = \"\" #@param {type:\"string\"}\n",
30
+ "#@markdown - Leave empty if you're not using a shared drive\n",
31
+ "\n",
32
+ "print(\"\u001b[0;33mConnecting...\")\n",
33
+ "drive.mount('/content/gdrive')\n",
34
+ "\n",
35
+ "if Shared_Drive!=\"\" and os.path.exists(\"/content/gdrive/Shareddrives\"):\n",
36
+ " mainpth=\"Shareddrives/\"+Shared_Drive\n",
37
+ "else:\n",
38
+ " mainpth=\"MyDrive\"\n",
39
+ "\n",
40
+ "clear_output()\n",
41
+ "inf('\\u2714 Done','success', '50px')\n",
42
+ "\n",
43
+ "#@markdown ---"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": null,
49
+ "metadata": {
50
+ "cellView": "form",
51
+ "id": "CFWtw-6EPrKi"
52
+ },
53
+ "outputs": [],
54
+ "source": [
55
+ "#@markdown # Install/Update AUTOMATIC1111 repo\n",
56
+ "from IPython.utils import capture\n",
57
+ "from IPython.display import clear_output\n",
58
+ "from subprocess import getoutput\n",
59
+ "import ipywidgets as widgets\n",
60
+ "import sys\n",
61
+ "import fileinput\n",
62
+ "import os\n",
63
+ "import time\n",
64
+ "import base64\n",
65
+ "import gdown\n",
66
+ "from gdown.download import get_url_from_gdrive_confirmation\n",
67
+ "import requests\n",
68
+ "from urllib.request import urlopen, Request\n",
69
+ "from urllib.parse import urlparse, parse_qs, unquote\n",
70
+ "from tqdm import tqdm\n",
71
+ "import six\n",
72
+ "\n",
73
+ "\n",
74
+ "blsaphemy=base64.b64decode((\"ZWJ1aQ==\").encode('ascii')).decode('ascii')\n",
75
+ "\n",
76
+ "if not os.path.exists(\"/content/gdrive\"):\n",
77
+ " print('\u001b[1;31mGdrive not connected, using temporary colab storage ...')\n",
78
+ " time.sleep(4)\n",
79
+ " mainpth=\"MyDrive\"\n",
80
+ " !mkdir -p /content/gdrive/$mainpth\n",
81
+ " Shared_Drive=\"\"\n",
82
+ "\n",
83
+ "if Shared_Drive!=\"\" and not os.path.exists(\"/content/gdrive/Shareddrives\"):\n",
84
+ " print('\u001b[1;31mShared drive not detected, using default MyDrive')\n",
85
+ " mainpth=\"MyDrive\"\n",
86
+ "\n",
87
+ "with capture.capture_output() as cap:\n",
88
+ " def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)\n",
89
+ " fgitclone = \"git clone --depth 1\"\n",
90
+ " %mkdir -p /content/gdrive/$mainpth/sd\n",
91
+ " %cd /content/gdrive/$mainpth/sd\n",
92
+ " !git clone -q --branch master https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy\n",
93
+ " !mkdir -p /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/cache/\n",
94
+ " os.environ['TRANSFORMERS_CACHE']=f\"/content/gdrive/{mainpth}/sd/stable-diffusion-w\"+blsaphemy+\"/cache\"\n",
95
+ " os.environ['TORCH_HOME'] = f\"/content/gdrive/{mainpth}/sd/stable-diffusion-w\"+blsaphemy+\"/cache\"\n",
96
+ "\n",
97
+ "with capture.capture_output() as cap:\n",
98
+ " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/\n",
99
+ " !git reset --hard\n",
100
+ " !git checkout master\n",
101
+ " time.sleep(1)\n",
102
+ " !rm webui.sh\n",
103
+ " !git pull\n",
104
+ "clear_output()\n",
105
+ "inf('\\u2714 Done','success', '50px')\n",
106
+ "\n",
107
+ "#@markdown ---"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": null,
113
+ "metadata": {
114
+ "cellView": "form",
115
+ "id": "ZGV_5H4xrOSp"
116
+ },
117
+ "outputs": [],
118
+ "source": [
119
+ "#@markdown # Requirements\n",
120
+ "\n",
121
+ "print('\u001b[1;32mInstalling requirements...')\n",
122
+ "\n",
123
+ "with capture.capture_output() as cap:\n",
124
+ " %cd /content/\n",
125
+ " !wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt\n",
126
+ " !dpkg -i *.deb\n",
127
+ " if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stablediffusiond'): #restore later\n",
128
+ " !tar -C /content/gdrive/$mainpth --zstd -xf sd_mrep.tar.zst\n",
129
+ " !tar -C / --zstd -xf gcolabdeps.tar.zst\n",
130
+ " !rm *.deb | rm *.zst | rm *.txt\n",
131
+ " if not os.path.exists('gdrive/'+mainpth+'/sd/libtcmalloc/libtcmalloc_minimal.so.4'):\n",
132
+ " %env CXXFLAGS=-std=c++14\n",
133
+ " !wget -q https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz && tar zxf gperftools-2.5.tar.gz && mv gperftools-2.5 gperftools\n",
134
+ " !wget -q https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/Patch\n",
135
+ " %cd /content/gperftools\n",
136
+ " !patch -p1 < /content/Patch\n",
137
+ " !./configure --enable-minimal --enable-libunwind --enable-frame-pointers --enable-dynamic-sized-delete-support --enable-sized-delete --enable-emergency-malloc; make -j4\n",
138
+ " !mkdir -p /content/gdrive/$mainpth/sd/libtcmalloc && cp .libs/libtcmalloc*.so* /content/gdrive/$mainpth/sd/libtcmalloc\n",
139
+ " %env LD_PRELOAD=/content/gdrive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4\n",
140
+ " %cd /content\n",
141
+ " !rm *.tar.gz Patch && rm -r /content/gperftools\n",
142
+ " else:\n",
143
+ " %env LD_PRELOAD=/content/gdrive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4\n",
144
+ " os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n",
145
+ " os.environ['PYTHONWARNINGS'] = 'ignore'\n",
146
+ " !sed -i 's@text = _formatwarnmsg(msg)@text =\\\"\\\"@g' /usr/lib/python3.10/warnings.py\n",
147
+ "\n",
148
+ "clear_output()\n",
149
+ "inf('\\u2714 Done','success', '50px')\n",
150
+ "\n",
151
+ "#@markdown ---"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": null,
157
+ "metadata": {
158
+ "cellView": "form",
159
+ "id": "p4wj_txjP3TC"
160
+ },
161
+ "outputs": [],
162
+ "source": [
163
+ "#@markdown # Model Download/Load\n",
164
+ "\n",
165
+ "Use_Temp_Storage = False #@param {type:\"boolean\"}\n",
166
+ "#@markdown - If not, make sure you have enough space on your gdrive\n",
167
+ "\n",
168
+ "#@markdown ---\n",
169
+ "\n",
170
+ "Model_Version = \"SDXL\" #@param [\"SDXL\", \"1.5\", \"v1.5 Inpainting\", \"V2.1-768px\"]\n",
171
+ "\n",
172
+ "#@markdown Or\n",
173
+ "PATH_to_MODEL = \"\" #@param {type:\"string\"}\n",
174
+ "#@markdown - Insert the full path of your custom model or to a folder containing multiple models\n",
175
+ "\n",
176
+ "#@markdown Or\n",
177
+ "MODEL_LINK = \"\" #@param {type:\"string\"}\n",
178
+ "\n",
179
+ "\n",
180
+ "def getsrc(url):\n",
181
+ " parsed_url = urlparse(url)\n",
182
+ " if parsed_url.netloc == 'civitai.com':\n",
183
+ " src='civitai'\n",
184
+ " elif parsed_url.netloc == 'drive.google.com':\n",
185
+ " src='gdrive'\n",
186
+ " elif parsed_url.netloc == 'huggingface.co':\n",
187
+ " src='huggingface'\n",
188
+ " else:\n",
189
+ " src='others'\n",
190
+ " return src\n",
191
+ "\n",
192
+ "src=getsrc(MODEL_LINK)\n",
193
+ "\n",
194
+ "def get_name(url, gdrive):\n",
195
+ " if not gdrive:\n",
196
+ " response = requests.get(url, allow_redirects=False)\n",
197
+ " if \"Location\" in response.headers:\n",
198
+ " redirected_url = response.headers[\"Location\"]\n",
199
+ " quer = parse_qs(urlparse(redirected_url).query)\n",
200
+ " if \"response-content-disposition\" in quer:\n",
201
+ " disp_val = quer[\"response-content-disposition\"][0].split(\";\")\n",
202
+ " for vals in disp_val:\n",
203
+ " if vals.strip().startswith(\"filename=\"):\n",
204
+ " filenm=unquote(vals.split(\"=\", 1)[1].strip())\n",
205
+ " return filenm.replace(\"\\\"\",\"\")\n",
206
+ " else:\n",
207
+ " headers = {\"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\"}\n",
208
+ " lnk=\"https://drive.google.com/uc?id={id}&export=download\".format(id=url[url.find(\"/d/\")+3:url.find(\"/view\")])\n",
209
+ " res = requests.session().get(lnk, headers=headers, stream=True, verify=True)\n",
210
+ " res = requests.session().get(get_url_from_gdrive_confirmation(res.text), headers=headers, stream=True, verify=True)\n",
211
+ " content_disposition = six.moves.urllib_parse.unquote(res.headers[\"Content-Disposition\"])\n",
212
+ " filenm = re.search(r\"filename\\*=UTF-8''(.*)\", content_disposition).groups()[0].replace(os.path.sep, \"_\")\n",
213
+ " return filenm\n",
214
+ "\n",
215
+ "\n",
216
+ "def dwn(url, dst, msg):\n",
217
+ " file_size = None\n",
218
+ " req = Request(url, headers={\"User-Agent\": \"torch.hub\"})\n",
219
+ " u = urlopen(req)\n",
220
+ " meta = u.info()\n",
221
+ " if hasattr(meta, 'getheaders'):\n",
222
+ " content_length = meta.getheaders(\"Content-Length\")\n",
223
+ " else:\n",
224
+ " content_length = meta.get_all(\"Content-Length\")\n",
225
+ " if content_length is not None and len(content_length) > 0:\n",
226
+ " file_size = int(content_length[0])\n",
227
+ "\n",
228
+ " with tqdm(total=file_size, disable=False, mininterval=0.5,\n",
229
+ " bar_format=msg+' |{bar:20}| {percentage:3.0f}%') as pbar:\n",
230
+ " with open(dst, \"wb\") as f:\n",
231
+ " while True:\n",
232
+ " buffer = u.read(8192)\n",
233
+ " if len(buffer) == 0:\n",
234
+ " break\n",
235
+ " f.write(buffer)\n",
236
+ " pbar.update(len(buffer))\n",
237
+ " f.close()\n",
238
+ "\n",
239
+ "\n",
240
+ "def sdmdls(ver, Use_Temp_Storage):\n",
241
+ "\n",
242
+ " if ver=='1.5':\n",
243
+ " if Use_Temp_Storage:\n",
244
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
245
+ " model='/content/temp_models/v1-5-pruned-emaonly.safetensors'\n",
246
+ " else:\n",
247
+ " model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors'\n",
248
+ " link='https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors'\n",
249
+ " elif ver=='V2.1-768px':\n",
250
+ " if Use_Temp_Storage:\n",
251
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
252
+ " model='/content/temp_models/v2-1_768-ema-pruned.safetensors'\n",
253
+ " else:\n",
254
+ " model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/models/Stable-diffusion/v2-1_768-ema-pruned.safetensors'\n",
255
+ " link='https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.safetensors'\n",
256
+ " elif ver=='v1.5 Inpainting':\n",
257
+ " if Use_Temp_Storage:\n",
258
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
259
+ " model='/content/temp_models/sd-v1-5-inpainting.ckpt'\n",
260
+ " else:\n",
261
+ " model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'\n",
262
+ " link='https://huggingface.co/runwayml/stable-diffusion-inpainting/resolve/main/sd-v1-5-inpainting.ckpt'\n",
263
+ " elif ver=='SDXL':\n",
264
+ " if Use_Temp_Storage:\n",
265
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
266
+ " model='/content/temp_models/sd_xl_base_1.0.safetensors'\n",
267
+ " else:\n",
268
+ " model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/models/Stable-diffusion/sd_xl_base_1.0.safetensors'\n",
269
+ " link='https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors'\n",
270
+ "\n",
271
+ " if not os.path.exists(model):\n",
272
+ " !gdown --fuzzy -O $model $link\n",
273
+ " if os.path.exists(model):\n",
274
+ " clear_output()\n",
275
+ " inf('\\u2714 Done','success', '50px')\n",
276
+ " else:\n",
277
+ " inf('\\u2718 Something went wrong, try again','danger', \"250px\")\n",
278
+ " else:\n",
279
+ " clear_output()\n",
280
+ " inf('\\u2714 Model already exists','primary', '300px')\n",
281
+ "\n",
282
+ " return model\n",
283
+ "\n",
284
+ "\n",
285
+ "if (PATH_to_MODEL !=''):\n",
286
+ " if os.path.exists(str(PATH_to_MODEL)):\n",
287
+ " inf('\\u2714 Using the trained model.','success', '200px')\n",
288
+ "\n",
289
+ " else:\n",
290
+ " while not os.path.exists(str(PATH_to_MODEL)):\n",
291
+ " inf('\\u2718 Wrong path, use the colab file explorer to copy the path : ','danger', \"400px\")\n",
292
+ " PATH_to_MODEL=input()\n",
293
+ " if os.path.exists(str(PATH_to_MODEL)):\n",
294
+ " inf('\\u2714 Using the custom model.','success', '200px')\n",
295
+ "\n",
296
+ " model=PATH_to_MODEL\n",
297
+ "\n",
298
+ "elif MODEL_LINK != \"\":\n",
299
+ "\n",
300
+ " if src=='civitai':\n",
301
+ " modelname=get_name(MODEL_LINK, False)\n",
302
+ " if Use_Temp_Storage:\n",
303
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
304
+ " model=f'/content/temp_models/{modelname}'\n",
305
+ " else:\n",
306
+ " model=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Stable-diffusion/{modelname}'\n",
307
+ " if not os.path.exists(model):\n",
308
+ " dwn(MODEL_LINK, model, 'Downloading the custom model')\n",
309
+ " clear_output()\n",
310
+ " else:\n",
311
+ " inf('\\u2714 Model already exists','primary', '300px')\n",
312
+ " elif src=='gdrive':\n",
313
+ " modelname=get_name(MODEL_LINK, True)\n",
314
+ " if Use_Temp_Storage:\n",
315
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
316
+ " model=f'/content/temp_models/{modelname}'\n",
317
+ " else:\n",
318
+ " model=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Stable-diffusion/{modelname}'\n",
319
+ " if not os.path.exists(model):\n",
320
+ " gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)\n",
321
+ " clear_output()\n",
322
+ " else:\n",
323
+ " inf('\\u2714 Model already exists','primary', '300px')\n",
324
+ " else:\n",
325
+ " modelname=os.path.basename(MODEL_LINK)\n",
326
+ " if Use_Temp_Storage:\n",
327
+ " os.makedirs('/content/temp_models', exist_ok=True)\n",
328
+ " model=f'/content/temp_models/{modelname}'\n",
329
+ " else:\n",
330
+ " model=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Stable-diffusion/{modelname}'\n",
331
+ " if not os.path.exists(model):\n",
332
+ " gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)\n",
333
+ " clear_output()\n",
334
+ " else:\n",
335
+ " inf('\\u2714 Model already exists','primary', '700px')\n",
336
+ "\n",
337
+ " if os.path.exists(model) and os.path.getsize(model) > 1810671599:\n",
338
+ " inf('\\u2714 Model downloaded, using the custom model.','success', '300px')\n",
339
+ " else:\n",
340
+ " !rm model\n",
341
+ " inf('\\u2718 Wrong link, check that the link is valid','danger', \"300px\")\n",
342
+ "\n",
343
+ "else:\n",
344
+ " model=sdmdls(Model_Version, Use_Temp_Storage)\n",
345
+ "\n",
346
+ "#@markdown ---"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "metadata": {
353
+ "cellView": "form",
354
+ "id": "Svx6Hx0iUPd1"
355
+ },
356
+ "outputs": [],
357
+ "source": [
358
+ "#@markdown # Download LoRA\n",
359
+ "\n",
360
+ "LoRA_LINK = \"\" #@param {type:\"string\"}\n",
361
+ "\n",
362
+ "if LoRA_LINK == \"\":\n",
363
+ " inf('\\u2714 Nothing to do','primary', '200px')\n",
364
+ "else:\n",
365
+ " os.makedirs('/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/models/Lora', exist_ok=True)\n",
366
+ "\n",
367
+ " src=getsrc(LoRA_LINK)\n",
368
+ "\n",
369
+ " if src=='civitai':\n",
370
+ " modelname=get_name(LoRA_LINK, False)\n",
371
+ " loramodel=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Lora/{modelname}'\n",
372
+ " if not os.path.exists(loramodel):\n",
373
+ " dwn(LoRA_LINK, loramodel, 'Downloading the LoRA model '+modelname)\n",
374
+ " clear_output()\n",
375
+ " else:\n",
376
+ " inf('\\u2714 Model already exists','primary', '200px')\n",
377
+ " elif src=='gdrive':\n",
378
+ " modelname=get_name(LoRA_LINK, True)\n",
379
+ " loramodel=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Lora/{modelname}'\n",
380
+ " if not os.path.exists(loramodel):\n",
381
+ " gdown.download(url=LoRA_LINK, output=loramodel, quiet=False, fuzzy=True)\n",
382
+ " clear_output()\n",
383
+ " else:\n",
384
+ " inf('\\u2714 Model already exists','primary', '200px')\n",
385
+ " else:\n",
386
+ " modelname=os.path.basename(LoRA_LINK)\n",
387
+ " loramodel=f'/content/gdrive/{mainpth}/sd/stable-diffusion-w{blsaphemy}/models/Lora/{modelname}'\n",
388
+ " if not os.path.exists(loramodel):\n",
389
+ " gdown.download(url=LoRA_LINK, output=loramodel, quiet=False, fuzzy=True)\n",
390
+ " clear_output()\n",
391
+ " else:\n",
392
+ " inf('\\u2714 Model already exists','primary', '200px')\n",
393
+ "\n",
394
+ " if os.path.exists(loramodel) :\n",
395
+ " inf('\\u2714 LoRA downloaded','success', '200px')\n",
396
+ " else:\n",
397
+ " inf('\\u2718 Wrong link, check that the link is valid','danger', \"300px\")\n",
398
+ "\n",
399
+ "#@markdown ---"
400
+ ]
401
+ },
402
+ {
403
+ "cell_type": "code",
404
+ "execution_count": null,
405
+ "metadata": {
406
+ "cellView": "form",
407
+ "id": "zC3Rz1b2TBcB"
408
+ },
409
+ "outputs": [],
410
+ "source": [
411
+ "#@markdown # ControlNet\n",
412
+ "from torch.hub import download_url_to_file\n",
413
+ "from urllib.parse import urlparse\n",
414
+ "import re\n",
415
+ "from subprocess import run\n",
416
+ "\n",
417
+ "XL_Model = \"None\" #@param [ \"None\", \"All\", \"Canny\", \"Depth\", \"Sketch\", \"OpenPose\", \"Recolor\"]\n",
418
+ "\n",
419
+ "v1_Model = \"None\" #@param [ \"None\", \"All (21GB)\", \"Canny\", \"Depth\", \"Lineart\", \"MLSD\", \"Normal\", \"OpenPose\", \"Scribble\", \"Seg\", \"ip2p\", \"Shuffle\", \"Inpaint\", \"Softedge\", \"Lineart_Anime\", \"Tile\", \"T2iadapter_Models\"]\n",
420
+ "\n",
421
+ "v2_Model = \"None\" #@param [ \"None\", \"All\", \"Canny\", \"Depth\", \"HED\", \"OpenPose\", \"Scribble\"]\n",
422
+ "\n",
423
+ "#@markdown - Download/update ControlNet extension and its models\n",
424
+ "\n",
425
+ "def download(url, model_dir):\n",
426
+ "\n",
427
+ " filename = os.path.basename(urlparse(url).path)\n",
428
+ " pth = os.path.abspath(os.path.join(model_dir, filename))\n",
429
+ " if not os.path.exists(pth):\n",
430
+ " print('Downloading: '+os.path.basename(url))\n",
431
+ " download_url_to_file(url, pth, hash_prefix=None, progress=True)\n",
432
+ " else:\n",
433
+ " print(f\"\u001b[1;32mThe model {filename} already exists\u001b[0m\")\n",
434
+ "\n",
435
+ "\n",
436
+ "Canny='https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/diffusers_xl_canny_mid.safetensors'\n",
437
+ "Depth='https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/diffusers_xl_depth_mid.safetensors'\n",
438
+ "Sketch='https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/sai_xl_sketch_256lora.safetensors'\n",
439
+ "OpenPose='https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/thibaud_xl_openpose_256lora.safetensors'\n",
440
+ "Recolor='https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/sai_xl_recolor_128lora.safetensors'\n",
441
+ "\n",
442
+ "\n",
443
+ "with capture.capture_output() as cap:\n",
444
+ " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/extensions\n",
445
+ " if not os.path.exists('sd-w'+blsaphemy+'-controlnet'):\n",
446
+ " !git clone https://github.com/Mikubill/sd-w$blsaphemy-controlnet.git\n",
447
+ " %cd /content\n",
448
+ " else:\n",
449
+ " %cd sd-w$blsaphemy-controlnet\n",
450
+ " !git reset --hard\n",
451
+ " !git pull\n",
452
+ " %cd /content\n",
453
+ "\n",
454
+ "mdldir='/content/gdrive/'+mainpth+'/sd/stable-diffusion-w'+blsaphemy+'/extensions/sd-w'+blsaphemy+'-controlnet/models'\n",
455
+ "for filename in os.listdir(mdldir):\n",
456
+ " if \"_sd14v1\" in filename:\n",
457
+ " renamed = re.sub(\"_sd14v1\", \"-fp16\", filename)\n",
458
+ " os.rename(os.path.join(mdldir, filename), os.path.join(mdldir, renamed))\n",
459
+ "\n",
460
+ "!wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt\n",
461
+ "!wget -q -O CN_models_v2.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_v2.txt\n",
462
+ "!wget -q -O CN_models_XL.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_XL.txt\n",
463
+ "\n",
464
+ "\n",
465
+ "with open(\"CN_models.txt\", 'r') as f:\n",
466
+ " mdllnk = f.read().splitlines()\n",
467
+ "with open(\"CN_models_v2.txt\", 'r') as d:\n",
468
+ " mdllnk_v2 = d.read().splitlines()\n",
469
+ "with open(\"CN_models_XL.txt\", 'r') as d:\n",
470
+ " mdllnk_XL = d.read().splitlines()\n",
471
+ "\n",
472
+ "!rm CN_models.txt CN_models_v2.txt CN_models_XL.txt\n",
473
+ "\n",
474
+ "\n",
475
+ "if XL_Model == \"All\":\n",
476
+ " for lnk_XL in mdllnk_XL:\n",
477
+ " download(lnk_XL, mdldir)\n",
478
+ " clear_output()\n",
479
+ " inf('\\u2714 Done','success', '50px')\n",
480
+ "\n",
481
+ "elif XL_Model == \"None\":\n",
482
+ " pass\n",
483
+ " clear_output()\n",
484
+ " inf('\\u2714 Done','success', '50px')\n",
485
+ "\n",
486
+ "else:\n",
487
+ " download(globals()[XL_Model], mdldir)\n",
488
+ " clear_output()\n",
489
+ " inf('\\u2714 Done','success', '50px')\n",
490
+ "\n",
491
+ "\n",
492
+ "Canny='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth'\n",
493
+ "Depth='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth'\n",
494
+ "Lineart='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth'\n",
495
+ "MLSD='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_mlsd.pth'\n",
496
+ "Normal='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_normalbae.pth'\n",
497
+ "OpenPose='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth'\n",
498
+ "Scribble='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_scribble.pth'\n",
499
+ "Seg='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_seg.pth'\n",
500
+ "ip2p='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p.pth'\n",
501
+ "Shuffle='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle.pth'\n",
502
+ "Inpaint='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint.pth'\n",
503
+ "Softedge='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_softedge.pth'\n",
504
+ "Lineart_Anime='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime.pth'\n",
505
+ "Tile='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth'\n",
506
+ "\n",
507
+ "\n",
508
+ "with capture.capture_output() as cap:\n",
509
+ " cfgnames=[os.path.basename(url).split('.')[0]+'.yaml' for url in mdllnk_v2]\n",
510
+ " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/extensions/sd-w$blsaphemy-controlnet/models\n",
511
+ " for name in cfgnames:\n",
512
+ " run(['cp', 'cldm_v21.yaml', name])\n",
513
+ " %cd /content\n",
514
+ "\n",
515
+ "if v1_Model == \"All (21GB)\":\n",
516
+ " for lnk in mdllnk:\n",
517
+ " download(lnk, mdldir)\n",
518
+ " clear_output()\n",
519
+ "\n",
520
+ "elif v1_Model == \"T2iadapter_Models\":\n",
521
+ " mdllnk=list(filter(lambda x: 't2i' in x, mdllnk))\n",
522
+ " for lnk in mdllnk:\n",
523
+ " download(lnk, mdldir)\n",
524
+ " clear_output()\n",
525
+ "\n",
526
+ "elif v1_Model == \"None\":\n",
527
+ " pass\n",
528
+ " clear_output()\n",
529
+ "\n",
530
+ "else:\n",
531
+ " download(globals()[v1_Model], mdldir)\n",
532
+ " clear_output()\n",
533
+ "\n",
534
+ "Canny='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_canny.safetensors'\n",
535
+ "Depth='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_depth.safetensors'\n",
536
+ "HED='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_hed.safetensors'\n",
537
+ "OpenPose='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_openposev2.safetensors'\n",
538
+ "Scribble='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_scribble.safetensors'\n",
539
+ "\n",
540
+ "\n",
541
+ "if v2_Model == \"All\":\n",
542
+ " for lnk_v2 in mdllnk_v2:\n",
543
+ " download(lnk_v2, mdldir)\n",
544
+ " clear_output()\n",
545
+ " inf('\\u2714 Done','success', '50px')\n",
546
+ "\n",
547
+ "elif v2_Model == \"None\":\n",
548
+ " pass\n",
549
+ " clear_output()\n",
550
+ " inf('\\u2714 Done','success', '50px')\n",
551
+ "\n",
552
+ "else:\n",
553
+ " download(globals()[v2_Model], mdldir)\n",
554
+ " clear_output()\n",
555
+ " inf('\\u2714 Done','success', '50px')\n",
556
+ "\n",
557
+ " #@markdown ---"
558
+ ]
559
+ },
560
+ {
561
+ "cell_type": "code",
562
+ "execution_count": null,
563
+ "metadata": {
564
+ "cellView": "form",
565
+ "id": "PjzwxTkPSPHf"
566
+ },
567
+ "outputs": [],
568
+ "source": [
569
+ "#@markdown # Start Stable-Diffusion\n",
570
+ "from IPython.utils import capture\n",
571
+ "import time\n",
572
+ "import sys\n",
573
+ "import fileinput\n",
574
+ "from pyngrok import ngrok, conf\n",
575
+ "import re\n",
576
+ "\n",
577
+ "\n",
578
+ "Use_Cloudflare_Tunnel = False #@param {type:\"boolean\"}\n",
579
+ "#@markdown - Offers better gradio responsivity\n",
580
+ "\n",
581
+ "Ngrok_token = \"\" #@param {type:\"string\"}\n",
582
+ "\n",
583
+ "#@markdown - Input your ngrok token if you want to use ngrok server\n",
584
+ "\n",
585
+ "User = \"\" #@param {type:\"string\"}\n",
586
+ "Password= \"\" #@param {type:\"string\"}\n",
587
+ "#@markdown - Add credentials to your Gradio interface (optional)\n",
588
+ "\n",
589
+ "auth=f\"--gradio-auth {User}:{Password}\"\n",
590
+ "if User ==\"\" or Password==\"\":\n",
591
+ " auth=\"\"\n",
592
+ "\n",
593
+ "\n",
594
+ "with capture.capture_output() as cap:\n",
595
+ " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/\n",
596
+ " !wget -q -O extras.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy/master/modules/extras.py\n",
597
+ " !wget -q -O sd_models.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy/master/modules/sd_models.py\n",
598
+ " !wget -q -O /usr/local/lib/python3.10/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py\n",
599
+ " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/\n",
600
+ "\n",
601
+ " !sed -i '[email protected]\\[\"sd_model_checkpoint\"] = [email protected]\\[\"sd_model_checkpoint\"] = checkpoint_info.title;model.half()@' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/sd_models.py\n",
602
+ " !sed -i '[email protected]_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/webui.py\n",
603
+ " !sed -i \"s@map_location='cpu'@map_location='cuda'@\" /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/extras.py\n",
604
+ "\n",
605
+ " !sed -i 's@possible_sd_paths =.*@possible_sd_paths = [\\\"/content/gdrive/{mainpth}/sd/stablediffusion\\\"]@' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/paths.py\n",
606
+ " !sed -i 's@\\.\\.\\/@src/@g' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/paths.py\n",
607
+ " !sed -i 's@src/generative-models@generative-models@g' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/paths.py\n",
608
+ "\n",
609
+ " !sed -i 's@print(\\\"No module.*@@' /content/gdrive/$mainpth/sd/stablediffusion/ldm/modules/diffusionmodules/model.py\n",
610
+ " !sed -i 's@\\[\"sd_model_checkpoint\"\\]@\\[\"sd_model_checkpoint\", \"sd_vae\", \"CLIP_stop_at_last_layers\", \"inpainting_mask_weight\", \"initial_noise_multiplier\"\\]@g' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/shared.py\n",
611
+ "\n",
612
+ "share=''\n",
613
+ "if Ngrok_token!=\"\":\n",
614
+ " ngrok.kill()\n",
615
+ " srv=ngrok.connect(7860, pyngrok_config=conf.PyngrokConfig(auth_token=Ngrok_token) , bind_tls=True).public_url\n",
616
+ "\n",
617
+ " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n",
618
+ " if line.strip().startswith('self.server_name ='):\n",
619
+ " line = f' self.server_name = \"{srv[8:]}\"\\n'\n",
620
+ " if line.strip().startswith('self.protocol = \"https\"'):\n",
621
+ " line = ' self.protocol = \"https\"\\n'\n",
622
+ " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n",
623
+ " line = ''\n",
624
+ " if line.strip().startswith('else \"http\"'):\n",
625
+ " line = ''\n",
626
+ " sys.stdout.write(line)\n",
627
+ "\n",
628
+ "elif Use_Cloudflare_Tunnel:\n",
629
+ " with capture.capture_output() as cap:\n",
630
+ " !pkill cloudflared\n",
631
+ " time.sleep(4)\n",
632
+ " !nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &\n",
633
+ " time.sleep(4)\n",
634
+ " with open('/content/srv.txt', \"r\") as file: text = file.read()\n",
635
+ " srv= re.findall(r\"https?://(?:\\S+?\\.)?trycloudflare\\.com\\S*\", text)[0]\n",
636
+ "\n",
637
+ " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n",
638
+ " if line.strip().startswith('self.server_name ='):\n",
639
+ " line = f' self.server_name = \"{srv[8:]}\"\\n'\n",
640
+ " if line.strip().startswith('self.protocol = \"https\"'):\n",
641
+ " line = ' self.protocol = \"https\"\\n'\n",
642
+ " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n",
643
+ " line = ''\n",
644
+ " if line.strip().startswith('else \"http\"'):\n",
645
+ " line = ''\n",
646
+ " sys.stdout.write(line)\n",
647
+ "\n",
648
+ " !rm /content/srv.txt\n",
649
+ "\n",
650
+ "else:\n",
651
+ " share='--share'\n",
652
+ "\n",
653
+ "ckptdir=''\n",
654
+ "if os.path.exists('/content/temp_models'):\n",
655
+ " ckptdir='--ckpt-dir /content/temp_models'\n",
656
+ "\n",
657
+ "try:\n",
658
+ " model\n",
659
+ " if os.path.isfile(model):\n",
660
+ " !python /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --ckpt \"$model\" --xformers $auth --disable-console-progressbars --upcast-sampling $ckptdir\n",
661
+ " else:\n",
662
+ " !python /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --ckpt-dir \"$model\" --xformers $auth --disable-console-progressbars --upcast-sampling\n",
663
+ "except:\n",
664
+ " !python /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --xformers $auth --disable-console-progressbars --upcast-sampling $ckptdir"
665
+ ]
666
+ }
667
+ ],
668
+ "metadata": {
669
+ "accelerator": "GPU",
670
+ "colab": {
671
+ "provenance": []
672
+ },
673
+ "gpuClass": "standard",
674
+ "kernelspec": {
675
+ "display_name": "Python 3",
676
+ "name": "python3"
677
+ },
678
+ "language_info": {
679
+ "name": "python"
680
+ }
681
+ },
682
+ "nbformat": 4,
683
+ "nbformat_minor": 0
684
+ }