Spaces:
Sleeping
Sleeping
Update recommendation.py
Browse files- recommendation.py +19 -7
recommendation.py
CHANGED
@@ -3,6 +3,7 @@ from sentence_transformers import SentenceTransformer
|
|
3 |
from torchvision import transforms
|
4 |
from models.encoder import Encoder
|
5 |
from indexer import Indexer
|
|
|
6 |
import torch
|
7 |
import os
|
8 |
|
@@ -18,22 +19,33 @@ dataset = concatenate_datasets([dataset['train'], dataset['test']])
|
|
18 |
latent_data = torch.load("data/latent_data_final.bin")
|
19 |
embeddings = torch.load("data/embeddings.bin")
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def get_recommendations(image, title, k):
|
25 |
-
|
26 |
title_embeds = model.encode([title], normalize_embeddings=True)
|
27 |
image = transforms.ToTensor()(image.convert("L"))
|
28 |
image_embeds = encoder(image).detach().numpy()
|
|
|
29 |
|
30 |
-
|
31 |
-
title_candidates = text_embedding_index.topk(title_embeds, k=k)
|
32 |
final_candidates = []
|
33 |
-
final_candidates.append(list(
|
34 |
#final_candidates.append(list(title_candidates[0]))
|
35 |
final_candidates = sum(final_candidates,[])
|
36 |
-
final_candidates = list(set(final_candidates))
|
37 |
results_dict = {"image":[], "title":[]}
|
38 |
for candidate in final_candidates:
|
39 |
results_dict['image'].append(dataset['image'][candidate])
|
|
|
3 |
from torchvision import transforms
|
4 |
from models.encoder import Encoder
|
5 |
from indexer import Indexer
|
6 |
+
import numpy as np
|
7 |
import torch
|
8 |
import os
|
9 |
|
|
|
19 |
latent_data = torch.load("data/latent_data_final.bin")
|
20 |
embeddings = torch.load("data/embeddings.bin")
|
21 |
|
22 |
+
|
23 |
+
def row_wise_normalize_and_concatenate(array1, array2):
|
24 |
+
normalized_array1 = array1 / np.linalg.norm(array1, axis=1, keepdims=True)
|
25 |
+
normalized_array2 = array2 / np.linalg.norm(array2, axis=1, keepdims=True)
|
26 |
+
|
27 |
+
concatenated_array = np.concatenate((normalized_array1, normalized_array2), axis=0)
|
28 |
+
|
29 |
+
return concatenated_array
|
30 |
+
|
31 |
+
result_array = row_wise_normalize_and_concatenate(latent_data, embeddings)
|
32 |
+
|
33 |
+
index = Indexer(result_array)
|
34 |
|
35 |
def get_recommendations(image, title, k):
|
36 |
+
title = [dataset[product_id]['title']]
|
37 |
title_embeds = model.encode([title], normalize_embeddings=True)
|
38 |
image = transforms.ToTensor()(image.convert("L"))
|
39 |
image_embeds = encoder(image).detach().numpy()
|
40 |
+
final_embeds = np.concatenate((image_embeds,title_embeds), axis=0)
|
41 |
|
42 |
+
candidates = image_embedding_index.topk(final_embeds,k=k)
|
43 |
+
# title_candidates = text_embedding_index.topk(title_embeds, k=k)
|
44 |
final_candidates = []
|
45 |
+
final_candidates.append(list(candidates[0]))
|
46 |
#final_candidates.append(list(title_candidates[0]))
|
47 |
final_candidates = sum(final_candidates,[])
|
48 |
+
# final_candidates = list(set(final_candidates))
|
49 |
results_dict = {"image":[], "title":[]}
|
50 |
for candidate in final_candidates:
|
51 |
results_dict['image'].append(dataset['image'][candidate])
|