Technology

DeepSeek OCR

DeepSeek-OCR: Contexts Optical Compression git clone https://github.com/deepseek-ai/DeepSeek-OCR.git conda create -n deepseek-ocr python=3.12.9 -y conda activate deepseek-ocr pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu118 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-isolation vLLM-Inference cd DeepSeek-OCR-master/DeepSeek-OCR-vllm python run_dpsk_ocr_image.py python run_dpsk_ocr_pdf.py python run_dpsk_ocr_eval_batch.py Transformers-Inference from transformers import AutoModel, AutoTokenizer import torch 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) # prompt = "\nFree OCR. " prompt = "\nConvert the document to markdown. " image_file = 'your_image.jpg' output_path = 'your/output/dir' res = 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) cd DeepSeek-OCR-master/DeepSeek-OCR-hf python run_dpsk_ocr.py Support-Modes Prompts examples # document: \nConvert the document to markdown. # other image: \nOCR this image. # without layouts: \nFree OCR. # figures in document: \nParse the figure. # general: \nDescribe this image in detail. # rec: \nLocate xxxx in the image. # '先天下之忧而忧' Visualizations Acknowledgement

DeepSeek OCR

DeepSeek-OCR: Contexts Optical Compression

git clone https://github.com/deepseek-ai/DeepSeek-OCR.git

conda create -n deepseek-ocr python=3.12.9 -y
conda activate deepseek-ocr

pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu118
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-isolation

vLLM-Inference

cd DeepSeek-OCR-master/DeepSeek-OCR-vllm

python run_dpsk_ocr_image.py

python run_dpsk_ocr_pdf.py

python run_dpsk_ocr_eval_batch.py

Transformers-Inference

from transformers import AutoModel, AutoTokenizer
import torch

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)

# prompt = "\nFree OCR. "
prompt = "\nConvert the document to markdown. "
image_file = 'your_image.jpg'
output_path = 'your/output/dir'

res = 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)

cd DeepSeek-OCR-master/DeepSeek-OCR-hf
python run_dpsk_ocr.py

Support-Modes

Prompts examples

# document: \nConvert the document to markdown.
# other image: \nOCR this image.
# without layouts: \nFree OCR.
# figures in document: \nParse the figure.
# general: \nDescribe this image in detail.
# rec: \nLocate xxxx in the image.
# '先天下之忧而忧'

Visualizations

Acknowledgement

Related Articles