Add 195 files (runs/hf-recognizer-mixed-v2-full/finalized/package/artifacts/model/model.safetensors ...)
1ef1de7 verified Download tests/test_table_analysis.py from prosperious/ocr-tech-k2: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/prosperious/ocr-tech-k2/resolve/main/tests/test_table_analysis.py
- Command line
-
hf download hf://prosperious/ocr-tech-k2/tests/test_table_analysis.py
-
curl -L -o test_table_analysis.py https://huggingface.co/prosperious/ocr-tech-k2/resolve/main/tests/test_table_analysis.py
4.43 kB
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from ocrtech.manifest import ManifestEntry, sha256_file, sha256_text, write_manifest | |
| from ocrtech.schemas import BBox, Document, Page, Table, TableCell | |
| from ocrtech.table_analysis import analyze_table_cells | |
| def test_analyze_table_cells_reads_benchmark_documents(tmp_path: Path) -> None: | |
| image = tmp_path / "sample.png" | |
| image.write_bytes(b"image") | |
| reference = tmp_path / "reference.json" | |
| reference.write_text( | |
| json.dumps({"text": "ignored", "tables": [[["नाम", "रकम"], ["राम", "१२"]]]}, ensure_ascii=False) + "\n", | |
| encoding="utf-8", | |
| ) | |
| text_hash = sha256_text("ignored") | |
| manifest = tmp_path / "eval.jsonl" | |
| write_manifest( | |
| [ | |
| ManifestEntry( | |
| sample_id="sample-1", | |
| dataset="fixture", | |
| split="eval", | |
| image_path=str(image), | |
| text="ignored", | |
| sha256=sha256_file(image), | |
| metadata={"reference_path": str(reference), "text_sha256": text_hash}, | |
| ) | |
| ], | |
| manifest, | |
| ) | |
| benchmark_dir = tmp_path / "benchmark" | |
| rows = [] | |
| for system, grid in { | |
| "candidate": [["नाम", "रकम"], ["राम", ""]], | |
| "surya": [["नाम", "रकम"], ["श्याम", "१२"]], | |
| "tesseract": [["नाम", "रकम"], ["राम", "१२"]], | |
| }.items(): | |
| output_dir = benchmark_dir / system / image.stem | |
| output_dir.mkdir(parents=True) | |
| document = _document(image, grid, duplicate_table=system == "surya") | |
| (output_dir / "document.json").write_text(json.dumps(document.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8") | |
| rows.append( | |
| { | |
| "baseline": system, | |
| "input_path": str(image), | |
| "status": "ok", | |
| "latency_seconds": 0.1, | |
| "sample_id": "sample-1", | |
| "slices": ["table"], | |
| "metrics": {}, | |
| } | |
| ) | |
| report = benchmark_dir / "report.json" | |
| report.write_text(json.dumps(rows, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") | |
| analysis = analyze_table_cells(report, manifest, tmp_path / "analysis", candidate="candidate", baselines=["surya", "tesseract"]) | |
| assert analysis.systems["candidate"].reference_cells == 4 | |
| assert analysis.systems["candidate"].reference_tables == 1 | |
| assert analysis.systems["candidate"].predicted_tables == 1 | |
| assert analysis.systems["candidate"].exact_cells == 3 | |
| assert analysis.systems["candidate"].missing_cells == 1 | |
| assert analysis.systems["surya"].predicted_tables == 2 | |
| assert analysis.systems["surya"].duplicate_overlapping_tables == 1 | |
| assert analysis.systems["tesseract"].exact_cells == 4 | |
| assert analysis.paired["surya"].candidate_better == 1 | |
| assert analysis.paired["surya"].baseline_better == 1 | |
| assert analysis.paired["tesseract"].baseline_exact_candidate_not == 1 | |
| candidate_sample = next(item for item in analysis.samples if item.system == "candidate") | |
| assert candidate_sample.reference_shapes == [[2, 2]] | |
| assert candidate_sample.predicted_shapes == [[2, 2]] | |
| assert candidate_sample.exact_cells == 3 | |
| surya_sample = next(item for item in analysis.samples if item.system == "surya") | |
| assert surya_sample.predicted_tables == 2 | |
| assert surya_sample.duplicate_overlapping_tables == 1 | |
| assert (tmp_path / "analysis" / "table-cell-analysis.md").exists() | |
| def _document(image: Path, grid: list[list[str]], *, duplicate_table: bool = False) -> Document: | |
| cells = [ | |
| TableCell(row=row_index, col=col_index, text=value, bbox=BBox(col_index * 10, row_index * 10, 10, 10)) | |
| for row_index, row in enumerate(grid) | |
| for col_index, value in enumerate(row) | |
| ] | |
| tables = [Table(table_id="p0-t0", page_index=0, bbox=BBox(0, 0, 20, 20), cells=cells)] | |
| if duplicate_table: | |
| tables.append( | |
| Table( | |
| table_id="p0-t1", | |
| page_index=0, | |
| bbox=BBox(0, 0, 20, 20), | |
| cells=[TableCell(row=0, col=0, text="".join(value for row in grid for value in row), bbox=BBox(0, 0, 20, 20))], | |
| ) | |
| ) | |
| return Document( | |
| source_path=str(image), | |
| pages=[Page(page_index=0)], | |
| tables=tables, | |
| ) | |