Instructions to use deepseek-ai/DeepSeek-OCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use deepseek-ai/DeepSeek-OCR with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="deepseek-ai/DeepSeek-OCR", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("deepseek-ai/DeepSeek-OCR", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use deepseek-ai/DeepSeek-OCR with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "deepseek-ai/DeepSeek-OCR" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-OCR", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/deepseek-ai/DeepSeek-OCR
- SGLang
How to use deepseek-ai/DeepSeek-OCR with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "deepseek-ai/DeepSeek-OCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-OCR", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "deepseek-ai/DeepSeek-OCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-OCR", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use deepseek-ai/DeepSeek-OCR with Docker Model Runner:
docker model run hf.co/deepseek-ai/DeepSeek-OCR
Compare images based on scoring
I tried comparing output tokens with Cosine Similarity, however the results were random.
for path, img in images:
emb = encoder.encode_image(img, base_size=base_size, device=device)
embeddings.append(emb)
print(f" {path}: shape {emb.shape}")
print()
# Compute pairwise similarities
n = len(images)
similarity_matrix = np.zeros((n, n))
for i in range(n):
for j in range(n):
if i == j:
similarity_matrix[i, j] = 1.0 if similarity_method == 'cosine' else 0.0
elif i < j:
sim = compute_similarity(
embeddings[i],
embeddings[j],
method=similarity_method
)
similarity_matrix[i, j] = sim
similarity_matrix[j, i] = sim
Is is possible to use the output of the encoder and use it for categorization tasks based on similarity scores?
How do you create your encoder? From code and architecture, we should be using stuff in deepencoder. It is made up of build_sam_vit_b, build_clip_l, MlpProjector. Where MlpProjector is the one that project the features that we want to use for similarity comparison.
in file: modeling_deepseekocr.py
from .deepencoder import build_sam_vit_b, build_clip_l, MlpProjector
...
global_local_features = torch.cat([local_features, global_features, self.view_seperator[None, :]], dim=0)
You should be using global_local_features for images similarity comparision.
NOTE: I didn't test this myself do let me know if i am wrong.
