File size: 5,637 Bytes
6bfcafa | 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 | """Harbor's file boundary into the existing ExtractBench evaluator."""
from __future__ import annotations
import argparse
import json
from collections.abc import Mapping, Sequence
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from extract_bench.evaluation.evaluators.extract import ExtractEvaluator
from extract_bench.evaluation.metrics.extract.unified_evidence_metric import ValueCell
from extract_bench.evaluation.score_case import load_extract_test_case
from extract_bench.schemas.extract_output import ExtractOutput
from extract_bench.schemas.pipeline_io import InferenceRequest, InferenceResult
from extract_bench.schemas.product import ProductType
def _feedback(cells: Sequence[ValueCell], metadata: Mapping[str, Any]) -> dict[str, Any]:
expected = sum(cell.gt_path is not None for cell in cells)
predicted = sum(cell.pred_path is not None for cell in cells)
matched = sum(cell.value_correct for cell in cells)
if (expected, predicted, matched) != (
metadata["expected_cells"],
metadata["predicted_cells"],
metadata["v_correct"],
):
raise ValueError("Value feedback does not account for the scorer's cell counts")
denominator = expected + predicted
entries = []
for cell in cells:
if cell.gt_path is None:
outcome = "prediction_only"
elif cell.pred_path is None:
outcome = "reference_only"
else:
outcome = "matched" if cell.value_correct else "value_differs"
entries.append(
{
"reference_path": cell.gt_path,
"output_path": cell.pred_path,
"outcome": outcome,
"f1_credit": 2.0 / denominator if cell.value_correct else 0.0,
}
)
return {
"schema_version": 1,
"status": "scored",
"path_space": "normalized_scoring_inputs",
"counts": {"expected": expected, "predicted": predicted, "matched": matched},
"value_incomplete": metadata["value_incomplete"],
"cells": entries,
}
def score_prediction(test_json: Path, prediction: Any) -> tuple[dict[str, float], dict[str, Any]]:
"""Use exactly the normal evaluator's normalization and metric selection."""
case = load_extract_test_case(test_json, source_file=Path("/app/document.pdf"))
now = datetime.now(UTC)
result = InferenceResult(
request=InferenceRequest(
example_id=case.test_id,
source_file_path=str(case.file_path),
product_type=ProductType.EXTRACT,
),
pipeline_name="harbor",
product_type=ProductType.EXTRACT,
raw_output={},
output=ExtractOutput(
example_id=case.test_id,
pipeline_name="harbor",
extracted_data=prediction,
field_citations=[],
),
started_at=now,
completed_at=now,
latency_in_ms=0,
)
cells: list[ValueCell] = []
bundle = ExtractEvaluator().compute_metrics(result, case, value_cells=cells)
metrics = {
metric.metric_name: metric.value
for metric in bundle.metrics
if metric.metric_name.startswith("extract_unified_value_")
}
value_f1 = next(metric for metric in bundle.metrics if metric.metric_name == "extract_unified_value_f1")
return metrics, _feedback(cells, value_f1.metadata)
def verify(test_json: Path, prediction_path: Path, log_dir: Path) -> dict[str, Any]:
"""Bad/missing submissions earn zero; broken grading raises an error.
Only submission decoding is caught. A missing reference, loader error or
scorer failure must fail the verifier instead of becoming a model score.
"""
log_dir.mkdir(parents=True, exist_ok=True)
reward_path = log_dir / "reward.txt"
for name in ("reward.txt", "metrics.json", "feedback.json"):
(log_dir / name).unlink(missing_ok=True)
try:
prediction = json.loads(prediction_path.read_text(encoding="utf-8"))
except (FileNotFoundError, UnicodeDecodeError, json.JSONDecodeError) as error:
report: dict[str, Any] = {"status": "invalid_submission", "error": str(error), "value_f1": 0.0}
else:
if not isinstance(prediction, dict):
report = {"status": "invalid_submission", "error": "Expected one JSON object", "value_f1": 0.0}
else:
metrics, feedback = score_prediction(test_json, prediction)
report = {
"status": "scored",
"value_f1": metrics["extract_unified_value_f1"],
"metrics": metrics,
}
if report["status"] == "invalid_submission":
feedback = {"schema_version": 1, "status": "invalid_submission", "error": report["error"], "cells": []}
report["perfect"] = int(report["value_f1"] == 1.0)
(log_dir / "metrics.json").write_text(json.dumps(report, indent=2, allow_nan=False) + "\n", encoding="utf-8")
(log_dir / "feedback.json").write_text(json.dumps(feedback, indent=2, allow_nan=False) + "\n", encoding="utf-8")
reward_path.write_text(f"{report['value_f1']}\n", encoding="utf-8")
return report
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--test-json", type=Path, default=Path("/tests/reference.test.json"))
parser.add_argument("--prediction", type=Path, default=Path("/app/output.json"))
parser.add_argument("--log-dir", type=Path, default=Path("/logs/verifier"))
args = parser.parse_args()
verify(args.test_json, args.prediction, args.log_dir)
if __name__ == "__main__":
main()
|