Instructions to use docling-project/SmolDocling-256M-preview with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use docling-project/SmolDocling-256M-preview with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="docling-project/SmolDocling-256M-preview") 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)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("docling-project/SmolDocling-256M-preview") model = AutoModelForMultimodalLM.from_pretrained("docling-project/SmolDocling-256M-preview", 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=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use docling-project/SmolDocling-256M-preview with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "docling-project/SmolDocling-256M-preview" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "docling-project/SmolDocling-256M-preview", "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/docling-project/SmolDocling-256M-preview
- SGLang
How to use docling-project/SmolDocling-256M-preview 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 "docling-project/SmolDocling-256M-preview" \ --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": "docling-project/SmolDocling-256M-preview", "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 "docling-project/SmolDocling-256M-preview" \ --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": "docling-project/SmolDocling-256M-preview", "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 docling-project/SmolDocling-256M-preview with Docker Model Runner:
docker model run hf.co/docling-project/SmolDocling-256M-preview
Update README.md
Browse files
README.md
CHANGED
|
@@ -47,7 +47,17 @@ SmolDocling is a multimodal Image-Text-to-Text model designed for efficient docu
|
|
| 47 |
|
| 48 |
You can use transformers or docling to perform inference:
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
```python
|
| 53 |
import torch
|
|
@@ -93,9 +103,59 @@ generated_texts = processor.batch_decode(
|
|
| 93 |
|
| 94 |
print(generated_texts[0])
|
| 95 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
## Supported Instructions
|
|
|
|
| 47 |
|
| 48 |
You can use transformers or docling to perform inference:
|
| 49 |
|
| 50 |
+
<details>
|
| 51 |
+
<summary>Inference using Docling</summary>
|
| 52 |
+
|
| 53 |
+
```python
|
| 54 |
+
|
| 55 |
+
print(generated_texts[0])
|
| 56 |
+
```
|
| 57 |
+
</details>
|
| 58 |
+
|
| 59 |
+
<details>
|
| 60 |
+
<summary>Single image inference using Tranformers</summary>
|
| 61 |
|
| 62 |
```python
|
| 63 |
import torch
|
|
|
|
| 103 |
|
| 104 |
print(generated_texts[0])
|
| 105 |
```
|
| 106 |
+
</details>
|
| 107 |
+
|
| 108 |
+
<details>
|
| 109 |
+
<summary> 🚀 Fast Batch Inference Using VLLM</summary>
|
| 110 |
+
|
| 111 |
+
```python
|
| 112 |
+
!pip install vllm
|
| 113 |
+
|
| 114 |
+
import time
|
| 115 |
+
import os
|
| 116 |
+
from vllm import LLM, SamplingParams
|
| 117 |
+
from PIL import Image
|
| 118 |
|
| 119 |
+
# Configuration
|
| 120 |
+
MODEL_PATH = "ds4sd/SmolDocling-256M-preview"
|
| 121 |
+
IMAGE_DIR = "images_dir"
|
| 122 |
+
OUTPUT_DIR = "output_pred_dir"
|
| 123 |
+
PROMPT_TEXT = "Convert page to Docling."
|
| 124 |
|
| 125 |
+
# Ensure output directory exists
|
| 126 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 127 |
+
|
| 128 |
+
# Initialize LLM
|
| 129 |
+
llm = LLM(model=MODEL_PATH, limit_mm_per_prompt={"image": 1})
|
| 130 |
+
|
| 131 |
+
sampling_params = SamplingParams(
|
| 132 |
+
temperature=0.0,
|
| 133 |
+
max_tokens=8192)
|
| 134 |
+
|
| 135 |
+
chat_template = f"<|im_start|>User:<image>{PROMPT_TEXT}<end_of_utterance>\nAssistant:"
|
| 136 |
+
|
| 137 |
+
image_files = sorted([f for f in os.listdir(IMAGE_DIR) if f.lower().endswith((".png", ".jpg", ".jpeg"))])
|
| 138 |
+
|
| 139 |
+
start_time = time.time()
|
| 140 |
+
total_tokens = 0
|
| 141 |
+
|
| 142 |
+
for idx, img_file in enumerate(image_files, 1):
|
| 143 |
+
img_path = os.path.join(IMAGE_DIR, img_file)
|
| 144 |
+
image = Image.open(img_path).convert("RGB")
|
| 145 |
+
|
| 146 |
+
llm_input = {"prompt": chat_template, "multi_modal_data": {"image": image}}
|
| 147 |
+
output = llm.generate([llm_input], sampling_params=sampling_params)[0]
|
| 148 |
+
|
| 149 |
+
output_text = output.outputs[0].text
|
| 150 |
+
output_filename = os.path.splitext(img_file)[0] + ".dt"
|
| 151 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 152 |
+
|
| 153 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 154 |
+
f.write(output_text)
|
| 155 |
+
|
| 156 |
+
print(f"Total time: {time.time() - start_time:.2f} sec")
|
| 157 |
+
```
|
| 158 |
+
</details>
|
| 159 |
|
| 160 |
|
| 161 |
## Supported Instructions
|