Skip to content

Commit b13dcd5

Browse files
committed
Add resource ids for every file in models.json
- allows mapping resources <-> models
1 parent 6c9152f commit b13dcd5

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

ai_diffusion/presets/models.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
"name": "OmniSR Superscale",
3030
"files": [
3131
{
32+
"id": "upscaler-fast_2x-all",
3233
"path": "models/upscale_models/OmniSR_X2_DIV2K.safetensors",
3334
"url": "https://huggingface.co/Acly/Omni-SR/resolve/main/OmniSR_X2_DIV2K.safetensors"
3435
},
3536
{
37+
"id": "upscaler-fast_3x-all",
3638
"path": "models/upscale_models/OmniSR_X3_DIV2K.safetensors",
3739
"url": "https://huggingface.co/Acly/Omni-SR/resolve/main/OmniSR_X3_DIV2K.safetensors"
3840
},
3941
{
42+
"id": "upscaler-fast_4x-all",
4043
"path": "models/upscale_models/OmniSR_X4_DIV2K.safetensors",
4144
"url": "https://huggingface.co/Acly/Omni-SR/resolve/main/OmniSR_X4_DIV2K.safetensors"
4245
}
@@ -107,10 +110,12 @@
107110
"name": "Fooocus Inpaint",
108111
"files": [
109112
{
113+
"id": "inpaint-fooocus_head-sdxl",
110114
"path": "models/inpaint/fooocus_inpaint_head.pth",
111115
"url": "https://huggingface.co/lllyasviel/fooocus_inpaint/resolve/main/fooocus_inpaint_head.pth"
112116
},
113117
{
118+
"id": "inpaint-fooocus_patch-sdxl",
114119
"path": "models/inpaint/inpaint_v26.fooocus.patch",
115120
"url": "https://huggingface.co/lllyasviel/fooocus_inpaint/resolve/main/inpaint_v26.fooocus.patch"
116121
}
@@ -357,10 +362,12 @@
357362
"name": "IP-Adapter Face (SD1.5)",
358363
"files": [
359364
{
365+
"id": "ip_adapter-face-sd15",
360366
"path": "models/ipadapter/ip-adapter-faceid-plusv2_sd15.bin",
361367
"url": "https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/ip-adapter-faceid-plusv2_sd15.bin"
362368
},
363369
{
370+
"id": "lora-face-sd15",
364371
"path": "models/loras/ip-adapter-faceid-plusv2_sd15_lora.safetensors",
365372
"url": "https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/ip-adapter-faceid-plusv2_sd15_lora.safetensors"
366373
}
@@ -382,10 +389,12 @@
382389
"name": "IP-Adapter Face (XL)",
383390
"files": [
384391
{
392+
"id": "ip_adapter-face-sdxl",
385393
"path": "models/ipadapter/ip-adapter-faceid-plusv2_sdxl.bin",
386394
"url": "https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/ip-adapter-faceid-plusv2_sdxl.bin"
387395
},
388396
{
397+
"id": "lora-face-sdxl",
389398
"path": "models/loras/ip-adapter-faceid-plusv2_sdxl_lora.safetensors",
390399
"url": "https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/ip-adapter-faceid-plusv2_sdxl_lora.safetensors"
391400
}
@@ -437,10 +446,12 @@
437446
"name": "Reference (Flux)",
438447
"files": [
439448
{
449+
"id": "clip_vision-redux-flux",
440450
"path": "models/clip_vision/sigclip_vision_patch14_384.safetensors",
441451
"url": "https://huggingface.co/Comfy-Org/sigclip_vision_384/resolve/main/sigclip_vision_patch14_384.safetensors"
442452
},
443453
{
454+
"id": "ip_adapter-reference-flux",
444455
"path": "models/style_models/flux1-redux-dev.safetensors",
445456
"url": "https://files.interstice.cloud/models/flux1-redux-dev.safetensors"
446457
}

ai_diffusion/resources.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ def text(self):
256256
return control.control_mode_text[self]
257257

258258

259+
def resource_id(kind: ResourceKind, arch: Arch, identifier: ControlMode | UpscalerName | str):
260+
if isinstance(identifier, Enum):
261+
identifier = identifier.name
262+
return f"{kind.name}-{identifier}-{arch.name}"
263+
264+
259265
class ResourceId(NamedTuple):
260266
kind: ResourceKind
261267
arch: Arch
@@ -288,29 +294,44 @@ class ModelRequirements(Enum):
288294
insightface = 1
289295

290296

297+
class ModelFile(NamedTuple):
298+
path: Path
299+
url: str
300+
id: ResourceId
301+
302+
@property
303+
def name(self):
304+
return self.path.name
305+
306+
@staticmethod
307+
def parse(data: dict[str, Any], parent_id: ResourceId):
308+
id = ResourceId.parse(data.get("id", parent_id.string))
309+
return ModelFile(Path(data["path"]), data["url"], id)
310+
311+
291312
class ModelResource(NamedTuple):
292313
name: str
293314
id: ResourceId
294-
files: dict[Path, str]
315+
files: list[ModelFile]
295316
alternatives: list[Path] | None = None # for backwards compatibility
296317
requirements: ModelRequirements = ModelRequirements.none
297318

298319
@property
299320
def filename(self):
300321
assert len(self.files) == 1
301-
return next(iter(self.files)).name
322+
return self.files[0].name
302323

303324
@property
304325
def folder(self):
305-
return next(iter(self.files)).parent
326+
return self.files[0].path.parent
306327

307328
@property
308329
def url(self):
309330
assert len(self.files) == 1
310-
return next(iter(self.files.values()))
331+
return self.files[0].url
311332

312333
def exists_in(self, path: Path):
313-
exact = all((path / filepath).exists() for filepath in self.files.keys())
334+
exact = all((path / file.path).exists() for file in self.files)
314335
alt = self.alternatives is not None and any((path / f).exists() for f in self.alternatives)
315336
return exact or alt
316337

@@ -329,7 +350,14 @@ def as_dict(self):
329350
result = {
330351
"id": self.id.string,
331352
"name": self.name,
332-
"files": [{"path": str(k.as_posix()), "url": v} for k, v in self.files.items()],
353+
"files": [
354+
{
355+
"id": f.id,
356+
"path": str(f.path.as_posix()),
357+
"url": f.url,
358+
}
359+
for f in self.files
360+
],
333361
}
334362
if self.alternatives:
335363
result["alternatives"] = [str(p.as_posix()) for p in self.alternatives]
@@ -340,7 +368,7 @@ def as_dict(self):
340368
@staticmethod
341369
def from_dict(data: dict[str, Any]):
342370
id = ResourceId.parse(data["id"])
343-
files = {Path(f["path"]): f["url"] for f in data["files"]}
371+
files = [ModelFile.parse(f, id) for f in data["files"]]
344372
alternatives = [Path(p) for p in data.get("alternatives", [])]
345373
requirements = (
346374
ModelRequirements[data["requirements"]]
@@ -417,12 +445,6 @@ def all_models(include_deprecated=False):
417445
return result
418446

419447

420-
def resource_id(kind: ResourceKind, arch: Arch, identifier: ControlMode | UpscalerName | str):
421-
if isinstance(identifier, Enum):
422-
identifier = identifier.name
423-
return f"{kind.name}-{identifier}-{arch.name}"
424-
425-
426448
def find_resource(id: ResourceId, include_deprecated=False):
427449
return next((m for m in all_models(include_deprecated) if m.id == id), None)
428450

ai_diffusion/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,10 @@ def cb(stage: str, message: str | DownloadProgress):
319319
for resource in to_install:
320320
if not resource.exists_in(self.path) and not resource.exists_in(self.comfy_dir):
321321
await self._install_requirements(resource.requirements, network, cb)
322-
for filepath, url in resource.files.items():
323-
target_file = self.path / filepath
322+
for file in resource.files:
323+
target_file = self.path / file.path
324324
target_file.parent.mkdir(parents=True, exist_ok=True)
325-
await _download_cached(resource.name, network, url, target_file, cb)
325+
await _download_cached(resource.name, network, file.url, target_file, cb)
326326
except Exception as e:
327327
log.exception(str(e))
328328
raise e

scripts/download_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ async def download(
7979
verbose=False,
8080
dry_run=False,
8181
):
82-
for filepath, url in model.files.items():
83-
target_file = destination / filepath
84-
url = _map_url(url)
82+
for file in model.files:
83+
target_file = destination / file.path
84+
url = _map_url(file.url)
8585
if verbose:
8686
print(f"Looking for {target_file}")
8787
if target_file.exists():

0 commit comments

Comments
 (0)