# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. """Layout boxes for OCRia-1 answers (experimental). OCRia-1 writes markdown without boxes. This package places that markdown on the page: 1. A layout detector (Docling's Heron, Apache-2.0, downloaded from its own repository) finds the page's regions. 2. Every word of the answer gets a position: its PDF word box when the page has a text layer, else the ink its attention points at, read from a second, teacher-forced pass of the same weights over the page and the answer. 3. Each region becomes an item holding the answer's words that fall in it. from ocria_grounding import load, load_detector, transcribe, ground_layout, render_pdf_page g, det = load(), load_detector() # OCRia-1 (CUDA) and Heron (cuda, else mps, else cpu) image, words = render_pdf_page("doc.pdf", 0) # 300-DPI page + its text-layer words (omit for image inputs) answer = transcribe(g, image) # or the raw answer from any other server (vLLM, ...) page = ground_layout(g, image, answer, detector=det, words=words) # {width, height, items: [{type, label, md, box: [x0, y0, x1, y1], start}]}, boxes in pixels of `image` Pass exactly the image and system prompt the answer was generated with. `to_layout_items_page` serializes the result as a ParseBench layout page. Without a detector, `ground_layout` (and the older `ground_page`) fall back to boxes from attention alone, which are much less accurate. """ from __future__ import annotations from pathlib import Path from .detector import DETECTOR, Detector from .ground import ground_page, to_box_markdown, to_layout_page from .heads import heads_for from .layout import ground_layout, to_layout_items_page from .pdf import render_pdf_page MODEL = "llamaindex/OCRia-1" __all__ = ["load_fast_layout", "MODEL", "DETECTOR", "load", "load_detector", "transcribe", "ground_layout", "to_layout_items_page", "render_pdf_page", "Detector", "ground_page", "to_box_markdown", "to_layout_page", "heads_for"] def load_detector(model: str = DETECTOR, device: str | None = None) -> Detector: """Docling's Heron layout detector (Apache-2.0, a separate download from its own repository).""" return Detector(model, device) def load(model: str = MODEL, device: str = "cuda", heads=None, system_prompt: str | None = None): """A Grounder for OCRia-1: `model` is the repo id or a local copy of it; the heads and the system prompt default to the ones shipped in this repository (prompt.txt).""" from .capture import Grounder if system_prompt is None: local = Path(__file__).resolve().parent.parent / "prompt.txt" if not local.exists(): from huggingface_hub import hf_hub_download local = Path(hf_hub_download(MODEL, "prompt.txt")) system_prompt = local.read_text() return Grounder(model, heads or heads_for(MODEL), system_prompt, device) def transcribe(grounder, image, max_new_tokens: int = 24576) -> str: """OCRia-1's raw answer for one page image (greedy), from the grounder's own model, so one model load serves both the transcription and the boxes. For throughput, transcribe with vLLM instead and pass its raw answer.""" import torch enc = grounder.proc(text=[grounder.prompt], images=[image.convert("RGB")], return_tensors="pt").to(grounder.device) with torch.no_grad(): ids = grounder.model.generate(**enc, max_new_tokens=max_new_tokens, do_sample=False) return grounder.proc.decode(ids[0, enc["input_ids"].shape[1]:], skip_special_tokens=True) def load_fast_layout(model_dir: str | Path | None = None): """Load the five trusted frozen layout artifacts once, without GPU or downloads.""" from parse_rl_utils.ocria_attention_grounding.api import FastLayout directory = Path(model_dir) if model_dir else Path(__file__).resolve().parent.parent / "fast_layout_models" return FastLayout(**{key: directory / f"{key}.joblib" for key in ("words", "pictures", "regions", "tables", "peaks")})