Image-Text-to-Text
Transformers
Safetensors
multilingual
unlimited-ocr
feature-extraction
baidu
vision-language
ocr
custom_code
Eval Results
Instructions to use baidu/Unlimited-OCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use baidu/Unlimited-OCR with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="baidu/Unlimited-OCR", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("baidu/Unlimited-OCR", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use baidu/Unlimited-OCR with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "baidu/Unlimited-OCR" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "baidu/Unlimited-OCR", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/baidu/Unlimited-OCR
- SGLang
How to use baidu/Unlimited-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 "baidu/Unlimited-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": "baidu/Unlimited-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 "baidu/Unlimited-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": "baidu/Unlimited-OCR", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use baidu/Unlimited-OCR with Docker Model Runner:
docker model run hf.co/baidu/Unlimited-OCR
Update files for compatibility with transformers
#13
by guarin HF Staff - opened
- chat_template.jinja +15 -0
- config.json +1 -1
- generation_config.json +9 -0
- model.safetensors +3 -0
- modeling_unlimitedocr.py +2 -7
- preprocessor_config.json +20 -0
- processor_config.json +2 -2
chat_template.jinja
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- for message in messages %}
|
| 2 |
+
{%- if message['content'] is string %}
|
| 3 |
+
{{- message['content'] }}
|
| 4 |
+
{%- else %}
|
| 5 |
+
{%- for content in message['content'] | selectattr('type', 'equalto', 'image') %}
|
| 6 |
+
{{- '<image>' }}
|
| 7 |
+
{%- endfor %}
|
| 8 |
+
{%- for content in message['content'] | selectattr('type', 'equalto', 'text') %}
|
| 9 |
+
{{- content['text'] }}
|
| 10 |
+
{%- endfor %}
|
| 11 |
+
{%- endif %}
|
| 12 |
+
{%- if message['role'] == 'assistant' %}
|
| 13 |
+
{{- eos_token }}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- endfor %}
|
config.json
CHANGED
|
@@ -51,7 +51,7 @@
|
|
| 51 |
"vocab_size": 129280,
|
| 52 |
"sliding_window_size": 128
|
| 53 |
},
|
| 54 |
-
"model_type": "
|
| 55 |
"projector_config": {
|
| 56 |
"input_dim": 2048,
|
| 57 |
"model_type": "mlp_projector",
|
|
|
|
| 51 |
"vocab_size": 129280,
|
| 52 |
"sliding_window_size": 128
|
| 53 |
},
|
| 54 |
+
"model_type": "unlimited_ocr",
|
| 55 |
"projector_config": {
|
| 56 |
"input_dim": 2048,
|
| 57 |
"model_type": "mlp_projector",
|
generation_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 0,
|
| 3 |
+
"eos_token_id": 1,
|
| 4 |
+
"no_repeat_ngram_size": 35,
|
| 5 |
+
"no_repeat_ngram_window_size": 128,
|
| 6 |
+
"max_new_tokens": 32768,
|
| 7 |
+
"transformers_version": "5.14.0.dev0",
|
| 8 |
+
"use_cache": true
|
| 9 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2bc48a7a110061ea58fff65d3169367eebe3aee371ca6968dc2219c1b2855fc6
|
| 3 |
+
size 6672547120
|
modeling_unlimitedocr.py
CHANGED
|
@@ -154,7 +154,6 @@ def process_image_with_refs(image, ref_texts, output_path, image_prefix=''):
|
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
-
|
| 158 |
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 159 |
best_ratio_diff = float('inf')
|
| 160 |
best_ratio = (1, 1)
|
|
@@ -426,7 +425,7 @@ class TPSTextStreamer(TextStreamer):
|
|
| 426 |
|
| 427 |
|
| 428 |
class UnlimitedOCRConfig(DeepseekV2Config):
|
| 429 |
-
model_type = "
|
| 430 |
|
| 431 |
class UnlimitedOCRModel(DeepseekV2Model):
|
| 432 |
config_class = UnlimitedOCRConfig
|
|
@@ -879,10 +878,6 @@ class UnlimitedOCRForCausalLM(DeepseekV2ForCausalLM):
|
|
| 879 |
# elif base_size == 640:
|
| 880 |
# valid_img_tokens += int(100 * ratio)
|
| 881 |
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
images_list.append(image_transform(global_view).to(torch.bfloat16))
|
| 887 |
|
| 888 |
# global_view_tensor = image_transform(global_view).to(torch.bfloat16)
|
|
@@ -1296,4 +1291,4 @@ class UnlimitedOCRForCausalLM(DeepseekV2ForCausalLM):
|
|
| 1296 |
with open(f'{output_path}/result.md', 'w', encoding='utf-8') as afile:
|
| 1297 |
afile.write(outputs)
|
| 1298 |
|
| 1299 |
-
return outputs, output_tokens
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
|
|
|
|
| 157 |
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 158 |
best_ratio_diff = float('inf')
|
| 159 |
best_ratio = (1, 1)
|
|
|
|
| 425 |
|
| 426 |
|
| 427 |
class UnlimitedOCRConfig(DeepseekV2Config):
|
| 428 |
+
model_type = "unlimited_ocr"
|
| 429 |
|
| 430 |
class UnlimitedOCRModel(DeepseekV2Model):
|
| 431 |
config_class = UnlimitedOCRConfig
|
|
|
|
| 878 |
# elif base_size == 640:
|
| 879 |
# valid_img_tokens += int(100 * ratio)
|
| 880 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
images_list.append(image_transform(global_view).to(torch.bfloat16))
|
| 882 |
|
| 883 |
# global_view_tensor = image_transform(global_view).to(torch.bfloat16)
|
|
|
|
| 1291 |
with open(f'{output_path}/result.md', 'w', encoding='utf-8') as afile:
|
| 1292 |
afile.write(outputs)
|
| 1293 |
|
| 1294 |
+
return outputs, output_tokens
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"image_processor_type": "UnlimitedOcrImageProcessor",
|
| 3 |
+
"do_convert_rgb": true,
|
| 4 |
+
"do_resize": true,
|
| 5 |
+
"resample": 3,
|
| 6 |
+
"do_rescale": true,
|
| 7 |
+
"rescale_factor": 0.00392156862745098,
|
| 8 |
+
"do_normalize": true,
|
| 9 |
+
"crop_to_patches": true,
|
| 10 |
+
"min_patches": 2,
|
| 11 |
+
"max_patches": 32,
|
| 12 |
+
"tile_size": 640,
|
| 13 |
+
"background_color": [127, 127, 127],
|
| 14 |
+
"size": {
|
| 15 |
+
"height": 1024,
|
| 16 |
+
"width": 1024
|
| 17 |
+
},
|
| 18 |
+
"image_mean": [0.5, 0.5, 0.5],
|
| 19 |
+
"image_std": [0.5, 0.5, 0.5]
|
| 20 |
+
}
|
processor_config.json
CHANGED
|
@@ -21,8 +21,8 @@
|
|
| 21 |
"image_token": "<image>",
|
| 22 |
"mask_prompt": false,
|
| 23 |
"normalize": true,
|
| 24 |
-
"pad_token": "<
|
| 25 |
"patch_size": 16,
|
| 26 |
-
"processor_class": "
|
| 27 |
"sft_format": "unlimitedocr"
|
| 28 |
}
|
|
|
|
| 21 |
"image_token": "<image>",
|
| 22 |
"mask_prompt": false,
|
| 23 |
"normalize": true,
|
| 24 |
+
"pad_token": "<|▁pad▁|>",
|
| 25 |
"patch_size": 16,
|
| 26 |
+
"processor_class": "UnlimitedOcrProcessor",
|
| 27 |
"sft_format": "unlimitedocr"
|
| 28 |
}
|