JoJosmin commited on
Commit
bc20d70
1 Parent(s): 6c806d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -18
app.py CHANGED
@@ -45,21 +45,6 @@ def load_image_from_url(url, max_retries=3):
45
  else:
46
  return None
47
 
48
- def initialize_faiss_index(collection):
49
- # 모든 임베딩을 가져와 numpy 배열로 변환
50
- all_data = collection.get(include=['embeddings', 'metadatas'])
51
- all_embeddings = np.array(all_data['embeddings']).astype('float32')
52
- all_metadatas = all_data['metadatas']
53
-
54
- # faiss 인덱스 생성 및 임베딩 추가
55
- dimension = all_embeddings.shape[1]
56
- index = faiss.IndexFlatIP(dimension) # 코사인 유사도를 사용하려면 IndexFlatIP를 사용
57
- index.add(all_embeddings)
58
-
59
- return index, all_metadatas
60
-
61
- faiss_index, all_metadatas = initialize_faiss_index(collection)
62
-
63
 
64
  # 세그먼트 마스크 기반 임베딩 추출
65
  def get_segmented_embedding(img, final_mask):
@@ -170,15 +155,14 @@ def find_similar_images(query_embedding, collection, top_k=5):
170
  # 쿼리 임베딩 정규화 후 faiss 검색
171
  query_embedding = query_embedding.reshape(1, -1).astype('float32')
172
  faiss.normalize_L2(query_embedding)
173
- _, indices = index.search(query_embedding, top_k)
174
 
175
  # 검색된 상위 결과를 반환
176
  structured_results = []
177
  for metadata, idx in zip(all_metadatas, indices[0]):
178
- similarity = np.dot(query_embedding, all_embeddings[idx]).item() # 코사인 유사도 계산
179
  structured_results.append({
180
  'info': metadata,
181
- 'similarity': similarity
182
  })
183
 
184
  return structured_results
 
45
  else:
46
  return None
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  # 세그먼트 마스크 기반 임베딩 추출
50
  def get_segmented_embedding(img, final_mask):
 
155
  # 쿼리 임베딩 정규화 후 faiss 검색
156
  query_embedding = query_embedding.reshape(1, -1).astype('float32')
157
  faiss.normalize_L2(query_embedding)
158
+ distance, indices = index.search(query_embedding, top_k)
159
 
160
  # 검색된 상위 결과를 반환
161
  structured_results = []
162
  for metadata, idx in zip(all_metadatas, indices[0]):
 
163
  structured_results.append({
164
  'info': metadata,
165
+ 'similarity': 1-distance
166
  })
167
 
168
  return structured_results