import json import os import shutil import threading import time import numpy as np from config import CACHE_DIR, CACHE_DISK_TTL, CACHE_IDLE_TTL, DEBUG, DISK_CACHE # { video_path: {"segments","chunks","visual","audio","precise","_atime"} } _cache: dict = {} # Files and directories this Space created itself (compressed uploads, fetched # links). Gradio's delete_cache never sees them, so until 2026-10 they lived for the # life of the process. Registered here and swept with everything else. TMP_TTL = 15 * 60 _tmp: dict = {} _tmp_lock = threading.Lock() def register_tmp(path: str) -> None: with _tmp_lock: _tmp[path] = time.time() def _sweep_tmp(now: float) -> None: with _tmp_lock: old = [p for p, t in _tmp.items() if now - t > TMP_TTL] for p in old: _tmp.pop(p, None) for p in old: try: if os.path.isdir(p): shutil.rmtree(p, ignore_errors=True) elif os.path.exists(p): os.remove(p) except Exception: pass def evict_stale() -> None: now = time.time() _sweep_tmp(now) for vp in [k for k, e in _cache.items() if now - e.get("_atime", now) > CACHE_IDLE_TTL]: _cache.pop(vp, None) try: if os.path.isdir(CACHE_DIR): for name in os.listdir(CACHE_DIR): d = os.path.join(CACHE_DIR, name) if os.path.isdir(d) and now - os.path.getmtime(d) > CACHE_DISK_TTL: shutil.rmtree(d, ignore_errors=True) except Exception: pass def fingerprint(path: str) -> str: import hashlib h = hashlib.sha1() st = os.stat(path) h.update(str(st.st_size).encode()) with open(path, "rb") as f: h.update(f.read(1 << 20)) if st.st_size > (1 << 20): f.seek(-(1 << 20), os.SEEK_END) h.update(f.read(1 << 20)) return h.hexdigest()[:16] def disk_load(fp: str) -> dict | None: if not DISK_CACHE: return None d = os.path.join(CACHE_DIR, fp) try: with open(os.path.join(d, "meta.json"), encoding="utf-8") as f: meta = json.load(f) arr = np.load(os.path.join(d, "emb.npz")) chunk_emb = arr["chunk_emb"] # Unpack seg_embs if present (new-format caches only) seg_embs_flat = arr["seg_embs_flat"] if "seg_embs_flat" in arr else None seg_embs_offsets = arr["seg_embs_offsets"] if "seg_embs_offsets" in arr else None chunks = [] for i, c in enumerate(meta["chunks"]): chunk: dict = { "text": c["text"], "start": c["start"], "end": c["end"], "logprob": c.get("logprob", 0.0), "entities": set(c.get("entities", [])), "seg_texts": c.get("seg_texts", []), "emb": chunk_emb[i], } if seg_embs_flat is not None and seg_embs_offsets is not None: s, e = int(seg_embs_offsets[i]), int(seg_embs_offsets[i + 1]) if e > s: chunk["seg_embs"] = seg_embs_flat[s:e] chunks.append(chunk) visual = {"emb": arr["visual_emb"], "time": arr["visual_time"]} print(f"[NINA] Disk cache hit ({fp}) — loaded {len(chunks)} chunks.") return { "segments": meta["segments"], "chunks": chunks, "visual": visual, "precise": {float(k): v for k, v in meta["precise"].items()}, } except Exception: return None def disk_save(fp: str, entry: dict) -> None: if not DISK_CACHE: return d = os.path.join(CACHE_DIR, fp) try: os.makedirs(d, exist_ok=True) chunks = entry["chunks"] meta = { "segments": entry["segments"], "precise": entry["precise"], "chunks": [ { "text": c["text"], "start": c["start"], "end": c["end"], "logprob": c.get("logprob", 0.0), "entities": sorted(c.get("entities", [])), "seg_texts": c.get("seg_texts", []), } for c in chunks ], } chunk_emb = ( np.stack([c["emb"] for c in chunks]) if chunks else np.zeros((0, 384), dtype=np.float32) ) # Pack variable-length seg_embs into a flat array + offsets all_seg = [c.get("seg_embs") for c in chunks] npz_kwargs: dict = { "chunk_emb": chunk_emb, "visual_emb": entry["visual"]["emb"], "visual_time": entry["visual"]["time"], } if any(e is not None for e in all_seg): flat = np.concatenate([e for e in all_seg if e is not None], axis=0) offsets = np.zeros(len(chunks) + 1, dtype=np.int32) for i, e in enumerate(all_seg): offsets[i + 1] = offsets[i] + (len(e) if e is not None else 0) npz_kwargs["seg_embs_flat"] = flat npz_kwargs["seg_embs_offsets"] = offsets with open(os.path.join(d, "meta.json"), "w", encoding="utf-8") as f: json.dump(meta, f) np.savez(os.path.join(d, "emb.npz"), **npz_kwargs) except Exception as e: if DEBUG: print(f"[DEBUG] disk cache save failed: {e}") def disk_update_precise(fp: str, precise: dict) -> None: if not DISK_CACHE: return p = os.path.join(CACHE_DIR, fp, "meta.json") try: with open(p, encoding="utf-8") as f: meta = json.load(f) meta["precise"] = precise with open(p, "w", encoding="utf-8") as f: json.dump(meta, f) except Exception: pass