| """ |
| Stable Diffusion WebUI (Automatic1111) for HuggingFace Spaces |
| """ |
| import os |
| from sys import executable |
| import subprocess |
| import pathlib |
| import gc |
|
|
| def Gitclone(URI: str, ClonePath: pathlib.Path) -> int: |
| if pathlib.Path.exists(ClonePath): |
| return 0 |
| for z in range(10): |
| i = subprocess.run([r"git", r"clone", str(URI), str(ClonePath)]) |
| if i.returncode == 0: |
| del i |
| return 0 |
| else: |
| del i |
| raise Exception(str.format("clone \'{0}\' failed", URI)) |
|
|
| def DownLoad(URI: str, DownloadPath: pathlib.Path, DownLoadFileName: str) -> int: |
| if (DownloadPath / DownLoadFileName).is_file(): |
| return 0 |
| for z in range(10): |
| i = subprocess.run([r"wget", r"-c", r"-O", str(DownloadPath / DownLoadFileName), URI]) |
| if i.returncode == 0: |
| del i |
| gc.collect() |
| return 0 |
| else: |
| del i |
| raise Exception(str.format("download \'{0}\' failed", URI)) |
|
|
| user_home = pathlib.Path.home().resolve() |
| os.chdir(str(user_home)) |
|
|
| |
| print("Cloning stable-diffusion-webui repo") |
| Gitclone(r"https://github.com/AUTOMATIC1111/stable-diffusion-webui.git", |
| user_home / r"stable-diffusion-webui") |
|
|
| os.chdir(str(user_home / r"stable-diffusion-webui")) |
|
|
| |
| print("Installing extensions") |
| Gitclone(r"https://github.com/Mikubill/sd-webui-controlnet", |
| user_home / r"stable-diffusion-webui" / r"extensions" / r"sd-webui-controlnet") |
| Gitclone(r"https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git", |
| user_home / r"stable-diffusion-webui" / r"extensions" / r"a1111-sd-webui-tagcomplete") |
|
|
| |
| print("Downloading model") |
| DownLoad(r"https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt", |
| user_home / r"stable-diffusion-webui" / r"models" / r"Stable-diffusion", |
| r"v1-5-pruned-emaonly.ckpt") |
|
|
| |
| print("Starting WebUI...") |
| os.chdir(user_home / r"stable-diffusion-webui") |
| gc.collect() |
|
|
| |
| while True: |
| ret = subprocess.run([ |
| executable, |
| user_home / r"stable-diffusion-webui" / r"launch.py", |
| r"--no-half", |
| r"--no-half-vae", |
| r"--enable-insecure-extension-access", |
| r"--skip-torch-cuda-test", |
| r"--listen", |
| r"--port", |
| r"7860" |
| ]) |
| if ret.returncode == 0: |
| del ret |
| gc.collect() |
| else: |
| del ret |
|
|
| del os, user_home, executable, subprocess</content> |
| <parameter name="filePath">app.py |