Run this model with HF Jobs

Run OpenTensorLab/OCRia-1-W4A16-AutoRound on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs and TRL to fine-tune https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Evaluate on a benchmark

Use HF Jobs to evaluate https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
File size: 4,246 Bytes
90d9faa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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")})