Loading...
Loading...
Expert skill for using DeepSeek-OCR, a vision-language model for optical character recognition with context optical compression supporting documents, PDFs, and images.
npx skill4agent add aradotso/trending-skills deepseek-ocrSkill by ara.so — Daily 2026 Skills collection.
git clone https://github.com/deepseek-ai/DeepSeek-OCR.git
cd DeepSeek-OCR
conda create -n deepseek-ocr python=3.12.9 -y
conda activate deepseek-ocr
# Install PyTorch with CUDA 11.8
pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 \
--index-url https://download.pytorch.org/whl/cu118
# Download vllm-0.8.5 whl from https://github.com/vllm-project/vllm/releases/tag/v0.8.5
pip install vllm-0.8.5+cu118-cp38-abi3-manylinux1_x86_64.whl
pip install -r requirements.txt
pip install flash-attn==2.7.3 --no-build-isolationuv venv
source .venv/bin/activate
uv pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightlydeepseek-ai/DeepSeek-OCRfrom huggingface_hub import snapshot_download
snapshot_download(repo_id="deepseek-ai/DeepSeek-OCR")from vllm import LLM, SamplingParams
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
from PIL import Image
llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor]
)
image = Image.open("document.png").convert("RGB")
prompt = "<image>\nFree OCR."
sampling_params = SamplingParams(
temperature=0.0,
max_tokens=8192,
extra_args=dict(
ngram_size=30,
window_size=90,
whitelist_token_ids={128821, 128822}, # <td>, </td> for table support
),
skip_special_tokens=False,
)
outputs = llm.generate(
[{"prompt": prompt, "multi_modal_data": {"image": image}}],
sampling_params
)
print(outputs[0].outputs[0].text)from vllm import LLM, SamplingParams
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
from PIL import Image
llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor]
)
image_paths = ["page1.png", "page2.png", "page3.png"]
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
model_input = [
{
"prompt": prompt,
"multi_modal_data": {"image": Image.open(p).convert("RGB")}
}
for p in image_paths
]
sampling_params = SamplingParams(
temperature=0.0,
max_tokens=8192,
extra_args=dict(
ngram_size=30,
window_size=90,
whitelist_token_ids={128821, 128822},
),
skip_special_tokens=False,
)
outputs = llm.generate(model_input, sampling_params)
for path, output in zip(image_paths, outputs):
print(f"=== {path} ===")
print(output.outputs[0].text)cd DeepSeek-OCR-master/DeepSeek-OCR-vllm
# Edit config.py: set INPUT_PATH, OUTPUT_PATH, model path, etc.
python run_dpsk_ocr_pdf.py # ~2500 tokens/s on A100-40Gcd DeepSeek-OCR-master/DeepSeek-OCR-vllm
python run_dpsk_ocr_eval_batch.pyimport os
import torch
from transformers import AutoModel, AutoTokenizer
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
model_name = "deepseek-ai/DeepSeek-OCR"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(
model_name,
_attn_implementation="flash_attention_2",
trust_remote_code=True,
use_safetensors=True,
)
model = model.eval().cuda().to(torch.bfloat16)
# Document to markdown
res = model.infer(
tokenizer,
prompt="<image>\n<|grounding|>Convert the document to markdown. ",
image_file="document.jpg",
output_path="./output/",
base_size=1024,
image_size=640,
crop_mode=True,
save_results=True,
test_compress=True,
)
print(res)cd DeepSeek-OCR-master/DeepSeek-OCR-hf
python run_dpsk_ocr.py| Use Case | Prompt |
|---|---|
| Document → Markdown | `<image>\n< |
| General OCR | `<image>\n< |
| Free OCR (no layout) | |
| Parse figure/chart | |
| General description | |
| Grounded REC | |
PROMPTS = {
"document_markdown": "<image>\n<|grounding|>Convert the document to markdown. ",
"ocr_image": "<image>\n<|grounding|>OCR this image. ",
"free_ocr": "<image>\nFree OCR. ",
"parse_figure": "<image>\nParse the figure. ",
"describe": "<image>\nDescribe this image in detail. ",
"rec": "<image>\nLocate <|ref|>{target}<|/ref|> in the image. ",
}| Mode | Resolution | Vision Tokens |
|---|---|---|
| Tiny | 512×512 | 64 |
| Small | 640×640 | 100 |
| Base | 1024×1024 | 256 |
| Large | 1280×1280 | 400 |
| Gundam (dynamic) | n×640×640 + 1×1024×1024 | variable |
# Transformers: control resolution via infer() params
res = model.infer(
tokenizer,
prompt=prompt,
image_file="image.jpg",
base_size=1024, # 512, 640, 1024, or 1280
image_size=640, # patch size for dynamic mode
crop_mode=True, # True = Gundam dynamic resolution
)DeepSeek-OCR-master/DeepSeek-OCR-vllm/config.py# Key config fields (example)
MODEL_PATH = "deepseek-ai/DeepSeek-OCR" # or local path
INPUT_PATH = "/data/input_images/"
OUTPUT_PATH = "/data/output/"
TENSOR_PARALLEL_SIZE = 1 # GPUs for tensor parallelism
MAX_TOKENS = 8192
TEMPERATURE = 0.0
NGRAM_SIZE = 30
WINDOW_SIZE = 90import os
from pathlib import Path
from PIL import Image
from vllm import LLM, SamplingParams
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
def batch_ocr(image_dir: str, output_dir: str, prompt: str = "<image>\nFree OCR."):
Path(output_dir).mkdir(parents=True, exist_ok=True)
llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor],
)
sampling_params = SamplingParams(
temperature=0.0,
max_tokens=8192,
extra_args=dict(ngram_size=30, window_size=90, whitelist_token_ids={128821, 128822}),
skip_special_tokens=False,
)
image_files = list(Path(image_dir).glob("*.png")) + list(Path(image_dir).glob("*.jpg"))
inputs = [
{"prompt": prompt, "multi_modal_data": {"image": Image.open(f).convert("RGB")}}
for f in image_files
]
outputs = llm.generate(inputs, sampling_params)
for img_path, output in zip(image_files, outputs):
out_file = Path(output_dir) / (img_path.stem + ".txt")
out_file.write_text(output.outputs[0].text)
print(f"Saved: {out_file}")
batch_ocr("/data/scans/", "/data/results/")import fitz # PyMuPDF
from PIL import Image
from io import BytesIO
from vllm import LLM, SamplingParams
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
def pdf_to_markdown(pdf_path: str) -> list[str]:
doc = fitz.open(pdf_path)
llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor],
)
sampling_params = SamplingParams(
temperature=0.0,
max_tokens=8192,
extra_args=dict(ngram_size=30, window_size=90, whitelist_token_ids={128821, 128822}),
skip_special_tokens=False,
)
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
inputs = []
for page in doc:
pix = page.get_pixmap(dpi=150)
img = Image.open(BytesIO(pix.tobytes("png"))).convert("RGB")
inputs.append({"prompt": prompt, "multi_modal_data": {"image": img}})
outputs = llm.generate(inputs, sampling_params)
return [o.outputs[0].text for o in outputs]
pages = pdf_to_markdown("report.pdf")
full_markdown = "\n\n---\n\n".join(pages)
print(full_markdown)import torch
from transformers import AutoModel, AutoTokenizer
model_name = "deepseek-ai/DeepSeek-OCR"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(
model_name,
_attn_implementation="flash_attention_2",
trust_remote_code=True,
use_safetensors=True,
).eval().cuda().to(torch.bfloat16)
target = "Total Amount"
prompt = f"<image>\nLocate <|ref|>{target}<|/ref|> in the image. "
res = model.infer(
tokenizer,
prompt=prompt,
image_file="invoice.jpg",
output_path="./output/",
base_size=1024,
image_size=640,
crop_mode=False,
save_results=True,
)
print(res) # Returns bounding box / location infotransformerstransformers>=4.51.1# Ensure torch is installed before flash-attn
pip install flash-attn==2.7.3 --no-build-isolationbase_size=512base_size=640crop_mode=FalseNGramPerReqLogitsProcessorLLMfrom vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
llm = LLM(..., logits_processors=[NGramPerReqLogitsProcessor])whitelist_token_ids={128821, 128822} # <td> and </td>llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
tensor_parallel_size=4, # number of GPUs
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor],
)DeepSeek-OCR-master/
├── DeepSeek-OCR-vllm/
│ ├── config.py # vLLM configuration
│ ├── run_dpsk_ocr_image.py # Single image inference
│ ├── run_dpsk_ocr_pdf.py # PDF batch inference
│ └── run_dpsk_ocr_eval_batch.py # Benchmark evaluation
└── DeepSeek-OCR-hf/
└── run_dpsk_ocr.py # HuggingFace Transformers inference