Harshveer commited on
Commit
553b1cb
1 Parent(s): 1b7f69a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +375 -0
app.py ADDED
@@ -0,0 +1,375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from IPython.utils import capture
2
+ from IPython.display import clear_output
3
+ from subprocess import getoutput
4
+ import ipywidgets as widgets
5
+ import sys
6
+ import fileinput
7
+ import os
8
+ import time
9
+
10
+ if not os.path.exists("./"):
11
+ print('Gdrive not connected, using colab storage ...')
12
+ time.sleep(4)
13
+ # !mkdir -p ./
14
+ with capture.capture_output() as cap:
15
+ def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)
16
+ fgitclone = "git clone --depth 1"
17
+ # %mkdir -p ./sd
18
+ # %cd ./sd
19
+ # !$fgitclone -q --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui
20
+ # !mkdir -p ./cache/huggingface
21
+ # !ln -s ./cache/huggingface /root/.cache/
22
+
23
+ with capture.capture_output() as cap:
24
+ %cd ./
25
+ # !git reset --hard
26
+ time.sleep(1)
27
+ # !rm webui.sh
28
+ print('')
29
+ # !git pull
30
+ clear_output()
31
+ inf('\u2714 Done','success', '50px')
32
+
33
+
34
+ print('Installing requirements...')
35
+
36
+ with capture.capture_output() as cap:
37
+ %cd /content/
38
+ !wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt
39
+ !dpkg -i *.deb
40
+ if not os.path.exists('./stablediffusion'):
41
+ !tar -C / --zstd -xf sd.tar.zst
42
+ !tar -C / --zstd -xf A1111_dep.tar.zst
43
+ !rm *.deb | rm *.zst | rm *.txt
44
+ %env LD_PRELOAD=libtcmalloc.so
45
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
46
+
47
+ clear_output()
48
+ inf('\u2714 Done','success', '50px')
49
+
50
+
51
+
52
+ Model_Version = "1.5" #@param [ "1.5", "v1.5 Inpainting ", "V2.1-512px", "V2.1-768px"]
53
+
54
+ Redownload_the_original_model = False #@param {type:"boolean"}
55
+
56
+ def rmv():
57
+ !wget -q -O ./models/Stable-diffusion/model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/README.md
58
+ !mv ./models/Stable-diffusion/model.ckpt ./models/Stable-diffusion/trashfile.f
59
+ time.sleep(2)
60
+ !rm ./models/Stable-diffusion/trashfile.f
61
+ clear_output()
62
+
63
+ if Redownload_the_original_model:
64
+ with capture.capture_output() as cap:
65
+ rmv()
66
+
67
+ #@markdown Or
68
+ Path_to_MODEL = "" #@param {type:"string"}
69
+ #@markdown - Insert the full path of your trained model or to a folder containing multiple models.
70
+
71
+ #@markdown Or
72
+ MODEL_LINK = "" #@param {type:"string"}
73
+ safetensors = False #@param {type:"boolean"}
74
+ Use_temp_storage = False #@param {type:"boolean"}
75
+
76
+
77
+
78
+ def newmdl():
79
+
80
+ if not os.path.exists('./models/Stable-diffusion/model.ckpt'):
81
+ %mkdir ./content/
82
+ %cd ./content/
83
+ clear_output()
84
+ !git init
85
+ !git lfs install --system --skip-repo
86
+ !$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-v1-5"
87
+ if os.path.exists('./content/stable-diffusion-v1-5/unet/diffusion_pytorch_model.bin'):
88
+ !wget -q -O stable-diffusion-v1-5/vae/diffusion_pytorch_model.bin https://huggingface.co/stabilityai/sd-vae-ft-mse/resolve/main/diffusion_pytorch_model.bin
89
+ !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
90
+ !sed -i '201s@.*@ model_path = "./content/stable-diffusion-v1-5"@' ./content/convertosd.py
91
+ !sed -i '202s@.*@ checkpoint_path= "./models/Stable-diffusion/model.ckpt"@' ./content/convertosd.py
92
+ clear_output()
93
+ !python ./content/convertosd.py
94
+ !rm ./content/convertosd.py
95
+ if os.path.exists('./models/Stable-diffusion/model.ckpt'):
96
+ model='./models/Stable-diffusion/model.ckpt'
97
+ clear_output()
98
+ inf('\u2714 Done','success', '50px')
99
+ else:
100
+ inf('\u2718 Something went wrong, try again','danger', "250px")
101
+ else:
102
+ inf('\u2718 Something went wrong','danger', "200px")
103
+
104
+ else:
105
+ model='./models/Stable-diffusion/model.ckpt'
106
+ clear_output()
107
+ inf('\u2714 Model already exists, check the box "Redownload_the_original_model" to redownload/download the V1.5','primary', '700px')
108
+
109
+ if os.path.exists('./content/.git'):
110
+ !rm -r ./content/.git
111
+
112
+ if os.path.exists('./content/stable-diffusion-v1-5'):
113
+ !rm -r ./content/stable-diffusion-v1-5
114
+
115
+ return model
116
+
117
+ def V2():
118
+
119
+ if not os.path.exists('./models/Stable-diffusion/model.ckpt'):
120
+ %cd ./content/
121
+ clear_output()
122
+ !mkdir "./content/stable-diffusion-V2"
123
+ %cd "./content/stable-diffusion-V2"
124
+ !git init
125
+ !git lfs install --system --skip-repo
126
+ if Model_Version == "V2.1-768px":
127
+ !git remote add -f origin "https://huggingface.co/stabilityai/stable-diffusion-2-1"
128
+ elif Model_Version == "V2.1-512px":
129
+ !git remote add -f origin "https://huggingface.co/stabilityai/stable-diffusion-2-1-base"
130
+ !git config core.sparsecheckout true
131
+ !echo -e "scheduler\ntext_encoder\ntokenizer\nunet\nvae\nfeature_extractor\nmodel_index.json\n!*.safetensors" > .git/info/sparse-checkout
132
+ !git pull origin fp16
133
+ %cd ./content
134
+ !wget -O convertosdv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosdv2.py
135
+ clear_output()
136
+ !python ./content/convertosdv2.py --fp16 ./content/stable-diffusion-V2 ./models/Stable-diffusion/model.ckpt
137
+ !rm ./content/convertosdv2.py
138
+ if os.path.exists('./models/Stable-diffusion/model.ckpt'):
139
+ model='./models/Stable-diffusion/model.ckpt'
140
+ clear_output()
141
+ inf('\u2714 Done','success', '50px')
142
+ else:
143
+ inf('\u2718 Something went wrong, try again','danger', "250px")
144
+
145
+ else:
146
+ model='./models/Stable-diffusion/model.ckpt'
147
+ clear_output()
148
+ inf('\u2714 Model already exists, check the box "Redownload_the_original_model" to redownload/download the V2','primary', '700px')
149
+
150
+ if os.path.exists('./content/.git'):
151
+ !rm -r ./content/.git
152
+ !rm -r ./content/convertosdv2.py
153
+ if os.path.exists('./content/stable-diffusion-V2'):
154
+ !rm -r ./content/stable-diffusion-V2
155
+
156
+ return model
157
+
158
+ def inpmdl():
159
+
160
+ if not os.path.exists('./models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):
161
+ %cd ./content/
162
+ clear_output()
163
+ !git init
164
+ !git lfs install --system --skip-repo
165
+ !$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-inpainting"
166
+ if os.path.exists('./content/stable-diffusion-inpainting'):
167
+ !$fgitclone "https://huggingface.co/stabilityai/sd-vae-ft-mse"
168
+ !rm -r ./content/stable-diffusion-inpainting/vae
169
+ !mv ./content/sd-vae-ft-mse ./content/stable-diffusion-inpainting/vae
170
+ !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
171
+ !sed -i '201s@.*@ model_path = "./content/stable-diffusion-inpainting"@' ./content/convertosd.py
172
+ !sed -i '202s@.*@ checkpoint_path= "./models/Stable-diffusion/sd-v1-5-inpainting.ckpt"@' ./content/convertosd.py
173
+ clear_output()
174
+ !python ./content/convertosd.py
175
+ !rm ./content/convertosd.py
176
+ if os.path.exists('./models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):
177
+ model='./models/Stable-diffusion/sd-v1-5-inpainting.ckpt'
178
+ clear_output()
179
+ inf('\u2714 Done','success', '50px')
180
+ else:
181
+ inf('\u2718 Something went wrong, try again','danger', "250px")
182
+ else:
183
+ inf('\u2718 Something went wrong','danger', "200px")
184
+
185
+
186
+ else:
187
+ model='./models/Stable-diffusion/sd-v1-5-inpainting.ckpt'
188
+ clear_output()
189
+ inf('\u2714 inpainting model already exists','primary', '250px')
190
+
191
+ if os.path.exists('./content/.git'):
192
+ !rm -r ./content/.git
193
+
194
+ if os.path.exists('./content/stable-diffusion-inpainting'):
195
+ !rm -r ./content/stable-diffusion-inpainting
196
+
197
+ return model
198
+
199
+ if (Path_to_MODEL !=''):
200
+ if os.path.exists(str(Path_to_MODEL)):
201
+ inf('\u2714 Using the trained model.','success', '200px')
202
+
203
+ else:
204
+ while not os.path.exists(str(Path_to_MODEL)):
205
+ inf('\u2718 Wrong path, use the colab file explorer to copy the path : ','danger', "400px")
206
+ Path_to_MODEL=input()
207
+ if os.path.exists(str(Path_to_MODEL)):
208
+ inf('\u2714 Using the trained model.','success', '200px')
209
+
210
+ model=Path_to_MODEL
211
+
212
+ elif MODEL_LINK != "":
213
+ gdrv="./models/Stable-diffusion"
214
+ tmp="./content"
215
+ pth=tmp if Use_temp_storage else gdrv
216
+ %cd $pth
217
+ clear_output()
218
+ if not safetensors:
219
+ modelname="model.ckpt"
220
+ else:
221
+ modelname="model.safetensors"
222
+ !gdown --fuzzy -O $modelname $MODEL_LINK
223
+ if os.path.exists(f'{pth}/{modelname}') and os.path.getsize(f'{pth}/{modelname}') > 1810671599:
224
+ model=f'{pth}/{modelname}'
225
+ clear_output()
226
+ inf('\u2714 Model downloaded, using the trained model.','success', '350px')
227
+ else:
228
+ if Use_temp_storage:
229
+ !rm $pth/$modelname
230
+ else:
231
+ rmv()
232
+ inf('\u2718 Wrong link, check that the link is valid','danger', "300px")
233
+
234
+
235
+ elif Model_Version=="1.5":
236
+ model=newmdl()
237
+
238
+ elif Model_Version=="V2.1-512px" or Model_Version=="V2.1-768px":
239
+ model=V2()
240
+
241
+ else:
242
+ model=inpmdl()
243
+
244
+
245
+
246
+ #@markdown # ControlNet
247
+ from torch.hub import download_url_to_file
248
+ from urllib.parse import urlparse
249
+
250
+ Model = "All (5.8GB)" #@param [ "All (5.8GB)", "Canny", "Depth", "HED", "MLSD", "Normal", "OpenPose", "Scribble", "Seg", "T2iadapter_Keypose", "T2iadapter_Seg", "T2iadapter_Sketch", "T2iadapter_Depth"]
251
+
252
+ #@markdown - Download/update ControlNet extension and its models.
253
+
254
+ def download(url, model_dir):
255
+
256
+ filename = os.path.basename(urlparse(url).path)
257
+ pth = os.path.abspath(os.path.join(model_dir, filename))
258
+ if not os.path.exists(pth):
259
+ print('Downloading: '+os.path.basename(url))
260
+ download_url_to_file(url, pth, hash_prefix=None, progress=True)
261
+ else:
262
+ print(f"The model {filename} already exists")
263
+
264
+ Canny='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_canny-fp16.safetensors'
265
+ Depth='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_depth-fp16.safetensors'
266
+ HED='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_hed-fp16.safetensors'
267
+ MLSD='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_mlsd-fp16.safetensors'
268
+ Normal='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_normal-fp16.safetensors'
269
+ OpenPose='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_openpose-fp16.safetensors'
270
+ Scribble='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_scribble-fp16.safetensors'
271
+ Seg='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_seg-fp16.safetensors'
272
+ T2iadapter_Keypose='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/t2iadapter_keypose-fp16.safetensors'
273
+ T2iadapter_Seg='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/t2iadapter_seg-fp16.safetensors'
274
+ T2iadapter_Sketch='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/t2iadapter_sketch-fp16.safetensors'
275
+ T2iadapter_Depth='https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/t2iadapter_depth-fp16.safetensors'
276
+
277
+ with capture.capture_output() as cap:
278
+ %cd ./extensions
279
+ if not os.path.exists("sd-webui-controlnet"):
280
+ !git clone https://github.com/Mikubill/sd-webui-controlnet.git
281
+ %cd ./content
282
+ else:
283
+ %cd sd-webui-controlnet
284
+ !git pull
285
+ %cd ./content
286
+
287
+ !cp ./extensions/sd-webui-controlnet/models/*.yaml ./models
288
+ mdldir="./extensions/sd-webui-controlnet/models"
289
+
290
+ if Model == "All (5.8GB)":
291
+ !wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt
292
+ with open("CN_models.txt", 'r') as f:
293
+ mdllnk = f.read().splitlines()
294
+
295
+
296
+ for lnk in mdllnk:
297
+ download(url=lnk, model_dir=mdldir)
298
+ !rm CN_models.txt
299
+ clear_output()
300
+ inf('\u2714 Done','success', '50px')
301
+
302
+ else:
303
+ download(globals()[Model], model_dir=mdldir)
304
+ clear_output()
305
+ inf('\u2714 Done','success', '50px')
306
+
307
+
308
+ #@markdown # Start Stable-Diffusion
309
+ from IPython.utils import capture
310
+ import time
311
+ import sys
312
+ import fileinput
313
+
314
+ Use_localtunnel = False #@param {type:"boolean"}
315
+
316
+ User = "" #@param {type:"string"}
317
+ Password= "" #@param {type:"string"}
318
+ #@markdown - Add credentials to your Gradio interface (optional).
319
+
320
+ auth=f"--gradio-auth {User}:{Password}"
321
+ if User =="" or Password=="":
322
+ auth=""
323
+
324
+ with capture.capture_output() as cap:
325
+ if not os.path.exists('/tools/node/bin/lt'):
326
+ !npm install -g localtunnel
327
+
328
+ with capture.capture_output() as cap:
329
+ %cd ./modules/
330
+ # !wget -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py
331
+ # !wget -O extras.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/modules/extras.py
332
+ # !wget -O sd_models.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/modules/sd_models.py
333
+ # !wget -q -O /usr/local/lib/python3.8/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py
334
+ %cd ./
335
+
336
+ # !sed -i "s@os.path.splitext(checkpoint_file)@os.path.splitext(checkpoint_file); map_location='cuda'@" ./modules/sd_models.py
337
+ # !sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' ./webui.py
338
+ # !sed -i "s@map_location='cpu'@map_location='cuda'@" ./modules/extras.py
339
+
340
+ share=''
341
+ if not Use_localtunnel:
342
+ share='--share'
343
+
344
+ else:
345
+ with capture.capture_output() as cap:
346
+ share=''
347
+ %cd ./content
348
+ !nohup lt --port 7860 > srv.txt 2>&1 &
349
+ time.sleep(2)
350
+ !grep -o 'https[^ ]*' ./content/srv.txt >srvr.txt
351
+ time.sleep(2)
352
+ srv= getoutput('cat ./content/srvr.txt')
353
+
354
+ for line in fileinput.input('/usr/local/lib/python3.8/dist-packages/gradio/blocks.py', inplace=True):
355
+ if line.strip().startswith('self.server_name ='):
356
+ line = f' self.server_name = "{srv[8:]}"\n'
357
+ if line.strip().startswith('self.protocol = "https"'):
358
+ line = ' self.protocol = "https"\n'
359
+ if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
360
+ line = ''
361
+ if line.strip().startswith('else "http"'):
362
+ line = ''
363
+ sys.stdout.write(line)
364
+
365
+ !rm ./content/srv.txt ./content/srvr.txt
366
+ %cd .
367
+
368
+ try:
369
+ model
370
+ if os.path.isfile(model):
371
+ !python ./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
372
+ else:
373
+ !python ./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
374
+ except:
375
+ !python ./webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --xformers $auth --disable-console-progressbars