Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified Download ocr-tech-mps-integration/scripts/run_paddleocr_vl.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech-mps-integration/scripts/run_paddleocr_vl.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech-mps-integration/scripts/run_paddleocr_vl.py
-
curl -L -o run_paddleocr_vl.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech-mps-integration/scripts/run_paddleocr_vl.py
3.37 kB
| #!/usr/bin/env python3 | |
| """Run PaddleOCR-VL on one image or page-bundle directory.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import tempfile | |
| from pathlib import Path | |
| from ocrtech.external_outputs import bundle_page_paths, materialize_external_markdown | |
| def build_parser() -> argparse.ArgumentParser: | |
| parser = argparse.ArgumentParser(description="Run PaddleOCR-VL and write ocrtech-compatible outputs.") | |
| parser.add_argument("--input", required=True, help="image path or directory of page images") | |
| parser.add_argument("--out", required=True, help="output directory") | |
| parser.add_argument("--pipeline-version", default="v1.6") | |
| return parser | |
| def main() -> int: | |
| args = build_parser().parse_args() | |
| input_path = Path(args.input) | |
| output_dir = Path(args.out) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| try: | |
| from paddleocr import PaddleOCRVL | |
| except ImportError as exc: | |
| raise RuntimeError("PaddleOCR-VL wrapper requires paddleocr with doc parser support installed.") from exc | |
| try: | |
| pipeline = PaddleOCRVL(pipeline_version=args.pipeline_version) | |
| except Exception as exc: | |
| raise RuntimeError( | |
| "PaddleOCR-VL pipeline initialization failed. Install PaddleX OCR extras, for example " | |
| '`pip install "paddlex[ocr]==3.7.1"`.' | |
| ) from exc | |
| page_payloads: list[dict[str, object]] = [] | |
| markdown_pages: list[str] = [] | |
| for page_path in bundle_page_paths(input_path): | |
| results = list(pipeline.predict(str(page_path))) | |
| if not results: | |
| raise RuntimeError(f"PaddleOCR-VL returned no results for {page_path}") | |
| if len(results) != 1: | |
| raise RuntimeError(f"PaddleOCR-VL returned {len(results)} results for one page: {page_path}") | |
| markdown, raw_json = _extract_result_payload(results[0]) | |
| page_payloads.append({"page_path": str(page_path), "raw_json": raw_json}) | |
| markdown_pages.append(markdown) | |
| joined_markdown = "\n\n<!-- pagebreak -->\n\n".join(markdown_pages) | |
| materialize_external_markdown( | |
| input_path, | |
| output_dir, | |
| joined_markdown, | |
| metadata={"engine": "paddleocr-vl", "pipeline_version": args.pipeline_version}, | |
| raw_payload={"pages": page_payloads}, | |
| ) | |
| return 0 | |
| def _extract_result_payload(result: object) -> tuple[str, object]: | |
| with tempfile.TemporaryDirectory(prefix="ocrtech-paddleocr-vl-") as tmp_dir_name: | |
| tmp_dir = Path(tmp_dir_name) | |
| if not hasattr(result, "save_to_markdown") or not hasattr(result, "save_to_json"): | |
| raise RuntimeError(f"unexpected PaddleOCR-VL result type: {type(result).__name__}") | |
| result.save_to_markdown(save_path=str(tmp_dir)) | |
| result.save_to_json(save_path=str(tmp_dir)) | |
| markdown_files = sorted(tmp_dir.rglob("*.md")) | |
| json_files = sorted(path for path in tmp_dir.rglob("*.json") if path.name != "document.json") | |
| if not markdown_files: | |
| raise RuntimeError("PaddleOCR-VL did not write any markdown output") | |
| markdown = markdown_files[0].read_text(encoding="utf-8").strip() | |
| raw_json: object = None | |
| if json_files: | |
| raw_json = json.loads(json_files[0].read_text(encoding="utf-8")) | |
| return markdown, raw_json | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |