JoJosmin commited on
Commit
1576524
โ€ข
1 Parent(s): b3f8a71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -131,25 +131,21 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
131
  # return structured_results
132
 
133
  def find_similar_images(query_embedding, collection, top_k=5):
134
- # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค
135
- all_data = collection.get(include=['embeddings', 'metadatas']) # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ๊ฐ€์ ธ์˜ด
136
- all_embeddings = np.array([data['embedding'] for data in all_data['embeddings']])
137
- all_metadatas = all_data['metadatas']
138
-
139
- # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ๊ณผ ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
140
- similarities = cosine_similarity(query_embedding.reshape(1, -1), all_embeddings).flatten()
141
-
142
- # ์œ ์‚ฌ๋„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ top_k ์ธ๋ฑ์Šค ์ถ”์ถœ
143
- top_indices = np.argsort(similarities)[::-1][:top_k] # ์œ ์‚ฌ๋„๊ฐ€ ํฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
144
-
145
- # ๊ฒฐ๊ณผ ์ •๋ฆฌ
146
- results = []
147
- for idx in top_indices:
148
- results.append({
149
- 'info': all_metadatas[idx],
150
- 'similarity': similarities[idx]
151
- })
152
-
153
  return results
154
 
155
 
 
131
  # return structured_results
132
 
133
  def find_similar_images(query_embedding, collection, top_k=5):
134
+ # ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ๊ณผ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
135
+ all_data = collection.get(include=['embeddings', 'metadatas'])
136
+ all_embeddings = np.array(all_data['embeddings']) # ์ž„๋ฒ ๋”ฉ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
137
+ all_metadatas = all_data['metadatas'] # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ
138
+
139
+ # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ๊ณผ์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
140
+ query_embedding = query_embedding.reshape(1, -1) # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ ์ฐจ์› ์กฐ์ •
141
+ similarities = cosine_similarity(query_embedding, all_embeddings).flatten() # ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
142
+
143
+ # ์œ ์‚ฌ๋„์™€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์Œ์œผ๋กœ ๋ฌถ๊ณ , ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
144
+ similarity_metadata_pairs = list(zip(similarities, all_metadatas))
145
+ sorted_pairs = sorted(similarity_metadata_pairs, key=lambda x: x[0], reverse=True)[:top_k]
146
+
147
+ # ์ตœ์ข… ๊ฒฐ๊ณผ ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
148
+ results = [{'info': metadata, 'similarity': similarity} for similarity, metadata in sorted_pairs]
 
 
 
 
149
  return results
150
 
151