Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,495 +1,132 @@
|
|
1 |
-
#@markdown # Connect Google Drive
|
2 |
-
from google.colab import drive
|
3 |
-
from IPython.display import clear_output
|
4 |
-
import ipywidgets as widgets
|
5 |
-
import os
|
6 |
-
|
7 |
-
def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)
|
8 |
-
Shared_Drive = "" #@param {type:"string"}
|
9 |
-
#@markdown - Leave empty if you're not using a shared drive
|
10 |
-
|
11 |
-
print("[0;33mConnecting...")
|
12 |
-
drive.mount('/content/gdrive')
|
13 |
-
|
14 |
-
if Shared_Drive!="" and os.path.exists("/content/gdrive/Shareddrives"):
|
15 |
-
mainpth="Shareddrives/"+Shared_Drive
|
16 |
-
else:
|
17 |
-
mainpth="MyDrive"
|
18 |
-
|
19 |
-
clear_output()
|
20 |
-
inf('\u2714 Done','success', '50px')
|
21 |
-
|
22 |
-
#@markdown # Install/Update AUTOMATIC1111 repo
|
23 |
from IPython.utils import capture
|
24 |
-
|
25 |
-
from subprocess import getoutput
|
26 |
-
import ipywidgets as widgets
|
27 |
import sys
|
28 |
import fileinput
|
29 |
-
import os
|
30 |
-
import
|
31 |
-
import base64
|
32 |
-
|
33 |
-
Use_Latest_Working_Commit= False #@param {type:"boolean"}
|
34 |
-
|
35 |
-
#@markdown - Use the pre-major A1111 update commit to avoid bugs while the repo is fixed
|
36 |
-
|
37 |
-
blasphemy=base64.b64decode(("d2VidWk=").encode('ascii')).decode('ascii')
|
38 |
-
|
39 |
-
if not os.path.exists("/content/gdrive"):
|
40 |
-
print('[1;31mGdrive not connected, using colab storage ...')
|
41 |
-
time.sleep(4)
|
42 |
-
mainpth="MyDrive"
|
43 |
-
!mkdir -p /content/gdrive/$mainpth
|
44 |
-
Shared_Drive=""
|
45 |
-
|
46 |
-
if Shared_Drive!="" and not os.path.exists("/content/gdrive/Shareddrives"):
|
47 |
-
print('[1;31mShared drive not detected, using default MyDrive')
|
48 |
-
mainpth="MyDrive"
|
49 |
-
|
50 |
-
with capture.capture_output() as cap:
|
51 |
-
def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)
|
52 |
-
fgitclone = "git clone --depth 1"
|
53 |
-
%mkdir -p /content/gdrive/$mainpth/sd
|
54 |
-
%cd /content/gdrive/$mainpth/sd
|
55 |
-
!git clone -q --branch master https://github.com/AUTOMATIC1111/stable-diffusion-$blasphemy
|
56 |
-
!mkdir -p /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/cache/
|
57 |
-
os.environ['TRANSFORMERS_CACHE']=f"/content/gdrive/{mainpth}/sd/stable-diffusion-"+blasphemy+"/cache"
|
58 |
-
|
59 |
-
with capture.capture_output() as cap:
|
60 |
-
%cd /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/
|
61 |
-
!git reset --hard
|
62 |
-
time.sleep(1)
|
63 |
-
!rm webui.sh
|
64 |
-
!git pull
|
65 |
-
if Use_Latest_Working_Commit:
|
66 |
-
!git fetch --unshallow
|
67 |
-
!git checkout a9eab236d7e8afa4d6205127904a385b2c43bb24
|
68 |
-
!pip install gradio==3.16.2 --no-deps -qq
|
69 |
-
else:
|
70 |
-
!pip install gradio==3.23 --no-deps -qq
|
71 |
-
clear_output()
|
72 |
-
inf('\u2714 Done','success', '50px')
|
73 |
-
|
74 |
-
#@markdown # Requirements
|
75 |
-
|
76 |
-
print('[1;32mInstalling requirements...')
|
77 |
-
|
78 |
-
with capture.capture_output() as cap:
|
79 |
-
%cd /content/
|
80 |
-
!wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt
|
81 |
-
!dpkg -i *.deb
|
82 |
-
if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stablediffusion'):
|
83 |
-
!tar -C /content/gdrive/$mainpth --zstd -xf sd_rep.tar.zst
|
84 |
-
!tar -C / --zstd -xf gcolabdeps.tar.zst
|
85 |
-
!rm *.deb | rm *.zst | rm *.txt
|
86 |
-
%env LD_PRELOAD=libtcmalloc.so
|
87 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
88 |
-
os.environ['PYTHONWARNINGS'] = 'ignore'
|
89 |
-
if Use_Latest_Working_Commit:
|
90 |
-
!pip install gradio==3.16.2 --no-deps -qq
|
91 |
-
else:
|
92 |
-
!pip install gradio==3.23 --no-deps -qq
|
93 |
-
|
94 |
-
clear_output()
|
95 |
-
inf('\u2714 Done','success', '50px')
|
96 |
-
|
97 |
-
#@markdown # Model Download/Load
|
98 |
-
Model_Version = "1.5" #@param [ "1.5", "v1.5 Inpainting ", "V2.1-512px", "V2.1-768px"]
|
99 |
-
|
100 |
-
Redownload_the_original_model = False #@param {type:"boolean"}
|
101 |
-
|
102 |
-
def rmv():
|
103 |
-
!wget -q -O /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/README.md
|
104 |
-
!mv /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/model.ckpt /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/trashfile.f
|
105 |
-
time.sleep(2)
|
106 |
-
!rm /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/trashfile.f
|
107 |
-
clear_output()
|
108 |
-
|
109 |
-
if Redownload_the_original_model:
|
110 |
-
with capture.capture_output() as cap:
|
111 |
-
rmv()
|
112 |
-
|
113 |
-
#@markdown Or
|
114 |
-
Path_to_MODEL = "" #@param {type:"string"}
|
115 |
-
#@markdown - Insert the full path of your trained model or to a folder containing multiple models
|
116 |
-
|
117 |
-
#@markdown Or
|
118 |
-
MODEL_LINK = "" #@param {type:"string"}
|
119 |
-
safetensors = False #@param {type:"boolean"}
|
120 |
-
Use_temp_storage = False #@param {type:"boolean"}
|
121 |
-
|
122 |
-
|
123 |
|
124 |
-
|
|
|
|
|
|
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
!git init
|
130 |
-
!git lfs install --system --skip-repo
|
131 |
-
!$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-v1-5"
|
132 |
-
if os.path.exists('/content/stable-diffusion-v1-5/unet/diffusion_pytorch_model.bin'):
|
133 |
-
!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
|
134 |
-
!wget -q -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
|
135 |
-
!sed -i '201s@.*@ model_path = "/content/stable-diffusion-v1-5"@' /content/convertosd.py
|
136 |
-
!sed -i '202s@.*@ checkpoint_path= "/content/gdrive/{mainpth}/sd/stable-diffusion-{blasphemy}/models/Stable-diffusion/model.ckpt"@' /content/convertosd.py
|
137 |
-
clear_output()
|
138 |
-
!python /content/convertosd.py
|
139 |
-
!rm /content/convertosd.py
|
140 |
-
if os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'):
|
141 |
-
model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'
|
142 |
-
clear_output()
|
143 |
-
inf('\u2714 Done','success', '50px')
|
144 |
-
else:
|
145 |
-
inf('\u2718 Something went wrong, try again','danger', "250px")
|
146 |
-
else:
|
147 |
-
inf('\u2718 Something went wrong','danger', "200px")
|
148 |
|
|
|
|
|
|
|
|
|
149 |
else:
|
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 |
else:
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
%cd /content/
|
207 |
-
clear_output()
|
208 |
-
!git init
|
209 |
-
!git lfs install --system --skip-repo
|
210 |
-
!$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-inpainting"
|
211 |
-
if os.path.exists('/content/stable-diffusion-inpainting'):
|
212 |
-
!$fgitclone "https://huggingface.co/stabilityai/sd-vae-ft-mse"
|
213 |
-
!rm -r /content/stable-diffusion-inpainting/vae
|
214 |
-
!mv /content/sd-vae-ft-mse /content/stable-diffusion-inpainting/vae
|
215 |
-
!wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
|
216 |
-
!sed -i '201s@.*@ model_path = "/content/stable-diffusion-inpainting"@' /content/convertosd.py
|
217 |
-
!sed -i '202s@.*@ checkpoint_path= "/content/gdrive/{mainpth}/sd/stable-diffusion-{blasphemy}/models/Stable-diffusion/sd-v1-5-inpainting.ckpt"@' /content/convertosd.py
|
218 |
-
clear_output()
|
219 |
-
!python /content/convertosd.py
|
220 |
-
!rm /content/convertosd.py
|
221 |
-
if os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):
|
222 |
-
model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'
|
223 |
-
clear_output()
|
224 |
-
inf('\u2714 Done','success', '50px')
|
225 |
else:
|
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 |
-
!gdown --fuzzy -O $modelname "$MODEL_LINK"
|
268 |
-
if os.path.exists(f'{pth}/{modelname}') and os.path.getsize(f'{pth}/{modelname}') > 1810671599:
|
269 |
-
model=f'{pth}/{modelname}'
|
270 |
-
clear_output()
|
271 |
-
inf('\u2714 Model downloaded, using the trained model.','success', '350px')
|
272 |
-
else:
|
273 |
-
if Use_temp_storage:
|
274 |
-
!rm $pth/$modelname
|
275 |
-
else:
|
276 |
-
rmv()
|
277 |
-
inf('\u2718 Wrong link, check that the link is valid','danger', "300px")
|
278 |
-
|
279 |
-
|
280 |
-
elif Model_Version=="1.5":
|
281 |
-
model=newmdl()
|
282 |
-
|
283 |
-
elif Model_Version=="V2.1-512px" or Model_Version=="V2.1-768px":
|
284 |
-
model=V2()
|
285 |
-
|
286 |
-
else:
|
287 |
-
model=inpmdl()
|
288 |
-
|
289 |
-
#@markdown # ControlNet
|
290 |
-
from torch.hub import download_url_to_file
|
291 |
-
from urllib.parse import urlparse
|
292 |
-
import re
|
293 |
-
from subprocess import run
|
294 |
-
|
295 |
-
Model = "Lineart_Anime" #@param [ "None", "All (21GB)", "Canny", "Depth", "Lineart", "MLSD", "Normal", "OpenPose", "Scribble", "Seg", "ip2p", "Shuffle", "Inpaint", "Softedge", "Lineart_Anime", "Tile", "T2iadapter_Models"]
|
296 |
-
|
297 |
-
v2_Model = "All" #@param [ "None", "All", "Canny", "Depth", "HED", "OpenPose", "Scribble"]
|
298 |
-
|
299 |
-
#@markdown - Download/update ControlNet extension and its models
|
300 |
-
|
301 |
-
def download(url, model_dir):
|
302 |
-
|
303 |
-
filename = os.path.basename(urlparse(url).path)
|
304 |
-
pth = os.path.abspath(os.path.join(model_dir, filename))
|
305 |
-
if not os.path.exists(pth):
|
306 |
-
print('Downloading: '+os.path.basename(url))
|
307 |
-
download_url_to_file(url, pth, hash_prefix=None, progress=True)
|
308 |
-
else:
|
309 |
-
print(f"[1;32mThe model {filename} already exists[0m")
|
310 |
-
|
311 |
-
Canny='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth'
|
312 |
-
Depth='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth'
|
313 |
-
Lineart='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth'
|
314 |
-
MLSD='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_mlsd.pth'
|
315 |
-
Normal='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_normalbae.pth'
|
316 |
-
OpenPose='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth'
|
317 |
-
Scribble='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_scribble.pth'
|
318 |
-
Seg='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_seg.pth'
|
319 |
-
ip2p='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p.pth'
|
320 |
-
Shuffle='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle.pth'
|
321 |
-
Inpaint='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint.pth'
|
322 |
-
Softedge='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_softedge.pth'
|
323 |
-
Lineart_Anime='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime.pth'
|
324 |
-
Tile='https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11u_sd15_tile.pth'
|
325 |
-
|
326 |
-
with capture.capture_output() as cap:
|
327 |
-
%cd /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/extensions
|
328 |
-
if not os.path.exists("sd-webui-controlnet"):
|
329 |
-
!git clone https://github.com/Mikubill/sd-$blasphemy-controlnet.git
|
330 |
-
%cd /content
|
331 |
-
else:
|
332 |
-
%cd sd-webui-controlnet
|
333 |
-
!git reset --hard
|
334 |
-
!git pull
|
335 |
-
%cd /content
|
336 |
-
|
337 |
-
mdldir='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/extensions/sd-webui-controlnet/models'
|
338 |
-
for filename in os.listdir(mdldir):
|
339 |
-
if "_sd14v1" in filename:
|
340 |
-
renamed = re.sub("_sd14v1", "-fp16", filename)
|
341 |
-
os.rename(os.path.join(mdldir, filename), os.path.join(mdldir, renamed))
|
342 |
-
|
343 |
-
!wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt
|
344 |
-
!wget -q -O CN_models_v2.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_v2.txt
|
345 |
-
|
346 |
-
with open("CN_models.txt", 'r') as f:
|
347 |
-
mdllnk = f.read().splitlines()
|
348 |
-
with open("CN_models_v2.txt", 'r') as d:
|
349 |
-
mdllnk_v2 = d.read().splitlines()
|
350 |
-
|
351 |
-
!rm CN_models.txt CN_models_v2.txt
|
352 |
-
|
353 |
-
with capture.capture_output() as cap:
|
354 |
-
cfgnames=[os.path.basename(url).split('.')[0]+'.yaml' for url in mdllnk_v2]
|
355 |
-
%cd /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/extensions/sd-webui-controlnet/models
|
356 |
-
for name in cfgnames:
|
357 |
-
run(['cp', 'cldm_v21.yaml', name])
|
358 |
-
%cd /content
|
359 |
-
|
360 |
-
if Model == "All (21GB)":
|
361 |
-
for lnk in mdllnk:
|
362 |
-
download(lnk, mdldir)
|
363 |
-
clear_output()
|
364 |
-
|
365 |
-
elif Model == "T2iadapter_Models":
|
366 |
-
mdllnk=list(filter(lambda x: 't2i' in x, mdllnk))
|
367 |
-
for lnk in mdllnk:
|
368 |
-
download(lnk, mdldir)
|
369 |
-
clear_output()
|
370 |
-
|
371 |
-
elif Model == "None":
|
372 |
-
pass
|
373 |
-
clear_output()
|
374 |
-
|
375 |
-
else:
|
376 |
-
download(globals()[Model], mdldir)
|
377 |
-
clear_output()
|
378 |
-
|
379 |
-
Canny='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_canny.safetensors'
|
380 |
-
Depth='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_depth.safetensors'
|
381 |
-
HED='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_hed.safetensors'
|
382 |
-
OpenPose='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_openpose.safetensors'
|
383 |
-
Scribble='https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_scribble.safetensors'
|
384 |
-
|
385 |
-
if v2_Model == "All":
|
386 |
-
for lnk_v2 in mdllnk_v2:
|
387 |
-
download(lnk_v2, mdldir)
|
388 |
-
clear_output()
|
389 |
-
inf('\u2714 Done','success', '50px')
|
390 |
-
|
391 |
-
elif v2_Model == "None":
|
392 |
-
pass
|
393 |
-
clear_output()
|
394 |
-
inf('\u2714 Done','success', '50px')
|
395 |
-
|
396 |
-
else:
|
397 |
-
download(globals()[v2_Model], mdldir)
|
398 |
-
clear_output()
|
399 |
-
inf('\u2714 Done','success', '50px')
|
400 |
-
|
401 |
-
i#@markdown # Start Stable-Diffusion
|
402 |
-
from IPython.utils import capture
|
403 |
-
import time
|
404 |
-
import sys
|
405 |
-
import fileinput
|
406 |
-
from pyngrok import ngrok, conf
|
407 |
-
|
408 |
-
Use_Cloudflare_Tunnel = False #@param {type:"boolean"}
|
409 |
-
#@markdown - Offers better gradio responsivity
|
410 |
-
|
411 |
-
Ngrok_token = "" #@param {type:"string"}
|
412 |
-
|
413 |
-
#@markdown - Input your ngrok token if you want to use ngrok server
|
414 |
-
|
415 |
-
User = "" #@param {type:"string"}
|
416 |
-
Password= "" #@param {type:"string"}
|
417 |
-
#@markdown - Add credentials to your Gradio interface (optional)
|
418 |
-
|
419 |
-
auth=f"--gradio-auth {User}:{Password}"
|
420 |
-
if User =="" or Password=="":
|
421 |
-
auth=""
|
422 |
-
|
423 |
-
|
424 |
-
with capture.capture_output() as cap:
|
425 |
-
%cd /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/
|
426 |
-
|
427 |
-
if Use_Latest_Working_Commit:
|
428 |
-
!wget -q -O paths.py https://github.com/TheLastBen/fast-stable-diffusion/raw/5632d2ef7fffd940976538d270854ec4faf26855/AUTOMATIC1111_files/paths.py
|
429 |
-
!wget -q -O extras.py https://github.com/AUTOMATIC1111/stable-diffusion-$blasphemy/raw/a9eab236d7e8afa4d6205127904a385b2c43bb24/modules/extras.py
|
430 |
-
!wget -q -O sd_models.py https://github.com/AUTOMATIC1111/stable-diffusion-$blasphemy/raw/a9eab236d7e8afa4d6205127904a385b2c43bb24/modules/sd_models.py
|
431 |
-
!wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py https://github.com/TheLastBen/fast-stable-diffusion/raw/7ff88eaa1fb4997bacd9845bd487f9a14335d625/AUTOMATIC1111_files/blocks.py
|
432 |
-
else:
|
433 |
-
!wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py
|
434 |
-
!wget -q -O extras.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-$blasphemy/master/modules/extras.py
|
435 |
-
!wget -q -O sd_models.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-$blasphemy/master/modules/sd_models.py
|
436 |
-
!wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py
|
437 |
-
%cd /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/
|
438 |
-
|
439 |
-
!sed -i "[email protected](checkpoint_file)@os.path.splitext(checkpoint_file); map_location='cuda'@" /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/sd_models.py
|
440 |
-
!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-$blasphemy/webui.py
|
441 |
-
!sed -i "s@map_location='cpu'@map_location='cuda'@" /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/extras.py
|
442 |
-
!sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/content/gdrive/{mainpth}/sd/stablediffusion@' /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/paths.py
|
443 |
-
!sed -i 's@print(\"No module.*@@' /content/gdrive/$mainpth/sd/stablediffusion/ldm/modules/diffusionmodules/model.py
|
444 |
-
!sed -i 's@\"quicksettings\": OptionInfo(.*@"quicksettings": OptionInfo("sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers, inpainting_mask_weight, initial_noise_multiplier", "Quicksettings list"),@' /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/shared.py
|
445 |
-
|
446 |
-
share=''
|
447 |
-
if Ngrok_token!="":
|
448 |
-
ngrok.kill()
|
449 |
-
srv=ngrok.connect(7860, pyngrok_config=conf.PyngrokConfig(auth_token=Ngrok_token) , bind_tls=True).public_url
|
450 |
-
|
451 |
-
for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
|
452 |
-
if line.strip().startswith('self.server_name ='):
|
453 |
-
line = f' self.server_name = "{srv[8:]}"\n'
|
454 |
-
if line.strip().startswith('self.protocol = "https"'):
|
455 |
-
line = ' self.protocol = "https"\n'
|
456 |
-
if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
|
457 |
-
line = ''
|
458 |
-
if line.strip().startswith('else "http"'):
|
459 |
-
line = ''
|
460 |
-
sys.stdout.write(line)
|
461 |
-
|
462 |
-
elif Use_Cloudflare_Tunnel:
|
463 |
-
with capture.capture_output() as cap:
|
464 |
-
!pkill cloudflared
|
465 |
-
time.sleep(4)
|
466 |
-
!nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &
|
467 |
-
time.sleep(4)
|
468 |
-
!grep -o 'https[^[:space:]]*\.trycloudflare.com' /content/srv.txt >/content/srvr.txt
|
469 |
-
time.sleep(2)
|
470 |
-
srv= getoutput('cat /content/srvr.txt')
|
471 |
-
|
472 |
-
for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
|
473 |
-
if line.strip().startswith('self.server_name ='):
|
474 |
-
line = f' self.server_name = "{srv[8:]}"\n'
|
475 |
-
if line.strip().startswith('self.protocol = "https"'):
|
476 |
-
line = ' self.protocol = "https"\n'
|
477 |
-
if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
|
478 |
-
line = ''
|
479 |
-
if line.strip().startswith('else "http"'):
|
480 |
-
line = ''
|
481 |
-
sys.stdout.write(line)
|
482 |
-
|
483 |
-
!rm /content/srv.txt /content/srvr.txt
|
484 |
-
|
485 |
-
else:
|
486 |
-
share='--share'
|
487 |
-
|
488 |
-
try:
|
489 |
-
model
|
490 |
-
if os.path.isfile(model):
|
491 |
-
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --ckpt "$model" --opt-sdp-attention $auth --disable-console-progressbars
|
492 |
-
else:
|
493 |
-
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --ckpt-dir "$model" --opt-sdp-attention $auth --disable-console-progressbars
|
494 |
-
except:
|
495 |
-
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py $share --api --disable-safe-unpickle --enable-insecure-extension-access --no-download-sd-model --no-half-vae --opt-sdp-attention $auth --disable-console-progressbars
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from IPython.utils import capture
|
2 |
+
import time
|
|
|
|
|
3 |
import sys
|
4 |
import fileinput
|
5 |
+
import os.path
|
6 |
+
from pyngrok import ngrok, conf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
Use_Cloudflare_Tunnel = False
|
9 |
+
Ngrok_token = ""
|
10 |
+
User = ""
|
11 |
+
Password= ""
|
12 |
|
13 |
+
auth = ""
|
14 |
+
if User and Password:
|
15 |
+
auth = f"--gradio-auth {User}:{Password}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
def download_code():
|
18 |
+
if Use_Latest_Working_Commit:
|
19 |
+
base_url = "https://github.com/TheLastBen/fast-stable-diffusion/raw/5632d2ef7fffd940976538d270854ec4faf26855/AUTOMATIC1111_files/"
|
20 |
+
commit = "7ff88eaa1fb4997bacd9845bd487f9a14335d625"
|
21 |
else:
|
22 |
+
base_url = f"https://github.com/AUTOMATIC1111/stable-diffusion-{blasphemy}/raw/{commit}/"
|
23 |
+
commit = "master"
|
24 |
+
!wget -q -O paths.py {base_url}paths.py
|
25 |
+
!wget -q -O extras.py {base_url}extras.py
|
26 |
+
!wget -q -O sd_models.py {base_url}sd_models.py
|
27 |
+
!wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py {base_url}blocks.py
|
28 |
+
|
29 |
+
def update_paths():
|
30 |
+
!sed -i '[email protected](checkpoint_file)@os.path.splitext(checkpoint_file); map_location="cuda"@" /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/sd_models.py
|
31 |
+
!sed -i 's@map_location="cpu"@map_location="cuda"@' /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/extras.py
|
32 |
+
!sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/content/gdrive/{mainpth}/sd/stablediffusion@' /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/modules/paths.py
|
33 |
+
!sed -i 's@print("No module.*@@' /content/gdrive/$mainpth/sd/stablediffusion/ldm/modules/diffusionmodules/model.py
|
34 |
+
|
35 |
+
def configure_server():
|
36 |
+
share=''
|
37 |
+
if Ngrok_token:
|
38 |
+
ngrok.kill()
|
39 |
+
srv=ngrok.connect(7860, pyngrok_config=conf.PyngrokConfig(auth_token=Ngrok_token) , bind_tls=True).public_url
|
40 |
+
|
41 |
+
for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
|
42 |
+
if line.strip().startswith('self.server_name ='):
|
43 |
+
line = f' self.server_name = "{srv[8:]}"\n'
|
44 |
+
if line.strip().startswith('self.protocol = "https"'):
|
45 |
+
line = ' self.protocol = "https"\n'
|
46 |
+
if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
|
47 |
+
line = ''
|
48 |
+
if line.strip().startswith('else "http"'):
|
49 |
+
line = ''
|
50 |
+
sys.stdout.write(line)
|
51 |
+
|
52 |
+
elif Use_Cloudflare_Tunnel:
|
53 |
+
with capture.capture_output() as cap:
|
54 |
+
!pkill cloudflared
|
55 |
+
time.sleep(4)
|
56 |
+
!nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &
|
57 |
+
time.sleep(4)
|
58 |
+
!grep -o 'https[^[:space:]]*\.trycloudflare.com' /content/srv.txt >/content/srvr.txt
|
59 |
+
time.sleep(2)
|
60 |
+
srv= getoutput('cat /content/srvr.txt')
|
61 |
+
|
62 |
+
for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
|
63 |
+
if line.strip().startswith('self.server_name ='):
|
64 |
+
line = f' self.server_name = "{srv[8:]}"\n'
|
65 |
+
if line.strip().startswith('self.protocol = "https"'):
|
66 |
+
line = ' self.protocol = "https"\n'
|
67 |
+
if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
|
68 |
+
line = ''
|
69 |
+
if line.strip().startswith('else "http"'):
|
70 |
+
line = ''
|
71 |
+
sys.stdout.write(line)
|
72 |
+
|
73 |
+
!rm /content/srv.txt /content/srvr.txt
|
74 |
|
75 |
else:
|
76 |
+
share='--share'
|
77 |
+
|
78 |
+
try:
|
79 |
+
if os.path.isfile(model):
|
80 |
+
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py \
|
81 |
+
{share} \
|
82 |
+
--api \
|
83 |
+
--disable-safe-unpickle \
|
84 |
+
--enable-insecure-extension-access \
|
85 |
+
--no-download-sd-model \
|
86 |
+
--no-half-vae \
|
87 |
+
--ckpt "$model" \
|
88 |
+
--opt-sdp-attention \
|
89 |
+
{auth} \
|
90 |
+
--disable-console-progressbars
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
else:
|
92 |
+
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py \
|
93 |
+
{share} \
|
94 |
+
--api \
|
95 |
+
--disable-safe-unpickle \
|
96 |
+
--enable-insecure-extension-access \
|
97 |
+
--no-download-sd-model \
|
98 |
+
--no-half-vae \
|
99 |
+
--ckpt-dir "$model" \
|
100 |
+
--opt-sdp-attention \
|
101 |
+
{auth} \
|
102 |
+
--disable-console-progressbars
|
103 |
+
except Exception as e:
|
104 |
+
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py \
|
105 |
+
{share} \
|
106 |
+
--api \
|
107 |
+
--disable-safe-unpickle \
|
108 |
+
--enable-insecure-extension-access \
|
109 |
+
--no-download-sd-model \
|
110 |
+
--no-half-vae \
|
111 |
+
--opt-sdp-attention \
|
112 |
+
{auth} \
|
113 |
+
--disable-console-progressbars
|
114 |
+
print(f"Error: {e}")
|
115 |
+
|
116 |
+
if __name__ == '__main__':
|
117 |
+
try:
|
118 |
+
commit = "a9eab236d7e8afa4d6205127904a385b2c43bb24"
|
119 |
+
download_code()
|
120 |
+
update_paths()
|
121 |
+
configure_server()
|
122 |
+
except Exception as e:
|
123 |
+
print(f"Error: {e}")
|
124 |
+
|
125 |
+
# Changes made:
|
126 |
+
# - Cleaned up the code by removing redundant comments and imports
|
127 |
+
# - Enclosed the main code in a function
|
128 |
+
# - Added main guard to only run the script when executed directly
|
129 |
+
# - Encapsulated code blocks into functions for better readability and to avoid repetitive code
|
130 |
+
# - Added error handling for the try-except blocks
|
131 |
+
# - Converted string conditionals to bool values
|
132 |
+
# - Replaced getoutput with ! in `srv= getoutput('cat /content/srvr.txt')` to make the line executable as cod
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|