JoJosmin commited on
Commit
196c895
ยท
verified ยท
1 Parent(s): 8392151

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -111,38 +111,32 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
111
 
112
  # return structured_results
113
 
114
- def get_all_embeddings_from_collection(collection):
115
- # ์ปฌ๋ ‰์…˜์—์„œ ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
116
- # ์ด ๊ฒฝ์šฐ collection ๊ฐ์ฒด๋Š” embeddings ์†์„ฑ ํฌํ•จ์„ ์ง€์ •ํ•ด ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
117
- all_embeddings_data = collection.get(include=['embeddings'])
118
-
119
- # ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
120
- all_embeddings = np.array(all_embeddings_data['embeddings'])
121
-
122
- return all_embeddings
123
-
124
  def find_similar_images(query_embedding, collection, top_k=5):
125
- # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž„๋ฒ ๋”ฉ ์ •๊ทœํ™”
126
- database_embeddings = get_all_embeddings_from_collection(collection)
127
- database_embeddings = normalize(database_embeddings, axis=1)
128
-
129
- # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ ์ •๊ทœํ™”
130
- query_embedding = normalize(query_embedding.reshape(1, -1), axis=1)
131
- #query_embedding = query_embedding.reshape(1, -1) # Reshape to 2D array for ChromaDB
132
  results = collection.query(
133
- query_embeddings=query_embedding,
134
  n_results=top_k,
135
- include=['metadatas', 'distances']
136
  )
137
 
 
138
  top_metadatas = results['metadatas'][0]
139
- top_distances = results['distances'][0]
140
-
 
 
 
 
 
 
 
 
 
141
  structured_results = []
142
- for metadata, distance in zip(top_metadatas, top_distances):
143
  structured_results.append({
144
  'info': metadata,
145
- 'similarity': 1 - distance
146
  })
147
 
148
  return structured_results
 
111
 
112
  # return structured_results
113
 
 
 
 
 
 
 
 
 
 
 
114
  def find_similar_images(query_embedding, collection, top_k=5):
115
+ # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
 
 
 
 
 
 
116
  results = collection.query(
117
+ query_embeddings=query_embedding.reshape(1, -1), # 2D ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
118
  n_results=top_k,
119
+ include=['metadatas', 'embeddings']
120
  )
121
 
122
+ # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์™€ ์ž„๋ฒ ๋”ฉ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
123
  top_metadatas = results['metadatas'][0]
124
+ top_embeddings = np.array(results['embeddings'][0]) # numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
125
+
126
+ # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ ์ •๊ทœํ™”
127
+ query_embedding_normalized = normalize(query_embedding.reshape(1, -1), axis=1)
128
+
129
+ # ์ž„๋ฒ ๋”ฉ ์ •๊ทœํ™”
130
+ top_embeddings_normalized = normalize(top_embeddings, axis=1)
131
+
132
+ # ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ
133
+ similarities = cosine_similarity(query_embedding_normalized, top_embeddings_normalized).flatten()
134
+
135
  structured_results = []
136
+ for metadata, similarity in zip(top_metadatas, similarities):
137
  structured_results.append({
138
  'info': metadata,
139
+ 'similarity': similarity # ์œ ์‚ฌ๋„๋Š” ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’
140
  })
141
 
142
  return structured_results