Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -44,12 +44,25 @@ def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tenso
|
|
44 |
sequence_lengths = attention_mask.sum(dim=1) - 1
|
45 |
batch_size = last_hidden_states.shape[0]
|
46 |
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
48 |
class EmbeddingModel:
|
49 |
def __init__(self):
|
50 |
self.tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
51 |
self.model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct', torch_dtype=torch.float16, device_map=device)
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
@spaces.GPU
|
54 |
def compute_embeddings(self, selected_task, input_text):
|
55 |
try:
|
@@ -84,16 +97,20 @@ class EmbeddingModel:
|
|
84 |
embeddings4 = self.compute_embeddings(self.selected_task, extra_sentence2)
|
85 |
|
86 |
# Convert embeddings to tensors
|
87 |
-
embeddings_tensor1 = torch.tensor(embeddings1).to(device)
|
88 |
-
embeddings_tensor2 = torch.tensor(embeddings2).to(device)
|
89 |
-
embeddings_tensor3 = torch.tensor(embeddings3).to(device)
|
90 |
-
embeddings_tensor4 = torch.tensor(embeddings4).to(device)
|
91 |
|
92 |
# Compute cosine similarity
|
93 |
-
similarity1 =
|
94 |
-
similarity2 =
|
95 |
-
similarity3 =
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
|
98 |
|
99 |
def app_interface():
|
|
|
44 |
sequence_lengths = attention_mask.sum(dim=1) - 1
|
45 |
batch_size = last_hidden_states.shape[0]
|
46 |
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
47 |
+
def clear_cuda_cache():
|
48 |
+
torch.cuda.empty_cache()
|
49 |
|
50 |
+
def free_memory(*args):
|
51 |
+
for arg in args:
|
52 |
+
del arg
|
53 |
+
|
54 |
class EmbeddingModel:
|
55 |
def __init__(self):
|
56 |
self.tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
57 |
self.model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct', torch_dtype=torch.float16, device_map=device)
|
58 |
+
|
59 |
+
def _compute_cosine_similarity(self, emb1, emb2):
|
60 |
+
tensor1 = torch.tensor(emb1).to(device).half()
|
61 |
+
tensor2 = torch.tensor(emb2).to(device).half()
|
62 |
+
similarity = F.cosine_similarity(tensor1, tensor2).item()
|
63 |
+
free_memory(tensor1, tensor2)
|
64 |
+
return similarity
|
65 |
+
|
66 |
@spaces.GPU
|
67 |
def compute_embeddings(self, selected_task, input_text):
|
68 |
try:
|
|
|
97 |
embeddings4 = self.compute_embeddings(self.selected_task, extra_sentence2)
|
98 |
|
99 |
# Convert embeddings to tensors
|
100 |
+
embeddings_tensor1 = torch.tensor(embeddings1).to(device).half()
|
101 |
+
embeddings_tensor2 = torch.tensor(embeddings2).to(device).half()
|
102 |
+
embeddings_tensor3 = torch.tensor(embeddings3).to(device).half()
|
103 |
+
embeddings_tensor4 = torch.tensor(embeddings4).to(device).half()
|
104 |
|
105 |
# Compute cosine similarity
|
106 |
+
similarity1 = self._compute_cosine_similarity(embeddings1, embeddings2)
|
107 |
+
similarity2 = self._compute_cosine_similarity(embeddings1, embeddings3)
|
108 |
+
similarity3 = self._compute_cosine_similarity(embeddings1, embeddings4)
|
109 |
+
|
110 |
+
# Free memory
|
111 |
+
free_memory(embeddings1, embeddings2, embeddings3, embeddings4)
|
112 |
+
|
113 |
+
return similarity1, similarity2, similarity3
|
114 |
|
115 |
|
116 |
def app_interface():
|