File size: 18,235 Bytes
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
 
 
 
 
 
 
 
 
f8c73f9
 
 
 
 
 
 
 
 
 
 
 
 
 
e0265b9
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
 
 
 
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
c61c435
 
 
 
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8c73f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0265b9
 
 
c61c435
e0265b9
f8c73f9
 
c61c435
 
 
 
 
 
 
 
 
 
e0265b9
 
 
f8c73f9
 
 
 
 
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
 
f8c73f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0265b9
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
e0265b9
 
c61c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8c73f9
c61c435
 
f8c73f9
 
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from __future__ import annotations

import json
import re
from dataclasses import asdict, dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from uuid import uuid4


def _now() -> str:
    return datetime.now(timezone.utc).isoformat()


def _normal(value: str) -> str:
    return re.sub(r"[^a-z0-9]+", " ", value.casefold()).strip()


def _friendly_name(value: str, fallback: str) -> str:
    text = str(value or "").strip()
    if not text:
        return fallback
    if re.search(r"^[A-Za-z]:[\\/]", text) or "/" in text or "\\" in text:
        return Path(text).name or fallback
    return text


def _is_lora_training_checkpoint(path: Path) -> bool:
    """Return whether a LoRA weight is an intermediate training snapshot.

    The LoRA trainer writes both the finished adapter and periodic weights such
    as ``name_epoch_0050.safetensors``.  The latter are useful for recovery,
    but are not independently selectable models in ADAM's model library.
    """
    name = path.stem.casefold()
    return bool(re.search(
        r"(?:^|[_\- ])(?:checkpoint(?:[_\- ]?(?:epoch|e|step))?|epoch|e|step)[_\- ]?\d+(?:[_\- ]|$)",
        name,
    ))


@dataclass(slots=True)
class Asset:
    id: str
    kind: str
    name: str
    path: str
    trainer: str = ""
    dataset_id: str = ""
    checkpoint: str = ""
    epochs: int = 0
    created_at: str = ""
    metadata: dict[str, Any] | None = None

    @classmethod
    def from_dict(cls, payload: dict[str, Any]) -> "Asset":
        return cls(
            id=str(payload.get("id") or uuid4().hex[:12]),
            kind=str(payload.get("kind", "")),
            name=str(payload.get("name", "")),
            path=str(payload.get("path", "")),
            trainer=str(payload.get("trainer", "")),
            dataset_id=str(payload.get("dataset_id", "")),
            checkpoint=str(payload.get("checkpoint", "")),
            epochs=int(payload.get("epochs", 0) or 0),
            created_at=str(payload.get("created_at") or _now()),
            metadata=dict(payload.get("metadata") or {}),
        )


