File size: 28,996 Bytes
2bae464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "47kV9o1Ni8GH"
      },
      "source": [
        "# **Colab From https://github.com/TheLastBen/fast-stable-diffusion, if you face any issues, feel free to discuss them.** [Support](https://ko-fi.com/thelastben)\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Y9EBc437WDOs"
      },
      "outputs": [],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/gdrive')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "cellView": "form",
        "id": "CFWtw-6EPrKi"
      },
      "outputs": [],
      "source": [
        "#@markdown # Install AUTOMATIC1111 repo\n",
        "from IPython.utils import capture\n",
        "from IPython.display import clear_output\n",
        "from subprocess import getoutput\n",
        "from google.colab import runtime\n",
        "from IPython.display import display_markdown\n",
        "import time\n",
        "\n",
        "\n",
        "with capture.capture_output() as cap:\n",
        "  fgitclone = \"git clone --depth 1\" \n",
        "  %cd /content/gdrive/MyDrive/\n",
        "  %mkdir  sd\n",
        "  %cd sd\n",
        "  !$fgitclone https://github.com/Stability-AI/stablediffusion\n",
        "  !$fgitclone https://github.com/AUTOMATIC1111/stable-diffusion-webui\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
        "  !mkdir -p cache/{huggingface,torch}\n",
        "  %cd /content/\n",
        "  !ln -s /content/gdrive/MyDrive/sd/stable-diffusion-webui/cache/huggingface ../root/.cache/\n",
        "  !ln -s /content/gdrive/MyDrive/sd/stable-diffusion-webui/cache/torch ../root/.cache/\n",
        "\n",
        "Update_repo = True\n",
        "if Update_repo:\n",
        "  with capture.capture_output() as cap:\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.sh\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/paths.py\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/ui.py\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/style.css\n",
        "    !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/shared.py\n",
        "    %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
        "  print('\u001b[1;32m')\n",
        "  !git pull\n",
        "  clear_output()\n",
        "  print('\u001b[1;32mDONE !')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "cellView": "form",
        "id": "ZGV_5H4xrOSp"
      },
      "outputs": [],
      "source": [
        "#@markdown # Requirements\n",
        "\n",
        "import os\n",
        "import time\n",
        "from IPython.utils import capture\n",
        "from subprocess import getoutput\n",
        "from IPython.display import clear_output\n",
        "from re import search\n",
        "\n",
        "print('\u001b[1;32mInstalling requirements...')\n",
        "with capture.capture_output() as cap:\n",
        "  \n",
        "  if not os.path.exists('/content/gdrive/MyDrive/sd/stablediffusion/src/k-diffusion/k_diffusion'):\n",
        "    !mkdir /content/gdrive/MyDrive/sd/stablediffusion/src\n",
        "    %cd /content/gdrive/MyDrive/sd/stablediffusion/src\n",
        "    !$fgitclone https://github.com/CompVis/taming-transformers\n",
        "    !$fgitclone https://github.com/openai/CLIP\n",
        "    !$fgitclone https://github.com/salesforce/BLIP\n",
        "    !$fgitclone https://github.com/sczhou/CodeFormer\n",
        "    !$fgitclone https://github.com/crowsonkb/k-diffusion\n",
        "    !mv /content/gdrive/MyDrive/sd/stablediffusion/src/CLIP /content/gdrive/MyDrive/sd/stablediffusion/src/clip\n",
        "    !mv  /content/gdrive/MyDrive/sd/stablediffusion/src/BLIP /content/gdrive/MyDrive/sd/stablediffusion/src/blip\n",
        "    !mv  /content/gdrive/MyDrive/sd/stablediffusion/src/CodeFormer /content/gdrive/MyDrive/sd/stablediffusion/src/codeformer\n",
        "    !cp -r /content/gdrive/MyDrive/sd/stablediffusion/src/k-diffusion/k_diffusion /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
        "\n",
        "  %cd /content/\n",
        "  for i in range(1,6):\n",
        "      !wget -q \"https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/Dependencies.{i}\"\n",
        "      !mv \"Dependencies.{i}\" \"Dependencies.7z.00{i}\"\n",
        "  !7z x -y Dependencies.7z.001\n",
        "  time.sleep(2)\n",
        "  !cp -r /content/usr/local/lib/python3.8/dist-packages /usr/local/lib/python3.8/\n",
        "  !rm -r /content/usr\n",
        "  for i in range(1,6):\n",
        "      !rm \"Dependencies.7z.00{i}\"\n",
        "  !pip install -q transformers==4.19.2\n",
        "  s = getoutput('nvidia-smi')\n",
        "  if \"A100\" in s:\n",
        "      !wget https://github.com/TheLastBen/fast-stable-diffusion/raw/main/precompiled/A100/A100\n",
        "      !rm -r /usr/local/lib/python3.8/dist-packages/xformers\n",
        "      %cd /usr/local/lib/python3.8/dist-packages/\n",
        "      !7z x -y /content/A100\n",
        "      !rm /content/A100\n",
        "\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules\n",
        "  !wget -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py\n",
        "\n",
        "  if os.path.exists(\"/content/gdrive/MyDrive/sd/stablediffusion/repositories/midas/midas/backbones\"):\n",
        "    !rm -r /content/gdrive/MyDrive/sd/stablediffusion/repositories/midas\n",
        "  if not os.path.exists(\"/content/gdrive/MyDrive/sd/stablediffusion/repositories/midas\"):\n",
        "    %cd /content/gdrive/MyDrive/sd/stablediffusion\n",
        "    !mkdir repositories\n",
        "    %cd repositories\n",
        "    !git clone https://github.com/isl-org/MiDaS.git midas\n",
        "    %cd midas\n",
        "    !git checkout -f b845b78\n",
        "\n",
        "  if not os.path.exists('/content/gdrive/MyDrive/sd/stablediffusion/repositories/BoostingMonocularDepth'):\n",
        "    %cd /content/gdrive/MyDrive/sd/stablediffusion/repositories\n",
        "    !git clone https://github.com/compphoto/BoostingMonocularDepth.git\n",
        "  %cd /content\n",
        "\n",
        "base = '/content/gdrive/MyDrive/sd/stable-diffusion'\n",
        "dirs = [base, f'{base}/src/taming-transformers', f'{base}/src/clip',\n",
        "        f'{base}/src/GFPGAN', f'{base}/src/blip', f'{base}/src/codeformer',\n",
        "        f'{base}/src/realesrgan', f'{base}/src/k-diffusion', f'{base}/src/ldm']\n",
        "for d in dirs:\n",
        "    !rm -rf {d + '/.git'}\n",
        "clear_output()\n",
        "print('\u001b[1;32mDone, proceed')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "p4wj_txjP3TC"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from IPython.display import clear_output\n",
        "import time\n",
        "\n",
        "#@markdown # Model Download/Load\n",
        "Model_Version = \"1.5\" #@param [ \"1.5\", \"V2.1-512px\", \"V2.1-768px\"]\n",
        "\n",
        "RunwayML_Inpainting_Model = False #@param {type:\"boolean\"}\n",
        "\n",
        "token = \"\" #@param {type:\"string\"}\n",
        "\n",
        "Redownload_the_original_model = False #@param {type:\"boolean\"}\n",
        "\n",
        "if Redownload_the_original_model:\n",
        "  if os.path.exists('/content/mainmodel.ckpt'):\n",
        "    !rm /content/mainmodel.ckpt\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion\n",
        "  !wget -q -O model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/README.md\n",
        "  !mv /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f  \n",
        "  time.sleep(2)\n",
        "  !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f\n",
        "  time.sleep(2)\n",
        "  clear_output()\n",
        "\n",
        "#@markdown Or\n",
        "Path_to_MODEL = \"\" #@param {type:\"string\"}\n",
        "#@markdown - Insert the full path of your trained model or to a folder containing multiple models.\n",
        "\n",
        "#@markdown Or\n",
        "MODEL_LINK = \"\" #@param {type:\"string\"}\n",
        "safetensors = False #@param {type:\"boolean\"}\n",
        "#@markdown - A direct link to a Model or a shared gdrive link.\n",
        "\n",
        "def newmdl(token):\n",
        "\n",
        "    if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
        "      if token==\"\":\n",
        "        token=input(\"Insert your huggingface token :\")\n",
        "      %cd /content/\n",
        "      clear_output()\n",
        "      !git init\n",
        "      !git lfs install --system --skip-repo\n",
        "      !$fgitclone --branch fp16 \"https://USER:{token}@huggingface.co/runwayml/stable-diffusion-v1-5\"\n",
        "      if os.path.exists('/content/stable-diffusion-v1-5'):\n",
        "        !$fgitclone \"https://USER:{token}@huggingface.co/stabilityai/sd-vae-ft-mse\"\n",
        "        !rm -r /content/stable-diffusion-v1-5/vae\n",
        "        !mv /content/sd-vae-ft-mse /content/stable-diffusion-v1-5/vae        \n",
        "        !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py\n",
        "        !sed -i '201s@.*@    model_path = \"/content/stable-diffusion-v1-5\"@' /content/convertosd.py\n",
        "        !sed -i '202s@.*@    checkpoint_path= \"/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\"@' /content/convertosd.py\n",
        "        clear_output()       \n",
        "        !python /content/convertosd.py \n",
        "        if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
        "          model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
        "          clear_output()\n",
        "          print('\u001b[1;32mDONE !')\n",
        "        else:\n",
        "          print('\u001b[1;31mSomething went wrong, try again')\n",
        "      else:\n",
        "        print('\u001b[1;31mMake sure you accept the terms at https://huggingface.co/runwayml/stable-diffusion-v1-5')\n",
        "\n",
        "    else:\n",
        "      model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
        "      clear_output()\n",
        "      print('\u001b[1;32mModel already exists, check the box \"Redownload_the_original_model\" to redownload/download the V1.5')\n",
        "\n",
        "    if os.path.exists('/content/.git'):\n",
        "      !rm -r /content/.git\n",
        "\n",
        "    if os.path.exists('/content/stable-diffusion-v1-5'):\n",
        "      !rm -r /content/stable-diffusion-v1-5\n",
        "\n",
        "\n",
        "def V2(token):\n",
        "\n",
        "    if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
        "      %cd /content/\n",
        "      clear_output()\n",
        "      !mkdir \"/content/stable-diffusion-V2\"\n",
        "      %cd \"/content/stable-diffusion-V2\"\n",
        "      !git init\n",
        "      !git lfs install --system --skip-repo\n",
        "      if Model_Version == \"V2.1-768px\":\n",
        "        !git remote add -f origin  \"https://USER:{token}@huggingface.co/stabilityai/stable-diffusion-2-1\"\n",
        "      elif Model_Version == \"V2.1-512px\":\n",
        "        !git remote add -f origin  \"https://USER:{token}@huggingface.co/stabilityai/stable-diffusion-2-1-base\" \n",
        "      !git config core.sparsecheckout true\n",
        "      !echo -e \"scheduler\\ntext_encoder\\ntokenizer\\nunet\\nvae\\nfeature_extractor\\nmodel_index.json\\n!*.safetensors\" > .git/info/sparse-checkout\n",
        "      !git pull origin fp16\n",
        "      %cd /content\n",
        "      !wget -O convertosdv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosdv2.py\n",
        "      clear_output()\n",
        "      !python /content/convertosdv2.py --fp16 /content/stable-diffusion-V2 /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\n",
        "      if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
        "        model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
        "        clear_output()\n",
        "        print('\u001b[1;32mDONE !')\n",
        "      else:\n",
        "        print('\u001b[1;31mSomething went wrong, try again')\n",
        "\n",
        "    else:\n",
        "      model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
        "      clear_output()\n",
        "      print('\u001b[1;32mModel already exists, check the box \"Redownload_the_original_model\" to redownload/download the V2.')\n",
        "\n",
        "    if os.path.exists('/content/.git'):\n",
        "      !rm -r /content/.git\n",
        "      !rm -r /content/convertosdv2.py\n",
        "    if os.path.exists('/content/stable-diffusion-V2'):\n",
        "      !rm -r /content/stable-diffusion-V2\n",
        "\n",
        "def inpmdl(token):\n",
        "\n",
        "    if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):\n",
        "      if token==\"\":\n",
        "        token=input(\"Insert your huggingface token :\")\n",
        "      %cd /content/\n",
        "      clear_output()\n",
        "      !git init\n",
        "      !git lfs install --system --skip-repo\n",
        "      !$fgitclone --branch fp16 \"https://USER:{token}@huggingface.co/runwayml/stable-diffusion-inpainting\"\n",
        "      if os.path.exists('/content/stable-diffusion-inpainting'):\n",
        "        !$fgitclone \"https://USER:{token}@huggingface.co/stabilityai/sd-vae-ft-mse\"\n",
        "        !rm -r /content/stable-diffusion-inpainting/vae\n",
        "        !mv /content/sd-vae-ft-mse /content/stable-diffusion-inpainting/vae        \n",
        "        !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py\n",
        "        !sed -i '201s@.*@    model_path = \"/content/stable-diffusion-inpainting\"@' /content/convertosd.py\n",
        "        !sed -i '202s@.*@    checkpoint_path= \"/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt\"@' /content/convertosd.py\n",
        "        clear_output()       \n",
        "        !python /content/convertosd.py \n",
        "        if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):\n",
        "          clear_output()\n",
        "          print('\u001b[1;32mDONE !')\n",
        "        else:\n",
        "          print('\u001b[1;31mSomething went wrong, try again')\n",
        "      else:\n",
        "        print('\u001b[1;31mMake sure you have accepted the terms at https://huggingface.co/runwayml/stable-diffusion-inpainting')\n",
        "\n",
        "    else:\n",
        "      clear_output()\n",
        "      print('\u001b[1;32mModel already exists.')\n",
        "\n",
        "    if os.path.exists('/content/.git'):\n",
        "      !rm -r /content/.git\n",
        "\n",
        "    if os.path.exists('/content/stable-diffusion-inpainting'):\n",
        "      !rm -r /content/stable-diffusion-inpainting   \n",
        "\n",
        "\n",
        "if (Path_to_MODEL !=''):\n",
        "  if os.path.exists(str(Path_to_MODEL)):\n",
        "    print('\u001b[1;32mUsing the trained model.')\n",
        "  else:\n",
        "      while not os.path.exists(str(Path_to_MODEL)):\n",
        "        print('\u001b[1;31mWrong path, use the colab file explorer to copy the path : ')\n",
        "        Path_to_MODEL=input()\n",
        "      if os.path.exists(str(Path_to_MODEL)):\n",
        "        print('\u001b[1;32mUsing the trained model.')\n",
        "\n",
        "  model=Path_to_MODEL\n",
        "\n",
        "elif MODEL_LINK != \"\":\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion\n",
        "  clear_output()\n",
        "  if not safetensors:\n",
        "    modelname=\"model.ckpt\"\n",
        "  else:\n",
        "    modelname=\"model.safetensors\"\n",
        "  !gdown --fuzzy -O $modelname $MODEL_LINK\n",
        "  if os.path.exists(f'/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'):\n",
        "    if os.path.getsize(f'/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}') > 1810671599:\n",
        "      model=f'/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'\n",
        "      clear_output()\n",
        "      print('\u001b[1;32mModel downloaded, using the trained model.')\n",
        "    else:\n",
        "      print('\u001b[1;31mWrong link, check that the link is valid')\n",
        "  else:\n",
        "    print('\u001b[1;31mWrong link, check that the link is valid')\n",
        "\n",
        "\n",
        "elif Model_Version==\"1.5\":\n",
        "  newmdl(token)\n",
        "\n",
        "elif Model_Version==\"V2.1-512px\" or Model_Version==\"V2.1-768px\":\n",
        "  V2(token)\n",
        "\n",
        "if RunwayML_Inpainting_Model:\n",
        "  inpmdl(token)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "PjzwxTkPSPHf"
      },
      "outputs": [],
      "source": [
        "#@markdown # Start stable-diffusion\n",
        "from IPython.utils import capture\n",
        "import time\n",
        "import sys\n",
        "import fileinput\n",
        "\n",
        "Model_Version = \"1.5\" #@param [\"1.5\", \"V2.1-512\", \"V2.1-768\"]\n",
        "#@markdown  - Important! Choose the correct version and resolution of the model\n",
        "\n",
        "Use_Gradio_Server = True #@param {type:\"boolean\"}\n",
        "#@markdown  - Only if you have trouble connecting to the local server\n",
        "\n",
        "Large_Model= False #@param {type:\"boolean\"}\n",
        "#@markdown  - Check if you have trouble loading a model 7GB+\n",
        "\n",
        "Enable_API = False\n",
        "\n",
        "   \n",
        "with capture.capture_output() as cap: \n",
        "  if not os.path.exists('/tools/node/bin/lt'):\n",
        "    !npm install -g localtunnel\n",
        "\n",
        "with capture.capture_output() as cap: \n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/  \n",
        "  !wget -O webui.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.py\n",
        "  !sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/\n",
        "  !wget -O shared.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/modules/shared.py\n",
        "  !wget -O ui.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/modules/ui.py\n",
        "  !sed -i 's@css = \"\".*@with open(os.path.join(script_path, \"style.css\"), \"r\", encoding=\"utf8\") as file:\\n        css = file.read()@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/ui.py\n",
        "  %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui\n",
        "  !wget -O style.css https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/style.css\n",
        "  !sed -i 's@min-height: 4.*@min-height: 5.5em;@g' /content/gdrive/MyDrive/sd/stable-diffusion-webui/style.css\n",
        "  !sed -i \"s@'extensions/depthmap2mask/scripts/depthmap_for_depth2img.py'@\\\"/content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/depthmap2mask/scripts/depthmap_for_depth2img.py\\\"@\" /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/depthmap2mask/scripts/depth2image_depthmask.py\n",
        "  !sed -i '34s@.*@sys.path.append(\"/content/gdrive/MyDrive/sd/stablediffusion/repositories/midas\")@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/stable-diffusion-webui-depthmap-script/scripts/depthmap.py\n",
        "  !sed -i '35s@.*@sys.path.append(\"/content/gdrive/MyDrive/sd/stablediffusion/repositories/BoostingMonocularDepth\")@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/stable-diffusion-webui-depthmap-script/scripts/depthmap.py\n",
        "  !sed -i 's@\"multiple_tqdm\": true,@\\\"multiple_tqdm\": false,@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/config.json\n",
        "  !sed -i '902s@.*@        self.logvar = self.logvar.to(self.device)@' /content/gdrive/MyDrive/sd/stablediffusion/ldm/models/diffusion/ddpm.py\n",
        "  if os.path.exists(\"/content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/openOutpaint-webUI-extension\"):\n",
        "    %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/openOutpaint-webUI-extension/scripts\n",
        "    !wget -O main.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/outpaint/main.py\n",
        "  %cd /content\n",
        "\n",
        "  if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers/src/infer.py'):\n",
        "    %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers\n",
        "    !wget -O animation.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/animation.py\n",
        "    !wget -O depth.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/depth.py\n",
        "    %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers/src\n",
        "    !wget -O infer.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/infer.py\n",
        "    !sed -i 's@import deforum_helpers.args@import scripts.deforum_helpers.args@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum.py\n",
        "    !sed -i 's@import deforum_helpers.settings@import scripts.deforum_helpers.settings@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum.py\n",
        "    !sed -i 's@from deforum_helpers.render@from scripts.deforum_helpers.render@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum.py\n",
        "    !sed -i 's@from deforum_helpers.settings@from scripts.deforum_helpers.settings@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers/args.py\n",
        "\n",
        "  if Large_Model:\n",
        "    !sed -i 's@cmd_opts.lowram else \\\"cpu\\\"@cmd_opts.lowram else \\\"cuda\\\"@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/shared.py\n",
        "  else:\n",
        "    !sed -i 's@cmd_opts.lowram else \\\"cuda\\\"@cmd_opts.lowram else \\\"cpu\\\"@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/shared.py  \n",
        "\n",
        "\n",
        "share=''\n",
        "if Use_Gradio_Server:\n",
        "  share='--share'\n",
        "  for line in fileinput.input('/usr/local/lib/python3.8/dist-packages/gradio/blocks.py', inplace=True):\n",
        "    if line.strip().startswith('self.server_name ='):\n",
        "        line = '            self.server_name = server_name\\n'\n",
        "    if line.strip().startswith('self.server_port ='):\n",
        "        line = '            self.server_port = server_port\\n'\n",
        "    sys.stdout.write(line)\n",
        "  clear_output()\n",
        "  \n",
        "else:\n",
        "  share=''\n",
        "  !nohup lt --port 7860 > srv.txt 2>&1 &\n",
        "  time.sleep(2)\n",
        "  !grep -o 'https[^ ]*' /content/srv.txt >srvr.txt\n",
        "  time.sleep(2)\n",
        "  srv= getoutput('cat /content/srvr.txt')\n",
        "\n",
        "  for line in fileinput.input('/usr/local/lib/python3.8/dist-packages/gradio/blocks.py', inplace=True):\n",
        "    if line.strip().startswith('self.server_name ='):\n",
        "        line = f'            self.server_name = \"{srv[8:]}\"\\n'\n",
        "    if line.strip().startswith('self.server_port ='):\n",
        "        line = '            self.server_port = 443\\n'\n",
        "    if line.strip().startswith('self.protocol = \"https\"'):\n",
        "        line = '            self.protocol = \"https\"\\n'\n",
        "    if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n",
        "        line = ''    \n",
        "    if line.strip().startswith('else \"http\"'):\n",
        "        line = ''              \n",
        "    sys.stdout.write(line)\n",
        "          \n",
        "  !sed -i '13s@.*@    \"PUBLIC_SHARE_TRUE\": \"\u001b[32mConnected\",@' /usr/local/lib/python3.8/dist-packages/gradio/strings.py\n",
        "  \n",
        "  !rm /content/srv.txt\n",
        "  !rm /content/srvr.txt\n",
        "\n",
        "with capture.capture_output() as cap: \n",
        "  %cd /content/gdrive/MyDrive/sd/stablediffusion\n",
        "\n",
        "api = '--api' if Enable_API else ''\n",
        "\n",
        "if Model_Version == \"V2.1-768\":\n",
        "  configf=\"--config /content/gdrive/MyDrive/sd/stablediffusion/configs/stable-diffusion/v2-inference-v.yaml\"\n",
        "  !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cuda\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py  \n",
        "elif Model_Version == \"V2.1-512\":\n",
        "  configf=\"--config /content/gdrive/MyDrive/sd/stablediffusion/configs/stable-diffusion/v2-inference.yaml\"\n",
        "  !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cuda\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py  \n",
        "else:\n",
        "  configf=\"\"\n",
        "  !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cpu\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py  \n",
        "\n",
        "if os.path.exists('/usr/local/lib/python3.8/dist-packages/xformers'):\n",
        "  xformers=\"--xformers\"\n",
        "else:\n",
        "  xformers=\"\"\n",
        "\n",
        "try:\n",
        "  model\n",
        "  if os.path.isfile(model):\n",
        "    !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --enable-insecure-extension-access --no-half-vae  --ckpt \"$model\" $configf $xformers\n",
        "  else:\n",
        "    !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --enable-insecure-extension-access --no-half-vae  --ckpt-dir \"$model\" $configf $xformers\n",
        "except:\n",
        "   !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --enable-insecure-extension-access --no-half-vae $configf $xformers"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}