Spaces:
Sleeping
Sleeping
Download s2f/model_assets.py from ProgramComputer/Speech2Face: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/spaces/ProgramComputer/Speech2Face/resolve/main/s2f/model_assets.py
- Command line
-
hf download hf://spaces/ProgramComputer/Speech2Face/s2f/model_assets.py
-
curl -L -o model_assets.py https://huggingface.co/spaces/ProgramComputer/Speech2Face/resolve/main/s2f/model_assets.py
3.51 kB
| from __future__ import annotations | |
| import json | |
| import os | |
| from pathlib import Path | |
| from threading import Lock | |
| from typing import Any | |
| from .asset_contract import checked_asset, need, sha256_file | |
| MODEL_REPOSITORY = "ProgramComputer/Speech2Face-models" | |
| REPOSITORY_SCHEMA = "speech2face.space-model-repository/v1" | |
| WEIGHT_FILES = ( | |
| "voice-arcface.keras", | |
| "voice-facenet.keras", | |
| "decoder-arcface.safetensors", | |
| "decoder-facenet.safetensors", | |
| ) | |
| _DOWNLOADS: dict[tuple[str, str, str, str, int], Path] = {} | |
| _DOWNLOAD_LOCK = Lock() | |
| def validate_repository_record(record: dict[str, str]) -> dict[str, str]: | |
| need( | |
| type(record) is dict | |
| and set(record) == {"schema", "repo_id", "revision"} | |
| and record["schema"] == REPOSITORY_SCHEMA | |
| and record["repo_id"] == MODEL_REPOSITORY, | |
| "Invalid model repository record", | |
| ) | |
| revision = record["revision"] | |
| need( | |
| isinstance(revision, str) | |
| and ( | |
| revision == "main" | |
| or len(revision) == 40 and all(c in "0123456789abcdef" for c in revision) | |
| ), | |
| "Invalid model repository revision", | |
| ) | |
| return record | |
| def load_repository_record(path: str | Path) -> dict[str, str]: | |
| try: | |
| return validate_repository_record(json.loads(Path(path).read_text(encoding="utf-8"))) | |
| except Exception: | |
| raise ValueError("Invalid model repository record") from None | |
| def _checked_download(path: Path, reference: dict[str, Any]) -> Path: | |
| try: | |
| need( | |
| path.is_file() | |
| and path.stat().st_size == reference["size_bytes"] | |
| and sha256_file(path) == reference["sha256"], | |
| "Downloaded model weight failed integrity verification", | |
| ) | |
| except Exception: | |
| raise ValueError("Downloaded model weight failed integrity verification") from None | |
| return path | |
| def resolve_model_asset(root: str | Path, reference: dict[str, Any]) -> Path: | |
| name = reference["file"] | |
| need(name in WEIGHT_FILES, "Unregistered model weight") | |
| local = Path(root) / name | |
| if local.exists() or local.is_symlink(): | |
| return checked_asset(root, reference) | |
| record = load_repository_record(Path(root) / "model-repository.json") | |
| need( | |
| type(reference["size_bytes"]) is int | |
| and reference["size_bytes"] > 0 | |
| and isinstance(reference["sha256"], str) | |
| and len(reference["sha256"]) == 64 | |
| and all(c in "0123456789abcdef" for c in reference["sha256"]), | |
| "Invalid model weight integrity record", | |
| ) | |
| key = record["repo_id"], record["revision"], name, reference["sha256"], reference["size_bytes"] | |
| with _DOWNLOAD_LOCK: | |
| if key not in _DOWNLOADS: | |
| try: | |
| from huggingface_hub import __version__, hf_hub_download | |
| need(__version__ == "2.4.0", "Use the pinned model-download runtime") | |
| downloaded = Path( | |
| hf_hub_download( | |
| repo_id=record["repo_id"], | |
| filename=name, | |
| repo_type="model", | |
| revision=record["revision"], | |
| token=os.environ.get("HF_TOKEN"), | |
| ) | |
| ) | |
| except Exception: | |
| raise ValueError("Model weight download failed") from None | |
| _DOWNLOADS[key] = _checked_download(downloaded, reference) | |
| return _DOWNLOADS[key] | |
| return _checked_download(_DOWNLOADS[key], reference) | |