Ransaka commited on
Commit
20ab731
1 Parent(s): 43bb95d

Update recommendation.py

Browse files
Files changed (1) hide show
  1. recommendation.py +5 -8
recommendation.py CHANGED
@@ -28,25 +28,22 @@ def row_wise_normalize_and_concatenate(array1, array2):
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
  image_embeds = image_embeds / np.linalg.norm(image_embeds, axis=1, keepdims=True)
41
  final_embeds = np.concatenate((image_embeds,title_embeds), axis=1)
42
 
43
- candidates = index.topk(final_embeds,k=k)
44
- # title_candidates = text_embedding_index.topk(title_embeds, k=k)
45
  final_candidates = []
46
  final_candidates.append(list(candidates[0]))
47
- #final_candidates.append(list(title_candidates[0]))
48
  final_candidates = sum(final_candidates,[])
49
- # final_candidates = list(set(final_candidates))
50
  results_dict = {"image":[], "title":[]}
51
  for candidate in final_candidates:
52
  results_dict['image'].append(dataset['image'][candidate])
 
28
 
29
  return concatenated_array
30
 
31
+ # result_array = row_wise_normalize_and_concatenate(latent_data, embeddings)
32
+ # index = Indexer(result_array)
33
+ index = Indexer(latent_data)
34
 
35
  def get_recommendations(image, title, k):
 
36
  title_embeds = model.encode([title], normalize_embeddings=True)
37
  image = transforms.ToTensor()(image.convert("L"))
38
  image_embeds = encoder(image).detach().numpy()
39
  image_embeds = image_embeds / np.linalg.norm(image_embeds, axis=1, keepdims=True)
40
  final_embeds = np.concatenate((image_embeds,title_embeds), axis=1)
41
 
42
+ # candidates = index.topk(final_embeds,k=k)
43
+ candidates = index.topk(image_embeds,k=k)
44
  final_candidates = []
45
  final_candidates.append(list(candidates[0]))
 
46
  final_candidates = sum(final_candidates,[])
 
47
  results_dict = {"image":[], "title":[]}
48
  for candidate in final_candidates:
49
  results_dict['image'].append(dataset['image'][candidate])