File size: 3,024 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""BBox DocVQA importer — optional, dual-gate source.

This source is only enabled when *both* the license and storage approval gates
are cleared (``docs/01`` §3.5, ``docs/02`` §4). The core pipeline must run
without it, so the importer exposes :func:`is_enabled` and the driver skips it
with a reason when a gate is still blocked — it is never silently substituted or
faked. Rows carry an evidence bounding box recorded in provenance for later
grounding.
"""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from ..hashing import canonical_json, sha256_bytes
from .base import (
    ImageResolver,
    ImageStore,
    IngestError,
    NormalizedItem,
    Policy,
    make_item,
)

NAME = "bbox_docvqa_train"
POLICY: Policy = "c2_train_candidate"
ALLOWED_SPLIT = "train"
REQUIRED_GATES = ("bbox_docvqa_license", "bbox_docvqa_storage")


def is_enabled(approval: Mapping[str, Mapping[str, Any]]) -> bool:
    """True only when both required gates are approved."""
    return all(approval.get(gate, {}).get("status") == "approved" for gate in REQUIRED_GATES)


def block_reason(approval: Mapping[str, Mapping[str, Any]]) -> str | None:
    """Return why the importer is disabled, or ``None`` when enabled."""
    unmet = [g for g in REQUIRED_GATES if approval.get(g, {}).get("status") != "approved"]
    if not unmet:
        return None
    return f"approval gates not approved: {', '.join(unmet)}"


def normalize(
    row: Mapping[str, Any],
    images: ImageStore,
    resolve: ImageResolver,
    *,
    revision: str,
    split: str,
    config: str = "default",
) -> NormalizedItem:
    """Normalize one BBox DocVQA native row to a :class:`NormalizedItem`."""
    if split != ALLOWED_SPLIT:
        raise IngestError(
            f"bbox_docvqa_train: only split {ALLOWED_SPLIT!r} may be ingested, got {split!r}"
        )
    question = str(row["question"])
    answers = row["answers"]
    if isinstance(answers, (list, tuple)):
        answer_raw = str(answers[0]) if answers else ""
    else:
        answer_raw = str(answers)
    if not answer_raw:
        raise IngestError(f"bbox_docvqa_train: row {row.get('id', '?')!r} has no answer")
    image_ref = str(row["image"])
    rel, digest = images.store(resolve(image_ref))
    extra: dict[str, Any] = {}
    box = row.get("evidence_box")
    if box is not None:
        extra["evidence_box_sha256"] = sha256_bytes(canonical_json(box).encode("utf-8"))
        extra["evidence_box"] = list(box)
    native_id = str(row.get("id", row.get("index")))
    return make_item(
        source=NAME,
        source_revision=revision,
        source_config=config,
        source_split=split,
        source_native_id=native_id,
        question=question,
        choices=(),
        answer_raw=answer_raw,
        answer_canonical=answer_raw,
        answer_type="short_text",
        image_paths=(rel,),
        image_sha256=(digest,),
        policy=POLICY,
        native_row=row,
        extra_provenance=extra,
    )