CoolFace
Apppublic

cymic/Waifu_Diffusion_Webui

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
launch.py153 linesDownload Raw Back to root
1# this scripts installs necessary requirements and launches main program in webui.py2import subprocess3import os4import sys5import importlib.util6import shlex7 8dir_repos = "repositories"9dir_tmp = "tmp"10 11python = sys.executable12git = os.environ.get('GIT', "git")13torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113")14requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")15commandline_args = os.environ.get('COMMANDLINE_ARGS', "")16 17gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379")18clip_package = os.environ.get('CLIP_PACKAGE', "git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1")19 20stable_diffusion_commit_hash = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "69ae4b35e0a0f6ee1af8bb9a5d0016ccb27e36dc")21taming_transformers_commit_hash = os.environ.get('TAMING_TRANSFORMERS_COMMIT_HASH', "24268930bf1dce879235a7fddd0b2355b84d7ea6")22k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "f4e99857772fc3a126ba886aadf795a332774878")23codeformer_commit_hash = os.environ.get('CODEFORMER_COMMIT_HASH', "c5b4593074ba6214284d6acd5f1719b6c5d739af")24blip_commit_hash = os.environ.get('BLIP_COMMIT_HASH', "48211a1594f1321b00f14c9f7a5b4813144b2fb9")25 26args = shlex.split(commandline_args)27 28 29def extract_arg(args, name):30    return [x for x in args if x != name], name in args31 32 33args, skip_torch_cuda_test = extract_arg(args, '--skip-torch-cuda-test')34 35 36def repo_dir(name):37    return os.path.join(dir_repos, name)38 39 40def run(command, desc=None, errdesc=None):41    if desc is not None:42        print(desc)43 44    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)45 46    if result.returncode != 0:47 48        message = f"""{errdesc or 'Error running command'}.49Command: {command}50Error code: {result.returncode}51stdout: {result.stdout.decode(encoding="utf8", errors="ignore") if len(result.stdout)>0 else '<empty>'}52stderr: {result.stderr.decode(encoding="utf8", errors="ignore") if len(result.stderr)>0 else '<empty>'}53"""54        raise RuntimeError(message)55 56    return result.stdout.decode(encoding="utf8", errors="ignore")57 58 59def run_python(code, desc=None, errdesc=None):60    return run(f'"{python}" -c "{code}"', desc, errdesc)61 62 63def run_pip(args, desc=None):64    return run(f'"{python}" -m pip {args} --prefer-binary', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")65 66 67def check_run(command):68    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)69    return result.returncode == 070 71 72def check_run_python(code):73    return check_run(f'"{python}" -c "{code}"')74 75 76def is_installed(package):77    try:78        spec = importlib.util.find_spec(package)79    except ModuleNotFoundError:80        return False81 82    return spec is not None83 84 85def git_clone(url, dir, name, commithash=None):86    # TODO clone into temporary dir and move if successful87 88    if os.path.exists(dir):89        if commithash is None:90            return91 92        current_hash = run(f'"{git}" -C {dir} rev-parse HEAD', None, f"Couldn't determine {name}'s hash: {commithash}").strip()93        if current_hash == commithash:94            return95 96        run(f'"{git}" -C {dir} fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}")97        run(f'"{git}" -C {dir} checkout {commithash}', f"Checking out commint for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}")98        return99 100    run(f'"{git}" clone "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}")101 102    if commithash is not None:103        run(f'"{git}" -C {dir} checkout {commithash}', None, "Couldn't checkout {name}'s hash: {commithash}")104 105 106try:107    commit = run(f"{git} rev-parse HEAD").strip()108except Exception:109    commit = "<none>"110 111print(f"Python {sys.version}")112print(f"Commit hash: {commit}")113 114 115if not is_installed("torch") or not is_installed("torchvision"):116    run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch")117 118if not skip_torch_cuda_test:119    run_python("import torch; assert torch.cuda.is_available(), 'Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'")120 121if not is_installed("gfpgan"):122    run_pip(f"install {gfpgan_package}", "gfpgan")123 124if not is_installed("clip"):125    run_pip(f"install {clip_package}", "clip")126 127os.makedirs(dir_repos, exist_ok=True)128 129git_clone("https://github.com/CompVis/stable-diffusion.git", repo_dir('stable-diffusion'), "Stable Diffusion", stable_diffusion_commit_hash)130git_clone("https://github.com/CompVis/taming-transformers.git", repo_dir('taming-transformers'), "Taming Transformers", taming_transformers_commit_hash)131git_clone("https://github.com/crowsonkb/k-diffusion.git", repo_dir('k-diffusion'), "K-diffusion", k_diffusion_commit_hash)132git_clone("https://github.com/sczhou/CodeFormer.git", repo_dir('CodeFormer'), "CodeFormer", codeformer_commit_hash)133git_clone("https://github.com/salesforce/BLIP.git", repo_dir('BLIP'), "BLIP", blip_commit_hash)134 135if not is_installed("lpips"):136    run_pip(f"install -r {os.path.join(repo_dir('CodeFormer'), 'requirements.txt')}", "requirements for CodeFormer")137 138run_pip(f"install -r {requirements_file}", "requirements for Web UI")139 140sys.argv += args141 142if "--exit" in args:143    print("Exiting because of --exit argument")144    exit(0)145 146def start_webui():147    print(f"Launching Web UI with arguments: {' '.join(sys.argv[1:])}")148    import webui149    webui.webui()150 151if __name__ == "__main__":152    start_webui()153