Views
No views yet

torch==2.6.0
transformers==4.46.3
tokenizers==0.20.3
einops
addict
easydict
pip install flash-attn==2.7.3 --no-build-isolation1from transformers import AutoModel, AutoTokenizer
2import torch
3import os
4os.environ["CUDA_VISIBLE_DEVICES"] = '0'
5model_name = 'deepseek-ai/DeepSeek-OCR'
6
7tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8model = AutoModel.from_pretrained(model_name, _attn_implementation='flash_attention_2', trust_remote_code=True, use_safetensors=True)
9model = model.eval().cuda().to(torch.bfloat16)
10
11# prompt = "<image>\nFree OCR. "
12prompt = "<image>\n<|grounding|>Convert the document to markdown. "
13image_file = 'your_image.jpg'
14output_path = 'your/output/dir'
15
16# infer(self, tokenizer, prompt='', image_file='', output_path = ' ', base_size = 1024, image_size = 640, crop_mode = True, test_compress = False, save_results = False):
17
18# Tiny: base_size = 512, image_size = 512, crop_mode = False
19# Small: base_size = 640, image_size = 640, crop_mode = False
20# Base: base_size = 1024, image_size = 1024, crop_mode = False
21# Large: base_size = 1280, image_size = 1280, crop_mode = False
22
23# Gundam: base_size = 1024, image_size = 640, crop_mode = True
24
25res = model.infer(tokenizer, prompt=prompt, image_file=image_file, output_path = output_path, base_size = 1024, image_size = 640, crop_mode=True, save_results = True, test_compress = True)1uv venv
2source .venv/bin/activate
3# Until v0.11.1 release, you need to install vLLM from nightly build
4uv pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightly1from vllm import LLM, SamplingParams
2from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
3from PIL import Image
4
5# Create model instance
6llm = LLM(
7 model="deepseek-ai/DeepSeek-OCR",
8 enable_prefix_caching=False,
9 mm_processor_cache_gb=0,
10 logits_processors=[NGramPerReqLogitsProcessor]
11)
12
13# Prepare batched input with your image file
14image_1 = Image.open("path/to/your/image_1.png").convert("RGB")
15image_2 = Image.open("path/to/your/image_2.png").convert("RGB")
16prompt = "<image>\nFree OCR."
17
18model_input = [
19 {
20 "prompt": prompt,
21 "multi_modal_data": {"image": image_1}
22 },
23 {
24 "prompt": prompt,
25 "multi_modal_data": {"image": image_2}
26 }
27]
28
29sampling_param = SamplingParams(
30 temperature=0.0,
31 max_tokens=8192,
32 # ngram logit processor args
33 extra_args=dict(
34 ngram_size=30,
35 window_size=90,
36 whitelist_token_ids={128821, 128822}, # whitelist: <td>, </td>
37 ),
38 skip_special_tokens=False,
39 )
40# Generate output
41model_outputs = llm.generate(model_input, sampling_param)
42
43# Print output
44for output in model_outputs:
45 print(output.outputs[0].text)![]() | ![]() |
![]() | ![]() |
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}