Views
No views yet
deepseek-ai/DeepSeek-OCR, created using bitsandbytes. It offers significantly reduced VRAM (up to 8 Gb!) usage while maintaining high accuracy, making it ideal for consumer GPUs.uv with Python 3.12.9. This matches the test environment of the original deepseek-ai/DeepSeek-OCR model.flash-attn.bitsandbytes, accelerate) and PyTorch compatibility (torchvision).1# 1. Create and activate the environment (Python 3.12.9 recommended)
2uv venv --python 3.12.9 .venv
3
4# 2. Install PyTorch
5uv pip install torch==2.6.0 torchvision
6
7# 3. Install Transformers and dependencies
8uv pip install transformers==4.46.3 tokenizers==0.20.3 einops addict easydict
9
10# 4. Install 4-bit (bitsandbytes) and 'device_map' (accelerate) support
11uv pip install bitsandbytes accelerate
12
13# 5. Install flash-attn (compiles from source, requires CUDA Toolkit)
14uv pip install flash-attn==2.7.3 --no-build-isolation_attn_implementation='flash_attention_2': Recommended for NVIDIA Ampere (RTX 30xx, A100) or newer GPUs._attn_implementation='eager': Required for NVIDIA Turing (RTX 20xx) GPUs and older, or for general compatibility if Flash Attention 2 fails.1from transformers import AutoModel, AutoTokenizer
2import torch
3import os
4
5os.environ["CUDA_VISIBLE_DEVICES"] = '0'
6model_id = 'Jalea96/DeepSeek-OCR-bnb-4bit-NF4'
7
8tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
9model = AutoModel.from_pretrained(
10 model_id,
11 _attn_implementation='flash_attention_2',
12 trust_remote_code=True,
13 use_safetensors=True,
14 device_map="auto",
15 torch_dtype=torch.bfloat16
16)
17model = model.eval()
18
19# --- 1. Set Image and Task Prompt ---
20prompt = "<image>\n<|grounding|>Convert the document to markdown. "
21image_file = 'your_image.jpg'
22output_path = 'your/output/dir'
23
24if not os.path.exists(output_path):
25 os.makedirs(output_path)
26
27# --- 2. Set Resolution ---
28# (Gundam is recommended for most documents)
29# Tiny: base_size = 512, image_size = 512, crop_mode = False
30# Small: base_size = 640, image_size = 640, crop_mode = False
31# Base: base_size = 1024, image_size = 1024, crop_mode = False
32# Large: base_size = 1280, image_size = 1280, crop_mode = False
33# Gundam:base_size = 1024, image_size = 640, crop_mode = True
34base_size, image_size, crop_mode = 1024, 640, True
35
36# --- 3. Run Inference ---
37res = model.infer(
38 tokenizer,
39 prompt=prompt,
40 image_file=image_file,
41 output_path=output_path,
42 base_size=base_size,
43 image_size=image_size,
44 crop_mode=crop_mode,
45 save_results=True, # Set to True to save visualization (image_vis.jpg)
46 test_compress=True
47)1from transformers import AutoModel, AutoTokenizer
2import torch
3import os
4
5os.environ["CUDA_VISIBLE_DEVICES"] = '0'
6model_id = 'Jalea96/DeepSeek-OCR-bnb-4bit-NF4'
7
8tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
9model = AutoModel.from_pretrained(
10 model_id,
11 _attn_implementation='eager',
12 trust_remote_code=True,
13 use_safetensors=True,
14 device_map="auto",
15 torch_dtype=torch.bfloat16
16)
17model = model.eval()
18
19# --- 1. Set Image and Task Prompt ---
20prompt = "<image>\n<|grounding|>Convert the document to markdown. "
21image_file = 'your_image.jpg'
22output_path = 'your/output/dir'
23
24if not os.path.exists(output_path):
25 os.makedirs(output_path)
26
27# --- 2. Set Resolution ---
28# (Gundam is recommended for most documents)
29# Tiny: base_size = 512, image_size = 512, crop_mode = False
30# Small: base_size = 640, image_size = 640, crop_mode = False
31# Base: base_size = 1024, image_size = 1024, crop_mode = False
32# Large: base_size = 1280, image_size = 1280, crop_mode = False
33# Gundam:base_size = 1024, image_size = 640, crop_mode = True
34base_size, image_size, crop_mode = 1024, 640, True
35
36# --- 3. Run Inference ---
37res = model.infer(
38 tokenizer,
39 prompt=prompt,
40 image_file=image_file,
41 output_path=output_path,
42 base_size=base_size,
43 image_size=image_size,
44 crop_mode=crop_mode,
45 save_results=True, # Set to True to save visualization (image_vis.jpg)
46 test_compress=True
47)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}