class AssetRegistry:
    """Persistent friendly-name index for datasets, models, and checkpoints."""

    def __init__(self, root: Path) -> None:
        self.path = root.resolve() / "data" / "assets.json"
        self.assets: list[Asset] = []
        self.load()

    def load(self) -> None:
        try:
            payload = json.loads(self.path.read_text(encoding="utf-8"))
            self.assets = [
                Asset.from_dict(item)
                for item in payload.get("assets", [])
                if isinstance(item, dict)
            ]
        except (OSError, ValueError, TypeError, json.JSONDecodeError):
            self.assets = []

    def save(self) -> None:
        self.path.parent.mkdir(parents=True, exist_ok=True)
        temporary = self.path.with_suffix(".tmp")
        temporary.write_text(
            json.dumps({"assets": [asdict(item) for item in self.assets]}, indent=2),
            encoding="utf-8",
        )
        temporary.replace(self.path)

    def register(
        self,
        *,
        kind: str,
        name: str,
        path: str,
        trainer: str = "",
        dataset_id: str = "",
        checkpoint: str = "",
        epochs: int = 0,
        metadata: dict[str, Any] | None = None,
        persist: bool = True,
    ) -> Asset:
        resolved = str(Path(path).expanduser().resolve())
        existing = next(
            (
                item
                for item in self.assets
                if item.kind == kind and Path(item.path) == Path(resolved)
            ),
            None,
        )
        asset = existing or Asset(uuid4().hex[:12], kind, name, resolved)
        asset.name = name.strip() or Path(resolved).name
        asset.trainer = trainer
        asset.dataset_id = dataset_id
        asset.checkpoint = checkpoint
        asset.epochs = int(epochs)
        asset.created_at = asset.created_at or _now()
        if metadata:
            current = dict(asset.metadata or {})
            current.update(metadata)
            asset.metadata = current
        if existing is None:
            self.assets.insert(0, asset)
        if persist:
            self.save()
        return asset

    def ingest_result(self, result: dict[str, Any]) -> None:
        entries = result.get("assets", [])
        if not isinstance(entries, list):
            return
        for item in entries:
            if not isinstance(item, dict):
                continue
            if item.get("kind") and item.get("path"):
                values = {
                    key: item[key]
                    for key in (
                        "kind", "name", "path", "trainer", "dataset_id",
                        "checkpoint", "epochs", "metadata",
                    )
                    if key in item
                }
                if "trigger_word" in item:
                    metadata = dict(values.get("metadata") or {})
                    metadata["trigger_word"] = str(item.get("trigger_word") or "")
                    values["metadata"] = metadata
                dataset_path = str(item.get("dataset_path", ""))
                if item.get("kind") == "model" and dataset_path and Path(dataset_path).is_dir():
                    dataset = self.register(
                        kind="dataset",
                        name=Path(dataset_path).name,
                        path=dataset_path,
                    )
                    values["dataset_id"] = dataset.id
                values.setdefault("name", Path(str(item["path"])).name)
                self.register(**values)

    def find(self, kind: str, query: str, *, trainer: str = "") -> list[Asset]:
        wanted = _normal(query)
        matches = []
        exact = []
        for item in self.assets:
            if item.kind != kind or (trainer and item.trainer != trainer):
                continue
            haystacks = {_normal(item.name), _normal(Path(item.path).name)}
            if wanted in haystacks:
                exact.append(item)
            elif any(wanted and wanted in value for value in haystacks):
                matches.append(item)
        return exact or matches

    def discover(self, config: Any, *, persist: bool = True) -> None:
        # Models are stored by their output folder (or the model file itself).
        # Keep the registry in step with the filesystem so removing an old
        # output cannot leave a ghost model that makes name matching ambiguous.
        self.assets = [
            item
            for item in self.assets
            if item.kind != "model" or (
                item.path.strip() and Path(item.path).expanduser().exists()
            )
            # Old ADAM versions registered LoRA epoch snapshots.  Prune those
            # stale records as well as skipping them during new discovery.
            and not (
                item.trainer == "lora"
                and _is_lora_training_checkpoint(Path(item.path))
            )
        ]
        folders = config.get("tool_folders", {})
        if not isinstance(folders, dict):
            return
        folders = dict(folders)
        app_root = self.path.parent.parent
        from adam.video_lora import discover_assets as discover_video_assets
        discover_video_assets(self, app_root, config)
        if not folders.get("oasis_trainer"):
            try:
                external = json.loads((app_root / "config" / "external_tools.json").read_text(encoding="utf-8"))
            except (OSError, ValueError, TypeError, json.JSONDecodeError):
                external = {}
            for entry in external.get("tools", []) if isinstance(external, dict) else []:
                if isinstance(entry, dict) and entry.get("id") == "external_oasis_game_trainer":
                    root = str(entry.get("backend", {}).get("root", ""))
                    if root:
                        folders["oasis_trainer"] = root
        external_lora_root = app_root / "LoRAModelsHere"
        if external_lora_root.is_dir():
            for path in external_lora_root.rglob("*.safetensors"):
                if (
                    path.is_file()
                    and "_comfy" not in path.stem.casefold()
                    and not _is_lora_training_checkpoint(path)
                ):
                    self.register(
                        kind="model",
                        name=path.stem.removesuffix("_cancelled"),
                        path=str(path),
                        trainer="lora",
                        checkpoint=str(path),
                        persist=False,
                    )
        base_model_root = app_root / "LoRA StableDiffusionModels Here"
        if base_model_root.is_dir():
            for path in base_model_root.iterdir():
                is_model_file = path.is_file() and path.suffix.casefold() in {
                    ".safetensors", ".ckpt", ".pt", ".bin"
                }
                is_diffusers_folder = path.is_dir() and (
                    (path / "model_index.json").is_file()
                    or (path / "unet" / "config.json").is_file()
                )
                if is_model_file or is_diffusers_folder:
                    self.register(
                        kind="base_model",
                        name=path.stem if path.is_file() else path.name,
                        path=str(path),
                        trainer="stable_diffusion",
                        persist=False,
                    )
        flow_datasets = self._flow_dataset_paths()
        collector = Path(str(folders.get("dataset_collector", ""))) / "Datasets"
        if collector.is_dir():
            for folder in collector.iterdir():
                if folder.is_dir():
                    self.register(
                        kind="dataset", name=folder.name, path=str(folder), persist=False
                    )
        for trainer, folder_name, output_name in (
            ("ddpm", "ddpm_trainer", "output"),
            ("lora", "lora_trainer", "output"),
            ("flow", "flow_trainer", "output_flow_models"),
            ("oasis", "oasis_trainer", "output_action_flow_models"),
        ):
            root = Path(str(folders.get(folder_name, ""))) / output_name
            if not root.is_dir():
                continue
            # LoRA Trainer versions do not all agree on their output layout.
            # Some write ``output/<run>/<name>.safetensors`` while others add
            # a second folder below the run.  Register the actual weight file
            # in either layout so the generator can load it directly.
            if trainer == "lora":
                for checkpoint_path in root.rglob("*.safetensors"):
                    if (
                        not checkpoint_path.is_file()
                        or "_comfy" in checkpoint_path.stem.casefold()
                        or _is_lora_training_checkpoint(checkpoint_path)
                    ):
                        continue
                    trigger_word = ""
                    for metadata_path in (checkpoint_path.parent / "model_info.json", checkpoint_path.parent.parent / "model_info.json"):
                        try:
                            metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
                            trigger_word = str(metadata.get("trigger_word") or "")
                            if trigger_word:
                                break
                        except (OSError, ValueError, TypeError, json.JSONDecodeError):
                            continue
                    self.register(
                        kind="model",
                        name=checkpoint_path.stem.removesuffix("_cancelled"),
                        path=str(checkpoint_path),
                        trainer="lora",
                        checkpoint=str(checkpoint_path),
                        metadata={"trigger_word": trigger_word or checkpoint_path.stem.removesuffix("_cancelled")},
                        persist=False,
                    )
                continue
            for folder in root.iterdir():
                if not folder.is_dir():
                    continue
                name = folder.name
                dataset_path = ""
                if trainer == "ddpm":
                    # DDPM writes a durable sidecar with the friendly model name and
                    # source dataset. Prefer it over a filesystem-safe folder name.
                    try:
                        metadata = json.loads((folder / "model_info.json").read_text(encoding="utf-8"))
                        name = str(metadata.get("model_name") or metadata.get("name") or name)
                        dataset_path = str(metadata.get("dataset_dir") or "")
                    except (OSError, ValueError, TypeError, json.JSONDecodeError):
                        pass
                    checkpoints = sorted(
                        folder.glob("checkpoint-*"),
                        key=lambda p: int(p.name.rsplit("-", 1)[-1])
                        if p.name.rsplit("-", 1)[-1].isdigit()
                        else -1,
                    )
                elif trainer == "flow":
                    checkpoints = []
                    try:
                        metadata = json.loads(
                            (folder / "flow_model_info.json").read_text(encoding="utf-8")
                        )
                        if metadata.get("model_type") != "rectified_flow":
                            continue
                        if not (folder / "unet" / "config.json").is_file():
                            continue
                        name = str(metadata.get("model_name") or metadata.get("name") or name)
                        dataset_path = flow_datasets.get(str(folder.resolve()), "")
                    except (OSError, ValueError, TypeError, json.JSONDecodeError):
                        continue
                else:
                    checkpoints = []
                    try:
                        metadata = json.loads(
                            (folder / "action_flow_model_info.json").read_text(encoding="utf-8")
                        )
                        if metadata.get("model_type") != "action_conditioned_rectified_flow_video":
                            continue
                        if not (folder / "unet" / "config.json").is_file():
                            continue
                        name = _friendly_name(
                            str(metadata.get("model_name") or metadata.get("name") or name),
                            folder.name,
                        )
                    except (OSError, ValueError, TypeError, json.JSONDecodeError):
                        continue
                checkpoint = (
                    str(folder) if trainer in {"flow", "oasis"} else str(checkpoints[-1]) if checkpoints else ""
                )
                dataset_id = ""
                if dataset_path and Path(dataset_path).is_dir():
                    dataset_asset = self.register(
                        kind="dataset",
                        name=Path(dataset_path).name,
                        path=dataset_path,
                        persist=False,
                    )
                    dataset_id = dataset_asset.id
                self.register(
                    kind="model",
                    name=name,
                    path=str(folder),
                    trainer=trainer,
                    dataset_id=dataset_id,
                    checkpoint=checkpoint,
                    metadata=({"trigger_word": trigger_word or name} if trainer == "lora" else None),
                    persist=False,
                )
        try:
            from adam.dataset_registry import DatasetRegistry

            dataset_registry = DatasetRegistry(app_root, config)
            valid_location_ids = {location.id for location in dataset_registry.known_locations()}
            self.assets = [
                item for item in self.assets
                if not (
                    item.kind == "dataset"
                    and isinstance(item.metadata, dict)
                    and item.metadata.get("dataset_registry_source") in {"adam", "tool"}
                    and item.metadata.get("dataset_location_id")
                    and item.metadata.get("dataset_location_id") not in valid_location_ids
                )
            ]
            dataset_registry.discover_into_assets(self, persist=False, update_cache=persist)
        except Exception:
            pass
        if persist:
            self.save()

    def _flow_dataset_paths(self) -> dict[str, str]:
        """Recover source datasets for Flow models created by ADAM in older runs."""
        jobs_path = self.path.parent / "jobs.json"
        try:
            payload = json.loads(jobs_path.read_text(encoding="utf-8"))
            jobs = payload.get("jobs", [])
        except (OSError, ValueError, TypeError, json.JSONDecodeError):
            return {}
        links: dict[str, str] = {}
        if not isinstance(jobs, list):
            return links
        for job in jobs:
            if not isinstance(job, dict):
                continue
            plan = job.get("plan", {})
            steps = plan.get("steps", []) if isinstance(plan, dict) else []
            if not isinstance(steps, list):
                continue
            for step in steps:
                if not isinstance(step, dict) or step.get("tool_id") != "flow_trainer":
                    continue
                arguments = step.get("arguments", {})
                if not isinstance(arguments, dict):
                    continue
                output = str(arguments.get("output_dir", ""))
                dataset = str(arguments.get("dataset_dir", ""))
                if not output or not dataset or not Path(dataset).is_dir():
                    continue
                try:
                    links[str(Path(output).expanduser().resolve())] = str(Path(dataset).expanduser().resolve())
                except OSError:
                    continue
        return links