Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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', '
|
136 |
)
|
137 |
|
|
|
138 |
top_metadatas = results['metadatas'][0]
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
structured_results = []
|
142 |
-
for metadata,
|
143 |
structured_results.append({
|
144 |
'info': metadata,
|
145 |
-
'similarity':
|
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
|