Image-Text-to-Text
Transformers
Safetensors
multilingual
minicpmv
feature-extraction
minicpm-v
vision
ocr
multi-image
video
custom_code
conversational
Eval Results
Instructions to use openbmb/MiniCPM-V-4_5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openbmb/MiniCPM-V-4_5 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="openbmb/MiniCPM-V-4_5", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("openbmb/MiniCPM-V-4_5", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use openbmb/MiniCPM-V-4_5 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "openbmb/MiniCPM-V-4_5" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openbmb/MiniCPM-V-4_5", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/openbmb/MiniCPM-V-4_5
- SGLang
How to use openbmb/MiniCPM-V-4_5 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 "openbmb/MiniCPM-V-4_5" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openbmb/MiniCPM-V-4_5", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "openbmb/MiniCPM-V-4_5" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openbmb/MiniCPM-V-4_5", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use openbmb/MiniCPM-V-4_5 with Docker Model Runner:
docker model run hf.co/openbmb/MiniCPM-V-4_5
Add native Transformers support (config and processor files only)
#23
by LucaMotz - opened
- chat_template.json +3 -0
- config.json +32 -6
- configuration_minicpm.py +1 -0
- preprocessor_config.json +29 -5
- processing_minicpmv.py +1 -1
- processor_config.json +18 -0
- tokenizer_config.json +2 -1
chat_template.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"chat_template": "{%- macro render_content(content, ns) -%}{%- if content is string -%}{{- content -}}{%- else -%}{%- set parts = namespace(items=[]) -%}{%- for item in content -%}{%- if item['type'] == 'image' -%}{%- set parts.items = parts.items + ['<image_id>' ~ ns.image_count ~ '</image_id><unk>'] -%}{%- set ns.image_count = ns.image_count + 1 -%}{%- elif item['type'] == 'text' -%}{%- set parts.items = parts.items + [item['text']] -%}{%- elif item['type'] == 'video' -%}{%- set parts.items = parts.items + ['<|video_pad|>'] -%}{%- endif -%}{%- endfor -%}{{- parts.items | join('\n') -}}{%- endif -%}{%- endmacro -%}{%- set images = namespace(image_count=0) -%}\n{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- render_content(messages[0].content, images) + '\\n\\n' }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + render_content(messages[0].content, images) + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == \"user\" and not(message.content is string and message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + render_content(message.content, images) + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {%- set content = render_content(message.content, images) %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is defined and message.reasoning_content is not none %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '</think>' in message.content %}\n {%- set content = message.content.split('</think>')[-1].lstrip('\\n') %}\n {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\\n').split('<think>')[-1].lstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\\n<think>\\n' + reasoning_content.strip('\\n') + '\\n</think>\\n\\n' + content.lstrip('\\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- render_content(message.content, images) }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\\n\\n</think>\\n\\n' }}\n {%- endif %}\n {%- if enable_thinking is defined and enable_thinking is true %}\n {{- '<think>\\n' }}\n {%- endif %}\n{%- endif %}"
|
| 3 |
+
}
|
config.json
CHANGED
|
@@ -37,22 +37,48 @@
|
|
| 37 |
"batch_3d_resampler": true,
|
| 38 |
"drop_vision_last_layer": false,
|
| 39 |
"image_size": 448,
|
| 40 |
-
"model_type": "
|
| 41 |
"patch_size": 14,
|
| 42 |
"query_num": 64,
|
| 43 |
"slice_config": {
|
| 44 |
"max_slice_nums": 9,
|
| 45 |
-
"patch_size": 14
|
| 46 |
-
"model_type": "minicpmv"
|
| 47 |
},
|
| 48 |
"slice_mode": true,
|
| 49 |
"vision_config": {
|
| 50 |
"hidden_size": 1152,
|
| 51 |
"image_size": 980,
|
| 52 |
"intermediate_size": 4304,
|
| 53 |
-
"model_type": "
|
| 54 |
"num_attention_heads": 16,
|
| 55 |
"num_hidden_layers": 27,
|
| 56 |
-
"patch_size": 14
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
|
|
|
| 37 |
"batch_3d_resampler": true,
|
| 38 |
"drop_vision_last_layer": false,
|
| 39 |
"image_size": 448,
|
| 40 |
+
"model_type": "minicpmv4_5",
|
| 41 |
"patch_size": 14,
|
| 42 |
"query_num": 64,
|
| 43 |
"slice_config": {
|
| 44 |
"max_slice_nums": 9,
|
| 45 |
+
"patch_size": 14
|
|
|
|
| 46 |
},
|
| 47 |
"slice_mode": true,
|
| 48 |
"vision_config": {
|
| 49 |
"hidden_size": 1152,
|
| 50 |
"image_size": 980,
|
| 51 |
"intermediate_size": 4304,
|
| 52 |
+
"model_type": "minicpmv4_5_vision",
|
| 53 |
"num_attention_heads": 16,
|
| 54 |
"num_hidden_layers": 27,
|
| 55 |
+
"patch_size": 14,
|
| 56 |
+
"layer_norm_eps": 1e-06,
|
| 57 |
+
"hidden_act": "gelu_pytorch_tanh"
|
| 58 |
+
},
|
| 59 |
+
"text_config": {
|
| 60 |
+
"model_type": "qwen3",
|
| 61 |
+
"vocab_size": 151748,
|
| 62 |
+
"hidden_size": 4096,
|
| 63 |
+
"intermediate_size": 12288,
|
| 64 |
+
"num_hidden_layers": 36,
|
| 65 |
+
"num_attention_heads": 32,
|
| 66 |
+
"num_key_value_heads": 8,
|
| 67 |
+
"head_dim": 128,
|
| 68 |
+
"hidden_act": "silu",
|
| 69 |
+
"max_position_embeddings": 40960,
|
| 70 |
+
"rms_norm_eps": 1e-06,
|
| 71 |
+
"attention_bias": false,
|
| 72 |
+
"bos_token_id": 151643,
|
| 73 |
+
"eos_token_id": 151645,
|
| 74 |
+
"initializer_range": 0.02,
|
| 75 |
+
"rope_parameters": {
|
| 76 |
+
"rope_type": "default",
|
| 77 |
+
"rope_theta": 1000000
|
| 78 |
+
}
|
| 79 |
+
},
|
| 80 |
+
"resampler_n_latents": 64,
|
| 81 |
+
"resampler_n_heads": 32,
|
| 82 |
+
"image_token_id": 128244,
|
| 83 |
+
"video_token_id": 151656
|
| 84 |
}
|
configuration_minicpm.py
CHANGED
|
@@ -68,6 +68,7 @@ class MiniCPMVConfig(Qwen3Config):
|
|
| 68 |
batch_vision_input=True,
|
| 69 |
slice_config=None,
|
| 70 |
vision_config=None,
|
|
|
|
| 71 |
use_image_id=True,
|
| 72 |
vision_batch_size=16,
|
| 73 |
batch_3d_resampler=True,
|
|
|
|
| 68 |
batch_vision_input=True,
|
| 69 |
slice_config=None,
|
| 70 |
vision_config=None,
|
| 71 |
+
text_config=None, # read by the native Transformers class, not here
|
| 72 |
use_image_id=True,
|
| 73 |
vision_batch_size=16,
|
| 74 |
batch_3d_resampler=True,
|
preprocessor_config.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
{
|
| 2 |
-
"image_processor_type": "
|
| 3 |
"auto_map": {
|
| 4 |
"AutoProcessor": "processing_minicpmv.MiniCPMVProcessor",
|
| 5 |
"AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
|
| 6 |
-
|
| 7 |
"processor_class": "MiniCPMVProcessor",
|
| 8 |
"max_slice_nums": 9,
|
| 9 |
"scale_resolution": 448,
|
|
@@ -18,7 +18,31 @@
|
|
| 18 |
"im_id_start": "<image_id>",
|
| 19 |
"im_id_end": "</image_id>",
|
| 20 |
"slice_mode": true,
|
| 21 |
-
"norm_mean": [
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"image_processor_type": "MiniCPMV2_5ImageProcessor",
|
| 3 |
"auto_map": {
|
| 4 |
"AutoProcessor": "processing_minicpmv.MiniCPMVProcessor",
|
| 5 |
"AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
|
| 6 |
+
},
|
| 7 |
"processor_class": "MiniCPMVProcessor",
|
| 8 |
"max_slice_nums": 9,
|
| 9 |
"scale_resolution": 448,
|
|
|
|
| 18 |
"im_id_start": "<image_id>",
|
| 19 |
"im_id_end": "</image_id>",
|
| 20 |
"slice_mode": true,
|
| 21 |
+
"norm_mean": [
|
| 22 |
+
0.5,
|
| 23 |
+
0.5,
|
| 24 |
+
0.5
|
| 25 |
+
],
|
| 26 |
+
"norm_std": [
|
| 27 |
+
0.5,
|
| 28 |
+
0.5,
|
| 29 |
+
0.5
|
| 30 |
+
],
|
| 31 |
+
"version": 2.6,
|
| 32 |
+
"do_convert_rgb": true,
|
| 33 |
+
"do_normalize": true,
|
| 34 |
+
"do_rescale": true,
|
| 35 |
+
"do_resize": true,
|
| 36 |
+
"image_mean": [
|
| 37 |
+
0.5,
|
| 38 |
+
0.5,
|
| 39 |
+
0.5
|
| 40 |
+
],
|
| 41 |
+
"image_std": [
|
| 42 |
+
0.5,
|
| 43 |
+
0.5,
|
| 44 |
+
0.5
|
| 45 |
+
],
|
| 46 |
+
"resample": 3,
|
| 47 |
+
"rescale_factor": 0.00392156862745098
|
| 48 |
}
|
processing_minicpmv.py
CHANGED
|
@@ -46,7 +46,7 @@ class MiniCPMVProcessor(ProcessorMixin):
|
|
| 46 |
image_processor_class = "AutoImageProcessor"
|
| 47 |
tokenizer_class = "AutoTokenizer"
|
| 48 |
|
| 49 |
-
def __init__(self, image_processor=None, tokenizer=None):
|
| 50 |
super().__init__(image_processor, tokenizer)
|
| 51 |
self.version = image_processor.version
|
| 52 |
|
|
|
|
| 46 |
image_processor_class = "AutoImageProcessor"
|
| 47 |
tokenizer_class = "AutoTokenizer"
|
| 48 |
|
| 49 |
+
def __init__(self, image_processor=None, tokenizer=None, **kwargs):
|
| 50 |
super().__init__(image_processor, tokenizer)
|
| 51 |
self.version = image_processor.version
|
| 52 |
|
processor_config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"image_token": "<unk>",
|
| 3 |
+
"num_image_tokens": 64,
|
| 4 |
+
"processor_class": "MiniCPMV4_5Processor",
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoProcessor": "processing_minicpmv.MiniCPMVProcessor"
|
| 7 |
+
},
|
| 8 |
+
"video_token": "<|video_pad|>",
|
| 9 |
+
"video_processor": {
|
| 10 |
+
"video_processor_type": "MiniCPMV4_5VideoProcessor",
|
| 11 |
+
"fps": 5,
|
| 12 |
+
"max_num_frames": 180,
|
| 13 |
+
"max_num_packing": 3,
|
| 14 |
+
"max_slice_nums": 1,
|
| 15 |
+
"scale_resolution": 448,
|
| 16 |
+
"patch_size": 14
|
| 17 |
+
}
|
| 18 |
+
}
|
tokenizer_config.json
CHANGED
|
@@ -949,5 +949,6 @@
|
|
| 949 |
null
|
| 950 |
]
|
| 951 |
},
|
| 952 |
-
"tokenizer_class": "MiniCPMVTokenizerFast"
|
|
|
|
| 953 |
}
|
|
|
|
| 949 |
null
|
| 950 |
]
|
| 951 |
},
|
| 952 |
+
"tokenizer_class": "MiniCPMVTokenizerFast",
|
| 953 |
+
"padding_side": "left"
|
| 954 |
}
|