JoJosmin commited on
Commit
9197ebc
โ€ข
1 Parent(s): bf0ed8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -8,7 +8,7 @@ import time
8
  import numpy as np
9
  from transformers import pipeline
10
  import chromadb
11
- from sklearn.metrics.pairwise import euclidean_distances # ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ ์ถ”๊ฐ€
12
 
13
  # Load segmentation model
14
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
@@ -79,25 +79,32 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
79
  return img_with_alpha.convert("RGB"), final_mask, detected_categories
80
 
81
  def find_similar_images(query_embedding, collection, top_k=5):
82
- query_embedding = query_embedding.reshape(1, -1) # Reshape to 2D array for ChromaDB
83
  results = collection.query(
84
- query_embeddings=query_embedding,
85
  n_results=top_k,
86
- include=['metadatas', 'distances']
87
  )
88
 
 
89
  top_metadatas = results['metadatas'][0]
90
- top_distances = results['distances'][0]
91
 
 
 
 
 
 
 
92
  structured_results = []
93
- for metadata, distance in zip(top_metadatas, top_distances):
94
  structured_results.append({
95
  'info': metadata,
96
- 'similarity': 1 - distance
97
  })
98
 
99
  return structured_results
100
 
 
101
  # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
102
  if 'step' not in st.session_state:
103
  st.session_state.step = 'input'
 
8
  import numpy as np
9
  from transformers import pipeline
10
  import chromadb
11
+ from sklearn.metrics.pairwise import euclidean_distances
12
 
13
  # Load segmentation model
14
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
 
79
  return img_with_alpha.convert("RGB"), final_mask, detected_categories
80
 
81
  def find_similar_images(query_embedding, collection, top_k=5):
82
+ # ChromaDB์—์„œ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ด๋ฏธ์ง€๋“ค์„ ์ฟผ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
83
  results = collection.query(
84
+ query_embeddings=query_embedding.reshape(1, -1), # 2D ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
85
  n_results=top_k,
86
+ include=['metadatas']
87
  )
88
 
89
+ # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
90
  top_metadatas = results['metadatas'][0]
 
91
 
92
+ # ๊ฐ ์ด๋ฏธ์ง€์˜ ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
93
+ collection_embeddings = np.array([metadata['embedding'] for metadata in top_metadatas])
94
+
95
+ # ์ฟผ๋ฆฌ ์ž„๋ฒ ๋”ฉ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž„๋ฒ ๋”ฉ ๊ฐ„์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
96
+ distances = euclidean_distances(query_embedding.reshape(1, -1), collection_embeddings).flatten()
97
+
98
  structured_results = []
99
+ for metadata, distance in zip(top_metadatas, distances):
100
  structured_results.append({
101
  'info': metadata,
102
+ 'similarity': 1 / (1 + distance) # ๊ฑฐ๋ฆฌ ๊ธฐ๋ฐ˜ ์œ ์‚ฌ๋„ (๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘์„์ˆ˜๋ก ์œ ์‚ฌ๋„๊ฐ€ ๋†’์Œ)
103
  })
104
 
105
  return structured_results
106
 
107
+
108
  # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
109
  if 'step' not in st.session_state:
110
  st.session_state.step = 'input'