Image-Text-to-Text
Transformers
Safetensors
qwen3_vl
ocr
document-parsing
document-ai
layout
vision-language-model
conversational
Instructions to use hanji-dev/hanji-parse-4b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use hanji-dev/hanji-parse-4b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="hanji-dev/hanji-parse-4b") 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("hanji-dev/hanji-parse-4b") model = AutoModelForMultimodalLM.from_pretrained("hanji-dev/hanji-parse-4b", 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 hanji-dev/hanji-parse-4b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "hanji-dev/hanji-parse-4b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "hanji-dev/hanji-parse-4b", "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/hanji-dev/hanji-parse-4b
- SGLang
How to use hanji-dev/hanji-parse-4b 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 "hanji-dev/hanji-parse-4b" \ --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": "hanji-dev/hanji-parse-4b", "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 "hanji-dev/hanji-parse-4b" \ --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": "hanji-dev/hanji-parse-4b", "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 hanji-dev/hanji-parse-4b with Docker Model Runner:
docker model run hf.co/hanji-dev/hanji-parse-4b
Trim model card
Browse files
README.md
CHANGED
|
@@ -22,10 +22,6 @@ content, a key-value panel, or a whole table) with a bounding box and its
|
|
| 22 |
transcribed text. Tables are transcribed as GitHub-Flavored Markdown inside a
|
| 23 |
single block. Figures, photos, and signatures are returned as image blocks.
|
| 24 |
|
| 25 |
-
It was the production parsing model behind the Hanji document-extraction API.
|
| 26 |
-
The accompanying open-source parse + schema-extraction server is available at
|
| 27 |
-
**https://github.com/youlearn-ai/hanji**.
|
| 28 |
-
|
| 29 |
## Output contract
|
| 30 |
|
| 31 |
The model returns **JSON only**: an array of records
|
|
@@ -48,10 +44,6 @@ The model returns **JSON only**: an array of records
|
|
| 48 |
|
| 49 |
## Usage — read this before running the model
|
| 50 |
|
| 51 |
-
The model is **tightly coupled to its serving contract**. It was trained and
|
| 52 |
-
served with one exact prompt, one image-preprocessing rule, and greedy
|
| 53 |
-
decoding. Deviating from any of these produces off-distribution output.
|
| 54 |
-
|
| 55 |
### 1. Image preprocessing
|
| 56 |
|
| 57 |
- Downscale so the image is at most **2,000,000 pixels** (2 MP), preserving
|
|
@@ -72,7 +64,7 @@ def preprocess(img: Image.Image) -> Image.Image:
|
|
| 72 |
return img.convert("RGB").resize((w, h), Image.LANCZOS)
|
| 73 |
```
|
| 74 |
|
| 75 |
-
### 2. The prompt
|
| 76 |
|
| 77 |
Send the page image followed by exactly this text as the user turn. Do not
|
| 78 |
paraphrase, extend, or reformat it.
|
|
@@ -202,7 +194,7 @@ model = AutoModelForImageTextToText.from_pretrained(
|
|
| 202 |
processor = AutoProcessor.from_pretrained(MODEL)
|
| 203 |
|
| 204 |
image = preprocess(Image.open("page.png")) # see preprocessing above
|
| 205 |
-
PROMPT = "..." # the exact prompt above
|
| 206 |
|
| 207 |
messages = [{
|
| 208 |
"role": "user",
|
|
@@ -220,11 +212,10 @@ out = model.generate(**inputs, max_new_tokens=8192, do_sample=False)
|
|
| 220 |
print(processor.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
|
| 221 |
```
|
| 222 |
|
| 223 |
-
### Serving (SGLang
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
repeated JSON keys draft extremely well):
|
| 228 |
|
| 229 |
```bash
|
| 230 |
python -m sglang.launch_server \
|
|
@@ -241,36 +232,9 @@ python -m sglang.launch_server \
|
|
| 241 |
```
|
| 242 |
|
| 243 |
A ready-to-run server that implements the full preprocessing + prompt contract
|
| 244 |
-
(and a
|
| 245 |
https://github.com/youlearn-ai/hanji.
|
| 246 |
|
| 247 |
-
## Training summary
|
| 248 |
-
|
| 249 |
-
- Base: `Qwen/Qwen3-VL-4B-Instruct` (Apache-2.0). Language layers fine-tuned
|
| 250 |
-
with LoRA (r=32, α=64, all attention + MLP projections); the **vision tower
|
| 251 |
-
was frozen**. This repository contains the merged full weights (bf16).
|
| 252 |
-
- 1,500 steps, effective batch 16, cosine schedule, images at up to 2 MP
|
| 253 |
-
native resolution.
|
| 254 |
-
- ~16k supervised pages spanning synthetic and real business documents:
|
| 255 |
-
forms, tables (including borderless and dense grids), checkbox and Y/N
|
| 256 |
-
grids, multilingual and RTL pages, degraded scans and faxes, receipts, and
|
| 257 |
-
court transcripts.
|
| 258 |
-
|
| 259 |
-
## Limitations
|
| 260 |
-
|
| 261 |
-
- **Prompt-coupled**: output quality degrades sharply off the exact prompt,
|
| 262 |
-
preprocessing, and greedy decoding described above.
|
| 263 |
-
- The base model was loaded from the `main` revision of
|
| 264 |
-
`Qwen/Qwen3-VL-4B-Instruct` at training time (mid-2026) without a pinned
|
| 265 |
-
commit; bit-exact re-derivation of the merge is therefore approximate.
|
| 266 |
-
- Signatures and handwritten marks are deliberately returned as `<image>`
|
| 267 |
-
blocks, never transcribed.
|
| 268 |
-
- Blocks are page-scoped; the model does not merge tables that continue
|
| 269 |
-
across pages (the serving pipeline may do so downstream).
|
| 270 |
-
- Not an instruction-following chat model: it does one task. For arbitrary
|
| 271 |
-
document Q&A, pair it with a downstream LLM (the open-source repo pairs it
|
| 272 |
-
with a schema-extraction pass).
|
| 273 |
-
|
| 274 |
## License
|
| 275 |
|
| 276 |
Apache-2.0. Fine-tuned from
|
|
|
|
| 22 |
transcribed text. Tables are transcribed as GitHub-Flavored Markdown inside a
|
| 23 |
single block. Figures, photos, and signatures are returned as image blocks.
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
## Output contract
|
| 26 |
|
| 27 |
The model returns **JSON only**: an array of records
|
|
|
|
| 44 |
|
| 45 |
## Usage — read this before running the model
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
### 1. Image preprocessing
|
| 48 |
|
| 49 |
- Downscale so the image is at most **2,000,000 pixels** (2 MP), preserving
|
|
|
|
| 64 |
return img.convert("RGB").resize((w, h), Image.LANCZOS)
|
| 65 |
```
|
| 66 |
|
| 67 |
+
### 2. The prompt
|
| 68 |
|
| 69 |
Send the page image followed by exactly this text as the user turn. Do not
|
| 70 |
paraphrase, extend, or reformat it.
|
|
|
|
| 194 |
processor = AutoProcessor.from_pretrained(MODEL)
|
| 195 |
|
| 196 |
image = preprocess(Image.open("page.png")) # see preprocessing above
|
| 197 |
+
PROMPT = "..." # the exact prompt above
|
| 198 |
|
| 199 |
messages = [{
|
| 200 |
"role": "user",
|
|
|
|
| 212 |
print(processor.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
|
| 213 |
```
|
| 214 |
|
| 215 |
+
### Serving (SGLang)
|
| 216 |
|
| 217 |
+
N-gram speculative decoding gives a large decode speedup on this output
|
| 218 |
+
format (the repeated JSON keys draft extremely well):
|
|
|
|
| 219 |
|
| 220 |
```bash
|
| 221 |
python -m sglang.launch_server \
|
|
|
|
| 232 |
```
|
| 233 |
|
| 234 |
A ready-to-run server that implements the full preprocessing + prompt contract
|
| 235 |
+
(and a schema-extraction API around it) is available at
|
| 236 |
https://github.com/youlearn-ai/hanji.
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
## License
|
| 239 |
|
| 240 |
Apache-2.0. Fine-tuned from
|