Set up the data, parsing and quality-control layer for a public benchmark on the Volve daily drilling reports (DDR) and WITSML real-time data. - Data manifest with SHA-256 hashes of the three Volve subtrees in use. - DDR XML parser into DuckDB (1,759 reports, 26 wellbores, 23,447 activities, 17 tables) with locked expected counts. - Render QC of the publisher HTML against the XML (272,544 items). - OCR client for the shared chandra vLLM service, with a page cache keyed on model, prompt hash, dpi, max_tokens and temperature, and OCR QC against the HTML on a 26-report pilot. - Run logging (scripts/runlog.sh, logs/index.csv), the P0 runner and 40 tests. - Research plan, technical overview and progress page (in Vietnamese). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
4.0 KiB
Python
80 lines
4.0 KiB
Python
"""volve_dia.ocr without the service: block parsing, the loop cut, the config and service checks and the page
|
|
cache."""
|
|
|
|
import dataclasses
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from volve_dia import ocr
|
|
|
|
CFG = dataclasses.replace(ocr.load_config(), loop_chars=60, loop_max_unit=10)
|
|
|
|
PAGE = ('<div data-bbox="10 20 990 60" data-label="Page-Header"><p>Summary report</p></div>'
|
|
'<div data-bbox="10 70 500 90" data-label="Section-Header"><p><b>Operations</b></p></div>'
|
|
'<div data-bbox="0 0 0 0" data-label="Picture"></div>'
|
|
'<div data-bbox="10 100 990 400" data-label="Table"><table><tr><th>Start time</th><th>Remark</th></tr>'
|
|
'<tr><td>00:00</td><td>MÆRSK & co<br>line 2</td></tr></table></div>'
|
|
'<div data-bbox="a b c d" data-label="Weird"><p>kept as paragraph</p></div>')
|
|
|
|
|
|
def test_html_blocks():
|
|
blocks = ocr.html_blocks(PAGE, 3)
|
|
assert [b.label for b in blocks] == ["page_header", "section_header", "table", "paragraph"] # empty block dropped
|
|
assert [b.raw_label for b in blocks] == ["Page-Header", "Section-Header", "Table", "Weird"]
|
|
assert [b.reading_order for b in blocks] == [0, 1, 2, 3] and {b.page for b in blocks} == {3}
|
|
assert blocks[0].bbox == [10.0, 20.0, 990.0, 60.0] and blocks[3].bbox == []
|
|
assert blocks[2].text == "Start time | Remark\n00:00 | MÆRSK & co\nline 2"
|
|
assert blocks[2].html.startswith("<table>") and blocks[1].html == "<p><b>Operations</b></p>"
|
|
|
|
|
|
def test_bbox():
|
|
assert ocr.bbox(["1", "2", "3", "4.06"]) == [1.0, 2.0, 3.0, 4.1]
|
|
assert ocr.bbox({"xyxy_normalized_1000": [1, 2, 3, 4]}) == [1.0, 2.0, 3.0, 4.0]
|
|
assert ocr.bbox([1, 2, 3]) == [] and ocr.bbox(None) == []
|
|
|
|
|
|
def test_loop_start_cuts_a_trailing_repetition():
|
|
head = "Real text. "
|
|
assert ocr.loop_start(head + "<tr></tr>" * 20, CFG) == len(head)
|
|
# the cut goes back to where the repetition starts, here the head's own closing tag
|
|
assert ocr.loop_start("<td>real</td></tr>" + "<tr></tr>" * 20, CFG) == len("<td>real</td>")
|
|
assert ocr.loop_start("no loop here, just text " * 2, CFG) is None # shorter than loop_chars
|
|
assert ocr.loop_start("".join(chr(65 + i % 26) + str(i) for i in range(40)), CFG) is None
|
|
|
|
|
|
def test_prompt_hash_is_checked(tmp_path):
|
|
prompt = tmp_path / "prompt.txt"
|
|
prompt.write_text("a different prompt")
|
|
cfg = tmp_path / "ocr.yaml"
|
|
cfg.write_text(ocr.CONFIG.read_text().replace("prompt_file: configs/ocr-prompt.txt",
|
|
f"prompt_file: {prompt}"))
|
|
with pytest.raises(ocr.OCRError, match="prompt changed"):
|
|
ocr.load_config(cfg)
|
|
|
|
|
|
def test_cache_is_keyed_by_settings(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("VOLVE_DIA_OUTPUTS", str(tmp_path))
|
|
rec = {"doc_sha256": "ab" * 32, "page": 2, **CFG.key(), "status": "ok", "content": PAGE}
|
|
ocr._write(rec)
|
|
assert ocr.page_file(rec["doc_sha256"], 2).is_file()
|
|
assert ocr.cached(rec["doc_sha256"], 2, CFG)["content"] == PAGE
|
|
assert ocr.cached(rec["doc_sha256"], 1, CFG) is None
|
|
assert ocr.cached(rec["doc_sha256"], 2, dataclasses.replace(CFG, dpi=150)) is None
|
|
blocks, records = ocr.load_blocks(rec["doc_sha256"], 2, CFG)
|
|
assert [r["status"] for r in records] == ["missing", "ok"] and len(blocks) == 4 and blocks[0].page == 2
|
|
|
|
|
|
def test_check_service_refuses_a_service_that_is_down_or_serves_another_model():
|
|
def client(status, body):
|
|
return httpx.Client(base_url="http://ocr.invalid", transport=httpx.MockTransport(
|
|
lambda request: httpx.Response(status, **body)))
|
|
|
|
ocr.check_service(client(200, {"json": {"data": [{"id": "chandra"}]}}), CFG)
|
|
for status, body, message in [(503, {"text": "<html>Pod not running</html>"}, "HTTP 503"),
|
|
(200, {"json": {"data": [{"id": "other"}]}}, "does not serve 'chandra'"),
|
|
(200, {"text": "<html>login</html>"}, "did not answer JSON")]:
|
|
with pytest.raises(ocr.OCRError, match=message) as err:
|
|
ocr.check_service(client(status, body), CFG)
|
|
assert "ocr.invalid" not in str(err.value)
|