Views
No views yet
1import torch
2from sentence_transformers import SentenceTransformer
3
4
5DOC1 = """
6Blue light is scattered in all directions by the tiny molecules of air in Earth's atmosphere.
7Blue is scattered more than other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.
8Closer to the horizon, the sky fades to a lighter blue or white.
9"""
10DOC2 = """
11When choosing colors, you can consider the following factors:
12Color theory: Understand how colors work together and how they can evoke different reactions.
13Color psychology: Consider how colors affect emotions, behaviors, and responses.
14Brand identity: Colors can convey meaning and information about a brand.
15Mood: Consider the mood you want to create. For example, brighter colors can feel cheerful, while cooler colors can be calming.
16Space: Consider the size of the space and the amount of natural light it receives. Dark colors can make a room feel smaller, while light colors can make it feel larger.
17Color wheel: Use the color wheel to identify primary, secondary, and tertiary colors.
18Color combinations: Decide how to best complement your preferred color with others.
19Color palette: Limit your color palette to a main color and one or two additional colors.
2060-30-10 rule: Use a primary color 60% of the time, a secondary color 30% of the time, and an accent color 10% of the time
21"""
22if __name__ == "__main__":
23 # load model
24 use_gpu = False
25 model_name = "infgrad/jasper_en_vision_language_v1"
26 model = SentenceTransformer(
27 model_name,
28 trust_remote_code=True,
29 device="cpu" if not use_gpu else "cuda",
30 model_kwargs={
31 "torch_dtype": torch.bfloat16 if use_gpu else torch.float32,
32 "attn_implementation": "sdpa"
33 },
34 # vector_dim must be 12288, 1024, 512, 256
35 ## 1024 is recommended
36 # set is_text_encoder 'True', if you do not encode image
37 config_kwargs={"is_text_encoder": False, "vector_dim": 1024},
38 )
39 # We can reduce the max_seq_length from the default of 2048 for faster encoding
40 model.max_seq_length = 1024
41
42 # data
43 q_list = [
44 "Why the sky is blue?",
45 "how to choose suitable color",
46 ]
47 doc_list = [
48 DOC1,
49 [{"type": "image_path", "content": "./assets/img1.png"}, {"type": "text", "content": "Hope this image helps!"}],
50 DOC2,
51 [{"type": "image_path", "content": "./assets/img2.png"}],
52 ]
53 q_vecs = model.encode(q_list, prompt_name="s2p_query")
54 doc_vecs = model.encode(doc_list)
55
56 # calculate similarity
57 similarities = model.similarity(q_vecs, doc_vecs)
58 print(similarities)
59 # the output is:
60 # tensor([[0.7775, 0.7594, 0.2429, 0.2187],
61 # [0.3226, 0.3054, 0.7421, 0.5484]])
@misc{zhang2025jasperstelladistillationsota,
title={Jasper and Stella: distillation of SOTA embedding models},
author={Dun Zhang and Jiacheng Li and Ziyang Zeng and Fulong Wang},
year={2025},
eprint={2412.19048},
archivePrefix={arXiv},
primaryClass={cs.IR},
url={https://arxiv.org/abs/2412.19048},
}