Spaces:
Sleeping
Sleeping
File size: 3,509 Bytes
5a5e72e | 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 | 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)
|