sungguk's picture
Release visual answerability benchmark v1.0.0
e1ced61 verified
Raw History Blame Contribute Delete
3.02 kB
"""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,
)