Views
No views yet

ibm-granite/granite-docling-258M using mlx-vlm version 0.3.3.
Refer to the original model card for more details on the model.1# Convert to HTML and Markdown:
2docling --to html --to md --pipeline vlm --vlm-model granite_docling "https://arxiv.org/pdf/2501.17887" # accepts files, urls or directories
3
4# Convert to HTML including layout visualization:
5docling --to html_split_page --show-layout --pipeline vlm --vlm-model granite_docling "https://arxiv.org/pdf/2501.17887"
mlx-vlm CLI, use this command:1pip install mlx_vlm
2python -m mlx_vlm.generate --model ibm-granite/granite-docling-258M-mlx --max-tokens 4096 --temperature 0.0 --prompt "Convert this page to docling." --image <path_to_image>mlx-vlm python SDK, parse the output as a DoclingDocument and export to various formats (e.g. Markdown, HTML), please refer to the code below.1# /// script
2# requires-python = ">=3.12"
3# dependencies = [
4# "docling-core",
5# "mlx-vlm",
6# "pillow",
7# "transformers",
8# ]
9# ///
10
11import webbrowser
12from pathlib import Path
13
14from docling_core.types.doc import ImageRefMode
15from docling_core.types.doc.document import DocTagsDocument, DoclingDocument
16from mlx_vlm import load, stream_generate
17from mlx_vlm.prompt_utils import apply_chat_template
18from mlx_vlm.utils import load_config
19from transformers.image_utils import load_image
20
21# Configuration
22MODEL_PATH = "ibm-granite/granite-docling-258M-mlx"
23PROMPT = "Convert this page to docling."
24SHOW_IN_BROWSER = True
25
26# Sample images (pick one...)
27# SAMPLE_IMAGE = "https://huggingface.co/ibm-granite/granite-docling-258M/resolve/main/assets/new_arxiv.png"
28# SAMPLE_IMAGE = "https://ibm.biz/docling-page-with-list"
29SAMPLE_IMAGE = "https://ibm.biz/docling-page-with-table"
30
31# Load model and processor
32print("Loading model...")
33model, processor = load(MODEL_PATH)
34config = load_config(MODEL_PATH)
35
36# Prepare input image and prompt
37print("Preparing input...")
38pil_image = load_image(SAMPLE_IMAGE)
39formatted_prompt = apply_chat_template(processor, config, PROMPT, num_images=1)
40
41# Generate DocTags output
42print("Generating DocTags...\n")
43output = ""
44for token in stream_generate(
45 model, processor, formatted_prompt, [pil_image], max_tokens=4096, verbose=False
46):
47 output += token.text
48 print(token.text, end="")
49 if "</doctag>" in token.text:
50 break
51
52print("\n\nProcessing output...")
53
54# Create DoclingDocument from generated DocTags
55doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([output], [pil_image])
56doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Sample Document")
57
58# Export to different formats
59print("\nMarkdown output:\n")
60print(doc.export_to_markdown())
61
62# Save as HTML with embedded images
63output_path = Path("./output.html")
64doc.save_as_html(output_path, image_mode=ImageRefMode.EMBEDDED)
65print(f"\nHTML saved to: {output_path}")
66
67# Open in browser
68if SHOW_IN_BROWSER:
69 webbrowser.open(f"file:///{str(output_path.resolve())}")