Image-Text-to-Text
Transformers
Safetensors
English
idefics3
text-generation
documents
code
formula
chart
ocr
layout
table
document-parse
docling
granite
extraction
math
conversational
Instructions to use ibm-granite/granite-docling-258M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-docling-258M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="ibm-granite/granite-docling-258M") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("ibm-granite/granite-docling-258M") model = AutoModelForMultimodalLM.from_pretrained("ibm-granite/granite-docling-258M", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ibm-granite/granite-docling-258M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ibm-granite/granite-docling-258M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-docling-258M", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/ibm-granite/granite-docling-258M
- SGLang
How to use ibm-granite/granite-docling-258M with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ibm-granite/granite-docling-258M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-docling-258M", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ibm-granite/granite-docling-258M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-docling-258M", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use ibm-granite/granite-docling-258M with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-docling-258M
Update README.md
Browse files
README.md
CHANGED
|
@@ -101,24 +101,25 @@ You can use **transformers**, **vllm**, or **onnx** to perform inference, and [D
|
|
| 101 |
# pip install torch
|
| 102 |
# pip install docling_core
|
| 103 |
# pip install transformers
|
|
|
|
| 104 |
|
| 105 |
import torch
|
| 106 |
from docling_core.types.doc import DoclingDocument
|
| 107 |
from docling_core.types.doc.document import DocTagsDocument
|
| 108 |
-
from transformers import AutoProcessor,
|
| 109 |
from transformers.image_utils import load_image
|
| 110 |
from pathlib import Path
|
| 111 |
|
| 112 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 113 |
|
| 114 |
# Load images
|
| 115 |
-
image = load_image("https://
|
| 116 |
|
| 117 |
# Initialize processor and model
|
| 118 |
processor = AutoProcessor.from_pretrained("ibm-granite/granite-docling-258M")
|
| 119 |
-
model =
|
| 120 |
"ibm-granite/granite-docling-258M",
|
| 121 |
-
|
| 122 |
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "sdpa",
|
| 123 |
).to(DEVICE)
|
| 124 |
|
|
@@ -128,7 +129,7 @@ messages = [
|
|
| 128 |
"role": "user",
|
| 129 |
"content": [
|
| 130 |
{"type": "image"},
|
| 131 |
-
{"type": "text", "text": "Convert
|
| 132 |
]
|
| 133 |
},
|
| 134 |
]
|
|
|
|
| 101 |
# pip install torch
|
| 102 |
# pip install docling_core
|
| 103 |
# pip install transformers
|
| 104 |
+
# pip install flash_attn --no-build-isolation # on supported Nvidia GPUs
|
| 105 |
|
| 106 |
import torch
|
| 107 |
from docling_core.types.doc import DoclingDocument
|
| 108 |
from docling_core.types.doc.document import DocTagsDocument
|
| 109 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 110 |
from transformers.image_utils import load_image
|
| 111 |
from pathlib import Path
|
| 112 |
|
| 113 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 114 |
|
| 115 |
# Load images
|
| 116 |
+
image = load_image("https://huggingface.co/ibm-granite/granite-docling-258M/resolve/main/assets/new_arxiv.png")
|
| 117 |
|
| 118 |
# Initialize processor and model
|
| 119 |
processor = AutoProcessor.from_pretrained("ibm-granite/granite-docling-258M")
|
| 120 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 121 |
"ibm-granite/granite-docling-258M",
|
| 122 |
+
dtype=torch.bfloat16,
|
| 123 |
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "sdpa",
|
| 124 |
).to(DEVICE)
|
| 125 |
|
|
|
|
| 129 |
"role": "user",
|
| 130 |
"content": [
|
| 131 |
{"type": "image"},
|
| 132 |
+
{"type": "text", "text": "Convert page to docling."}
|
| 133 |
]
|
| 134 |
},
|
| 135 |
]
|