Spaces:
Running on Zero
Running on Zero
File size: 4,112 Bytes
90b5edd | 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 |
import hashlib
import hmac
import json
from pathlib import Path
from deployment_config import (
IS_DEMO,
IS_PUBLIC,
SAMPLE_VIDEO_IDS,
)
from sample_video_identity import get_sample_video_id
BASE_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = BASE_DIR.parent
DEPLOYMENT_CACHE_DIR = PROJECT_ROOT / "deployment_cache"
ALLOWED_CACHE_NAMES = frozenset({
"siglip",
"whisper",
"audio_embeddings",
"yolo",
"ocr",
})
def use_deployment_cache(video_path) -> bool:
"""
Use exported caches for registered sample videos in
demo or public mode.
Visitor uploads continue through the ordinary cache
and indexing pipeline.
"""
if not (IS_DEMO or IS_PUBLIC):
return False
try:
video_id = get_sample_video_id(video_path)
except (
PermissionError,
ValueError,
TypeError,
KeyError,
FileNotFoundError,
):
return False
return video_id in SAMPLE_VIDEO_IDS
def get_deployment_cache_dir(video_id: str) -> Path:
if video_id not in SAMPLE_VIDEO_IDS:
raise PermissionError(
"Deployment caches are restricted to registered samples."
)
return DEPLOYMENT_CACHE_DIR / video_id
def get_deployment_manifest(video_id: str) -> dict | None:
if not (IS_DEMO or IS_PUBLIC):
return None
manifest_path = (
get_deployment_cache_dir(video_id) / "manifest.json"
)
if not manifest_path.is_file():
raise FileNotFoundError(
f"Deployment cache manifest missing for {video_id}."
)
with manifest_path.open("r", encoding="utf-8") as file:
manifest = json.load(file)
if not isinstance(manifest, dict):
raise ValueError("Invalid deployment cache manifest.")
if manifest.get("video_id") != video_id:
raise ValueError(
"Deployment cache manifest video ID mismatch."
)
if not isinstance(manifest.get("files"), dict):
raise ValueError("Invalid deployment cache file mapping.")
if not isinstance(manifest.get("sha256"), dict):
raise ValueError("Missing deployment cache checksums.")
return manifest
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as file:
for chunk in iter(
lambda: file.read(1024 * 1024),
b"",
):
digest.update(chunk)
return digest.hexdigest()
def resolve_deployment_cache(
video_id: str,
cache_name: str,
) -> Path:
if not (IS_DEMO or IS_PUBLIC):
raise RuntimeError(
"Deployment caches require demo or public mode."
)
if cache_name not in ALLOWED_CACHE_NAMES:
raise ValueError(
f"Unknown deployment cache type: {cache_name}"
)
manifest = get_deployment_manifest(video_id)
filename = manifest["files"].get(cache_name)
expected_hash = manifest["sha256"].get(cache_name)
if not isinstance(filename, str) or not filename:
raise FileNotFoundError(
f"Required deployment cache missing: {cache_name}"
)
if not isinstance(expected_hash, str) or len(expected_hash) != 64:
raise ValueError(
f"Invalid checksum for deployment cache: {cache_name}"
)
cache_dir = get_deployment_cache_dir(video_id).resolve()
cache_path = (cache_dir / filename).resolve()
if not cache_path.is_relative_to(cache_dir):
raise ValueError("Invalid deployment cache path.")
if not cache_path.is_file():
raise FileNotFoundError(
f"Deployment cache file not found: {cache_path}"
)
actual_hash = sha256_file(cache_path)
if not hmac.compare_digest(
actual_hash.lower(),
expected_hash.lower(),
):
raise ValueError(
f"Deployment cache checksum mismatch: {cache_name}"
)
return cache_path
|