Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -284,4 +284,212 @@ elif Model_Version=="V2.1-512px" or Model_Version=="V2.1-768px":
|
|
284 |
model=V2()
|
285 |
|
286 |
else:
|
287 |
-
model=inpmdl()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|