Views
No views yet

1from transformers import AutoProcessor, AutoModelForImageTextToText
2
3model = AutoModelForImageTextToText.from_pretrained(
4 "deepseek-community/DeepSeek-OCR-2", device_map="auto"
5)
6processor = AutoProcessor.from_pretrained("deepseek-community/DeepSeek-OCR-2")
7
8image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/image_ocr.jpg"
9inputs = processor(images=image, text="<image>\nFree OCR.", return_tensors="pt").to(model.device)
10
11generate_ids = model.generate(**inputs, do_sample=False, max_new_tokens=256)
12print(processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True))
13# "R&D QUALITY IMPROVEMENT\nSUGGESTION/SOLUTION FORM\nName/Phone Ext. : (...)"<|grounding|> token enables coordinate-aware output with <|ref|> and <|det|> tags.1inputs = processor(
2 images=image,
3 text="<image>\n<|grounding|>Convert the document to markdown.",
4 return_tensors="pt",
5).to(model.device)
6
7generate_ids = model.generate(**inputs, do_sample=False, max_new_tokens=256)
8print(processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=False))
9# "<|ref|>title<|/ref|><|det|>[[330, 198, 558, 230]]<|/det|>\n# R&D QUALITY (...)"1# document: "<image>\n<|grounding|>Convert the document to markdown."
2# without layouts: "<image>\nFree OCR."1@article{wei2025deepseek,
2 title={DeepSeek-OCR: Contexts Optical Compression},
3 author={Wei, Haoran and Sun, Yaofeng and Li, Yukun},
4 journal={arXiv preprint arXiv:2510.18234},
5 year={2025}
6}
7
8@article{wei2026deepseek,
9 title={DeepSeek-OCR 2: Visual Causal Flow},
10 author={Wei, Haoran and Sun, Yaofeng and Li, Yukun},
11 journal={arXiv preprint arXiv:2601.20552},
12 year={2026}
13}