JoJosmin commited on
Commit
a91719a
โ€ข
1 Parent(s): 5069701

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -16
app.py CHANGED
@@ -89,14 +89,10 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
89
  def find_similar_images(query_embedding, collection, top_k=5):
90
  # ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์˜ด
91
  all_embeddings = collection.get(include=['embeddings'])['embeddings']
92
- if len(all_embeddings) == 0:
93
- st.error("No embeddings found in the collection.")
94
- return []
95
-
96
  database_embeddings = np.array(all_embeddings)
97
 
98
  # ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
99
- similarities = cosine_similarity(database_embeddings, query_embedding.reshape(1, -1)).squeeze()
100
  top_indices = np.argsort(similarities)[::-1][:top_k]
101
 
102
  # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ด
@@ -108,23 +104,13 @@ def find_similar_images(query_embedding, collection, top_k=5):
108
  # ๋ฒกํ„ฐ ID๋ฅผ ํ•จ๊ป˜ ๊ฐ€์ ธ์™€์„œ ํ™•์ธ
109
  vector_data = collection.get(include=['embeddings', 'metadatas', 'ids'])
110
 
111
- for idx, vector_id in enumerate(vector_data['ids']):
112
- st.write(f"ID: {vector_id}, Embedding: {vector_data['embeddings'][idx]}")
113
- if len(all_data) == 0:
114
- st.error("No metadatas found in the collection.")
115
- return []
116
-
117
  top_metadatas = [all_data[idx] for idx in top_indices]
118
 
119
  results = []
120
  for idx, metadata in enumerate(top_metadatas):
121
- image_urls = metadata['image_url'].split(',')
122
- representative_image_url = image_urls[0] if image_urls else None
123
-
124
  results.append({
125
  'info': metadata,
126
- 'similarity': similarities[top_indices[idx]],
127
- 'image_url': representative_image_url
128
  })
129
  return results
130
 
 
89
  def find_similar_images(query_embedding, collection, top_k=5):
90
  # ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์˜ด
91
  all_embeddings = collection.get(include=['embeddings'])['embeddings']
 
 
 
 
92
  database_embeddings = np.array(all_embeddings)
93
 
94
  # ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
95
+ similarities = np.dot(database_embeddings, query_embedding.T).squeeze()
96
  top_indices = np.argsort(similarities)[::-1][:top_k]
97
 
98
  # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ด
 
104
  # ๋ฒกํ„ฐ ID๋ฅผ ํ•จ๊ป˜ ๊ฐ€์ ธ์™€์„œ ํ™•์ธ
105
  vector_data = collection.get(include=['embeddings', 'metadatas', 'ids'])
106
 
 
 
 
 
 
 
107
  top_metadatas = [all_data[idx] for idx in top_indices]
108
 
109
  results = []
110
  for idx, metadata in enumerate(top_metadatas):
 
 
 
111
  results.append({
112
  'info': metadata,
113
+ 'similarity': similarities[top_indices[idx]]
 
114
  })
115
  return results
116