OvisOCR3 with compact vision weights for MLX

This Apple Silicon package stores compatible vision linear weights in 8-bit affine groups. Its model file is 533.8 MB, compared with 625.2 MB for the tested four-bit MLX package. The language tensors are byte-identical with the tested four-bit package. Use it with MLX-VLM on Apple Silicon.

Run two adjacent pages

Install the runtime and download the model into a new directory.

python -m pip install 'mlx-vlm==0.8.1' 'huggingface-hub==2.4.0'
hf download strangelab/ovisocr3-mlx-compact-vision --local-dir ovisocr3

Render two consecutive PDF pages to images. Save the following as merge_pages.py and run python merge_pages.py ovisocr3 page1.png page2.png. It reports the input page numbers and merged HTML. On pages without a table on each side, it returns an empty list. Treat generated HTML as untrusted when rendering it.

import json
from pathlib import Path
import re
import sys

from mlx_vlm import generate, load
from mlx_vlm.prompt_utils import apply_chat_template
from PIL import Image

folder = Path(sys.argv[1])
pages = [Image.open(path).convert("RGB") for path in sys.argv[2:]]
if len(pages) != 2:
    raise ValueError("Supply two adjacent page images")

model, processor = load(str(folder))
processor.image_processor.min_pixels = 448 * 448
processor.image_processor.max_pixels = 2880 * 2880

def infer(images, text):
    prompt = apply_chat_template(processor, model.config, text,
                                 num_images=len(images), enable_thinking=False)
    result = generate(model, processor, prompt, image=images,
                      max_tokens=16384, temperature=0.)
    if result.generation_tokens >= 16384:
        raise RuntimeError("The transcription hit its token limit")
    return result.text.strip()

markdown_prompt = (folder / "prompts/document_markdown.txt").read_text()
table_prompt = (folder / "prompts/crosspage_table.txt").read_text().strip()
captures = []
for i, page in enumerate(pages, 1):
    markdown = infer([page], markdown_prompt)
    markdown = re.sub(r"(?<![\w/])(?:page_\d+/)?(images/bbox_\d+_\d+_\d+_\d+\.jpg)",
                      rf"page_{i}/\1", markdown)
    if not re.search(r"<table\b.*?</table\s*>", markdown, flags=re.I | re.S):
        print("[]")
        sys.exit(0)
    captures.append(f'<INPUT_PAGE index="{i}">\n{markdown}\n</INPUT_PAGE>')

answer = infer(pages, table_prompt + "\n\nInput Markdown:\n\n" + "\n\n".join(captures))
print(json.dumps(json.loads(answer), indent=2, ensure_ascii=False))

For a reproducible example, render pages 4 and 5 of the public TenderNed clarification PDF at 150 DPI. The two page fragments belong to one table. Record 7 should contain the continued answer from page 5.

Source and scope

The weights and prompts originate from ATH-MaaS/OvisOCR3. The size baseline is arlenecc/OvisOCR3-0.8B-MLX-4bit. The tests cover two adjacent pages, including tables that continue mid-row and separate tables with similar columns. Review extracted cell ownership and content against the original when it matters.

Downloads last month
18
Safetensors
Model size
0.9B params
Tensor type
U32
ยท
BF16
ยท
MLX
Hardware compatibility
Log In to add your hardware

4-bit

Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for strangelab/ovisocr3-mlx-compact-vision

Quantized
(11)
this model