-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_embeddings.py
52 lines (42 loc) · 1.86 KB
/
text_to_embeddings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import torch
import numpy as np
from numpy.linalg import norm
import torch
from angle_emb import AnglE
class TextToEmbeddings:
def __init__(self, model="./models/UAE-Large-V1"):
self.angle = AnglE.from_pretrained(
model_name_or_path=model, pooling_strategy="cls"
)
if torch.cuda.is_available():
self.angle = AnglE.from_pretrained(
model_name_or_path=model, pooling_strategy="cls"
).cuda()
elif torch.backends.mps.is_available():
self.angle = AnglE.from_pretrained(
model_name_or_path=model, pooling_strategy="cls"
).to("mps")
# not using prompt for retrieval
def encode(self, text):
return self.angle.encode(text, to_numpy=True)
def cosine_similarity(self, vec1, vec2):
return np.dot(vec1, vec2) / (norm(vec1) * norm(vec2))
def get_similarity(self, prompt, caption):
prompt_enc = self.encode(prompt)
caption_enc = self.encode(caption)
return self.cosine_similarity(prompt_enc[0], caption_enc[0])
if __name__ == "__main__":
embed_model = TextToEmbeddings()
prompt = "This picture describes a telephone booth in a park with a blue sky."
item1 = "This is a red telephone booth on a black background."
item2 = "This is the picture of a blue sky with white cloud's."
item3 = "The scene describes a cobble stone wall against a black background."
item4 = "The is a picture of a red sports car against a bridge"
item5 = "the picture is of a man in a background full of stars."
texts = [item1, item2, item3, item4, item5]
prompt_vec = embed_model.encode(prompt)
item_vecs = embed_model.encode(texts)
for idx, item_vec in enumerate(item_vecs):
print(
f"{idx+1} item and prompt similarity: {embed_model.cosine_similarity(prompt_vec, item_vec)}"
)