Skip to content

Commit 7088d56

Browse files
author
Lincoln Stein
committed
add script to sync models db with models.yaml
1 parent 589a795 commit 7088d56

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

invokeai/app/services/model_install/model_install_default.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,14 @@ def prune_jobs(self) -> None:
305305
unfinished_jobs = [x for x in self._install_jobs if not x.in_terminal_state]
306306
self._install_jobs = unfinished_jobs
307307

308-
def _migrate_yaml(self) -> None:
308+
def _migrate_yaml(self, rename_yaml: Optional[bool] = True, overwrite_db: Optional[bool] = False) -> None:
309309
db_models = self.record_store.all_models()
310310

311+
if overwrite_db:
312+
for model in db_models:
313+
self.record_store.del_model(model.key)
314+
db_models = self.record_store.all_models()
315+
311316
legacy_models_yaml_path = (
312317
self._app_config.legacy_models_yaml_path or self._app_config.root_path / "configs" / "models.yaml"
313318
)
@@ -357,7 +362,8 @@ def _migrate_yaml(self) -> None:
357362
self._logger.warning(f"Model at {model_path} could not be migrated: {e}")
358363

359364
# Rename `models.yaml` to `models.yaml.bak` to prevent re-migration
360-
legacy_models_yaml_path.rename(legacy_models_yaml_path.with_suffix(".yaml.bak"))
365+
if rename_yaml:
366+
legacy_models_yaml_path.rename(legacy_models_yaml_path.with_suffix(".yaml.bak"))
361367

362368
# Unset the path - we are done with it either way
363369
self._app_config.legacy_models_yaml_path = None
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/env python
2+
3+
from argparse import ArgumentParser, Namespace
4+
from pathlib import Path
5+
6+
from invokeai.app.services.config import InvokeAIAppConfig, get_config
7+
from invokeai.app.services.download import DownloadQueueService
8+
from invokeai.app.services.model_install import ModelInstallService
9+
from invokeai.app.services.model_records import ModelRecordServiceSQL
10+
from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase
11+
from invokeai.backend.util.logging import InvokeAILogger
12+
13+
14+
def get_args() -> Namespace:
15+
parser = ArgumentParser(description="Update models database from yaml file")
16+
parser.add_argument("--root", type=Path, required=False, default=None)
17+
parser.add_argument("--yaml_file", type=Path, required=False, default=None)
18+
return parser.parse_args()
19+
20+
21+
def populate_config() -> InvokeAIAppConfig:
22+
args = get_args()
23+
config = get_config()
24+
if args.root:
25+
config._root = args.root
26+
if args.yaml_file:
27+
config.legacy_models_yaml_path = args.yaml_file
28+
else:
29+
config.legacy_models_yaml_path = config.root_path / "configs/models.yaml"
30+
return config
31+
32+
33+
def initialize_installer(config: InvokeAIAppConfig) -> ModelInstallService:
34+
logger = InvokeAILogger.get_logger(config=config)
35+
db = SqliteDatabase(config.db_path, logger)
36+
record_store = ModelRecordServiceSQL(db)
37+
queue = DownloadQueueService()
38+
queue.start()
39+
installer = ModelInstallService(app_config=config, record_store=record_store, download_queue=queue)
40+
return installer
41+
42+
43+
def main() -> None:
44+
config = populate_config()
45+
installer = initialize_installer(config)
46+
installer._migrate_yaml(rename_yaml=False, overwrite_db=True)
47+
print("\n<INSTALLED MODELS>")
48+
print("\t".join(["key", "name", "type", "path"]))
49+
for model in installer.record_store.all_models():
50+
print("\t".join([model.key, model.name, model.type, (config.models_path / model.path).as_posix()]))
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)