From 4fae967909516cf707dbcf74a77ed3f9758794cb Mon Sep 17 00:00:00 2001 From: lvmin Date: Thu, 10 Aug 2023 03:35:57 -0700 Subject: [PATCH] i --- launch.py | 24 +++++++++++- modules/launch_util.py | 82 ++++++++++++++++++++++++++++++++++++++- requirements_versions.txt | 12 ++++++ 3 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 requirements_versions.txt diff --git a/launch.py b/launch.py index c32c7228..8f7e27d4 100644 --- a/launch.py +++ b/launch.py @@ -1,7 +1,11 @@ import os import sys +import platform -from modules.launch_util import commit_hash, fooocus_tag +from modules.launch_util import commit_hash, fooocus_tag, is_installed, run, python, run_pip, repo_dir, git_clone + + +REINSTALL_ALL = True def prepare_environment(): @@ -21,6 +25,24 @@ def prepare_environment(): print(f"Version: {tag}") print(f"Commit hash: {commit}") + git_clone(comfy_repo, repo_dir('StabilityAI-official-comfyui'), "Inference Engine", comfy_commit_hash) + + if REINSTALL_ALL or not is_installed("torch") or not is_installed("torchvision"): + run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True) + + if REINSTALL_ALL or not is_installed("xformers"): + if platform.system() == "Windows": + if platform.python_version().startswith("3.10"): + run_pip(f"install -U -I --no-deps {xformers_package}", "xformers", live=True) + else: + print("Installation of xformers is not supported in this version of Python.") + print("You can also check this and build manually: https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers#building-xformers-on-windows-by-duckness") + if not is_installed("xformers"): + exit(0) + elif platform.system() == "Linux": + run_pip(f"install -U -I --no-deps {xformers_package}", "xformers") + + prepare_environment() diff --git a/modules/launch_util.py b/modules/launch_util.py index 2806ba0c..a14e8094 100644 --- a/modules/launch_util.py +++ b/modules/launch_util.py @@ -1,10 +1,20 @@ import os - +import importlib import subprocess +import sys + from functools import lru_cache +python = sys.executable git = os.environ.get('GIT', "git") +default_command_live = (os.environ.get('LAUNCH_LIVE_OUTPUT') == "1") +index_url = os.environ.get('INDEX_URL', "") + +modules_path = os.path.dirname(os.path.realpath(__file__)) +script_path = os.path.dirname(modules_path) +dir_repos = "repositories" + fooocus_tag = '1.0.0' @@ -15,3 +25,73 @@ def commit_hash(): except Exception: return "" + +def git_clone(url, dir, name, commithash=None): + # TODO clone into temporary dir and move if successful + + if os.path.exists(dir): + if commithash is None: + return + + current_hash = run(f'"{git}" -C "{dir}" rev-parse HEAD', None, f"Couldn't determine {name}'s hash: {commithash}", live=False).strip() + if current_hash == commithash: + return + + run(f'"{git}" -C "{dir}" fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}") + run(f'"{git}" -C "{dir}" checkout {commithash}', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True) + return + + run(f'"{git}" clone "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}", live=True) + + if commithash is not None: + run(f'"{git}" -C "{dir}" checkout {commithash}', None, "Couldn't checkout {name}'s hash: {commithash}") + + +def repo_dir(name): + return os.path.join(script_path, dir_repos, name) + + +def is_installed(package): + try: + spec = importlib.util.find_spec(package) + except ModuleNotFoundError: + return False + + return spec is not None + + +def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live) -> str: + if desc is not None: + print(desc) + + run_kwargs = { + "args": command, + "shell": True, + "env": os.environ if custom_env is None else custom_env, + "encoding": 'utf8', + "errors": 'ignore', + } + + if not live: + run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE + + result = subprocess.run(**run_kwargs) + + if result.returncode != 0: + error_bits = [ + f"{errdesc or 'Error running command'}.", + f"Command: {command}", + f"Error code: {result.returncode}", + ] + if result.stdout: + error_bits.append(f"stdout: {result.stdout}") + if result.stderr: + error_bits.append(f"stderr: {result.stderr}") + raise RuntimeError("\n".join(error_bits)) + + return (result.stdout or "") + + +def run_pip(command, desc=None, live=default_command_live): + index_url_line = f' --index-url {index_url}' if index_url != '' else '' + return run(f'"{python}" -m pip {command} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}", live=live) diff --git a/requirements_versions.txt b/requirements_versions.txt new file mode 100644 index 00000000..14524485 --- /dev/null +++ b/requirements_versions.txt @@ -0,0 +1,12 @@ +torch +torchsde +einops +transformers>=4.25.1 +safetensors>=0.3.0 +aiohttp +accelerate +pyyaml +Pillow +scipy +tqdm +psutil