Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified Download ocr-tech/tests/test_audit_limbu_book_missing_line_review_queue.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_limbu_book_missing_line_review_queue.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tests/test_audit_limbu_book_missing_line_review_queue.py
-
curl -L -o test_audit_limbu_book_missing_line_review_queue.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_limbu_book_missing_line_review_queue.py
5.93 kB
| from __future__ import annotations | |
| import csv | |
| import importlib.util | |
| import json | |
| import sys | |
| from argparse import Namespace | |
| from pathlib import Path | |
| from PIL import Image | |
| def _load_tool(name: str): | |
| path = Path(__file__).resolve().parents[1] / "tools" / f"{name}.py" | |
| spec = importlib.util.spec_from_file_location(name, path) | |
| assert spec is not None | |
| assert spec.loader is not None | |
| module = importlib.util.module_from_spec(spec) | |
| sys.modules[spec.name] = module | |
| spec.loader.exec_module(module) | |
| return module | |
| def _write_candidate_tsv(path: Path, crop: Path) -> None: | |
| fieldnames = [ | |
| "priority_rank", | |
| "review_priority", | |
| "review_category", | |
| "suggested_review_action", | |
| "row_id", | |
| "sample_id", | |
| "candidate_line_id", | |
| "candidate_structural_role", | |
| "candidate_confidence", | |
| "max_selected_iou", | |
| "candidate_bbox", | |
| "candidate_text", | |
| "best_selected_text", | |
| "crop_path", | |
| "review_status", | |
| "corrected_text", | |
| "reviewer", | |
| "notes", | |
| ] | |
| with path.open("w", encoding="utf-8", newline="") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t", lineterminator="\n") | |
| writer.writeheader() | |
| writer.writerow( | |
| { | |
| "priority_rank": "1", | |
| "review_priority": "high", | |
| "review_category": "body_line_candidate", | |
| "suggested_review_action": "transcribe_visible_line_or_reject", | |
| "row_id": "candidate-only-0001-p001", | |
| "sample_id": "p001", | |
| "candidate_line_id": "img-line-0011", | |
| "candidate_structural_role": "text", | |
| "candidate_confidence": "0.47", | |
| "max_selected_iou": "0.31", | |
| "candidate_bbox": "[10,20,100,30]", | |
| "candidate_text": "क्ङ्बा परक्", | |
| "best_selected_text": "nearby", | |
| "crop_path": str(crop), | |
| "review_status": "pending", | |
| "corrected_text": "", | |
| "reviewer": "", | |
| "notes": "", | |
| } | |
| ) | |
| def _write_queue(tmp_path: Path) -> Path: | |
| prepare = _load_tool("prepare_limbu_book_missing_line_review_queue") | |
| book = tmp_path / "book" | |
| book.mkdir() | |
| (book / "book-run.json").write_text( | |
| json.dumps( | |
| { | |
| "pages": [ | |
| { | |
| "page_number": 1, | |
| "page_id": "p001", | |
| "image_path": str(tmp_path / "page.png"), | |
| "output_dir": str(book / "pages" / "p001"), | |
| "book_document_dir": str(book / "line-assembled-documents" / "p001"), | |
| } | |
| ] | |
| }, | |
| ensure_ascii=False, | |
| ) | |
| + "\n", | |
| encoding="utf-8", | |
| ) | |
| page = tmp_path / "page.png" | |
| Image.new("RGB", (180, 120), "white").save(page) | |
| crop = tmp_path / "crop.png" | |
| Image.new("RGB", (40, 20), "white").save(crop) | |
| candidate_tsv = tmp_path / "candidate.tsv" | |
| _write_candidate_tsv(candidate_tsv, crop) | |
| out = tmp_path / "queue" | |
| prepare.prepare_queue( | |
| Namespace( | |
| book_output_dir=book, | |
| candidate_review_file=candidate_tsv, | |
| out=out, | |
| pending_only=True, | |
| ) | |
| ) | |
| return out | |
| def test_audit_limbu_book_missing_line_review_queue_passes_clean_artifact(tmp_path: Path) -> None: | |
| audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue") | |
| queue = _write_queue(tmp_path) | |
| audit = audit_tool.audit_queue(queue) | |
| assert audit["audit_passed"] is True | |
| assert audit["operational_review_queue_safe"] is True | |
| assert audit["row_count"] == 1 | |
| assert audit["pending_count"] == 1 | |
| assert audit["accepted_count"] == 0 | |
| assert audit["page_context_count"] == 1 | |
| assert audit["category_counts"] == {"body_line_candidate": 1} | |
| assert (queue / "book-missing-line-review-dashboard.html").exists() | |
| assert (queue / "book-missing-line-review-queue-audit.json").exists() | |
| def test_audit_limbu_book_missing_line_review_queue_rejects_tampered_crop_hash(tmp_path: Path) -> None: | |
| audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue") | |
| queue = _write_queue(tmp_path) | |
| tsv = queue / "book-missing-line-review-queue.tsv" | |
| with tsv.open("r", encoding="utf-8", newline="") as handle: | |
| reader = csv.DictReader(handle, delimiter="\t") | |
| fieldnames = reader.fieldnames | |
| rows = list(reader) | |
| assert fieldnames is not None | |
| rows[0]["crop_sha256"] = "0" * 64 | |
| with tsv.open("w", encoding="utf-8", newline="") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t", lineterminator="\n") | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| audit = audit_tool.audit_queue(queue, tmp_path / "audit-tampered") | |
| assert audit["audit_passed"] is False | |
| assert any("review TSV SHA-256 mismatch" in issue for issue in audit["issues"]) | |
| assert any("crop SHA-256 mismatch" in issue for issue in audit["issues"]) | |
| def test_audit_limbu_book_missing_line_review_queue_rejects_tampered_dashboard_hash(tmp_path: Path) -> None: | |
| audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue") | |
| queue = _write_queue(tmp_path) | |
| report_path = queue / "book-missing-line-review-queue.json" | |
| report = json.loads(report_path.read_text(encoding="utf-8")) | |
| report["review_dashboard_sha256"] = "0" * 64 | |
| report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") | |
| audit = audit_tool.audit_queue(queue, tmp_path / "audit-dashboard-tampered") | |
| assert audit["audit_passed"] is False | |
| assert any("review dashboard SHA-256 mismatch" in issue for issue in audit["issues"]) | |