File size: 11,558 Bytes
2ea2835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""Immutable OpenVid row ledger shared by frozen video references.

Unlike prompt datasets, a feature-reference ledger retains videos whose caption
is empty.  Feature rows are identified exclusively by the contiguous source
sequence and MP4 basename; captions are descriptive payloads, never keys.
"""

from __future__ import annotations

import csv
import hashlib
import json
import os
from dataclasses import dataclass
from pathlib import Path
import tempfile
from typing import Iterable, Mapping


LEDGER_KIND = "openvid_video_reference_ledger"
LEDGER_SCHEMA_VERSION = 1


def file_sha256(path: str | os.PathLike, chunk_size: int = 8 << 20) -> str:
    digest = hashlib.sha256()
    with open(path, "rb") as handle:
        while block := handle.read(int(chunk_size)):
            digest.update(block)
    return digest.hexdigest()


def canonical_sha256(value) -> str:
    payload = json.dumps(
        value,
        ensure_ascii=False,
        sort_keys=True,
        separators=(",", ":"),
        allow_nan=False,
    ).encode("utf-8")
    return hashlib.sha256(payload).hexdigest()


def _atomic_bytes(path: Path, payload: bytes) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    descriptor, temporary_name = tempfile.mkstemp(
        dir=path.parent, prefix=f".{path.name}.", suffix=".tmp")
    temporary = Path(temporary_name)
    try:
        with os.fdopen(descriptor, "wb") as handle:
            handle.write(payload)
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(temporary, path)
    finally:
        temporary.unlink(missing_ok=True)


@dataclass(frozen=True)
class OpenVidReferenceRow:
    sequence: int
    role: str
    video_name: str
    caption: str
    video_size_bytes: int
    video_mtime_ns: int

    def validate(self, *, anchor_cohort_size: int) -> None:
        if self.sequence < 0:
            raise ValueError("OpenVid reference sequence must be non-negative")
        expected_role = (
            "anchor" if self.sequence < int(anchor_cohort_size) else "reference")
        if self.role != expected_role:
            raise ValueError(
                f"row {self.sequence} role {self.role!r} != {expected_role!r}")
        if (
            Path(self.video_name).name != self.video_name
            or not self.video_name.lower().endswith(".mp4")
        ):
            raise ValueError(f"invalid MP4 basename: {self.video_name!r}")
        if self.video_size_bytes <= 0 or self.video_mtime_ns <= 0:
            raise ValueError("video size/mtime identity must be positive")

    def to_dict(self) -> dict:
        return {
            "caption": self.caption,
            "role": self.role,
            "sequence": self.sequence,
            "video_mtime_ns": self.video_mtime_ns,
            "video_name": self.video_name,
            "video_size_bytes": self.video_size_bytes,
        }

    @classmethod
    def from_mapping(
        cls, value: Mapping, *, anchor_cohort_size: int
    ) -> "OpenVidReferenceRow":
        row = cls(
            sequence=int(value["sequence"]),
            role=str(value["role"]),
            video_name=str(value["video_name"]),
            caption=str(value.get("caption", "")),
            video_size_bytes=int(value["video_size_bytes"]),
            video_mtime_ns=int(value["video_mtime_ns"]),
        )
        if set(value) != set(row.to_dict()):
            raise ValueError("OpenVid reference row schema mismatch")
        row.validate(anchor_cohort_size=anchor_cohort_size)
        return row


class OpenVidReferencePromptDataset:
    """Prompt-only view of a frozen feature ledger.

    The nine captionless videos remain in the 77,615-row feature reference but
    are intentionally absent from text-conditioned student sampling.
    """

    def __init__(
        self,
        ledger_path: str | os.PathLike,
        *,
        expected_count: int = 77_615,
    ) -> None:
        rows, metadata = load_openvid_reference_ledger(
            ledger_path, expected_count=expected_count)
        self.metadata = metadata
        self.rows = tuple(row for row in rows if row.caption.strip())
        skipped = len(rows) - len(self.rows)
        if skipped != int(metadata["caption_empty_count"]):
            raise ValueError("OpenVid empty-caption ledger count mismatch")
        if not self.rows:
            raise ValueError("OpenVid ledger has no usable prompts")

    def __len__(self) -> int:
        return len(self.rows)

    def __getitem__(self, index: int) -> dict:
        row = self.rows[int(index)]
        return {
            "idx": row.sequence,
            "prompts": row.caption,
            "sample_ids": row.video_name,
        }


def _video_name_sequence_sha256(rows: Iterable[OpenVidReferenceRow]) -> str:
    digest = hashlib.sha256()
    for row in rows:
        digest.update(row.video_name.encode("utf-8"))
        digest.update(b"\n")
    return digest.hexdigest()


def _local_source_identity_sha256(rows: Iterable[OpenVidReferenceRow]) -> str:
    digest = hashlib.sha256()
    for row in rows:
        digest.update(row.video_name.encode("utf-8"))
        digest.update(b"\0")
        digest.update(str(row.video_size_bytes).encode("ascii"))
        digest.update(b"\0")
        digest.update(str(row.video_mtime_ns).encode("ascii"))
        digest.update(b"\0")
    return digest.hexdigest()


def freeze_openvid_reference_ledger(
    *,
    source_csv: str | os.PathLike,
    video_root: str | os.PathLike,
    ledger_path: str | os.PathLike,
    expected_count: int = 77_615,
    anchor_cohort_size: int = 1_024,
) -> dict:
    """Validate the CSV/video mirror and atomically publish JSONL + metadata."""
    source_csv = Path(source_csv).expanduser().resolve(strict=True)
    video_root = Path(video_root).expanduser().resolve(strict=True)
    ledger_path = Path(ledger_path).expanduser().resolve()
    expected_count = int(expected_count)
    anchor_cohort_size = int(anchor_cohort_size)
    if not video_root.is_dir():
        raise NotADirectoryError(video_root)
    if not 0 < anchor_cohort_size <= expected_count:
        raise ValueError("invalid OpenVid anchor cohort size")

    rows: list[OpenVidReferenceRow] = []
    seen: set[str] = set()
    with source_csv.open("r", newline="", encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        required = {"sequence", "role", "video", "caption"}
        if reader.fieldnames is None or not required.issubset(reader.fieldnames):
            raise ValueError(
                f"OpenVid reference CSV requires {sorted(required)}")
        for csv_line, value in enumerate(reader, start=2):
            try:
                sequence = int(value["sequence"])
            except (TypeError, ValueError) as exc:
                raise ValueError(
                    f"invalid sequence at CSV line {csv_line}") from exc
            if sequence != len(rows):
                raise ValueError(
                    "OpenVid sequence is not contiguous in CSV order: "
                    f"{sequence} != {len(rows)}")
            video_name = str(value["video"]).strip()
            if video_name in seen:
                raise ValueError(f"duplicate OpenVid video: {video_name}")
            video_path = video_root / video_name
            if not video_path.is_file():
                raise FileNotFoundError(video_path)
            stat = video_path.stat()
            row = OpenVidReferenceRow(
                sequence=sequence,
                role=str(value["role"]).strip(),
                video_name=video_name,
                # Empty captions are valid reference rows.
                caption=str(value["caption"] or ""),
                video_size_bytes=int(stat.st_size),
                video_mtime_ns=int(stat.st_mtime_ns),
            )
            row.validate(anchor_cohort_size=anchor_cohort_size)
            rows.append(row)
            seen.add(video_name)
    if len(rows) != expected_count:
        raise ValueError(
            f"OpenVid reference count {len(rows)} != {expected_count}")

    lines = "".join(
        json.dumps(
            row.to_dict(), ensure_ascii=False, sort_keys=True,
            separators=(",", ":"), allow_nan=False,
        ) + "\n"
        for row in rows
    ).encode("utf-8")
    manifest_sha256 = hashlib.sha256(lines).hexdigest()
    metadata = {
        "anchor_cohort_size": anchor_cohort_size,
        "anchor_video_name_sequence_sha256": (
            _video_name_sequence_sha256(rows[:anchor_cohort_size])),
        "caption_empty_count": sum(not row.caption.strip() for row in rows),
        "csv_path": str(source_csv),
        "csv_sha256": file_sha256(source_csv),
        "kind": LEDGER_KIND,
        "ledger_file": ledger_path.name,
        "ledger_sha256": manifest_sha256,
        "local_source_identity_sha256": (
            _local_source_identity_sha256(rows)),
        "reference_count": len(rows),
        "schema_version": LEDGER_SCHEMA_VERSION,
        "video_name_sequence_sha256": _video_name_sequence_sha256(rows),
        "video_root": str(video_root),
    }
    _atomic_bytes(ledger_path, lines)
    _atomic_bytes(
        ledger_path.with_suffix(".meta.json"),
        (json.dumps(
            metadata, ensure_ascii=False, sort_keys=True, indent=2,
            allow_nan=False,
        ) + "\n").encode("utf-8"),
    )
    return metadata


def load_openvid_reference_ledger(
    ledger_path: str | os.PathLike,
    *,
    expected_count: int | None = None,
) -> tuple[list[OpenVidReferenceRow], dict]:
    path = Path(ledger_path).expanduser().resolve(strict=True)
    meta_path = path.with_suffix(".meta.json")
    metadata = json.loads(meta_path.read_text(encoding="utf-8"))
    if (
        metadata.get("kind") != LEDGER_KIND
        or int(metadata.get("schema_version", -1)) != LEDGER_SCHEMA_VERSION
        or metadata.get("ledger_file") != path.name
        or metadata.get("ledger_sha256") != file_sha256(path)
    ):
        raise ValueError("OpenVid reference ledger metadata/hash mismatch")
    anchor_count = int(metadata["anchor_cohort_size"])
    rows = []
    with path.open("r", encoding="utf-8") as handle:
        for line_number, line in enumerate(handle, start=1):
            if not line.strip():
                raise ValueError(f"blank OpenVid ledger line {line_number}")
            row = OpenVidReferenceRow.from_mapping(
                json.loads(line), anchor_cohort_size=anchor_count)
            if row.sequence != len(rows):
                raise ValueError("OpenVid ledger sequence is not contiguous")
            rows.append(row)
    if len(rows) != int(metadata.get("reference_count", -1)):
        raise ValueError("OpenVid ledger row count mismatch")
    if expected_count is not None and len(rows) != int(expected_count):
        raise ValueError(
            f"OpenVid ledger count {len(rows)} != {int(expected_count)}")
    if (
        _video_name_sequence_sha256(rows)
        != metadata.get("video_name_sequence_sha256")
        or _video_name_sequence_sha256(rows[:anchor_count])
        != metadata.get("anchor_video_name_sequence_sha256")
        or _local_source_identity_sha256(rows)
        != metadata.get("local_source_identity_sha256")
    ):
        raise ValueError("OpenVid ledger row identity mismatch")
    return rows, metadata


__all__ = [
    "LEDGER_KIND",
    "LEDGER_SCHEMA_VERSION",
    "OpenVidReferenceRow",
    "OpenVidReferencePromptDataset",
    "canonical_sha256",
    "file_sha256",
    "freeze_openvid_reference_ledger",
    "load_openvid_reference_ledger",
]