Tom Aarsen commited on
Commit
4e79059
·
1 Parent(s): 724f4a2

Integrate with Sentence Transformers

Browse files
1_Pooling/config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "embedding_dimension": 4096,
3
+ "pooling_mode": "lasttoken",
4
+ "include_prompt": true
5
+ }
README.md CHANGED
@@ -3,6 +3,7 @@ license: apache-2.0
3
  pipeline_tag: feature-extraction
4
  library_name: transformers
5
  tags:
 
6
  - multimodal
7
  - text
8
  - image
@@ -63,6 +64,53 @@ Ovis-VL-Embedding-9B is a bi-encoder, not a cross-encoder:
63
 
64
  No answer generation or query-candidate cross-attention is used during retrieval. Classification labels, passages, images, documents, videos, and interleaved multimodal items are all treated as candidates in the same embedding space.
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  ## Performance
67
 
68
  ### MMEB-v2
 
3
  pipeline_tag: feature-extraction
4
  library_name: transformers
5
  tags:
6
+ - sentence-transformers
7
  - multimodal
8
  - text
9
  - image
 
64
 
65
  No answer generation or query-candidate cross-attention is used during retrieval. Classification labels, passages, images, documents, videos, and interleaved multimodal items are all treated as candidates in the same embedding space.
66
 
67
+ ## Usage
68
+
69
+ ### Using Sentence Transformers
70
+
71
+ Install Sentence Transformers with image support and a compatible Transformers version:
72
+
73
+ ```bash
74
+ pip install "sentence-transformers[image]>=5.4.0" "transformers>=5.12.1"
75
+ ```
76
+
77
+ ```python
78
+ from sentence_transformers import SentenceTransformer
79
+
80
+ model = SentenceTransformer("ATH-MaaS/Ovis-VL-Embedding-9B")
81
+
82
+ query = "Which planet is known as the Red Planet?"
83
+ documents = [
84
+ "Venus is often called Earth's twin because of its similar size and proximity.",
85
+ "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
86
+ "Jupiter, the largest planet in our solar system, has a prominent red spot.",
87
+ "Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
88
+ ]
89
+
90
+ query_embedding = model.encode_query(query)
91
+ document_embeddings = model.encode_document(documents)
92
+ print(query_embedding.shape, document_embeddings.shape)
93
+ # (4096,) (4, 4096)
94
+
95
+ print(model.similarity(query_embedding, document_embeddings))
96
+ # tensor([[0.8671, 0.9523, 0.8929, 0.8882]])
97
+
98
+ # And a multimodal example:
99
+ text_embeddings = model.encode(["Ovis logo", "A green car", "children playing"])
100
+ image_embeddings = model.encode(
101
+ "https://huggingface.co/ATH-MaaS/Ovis-VL-Embedding-9B/resolve/main/figures/ovis_logo.png"
102
+ )
103
+ print(text_embeddings.shape, image_embeddings.shape)
104
+ # (3, 4096) (4096,)
105
+
106
+ print(model.similarity(image_embeddings, text_embeddings))
107
+ # tensor([[0.8765, 0.7191, 0.7524]])
108
+ ```
109
+
110
+ Both `query` and `document` prompts use `Represent the user's input.`. The default prompt key is `document`, so `encode()` also applies the same instruction. Use `prompt_name="query"` or `prompt_name="document"` to select a saved prompt, or pass a task-specific instruction with `model.encode_query(query, prompt="Retrieve passages that answer the question.")`.
111
+
112
+ Images can be URLs, local paths, or PIL images. For an image with accompanying text, pass `{"image": image, "text": text}`. Content follows the dictionary key order. Video inputs use `{"video": video}`. Install `sentence-transformers[video]` when loading video files. See the [input formats documentation](https://sbert.net/docs/input_formats.html) for more examples.
113
+
114
  ## Performance
115
 
116
  ### MMEB-v2
config_sentence_transformers.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "__version__": {
3
+ "pytorch": "2.10.0+cu128",
4
+ "sentence_transformers": "5.4.0",
5
+ "transformers": "5.22.1"
6
+ },
7
+ "default_prompt_name": "document",
8
+ "model_type": "SentenceTransformer",
9
+ "prompts": {
10
+ "document": "Represent the user's input.",
11
+ "query": "Represent the user's input."
12
+ },
13
+ "similarity_fn_name": "cosine"
14
+ }
model/sentence_bert_config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "video": {
13
+ "method": "forward",
14
+ "method_output_name": "last_hidden_state"
15
+ },
16
+ "message": {
17
+ "method": "forward",
18
+ "method_output_name": "last_hidden_state",
19
+ "format": "structured"
20
+ }
21
+ },
22
+ "module_output_name": "token_embeddings",
23
+ "processing_kwargs": {
24
+ "chat_template": {
25
+ "chat_template": "{%- for message in messages %}\n {%- if message.role not in ['system', 'user'] %}\n {{- raise_exception('Embedding inputs must contain system or user messages.') }}\n {%- endif %}\n {%- set content %}\n {%- if message.content is string %}\n {{- message.content }}\n {%- else %}\n {%- for item in message.content %}\n {%- if item.type == 'image' %}\n {{- '<|vision_start|><|image_pad|><|vision_end|>' }}\n {%- elif item.type == 'video' %}\n {{- '<|vision_start|><|video_pad|><|vision_end|>' }}\n {%- elif item.type == 'text' %}\n {{- item.text }}\n {%- endif %}\n {%- endfor %}\n {%- endif %}\n {%- endset %}\n {{- '<|im_start|>' + message.role + '\\n' + content|trim + '<|im_end|>\\n' }}\n{%- endfor %}\n{{- '<|im_start|>assistant\\n<think>\\n\\n</think>\\n\\n<|endoftext|>' }}\n"
26
+ }
27
+ }
28
+ }
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "model",
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"
19
+ }
20
+ ]