"""Local PDF experiment. No network calls and no package installation. Run from the repository root with tools/lab-venv/bin/python. The upstream archive is preserved; this guide scores PDF inputs only. """ from pathlib import Path import hashlib import importlib.metadata import json import platform import shutil import sys import time from datetime import datetime ROOT = Path(__file__).resolve().parents[3] LAB = Path(__file__).resolve().parent SAMPLES = ["prose-single", "table-simple", "table-merged-header", "two-column", "scan-image", "mixed", "equation", "rotated-header"] def digest(path): return hashlib.sha256(path.read_bytes()).hexdigest() def main(): import pymupdf import pymupdf4llm import pdfplumber import markitdown.converters._pdf_converter as converter from markitdown import MarkItDown sources = LAB / "sources" sources.mkdir(exist_ok=True) for src, name in [ (Path(converter.__file__), "markitdown-pdf.py"), (Path(pymupdf4llm.__file__), "pymupdf4llm-init.py"), (ROOT / "tools/lab_score.py", "lab_score.py"), (ROOT / "overlays/sites/markovo/refs/developer.md", "markovo-developer.md"), (ROOT / "overlays/sites/markovo/refs/install.md", "markovo-install.md"), (ROOT / "overlays/sites/markovo/refs/seo-keyword-library.md", "markovo-capabilities.md"), ]: shutil.copyfile(src, sources / name) upstream = LAB / "upstream-runs.json" if not upstream.exists(): shutil.copyfile(LAB / "runs.json", upstream) archive = json.loads(upstream.read_text()) # Paired intervention: identical first page, one native table appended. added = "columns-plus-table" target = LAB / "corpus" / f"{added}.pdf" with pymupdf.open(LAB / "corpus/two-column.pdf") as doc: with pymupdf.open(LAB / "corpus/table-simple.pdf") as table: doc.insert_pdf(table) doc.save(target) truth = json.loads((LAB / "corpus/two-column.truth.json").read_text()) truth["scope"] = "Only unchanged column-page paragraphs are scored; appended table is an intervention, not additional truth." truth["derived_from"] = ["two-column.pdf", "table-simple.pdf"] (LAB / "corpus" / f"{added}.truth.json").write_text(json.dumps(truth, indent=2) + "\n") # A wider gutter separates the column groups for the native engine. # This is a separate fixture, not an edit of the upstream column PDF. wide = "columns-wide" wide_path = LAB / "corpus" / f"{wide}.pdf" with pymupdf.open() as doc: page = doc.new_page(width=650, height=900) for rect, paragraphs in [ (pymupdf.Rect(45, 55, 270, 845), truth["paragraphs"][:3]), (pymupdf.Rect(380, 70, 605, 860), truth["paragraphs"][3:]), ]: remaining = page.insert_textbox(rect, "\n\n".join(paragraphs), fontsize=10, lineheight=1.3) if remaining < 0: raise RuntimeError("Generated fixture text did not fit") doc.save(wide_path) wide_truth = {"kind": "columns", "paragraphs": truth["paragraphs"], "derived_from": "two-column.truth.json; new geometry, identical paragraph text"} (LAB / "corpus" / f"{wide}.truth.json").write_text(json.dumps(wide_truth, indent=2) + "\n") wide_added = "columns-wide-plus-table" with pymupdf.open(wide_path) as doc: with pymupdf.open(LAB / "corpus/table-simple.pdf") as table: doc.insert_pdf(table) doc.save(LAB / "corpus" / f"{wide_added}.pdf") (LAB / "corpus" / f"{wide_added}.truth.json").write_text(json.dumps({ **wide_truth, "scope": "Only unchanged wide-column paragraphs are scored; appended table is the intervention" }, indent=2) + "\n") names = SAMPLES + [added, wide, wide_added] results = [] for item in archive["results"]: if item["tool"] == "markovo" and item["sample"] in SAMPLES: results.append({**item, "elapsed_s": None, "provenance": "upstream cached API output; not submitted in this session"}) tools = {"markovo": {**archive["tools"]["markovo"], "elapsed_s": None, "version_note": "rate_card_version, not a software release", "provenance": "cached jobs created on 2026-10-06"}} routes = {} for name in names: with pdfplumber.open(LAB / "corpus" / f"{name}.pdf") as pdf: flags = [converter._extract_form_content_from_words(p) is not None for p in pdf.pages] routes[name] = {"form_page_flags": flags, "form_page_count": sum(flags), "document_branch": "pdfminer-whole-document" if not any(flags) else "pdfplumber-page-chunks"} md = MarkItDown(enable_plugins=False) for tool in ["markitdown", "pymupdf4llm"]: out_dir = LAB / tool out_dir.mkdir(exist_ok=True) tools[tool] = {"version": importlib.metadata.version(tool), "device": "cpu", "options": {"enable_plugins": False} if tool == "markitdown" else {"use_ocr": False, "page_chunks": True}, "elapsed_s": 0, "error": None} for name in names: path = LAB / "corpus" / f"{name}.pdf" start = time.perf_counter() error = None try: if tool == "markitdown": text = md.convert_local(str(path)).markdown else: chunks = pymupdf4llm.to_markdown(str(path), page_chunks=True, use_ocr=False) text = "\n\n".join(c["text"] for c in chunks) output = out_dir / f"{name}.md" output.write_text(text, encoding="utf-8") except Exception as exc: error = f"{type(exc).__name__}: {exc}" output = None elapsed = round(time.perf_counter() - start, 6) tools[tool]["elapsed_s"] += elapsed results.append({"tool": tool, "sample": name, "output": str(output.relative_to(LAB)) if output else None, "elapsed_s": elapsed, "error": error}) print(tool, name, "ok" if error is None else error, elapsed, flush=True) runs = {"python": platform.python_version(), "date": datetime.now().date().isoformat(), "tools": tools, "results": results} (LAB / "runs.json").write_text(json.dumps(runs, indent=2) + "\n") (LAB / "routing.json").write_text(json.dumps(routes, indent=2) + "\n") env = {"date": datetime.now().isoformat(), "python": sys.version, "packages": {p: importlib.metadata.version(p) for p in ["markitdown", "pdfplumber", "pdfminer.six", "pymupdf", "pymupdf4llm", "pymupdf-layout"]}, "input_sha256": {p.name: digest(p) for p in sorted((LAB / "corpus").glob("*.pdf"))}, "source_sha256": {p.name: digest(p) for p in sorted(sources.iterdir())}, "command": "PYTHONDONTWRITEBYTECODE=1 tools/lab-venv/bin/python runs/2026-11-08-markovo-pdf-to-text-convert/lab/run_local.py"} (LAB / "environment.json").write_text(json.dumps(env, indent=2) + "\n") if __name__ == "__main__": main()