JoJosmin commited on
Commit
a76fd9c
·
verified ·
1 Parent(s): 46f7785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -42
app.py CHANGED
@@ -110,53 +110,27 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
110
 
111
  # return structured_results
112
 
113
- #def find_similar_images(query_embedding, collection, top_k=5):
114
- # query_embedding = query_embedding.reshape(1, -1) # Reshape to 2D array for ChromaDB
115
- # results = collection.query(
116
- # query_embeddings=query_embedding,
117
- # n_results=top_k,
118
- # include=['metadatas', 'distances']
119
- # )
120
- #
121
- # top_metadatas = results['metadatas'][0]
122
- # top_distances = results['distances'][0]
123
- #
124
- # structured_results = []
125
- # for metadata, distance in zip(top_metadatas, top_distances):
126
- # structured_results.append({
127
- # 'info': metadata,
128
- # 'similarity': 1 - distance
129
- # })
130
- #
131
- # return structured_results
132
-
133
- def get_all_embeddings_from_collection(collection):
134
- all_data = collection.get(include=['embeddings']) # 모든 데이터 가져오기
135
- all_embeddings = np.array([data['embedding'] for data in all_data['embeddings']]) # 임베딩 추출
136
- return all_embeddings
137
-
138
  def find_similar_images(query_embedding, collection, top_k=5):
139
- database_embeddings = get_all_embeddings_from_collection(collection)
140
-
141
- # 코사인 유사도 계산
142
- similarities = cosine_similarity(query_embedding.reshape(1, -1), database_embeddings).flatten()
 
 
143
 
144
- # 유사도 기준으로 정렬하여 top_k 인덱스 추출
145
- top_indices = np.argsort(similarities)[::-1][:top_k] # 유사도가 큰 순서대로 정렬
146
-
147
- # 메타데이터 가져오기
148
- all_data = collection.get(include=['metadatas'])['metadatas']
149
- top_metadatas = [all_data[idx] for idx in top_indices]
150
-
151
- # 결과 정리
152
- results = []
153
- for idx, metadata in enumerate(top_metadatas):
154
- results.append({
155
  'info': metadata,
156
- 'similarity': similarities[top_indices[idx]]
157
  })
158
 
159
- return results
 
 
160
 
161
  # 세션 상태 초기화
162
  if 'step' not in st.session_state:
 
110
 
111
  # return structured_results
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  def find_similar_images(query_embedding, collection, top_k=5):
114
+ query_embedding = query_embedding.reshape(1, -1) # Reshape to 2D array for ChromaDB
115
+ results = collection.query(
116
+ query_embeddings=query_embedding,
117
+ n_results=top_k,
118
+ include=['metadatas', 'distances']
119
+ )
120
 
121
+ top_metadatas = results['metadatas'][0]
122
+ top_distances = results['distances'][0]
123
+
124
+ structured_results = []
125
+ for metadata, distance in zip(top_metadatas, top_distances):
126
+ structured_results.append({
 
 
 
 
 
127
  'info': metadata,
128
+ 'similarity': 1 - distance
129
  })
130
 
131
+ return structured_results
132
+
133
+
134
 
135
  # 세션 상태 초기화
136
  if 'step' not in st.session_state: