Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
83 |
results = collection.query(
|
84 |
-
query_embeddings=query_embedding,
|
85 |
n_results=top_k,
|
86 |
-
include=['metadatas'
|
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,
|
94 |
structured_results.append({
|
95 |
'info': metadata,
|
96 |
-
'similarity': 1
|
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'
|