Visual Document Retrieval
PEFT
Safetensors
ColPali
vidore
multimodal_embedding
multilingual_embedding
Text-to-Visual Document (T→VD) retrieval

Integrate with Sentence Transformers v5.4

#3
by tomaarsen HF Staff - opened
1_Pooling/config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "embedding_dimension": 3584,
3
+ "pooling_mode": "lasttoken",
4
+ "include_prompt": true
5
+ }
README.md CHANGED
@@ -17,6 +17,7 @@ tags:
17
  - multimodal_embedding
18
  - multilingual_embedding
19
  - Text-to-Visual Document (T→VD) retrieval
 
20
  license: apache-2.0
21
  ---
22
 
@@ -46,12 +47,51 @@ license: apache-2.0
46
 
47
  ## Getting Started
48
 
49
- To use `nomic-embed-multimodal-7b`, please install `colpali` from source
50
 
 
51
  ```bash
52
- pip install git+https://github.com/illuin-tech/colpali.git
53
  ```
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  ```python
57
  import torch
@@ -91,6 +131,9 @@ with torch.no_grad():
91
  query_embeddings = model(**batch_queries)
92
 
93
  scores = processor.score(list(torch.unbind(query_embeddings)), list(torch.unbind(image_embeddings)))
 
 
 
94
  ```
95
 
96
 
 
17
  - multimodal_embedding
18
  - multilingual_embedding
19
  - Text-to-Visual Document (T→VD) retrieval
20
+ - sentence-transformers
21
  license: apache-2.0
22
  ---
23
 
 
47
 
48
  ## Getting Started
49
 
50
+ ### Using Sentence Transformers
51
 
52
+ Install Sentence Transformers:
53
  ```bash
54
+ pip install "sentence_transformers[image]"
55
  ```
56
 
57
+ ```python
58
+ import torch
59
+ from PIL import Image
60
+ from sentence_transformers import SentenceTransformer
61
+
62
+ model = SentenceTransformer(
63
+ "nomic-ai/nomic-embed-multimodal-7b",
64
+ model_kwargs={"dtype": torch.bfloat16},
65
+ )
66
+
67
+ # Images can be paths, URLs, or PIL Images
68
+ images = [
69
+ Image.new("RGB", (128, 128), color="white"),
70
+ Image.new("RGB", (64, 32), color="black"),
71
+ ]
72
+ queries = [
73
+ "What is the organizational structure for our R&D department?",
74
+ "Can you provide a breakdown of last year’s financial performance?",
75
+ ]
76
+
77
+ image_embeddings = model.encode(images)
78
+ query_embeddings = model.encode(queries)
79
+ print(image_embeddings.shape, query_embeddings.shape)
80
+ # (2, 3584) (2, 3584)
81
+
82
+ scores = model.similarity(query_embeddings, image_embeddings)
83
+ print(scores)
84
+ # tensor([[ 0.0076, -0.0014],
85
+ # [ 0.0156, -0.0040]])
86
+ ```
87
+
88
+ ### Using ColPali
89
+
90
+ Install `colpali-engine`. Pin to `<0.3.11` as later versions refactored `BiQwen2_5_Processor.process_queries` in a way that no longer matches the format this model was trained with (they dropped the `"Query: "` prefix and switched to a 10-token `<|endoftext|>` augmentation buffer), and loading newer code against these weights produces different scores:
91
+
92
+ ```bash
93
+ pip install "colpali-engine<0.3.11"
94
+ ```
95
 
96
  ```python
97
  import torch
 
131
  query_embeddings = model(**batch_queries)
132
 
133
  scores = processor.score(list(torch.unbind(query_embeddings)), list(torch.unbind(image_embeddings)))
134
+ print(scores)
135
+ # tensor([[ 0.0089, -0.0003],
136
+ # [ 0.0070, 0.0098]])
137
  ```
138
 
139
 
chat_template.jinja ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- for message in messages -%}
2
+ {%- set has_image = namespace(value=false) -%}
3
+ {%- if message['content'] is not string -%}
4
+ {%- for content in message['content'] -%}
5
+ {%- if content.get('type', '') == 'image' or 'image' in content -%}
6
+ {%- set has_image.value = true -%}
7
+ {%- endif -%}
8
+ {%- endfor -%}
9
+ {%- endif -%}
10
+ {%- if has_image.value -%}
11
+ <|im_start|>user
12
+ {% for content in message['content'] -%}
13
+ {%- if content.get('type', '') == 'image' or 'image' in content -%}
14
+ <|vision_start|><|image_pad|><|vision_end|>
15
+ {%- endif -%}
16
+ {%- endfor -%}
17
+ Describe the image.<|im_end|><|endoftext|>
18
+ {%- else -%}
19
+ {{- 'Query: ' -}}
20
+ {%- if message['content'] is string -%}
21
+ {{ message['content'] }}
22
+ {%- else -%}
23
+ {%- for content in message['content'] -%}
24
+ {%- if content.get('type', '') == 'text' -%}
25
+ {{ content['text'] }}
26
+ {%- endif -%}
27
+ {%- endfor -%}
28
+ {%- endif -%}
29
+ <|endoftext|>
30
+ {%- endif -%}
31
+ {%- endfor -%}
chat_template.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
3
- }
 
 
 
 
config_sentence_transformers.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "__version__": {
3
+ "sentence_transformers": "5.4.0",
4
+ "transformers": "5.5.0.dev0"
5
+ },
6
+ "model_type": "SentenceTransformer",
7
+ "similarity_fn_name": "cosine"
8
+ }
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.base.modules.transformer.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.sentence_transformer.modules.pooling.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.sentence_transformer.modules.normalize.Normalize"
19
+ }
20
+ ]
processor_config.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
  "max_num_visual_tokens": 768,
3
- "processor_class": "BiQwen2_5_Processor"
4
  }
 
1
  {
2
  "max_num_visual_tokens": 768,
3
+ "processor_class": "Qwen2_5_VLProcessor"
4
  }
sentence_bert_config.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "transformer_task": "feature-extraction",
3
+ "modality_config": {
4
+ "text": {
5
+ "method": "forward",
6
+ "method_output_name": "last_hidden_state"
7
+ },
8
+ "image": {
9
+ "method": "forward",
10
+ "method_output_name": "last_hidden_state"
11
+ },
12
+ "message": {
13
+ "method": "forward",
14
+ "method_output_name": "last_hidden_state",
15
+ "format": "structured"
16
+ }
17
+ },
18
+ "module_output_name": "token_embeddings",
19
+ "unpad_inputs": false
20
+ }