File size: 5,858 Bytes
05f095d
 
 
1d697a7
05f095d
 
 
 
 
 
 
 
 
1d697a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f095d
 
 
1d697a7
05f095d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa6bb8
05f095d
8aa6bb8
 
 
 
 
05f095d
 
8aa6bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f095d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa6bb8
 
 
 
 
 
05f095d
 
 
 
 
 
 
 
 
8aa6bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
05f095d
 
8aa6bb8
05f095d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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