Spaces:
Running
Running
traopia
commited on
Commit
·
a8bd039
1
Parent(s):
2dec78e
rename to app.py
Browse files- app.py +125 -119
- app_twotabs.py → app_onetab.py +119 -125
app.py
CHANGED
@@ -43,26 +43,7 @@ def load_data_hf():
|
|
43 |
return df, embeddings
|
44 |
|
45 |
|
46 |
-
|
47 |
-
# def load_data1():
|
48 |
-
# # Login using e.g. `huggingface-cli login` to access this dataset
|
49 |
-
# path = hf_hub_download(
|
50 |
-
# repo_id="traopia/fashion_show_data_all_embeddings",
|
51 |
-
# filename="fashion_show_data_all_embeddings.json"
|
52 |
-
# )
|
53 |
-
# df = pd.read_json(path, lines = True)
|
54 |
-
|
55 |
-
# #df = pd.read_json("hf://datasets/traopia/fashion_show_data_all_embeddings.json/fashion_show_data_all_embeddings.json", lines=True)
|
56 |
-
# df["fashion_clip_image"] = df["fashion_clip_image"].apply(lambda x: x[0] if isinstance(x, list) else x)
|
57 |
-
# df["image_urls"] = df["image_urls"].apply(lambda x: x[0] if x is not None else None)
|
58 |
-
# df = df.rename(columns={"fashion_house":"designer", "image_urls":"url", "URL":"collection"})
|
59 |
-
|
60 |
-
# df = df.dropna(subset="fashion_clip_image")
|
61 |
-
# df = df.reset_index(drop=True)
|
62 |
-
# df["key"] = df.index
|
63 |
-
# embeddings = np.vstack(df["fashion_clip_image"].values)
|
64 |
-
|
65 |
-
# return df, embeddings
|
66 |
|
67 |
df, embeddings = load_data_hf()
|
68 |
|
@@ -107,108 +88,133 @@ def find_similar(idx, metadata):
|
|
107 |
|
108 |
|
109 |
|
110 |
-
|
111 |
with gr.Blocks() as demo:
|
112 |
gr.Markdown("# 👗 FashionDB Explorer")
|
113 |
|
114 |
-
with gr.
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
back_button = gr.Button("Back to Home")
|
214 |
|
|
|
43 |
return df, embeddings
|
44 |
|
45 |
|
46 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
df, embeddings = load_data_hf()
|
49 |
|
|
|
88 |
|
89 |
|
90 |
|
91 |
+
|
92 |
with gr.Blocks() as demo:
|
93 |
gr.Markdown("# 👗 FashionDB Explorer")
|
94 |
|
95 |
+
with gr.Tabs():
|
96 |
+
# TEXT SEARCH TAB
|
97 |
+
with gr.Tab("Search by Text"):
|
98 |
+
with gr.Row():
|
99 |
+
fashion_house = gr.Dropdown(label="Fashion House", choices=sorted(df["designer"].dropna().unique()), multiselect=True)
|
100 |
+
category = gr.Dropdown(label="Category", choices=sorted(df["category"].dropna().unique()), multiselect=True)
|
101 |
+
season = gr.Dropdown(label="Season", choices=sorted(df["season"].dropna().unique()), multiselect=True)
|
102 |
+
min_year = int(df['year'].min())
|
103 |
+
max_year = int(df['year'].max())
|
104 |
+
start_year = gr.Slider(label="Start Year", minimum=min_year, maximum=max_year, value=2000, step=1)
|
105 |
+
end_year = gr.Slider(label="End Year", minimum=min_year, maximum=max_year, value=2024, step=1)
|
106 |
+
|
107 |
+
query = gr.Textbox(label="Search by text", placeholder="e.g., pink dress")
|
108 |
+
search_button = gr.Button("Search")
|
109 |
+
|
110 |
+
result_gallery = gr.Gallery(label="Search Results", columns=5, height="auto")
|
111 |
+
metadata_output = gr.Markdown()
|
112 |
+
reference_image = gr.Image(label="Reference Image", interactive=False)
|
113 |
+
similar_gallery = gr.Gallery(label="Similar Images", columns=5, height="auto")
|
114 |
+
|
115 |
+
metadata_state = gr.State([])
|
116 |
+
selected_idx = gr.Number(value=0, visible=False)
|
117 |
+
|
118 |
+
def handle_search(fh, cat, sea, sy, ey, q):
|
119 |
+
imgs, meta = filter_and_search(fh, cat, sea, sy, ey, q)
|
120 |
+
return imgs, meta, "", [], None
|
121 |
+
|
122 |
+
search_button.click(
|
123 |
+
handle_search,
|
124 |
+
inputs=[fashion_house, category, season, start_year, end_year, query],
|
125 |
+
outputs=[result_gallery, metadata_state, metadata_output, similar_gallery, reference_image]
|
126 |
+
)
|
127 |
+
|
128 |
+
def handle_click(evt: gr.SelectData, metadata):
|
129 |
+
idx = evt.index
|
130 |
+
md = show_metadata(idx, metadata)
|
131 |
+
img_path = metadata[idx]["url"]
|
132 |
+
return idx, md, img_path
|
133 |
+
|
134 |
+
result_gallery.select(
|
135 |
+
handle_click,
|
136 |
+
inputs=[metadata_state],
|
137 |
+
outputs=[selected_idx, metadata_output, reference_image]
|
138 |
+
)
|
139 |
+
|
140 |
+
def show_similar(idx, metadata):
|
141 |
+
if idx is None or not str(idx).isdigit():
|
142 |
+
return [], []
|
143 |
+
return find_similar(int(idx), metadata)
|
144 |
+
|
145 |
+
similar_metadata_state = gr.State()
|
146 |
+
similar_metadata_output = gr.Markdown()
|
147 |
+
|
148 |
+
show_similar_button = gr.Button("Show Similar Images")
|
149 |
+
show_similar_button.click(
|
150 |
+
show_similar,
|
151 |
+
inputs=[selected_idx, metadata_state],
|
152 |
+
outputs=[similar_gallery, similar_metadata_state]
|
153 |
+
)
|
154 |
+
|
155 |
+
def handle_similar_click(evt: gr.SelectData, metadata):
|
156 |
+
idx = evt.index
|
157 |
+
md = show_metadata(idx, metadata)
|
158 |
+
img_path = metadata[idx]["url"]
|
159 |
+
return idx, md, img_path
|
160 |
+
|
161 |
+
similar_gallery.select(
|
162 |
+
handle_similar_click,
|
163 |
+
inputs=[similar_metadata_state],
|
164 |
+
outputs=[selected_idx, similar_metadata_output, reference_image]
|
165 |
+
)
|
166 |
+
|
167 |
+
# IMAGE SEARCH TAB
|
168 |
+
with gr.Tab("Search by Image"):
|
169 |
+
with gr.Row():
|
170 |
+
fashion_house_img = gr.Dropdown(label="Fashion House", choices=sorted(df["designer"].dropna().unique()), multiselect=True)
|
171 |
+
category_img = gr.Dropdown(label="Category", choices=sorted(df["category"].dropna().unique()), multiselect=True)
|
172 |
+
season_img = gr.Dropdown(label="Season", choices=sorted(df["season"].dropna().unique()), multiselect=True)
|
173 |
+
start_year_img = gr.Slider(label="Start Year", minimum=min_year, maximum=max_year, value=2000, step=1)
|
174 |
+
end_year_img = gr.Slider(label="End Year", minimum=min_year, maximum=max_year, value=2024, step=1)
|
175 |
+
|
176 |
+
uploaded_image = gr.Image(label="Upload an image", type="pil")
|
177 |
+
search_by_image_button = gr.Button("Search by Image")
|
178 |
+
|
179 |
+
uploaded_result_gallery = gr.Gallery(label="Search Results by Image", columns=5, height="auto")
|
180 |
+
uploaded_metadata_state = gr.State([])
|
181 |
+
uploaded_metadata_output = gr.Markdown()
|
182 |
+
uploaded_reference_image = gr.Image(label="Reference Image", interactive=False)
|
183 |
+
|
184 |
+
def handle_search_by_image(image, fh, cat, sea, sy, ey):
|
185 |
+
if image is None:
|
186 |
+
return [], "Please upload an image first.", None
|
187 |
+
# Apply filters
|
188 |
+
filtered_df = df.copy()
|
189 |
+
if fh: filtered_df = filtered_df[filtered_df["designer"].isin(fh)]
|
190 |
+
if cat: filtered_df = filtered_df[filtered_df["category"].isin(cat)]
|
191 |
+
if sea: filtered_df = filtered_df[filtered_df["season"].isin(sea)]
|
192 |
+
filtered_df = filtered_df[(filtered_df["year"] >= sy) & (filtered_df["year"] <= ey)]
|
193 |
+
|
194 |
+
results_df = search_images_by_image(image, filtered_df, embeddings)
|
195 |
+
images = results_df['url'].tolist()
|
196 |
+
metadata = results_df.to_dict(orient="records")
|
197 |
+
return images, metadata, ""
|
198 |
+
|
199 |
+
search_by_image_button.click(
|
200 |
+
handle_search_by_image,
|
201 |
+
inputs=[uploaded_image, fashion_house_img, category_img, season_img, start_year_img, end_year_img],
|
202 |
+
outputs=[uploaded_result_gallery, uploaded_metadata_state, uploaded_metadata_output]
|
203 |
+
)
|
204 |
+
|
205 |
+
uploaded_selected_idx = gr.Number(visible=False)
|
206 |
+
|
207 |
+
def handle_uploaded_click(evt: gr.SelectData, metadata):
|
208 |
+
idx = evt.index
|
209 |
+
md = show_metadata(idx, metadata)
|
210 |
+
img_path = metadata[idx]["url"]
|
211 |
+
return idx, md, img_path
|
212 |
+
|
213 |
+
uploaded_result_gallery.select(
|
214 |
+
handle_uploaded_click,
|
215 |
+
inputs=[uploaded_metadata_state],
|
216 |
+
outputs=[uploaded_selected_idx, uploaded_metadata_output, uploaded_reference_image]
|
217 |
+
)
|
218 |
|
219 |
back_button = gr.Button("Back to Home")
|
220 |
|
app_twotabs.py → app_onetab.py
RENAMED
@@ -43,7 +43,26 @@ def load_data_hf():
|
|
43 |
return df, embeddings
|
44 |
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
df, embeddings = load_data_hf()
|
49 |
|
@@ -88,133 +107,108 @@ def find_similar(idx, metadata):
|
|
88 |
|
89 |
|
90 |
|
91 |
-
|
92 |
with gr.Blocks() as demo:
|
93 |
gr.Markdown("# 👗 FashionDB Explorer")
|
94 |
|
95 |
-
with gr.
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
results_df = search_images_by_image(image, filtered_df, embeddings)
|
195 |
-
images = results_df['url'].tolist()
|
196 |
-
metadata = results_df.to_dict(orient="records")
|
197 |
-
return images, metadata, ""
|
198 |
-
|
199 |
-
search_by_image_button.click(
|
200 |
-
handle_search_by_image,
|
201 |
-
inputs=[uploaded_image, fashion_house_img, category_img, season_img, start_year_img, end_year_img],
|
202 |
-
outputs=[uploaded_result_gallery, uploaded_metadata_state, uploaded_metadata_output]
|
203 |
-
)
|
204 |
-
|
205 |
-
uploaded_selected_idx = gr.Number(visible=False)
|
206 |
-
|
207 |
-
def handle_uploaded_click(evt: gr.SelectData, metadata):
|
208 |
-
idx = evt.index
|
209 |
-
md = show_metadata(idx, metadata)
|
210 |
-
img_path = metadata[idx]["url"]
|
211 |
-
return idx, md, img_path
|
212 |
-
|
213 |
-
uploaded_result_gallery.select(
|
214 |
-
handle_uploaded_click,
|
215 |
-
inputs=[uploaded_metadata_state],
|
216 |
-
outputs=[uploaded_selected_idx, uploaded_metadata_output, uploaded_reference_image]
|
217 |
-
)
|
218 |
|
219 |
back_button = gr.Button("Back to Home")
|
220 |
|
|
|
43 |
return df, embeddings
|
44 |
|
45 |
|
46 |
+
# from huggingface_hub import hf_hub_download
|
47 |
+
# def load_data1():
|
48 |
+
# # Login using e.g. `huggingface-cli login` to access this dataset
|
49 |
+
# path = hf_hub_download(
|
50 |
+
# repo_id="traopia/fashion_show_data_all_embeddings",
|
51 |
+
# filename="fashion_show_data_all_embeddings.json"
|
52 |
+
# )
|
53 |
+
# df = pd.read_json(path, lines = True)
|
54 |
+
|
55 |
+
# #df = pd.read_json("hf://datasets/traopia/fashion_show_data_all_embeddings.json/fashion_show_data_all_embeddings.json", lines=True)
|
56 |
+
# df["fashion_clip_image"] = df["fashion_clip_image"].apply(lambda x: x[0] if isinstance(x, list) else x)
|
57 |
+
# df["image_urls"] = df["image_urls"].apply(lambda x: x[0] if x is not None else None)
|
58 |
+
# df = df.rename(columns={"fashion_house":"designer", "image_urls":"url", "URL":"collection"})
|
59 |
+
|
60 |
+
# df = df.dropna(subset="fashion_clip_image")
|
61 |
+
# df = df.reset_index(drop=True)
|
62 |
+
# df["key"] = df.index
|
63 |
+
# embeddings = np.vstack(df["fashion_clip_image"].values)
|
64 |
+
|
65 |
+
# return df, embeddings
|
66 |
|
67 |
df, embeddings = load_data_hf()
|
68 |
|
|
|
107 |
|
108 |
|
109 |
|
110 |
+
# Gradio UI
|
111 |
with gr.Blocks() as demo:
|
112 |
gr.Markdown("# 👗 FashionDB Explorer")
|
113 |
|
114 |
+
with gr.Row():
|
115 |
+
fashion_house = gr.Dropdown(label="Fashion House", choices=sorted(df["designer"].dropna().unique()), multiselect=True)
|
116 |
+
category = gr.Dropdown(label="Category", choices=sorted(df["category"].dropna().unique()), multiselect=True)
|
117 |
+
season = gr.Dropdown(label="Season", choices=sorted(df["season"].dropna().unique()), multiselect=True)
|
118 |
+
|
119 |
+
|
120 |
+
min_year = int(df['year'].min())
|
121 |
+
max_year = int(df['year'].max())
|
122 |
+
|
123 |
+
start_year = gr.Slider(label="Start Year", minimum=min_year, maximum=max_year, value=2000, step=1)
|
124 |
+
end_year = gr.Slider(label="End Year", minimum=min_year, maximum=max_year, value=2024, step=1)
|
125 |
+
|
126 |
+
query = gr.Textbox(label="Search by text", placeholder="(optional): e.g., pink dress ")
|
127 |
+
search_button = gr.Button("Search by text")
|
128 |
+
|
129 |
+
uploaded_image = gr.Image(label="Upload an image", type="pil") # or type="pil" if you prefer PIL Image object
|
130 |
+
search_by_image_button = gr.Button("Search by Image")
|
131 |
+
|
132 |
+
def handle_search_by_image(uploaded_image):
|
133 |
+
if uploaded_image is None:
|
134 |
+
return [], "Please upload an image first."
|
135 |
+
results_df = search_images_by_image(uploaded_image, df, embeddings)
|
136 |
+
# Convert results DataFrame to image URLs (or paths) for gallery display
|
137 |
+
images = results_df['url'].tolist()
|
138 |
+
metadata = results_df.to_dict(orient='records')
|
139 |
+
return images, metadata, ""
|
140 |
+
|
141 |
+
uploaded_metadata_state = gr.State([])
|
142 |
+
uploaded_metadata_output = gr.Markdown()
|
143 |
+
uploaded_result_gallery = gr.Gallery(label="Search Results by Image", columns=5, height="auto")
|
144 |
+
|
145 |
+
search_by_image_button.click(
|
146 |
+
fn=handle_search_by_image,
|
147 |
+
inputs=[uploaded_image],
|
148 |
+
outputs=[uploaded_result_gallery, uploaded_metadata_state, uploaded_metadata_output]
|
149 |
+
)
|
150 |
+
|
151 |
+
result_gallery = gr.Gallery(label="Search Results", columns=5, height="auto")
|
152 |
+
metadata_output = gr.Markdown()
|
153 |
+
reference_image = gr.Image(label="Reference Image", interactive=False)
|
154 |
+
similar_gallery = gr.Gallery(label="Similar Images", columns = 5, height="auto")
|
155 |
+
|
156 |
+
metadata_state = gr.State([])
|
157 |
+
selected_idx = gr.Number(value=0, visible=False)
|
158 |
+
|
159 |
+
def handle_search(*args):
|
160 |
+
imgs, meta = filter_and_search(*args)
|
161 |
+
return imgs, meta, "", []
|
162 |
+
|
163 |
+
search_button.click(
|
164 |
+
handle_search,
|
165 |
+
inputs=[fashion_house, category, season, start_year, end_year, query],
|
166 |
+
outputs=[result_gallery, metadata_state, metadata_output, similar_gallery]
|
167 |
+
)
|
168 |
+
|
169 |
+
|
170 |
+
def handle_click(evt: gr.SelectData, metadata):
|
171 |
+
idx = evt.index
|
172 |
+
md = show_metadata(idx, metadata)
|
173 |
+
img_path = metadata[idx]["url"]
|
174 |
+
return idx, md, img_path
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
result_gallery.select(
|
179 |
+
handle_click,
|
180 |
+
inputs=[metadata_state],
|
181 |
+
outputs=[selected_idx, metadata_output, reference_image]
|
182 |
+
)
|
183 |
+
|
184 |
+
def show_similar(idx, metadata):
|
185 |
+
if idx is None or not str(idx).isdigit():
|
186 |
+
return [],[] # safe fallback
|
187 |
+
return find_similar(int(idx), metadata)
|
188 |
+
|
189 |
+
similar_metadata_state = gr.State()
|
190 |
+
similar_metadata_output = gr.Markdown()
|
191 |
+
|
192 |
+
show_similar_button = gr.Button("Show Similar Images")
|
193 |
+
show_similar_button.click(
|
194 |
+
show_similar,
|
195 |
+
inputs=[selected_idx, metadata_state],
|
196 |
+
outputs=[similar_gallery, similar_metadata_state]
|
197 |
+
)
|
198 |
+
|
199 |
+
|
200 |
+
def handle_similar_click(evt: gr.SelectData, metadata):
|
201 |
+
idx = evt.index
|
202 |
+
md = show_metadata(idx, metadata)
|
203 |
+
img_path = metadata[idx]["url"]
|
204 |
+
return idx, md, img_path
|
205 |
+
|
206 |
+
|
207 |
+
similar_gallery.select(
|
208 |
+
handle_similar_click,
|
209 |
+
inputs=[similar_metadata_state],
|
210 |
+
outputs=[selected_idx, similar_metadata_output, reference_image]
|
211 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
back_button = gr.Button("Back to Home")
|
214 |
|