You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Licensed under the Apache License, Version 2.0 (the "License");
4
+
# you may not use this file except in compliance with the License.
5
+
# You may obtain a copy of the License at
6
+
#
7
+
# http://www.apache.org/licenses/LICENSE-2.0
8
+
#
9
+
# Unless required by applicable law or agreed to in writing, software
10
+
# distributed under the License is distributed on an "AS IS" BASIS,
11
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+
# See the License for the specific language governing permissions and
13
+
# limitations under the License.
14
+
15
+
fromsparknlp.commonimport*
16
+
17
+
classE5VEmbeddings(AnnotatorModel,
18
+
HasBatchedAnnotateImage,
19
+
HasImageFeatureProperties,
20
+
HasEngine,
21
+
HasRescaleFactor):
22
+
"""Universal multimodal embeddings using the E5-V model (see https://huggingface.co/royokong/e5-v).
23
+
24
+
E5-V bridges the modality gap between different input types (text, image) and demonstrates strong performance in multimodal embeddings, even without fine-tuning. It also supports a single-modality training approach, where the model is trained exclusively on text pairs, often yielding better performance than multimodal training.
25
+
26
+
Pretrained models can be loaded with :meth:`.pretrained` of the companion object:
27
+
28
+
>>> e5vEmbeddings = E5VEmbeddings.pretrained() \
29
+
... .setInputCols(["image_assembler"]) \
30
+
... .setOutputCol("e5v")
31
+
32
+
The default model is ``"e5v_int4"``, if no name is provided.
33
+
34
+
For available pretrained models please see the `Models Hub <https://sparknlp.org/models?task=Question+Answering>`__.
35
+
36
+
====================== ======================
37
+
Input Annotation types Output Annotation type
38
+
====================== ======================
39
+
``IMAGE`` ``SENTENCE_EMBEDDINGS``
40
+
====================== ======================
41
+
42
+
Examples
43
+
--------
44
+
Image + Text Embedding:
45
+
>>> import sparknlp
46
+
>>> from sparknlp.base import *
47
+
>>> from sparknlp.annotator import *
48
+
>>> from pyspark.ml import Pipeline
49
+
>>> image_df = spark.read.format("image").option("dropInvalid", value = True).load(imageFolder)
50
+
>>> imagePrompt = "<|start_header_id|>user<|end_header_id|>\n\n<image>\\nSummary above image in one word: <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\n"
>>> from sparknlp.util import EmbeddingsDataFrameUtils
67
+
>>> textPrompt = "<|start_header_id|>user<|end_header_id|>\n\n<sent>\\nSummary above sentence in one word: <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\n"
imagePrompt="<|start_header_id|>user<|end_header_id|>\n\n<image>\\nSummary above image in one word: <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\n"
# Simulate text-only embedding using emptyImageRow and imageSchema
49
+
fromsparknlp.utilimportEmbeddingsDataFrameUtils
50
+
textPrompt="<|start_header_id|>user<|end_header_id|>\n\n<sent>\\nSummary above sentence in one word: <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\n"
0 commit comments