|
| 1 | +'''ldm.invoke.config.post_install |
| 2 | +
|
| 3 | +This defines a single exportable function, post_install(), which does |
| 4 | +post-installation stuff like migrating models directories, adding new |
| 5 | +config files, etc. |
| 6 | +
|
| 7 | +From the command line, its entry point is invokeai-postinstall. |
| 8 | +''' |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from packaging import version |
| 13 | +from pathlib import Path |
| 14 | +from shutil import move,rmtree,copyfile |
| 15 | +from typing import Union |
| 16 | + |
| 17 | +import invokeai.configs as conf |
| 18 | +import ldm.invoke |
| 19 | +from ..globals import Globals, global_cache_dir, global_config_dir |
| 20 | + |
| 21 | +def post_install(): |
| 22 | + ''' |
| 23 | + Do version and model updates, etc. |
| 24 | + Should be called once after every version update. |
| 25 | + ''' |
| 26 | + _migrate_models() |
| 27 | + _run_patches() |
| 28 | + |
| 29 | + |
| 30 | +def _migrate_models(): |
| 31 | + """ |
| 32 | + Migrate the ~/invokeai/models directory from the legacy format used through 2.2.5 |
| 33 | + to the 2.3.0 "diffusers" version. This should be a one-time operation, called at |
| 34 | + script startup time. |
| 35 | + """ |
| 36 | + # Three transformer models to check: bert, clip and safety checker, and |
| 37 | + # the diffusers as well |
| 38 | + models_dir = Path(Globals.root, "models") |
| 39 | + legacy_locations = [ |
| 40 | + Path( |
| 41 | + models_dir, |
| 42 | + "CompVis/stable-diffusion-safety-checker/models--CompVis--stable-diffusion-safety-checker", |
| 43 | + ), |
| 44 | + Path("bert-base-uncased/models--bert-base-uncased"), |
| 45 | + Path( |
| 46 | + "openai/clip-vit-large-patch14/models--openai--clip-vit-large-patch14" |
| 47 | + ), |
| 48 | + ] |
| 49 | + legacy_locations.extend(list(global_cache_dir("diffusers").glob("*"))) |
| 50 | + legacy_layout = False |
| 51 | + for model in legacy_locations: |
| 52 | + legacy_layout = legacy_layout or model.exists() |
| 53 | + if not legacy_layout: |
| 54 | + return |
| 55 | + |
| 56 | + print( |
| 57 | + """ |
| 58 | +>> ALERT: |
| 59 | +>> The location of your previously-installed diffusers models needs to move from |
| 60 | +>> invokeai/models/diffusers to invokeai/models/hub due to a change introduced by |
| 61 | +>> diffusers version 0.14. InvokeAI will now move all models from the "diffusers" directory |
| 62 | +>> into "hub" and then remove the diffusers directory. This is a quick, safe, one-time |
| 63 | +>> operation. However if you have customized either of these directories and need to |
| 64 | +>> make adjustments, please press ctrl-C now to abort and relaunch InvokeAI when you are ready. |
| 65 | +>> Otherwise press <enter> to continue.""" |
| 66 | + ) |
| 67 | + print("** This is a quick one-time operation.") |
| 68 | + input("continue> ") |
| 69 | + |
| 70 | + # transformer files get moved into the hub directory |
| 71 | + if _is_huggingface_hub_directory_present(): |
| 72 | + hub = global_cache_dir("hub") |
| 73 | + else: |
| 74 | + hub = models_dir / "hub" |
| 75 | + |
| 76 | + os.makedirs(hub, exist_ok=True) |
| 77 | + for model in legacy_locations: |
| 78 | + source = models_dir / model |
| 79 | + dest = hub / model.stem |
| 80 | + if dest.exists() and not source.exists(): |
| 81 | + continue |
| 82 | + print(f"** {source} => {dest}") |
| 83 | + if source.exists(): |
| 84 | + if dest.is_symlink(): |
| 85 | + print(f"** Found symlink at {dest.name}. Not migrating.") |
| 86 | + elif dest.exists(): |
| 87 | + if source.is_dir(): |
| 88 | + rmtree(source) |
| 89 | + else: |
| 90 | + source.unlink() |
| 91 | + else: |
| 92 | + move(source, dest) |
| 93 | + |
| 94 | + # now clean up by removing any empty directories |
| 95 | + empty = [ |
| 96 | + root |
| 97 | + for root, dirs, files, in os.walk(models_dir) |
| 98 | + if not len(dirs) and not len(files) |
| 99 | + ] |
| 100 | + for d in empty: |
| 101 | + os.rmdir(d) |
| 102 | + print("** Migration is done. Continuing...") |
| 103 | + |
| 104 | + |
| 105 | +def _is_huggingface_hub_directory_present() -> bool: |
| 106 | + return ( |
| 107 | + os.getenv("HF_HOME") is not None or os.getenv("XDG_CACHE_HOME") is not None |
| 108 | + ) |
| 109 | + |
| 110 | +# This routine performs any patch-ups needed after installation |
| 111 | +def _run_patches(): |
| 112 | + _install_missing_config_files() |
| 113 | + version_file = Path(Globals.root,'.version') |
| 114 | + if version_file.exists(): |
| 115 | + with open(version_file,'r') as f: |
| 116 | + root_version = version.parse(f.readline() or 'v2.3.2') |
| 117 | + else: |
| 118 | + root_version = version.parse('v2.3.2') |
| 119 | + app_version = version.parse(ldm.invoke.__version__) |
| 120 | + if root_version < app_version: |
| 121 | + try: |
| 122 | + _do_version_update(root_version, ldm.invoke.__version__) |
| 123 | + with open(version_file,'w') as f: |
| 124 | + f.write(ldm.invoke.__version__) |
| 125 | + except: |
| 126 | + print("** Version patching failed. Please try invokeai-postinstall later.") |
| 127 | + |
| 128 | +def _install_missing_config_files(): |
| 129 | + """ |
| 130 | + install ckpt configuration files that may have been added to the |
| 131 | + distro after original root directory configuration |
| 132 | + """ |
| 133 | + root_configs = Path(global_config_dir(), 'stable-diffusion') |
| 134 | + repo_configs = None |
| 135 | + for f in conf.__path__: |
| 136 | + if Path(f, 'stable-diffusion', 'v1-inference.yaml').exists(): |
| 137 | + repo_configs = Path(f, 'stable-diffusion') |
| 138 | + break |
| 139 | + if not repo_configs: |
| 140 | + return |
| 141 | + for src in repo_configs.iterdir(): |
| 142 | + dest = root_configs / src.name |
| 143 | + if not dest.exists(): |
| 144 | + copyfile(src,dest) |
| 145 | + |
| 146 | +def _do_version_update(root_version: version.Version, app_version: Union[str, version.Version]): |
| 147 | + """ |
| 148 | + Make any updates to the launcher .sh and .bat scripts that may be needed |
| 149 | + from release to release. This is not an elegant solution. Instead, the |
| 150 | + launcher should be moved into the source tree and installed using pip. |
| 151 | + """ |
| 152 | + if root_version < version.Version('v2.3.4'): |
| 153 | + dest = Path(Globals.root,'loras') |
| 154 | + dest.mkdir(exist_ok=True) |
| 155 | + if root_version < version.Version('v2.3.3'): |
| 156 | + if sys.platform == "linux": |
| 157 | + print('>> Downloading new version of launcher script and its config file') |
| 158 | + from ldm.util import download_with_progress_bar |
| 159 | + url_base = f'https://raw.githubusercontent.com/invoke-ai/InvokeAI/v{str(app_version)}/installer/templates/' |
| 160 | + |
| 161 | + dest = Path(Globals.root,'invoke.sh.in') |
| 162 | + assert download_with_progress_bar(url_base+'invoke.sh.in',dest) |
| 163 | + dest.replace(Path(Globals.root,'invoke.sh')) |
| 164 | + os.chmod(Path(Globals.root,'invoke.sh'), 0o0755) |
| 165 | + |
| 166 | + dest = Path(Globals.root,'dialogrc') |
| 167 | + assert download_with_progress_bar(url_base+'dialogrc',dest) |
| 168 | + dest.replace(Path(Globals.root,'.dialogrc')) |
0 commit comments