Spaces:
Runtime error
Runtime error
Anusha806
commited on
Commit
ยท
295c69a
1
Parent(s):
6719177
commit37
Browse files
app.py
CHANGED
|
@@ -1086,136 +1086,125 @@ button[role="tab"]:hover {
|
|
| 1086 |
|
| 1087 |
"""
|
| 1088 |
|
| 1089 |
-
|
| 1090 |
with gr.Blocks(css=custom_css) as demo:
|
| 1091 |
-
|
| 1092 |
-
|
| 1093 |
-
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
-
|
| 1134 |
-
|
| 1135 |
-
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
| 1147 |
-
|
| 1148 |
-
|
| 1149 |
-
|
| 1150 |
-
|
| 1151 |
-
|
| 1152 |
-
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
| 1157 |
-
|
| 1158 |
-
|
| 1159 |
-
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
|
| 1163 |
-
return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 1164 |
-
|
| 1165 |
-
# Text Search
|
| 1166 |
-
# Text Search
|
| 1167 |
-
text_search_btn.click(
|
| 1168 |
-
unified_search,
|
| 1169 |
-
inputs=[query, gr.State(None), alpha, search_offset, gender_dropdown],
|
| 1170 |
-
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1171 |
-
)
|
| 1172 |
-
|
| 1173 |
-
voice_search_btn.click(
|
| 1174 |
-
handle_voice_search,
|
| 1175 |
-
inputs=[voice_input, alpha, search_offset, voice_gender_dropdown],
|
| 1176 |
-
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1177 |
-
)
|
| 1178 |
-
|
| 1179 |
-
# Image Search
|
| 1180 |
-
image_search_btn.click(
|
| 1181 |
-
unified_search,
|
| 1182 |
-
inputs=[gr.State(""), image_input, alpha, search_offset, image_gender_dropdown],
|
| 1183 |
-
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1184 |
-
)
|
| 1185 |
-
|
| 1186 |
-
# --- Load More Button ---
|
| 1187 |
-
def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 1188 |
-
start = offset
|
| 1189 |
-
end = offset + 12
|
| 1190 |
-
gender_override = gender_ui
|
| 1191 |
-
|
| 1192 |
-
if img is not None:
|
| 1193 |
-
new_results = search_by_image(img, a, start, end)
|
| 1194 |
-
elif q.strip():
|
| 1195 |
-
new_results = search_fashion(q, a, start, end, gender_override)
|
| 1196 |
-
else:
|
| 1197 |
-
new_results = []
|
| 1198 |
-
|
| 1199 |
-
filtered_new = []
|
| 1200 |
-
new_ids = set()
|
| 1201 |
-
for item in new_results:
|
| 1202 |
-
img_obj, caption = item
|
| 1203 |
-
if caption not in prev_ids:
|
| 1204 |
-
filtered_new.append(item)
|
| 1205 |
-
new_ids.add(caption)
|
| 1206 |
-
|
| 1207 |
-
combined = prev_results + filtered_new
|
| 1208 |
-
updated_ids = prev_ids.union(new_ids)
|
| 1209 |
|
| 1210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1211 |
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
|
| 1216 |
-
|
|
|
|
| 1217 |
|
| 1218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1219 |
|
| 1220 |
demo.launch()
|
| 1221 |
|
|
|
|
| 1086 |
|
| 1087 |
"""
|
| 1088 |
|
|
|
|
| 1089 |
with gr.Blocks(css=custom_css) as demo:
|
| 1090 |
+
with gr.Column(elem_id="app-bg"):
|
| 1091 |
+
gr.Markdown("# ๐๏ธ Fashion Product Hybrid Search")
|
| 1092 |
+
|
| 1093 |
+
alpha = gr.Slider(0, 1, value=0.5, label="Hybrid Weight (alpha: 0=sparse, 1=dense)")
|
| 1094 |
+
|
| 1095 |
+
with gr.Tabs():
|
| 1096 |
+
with gr.Tab("Text Search"):
|
| 1097 |
+
query = gr.Textbox(
|
| 1098 |
+
label="Text Query",
|
| 1099 |
+
placeholder="e.g., floral summer dress for women"
|
| 1100 |
+
)
|
| 1101 |
+
gender_dropdown = gr.Dropdown(
|
| 1102 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 1103 |
+
label="Gender Filter (optional)"
|
| 1104 |
+
)
|
| 1105 |
+
text_search_btn = gr.Button("Search by Text", elem_classes="search-btn")
|
| 1106 |
+
|
| 1107 |
+
with gr.Tab("๐๏ธ Voice Search"):
|
| 1108 |
+
voice_input = gr.Audio(label="Speak Your Query", type="filepath")
|
| 1109 |
+
voice_gender_dropdown = gr.Dropdown(
|
| 1110 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 1111 |
+
label="Gender"
|
| 1112 |
+
)
|
| 1113 |
+
voice_search_btn = gr.Button("Search by Voice")
|
| 1114 |
+
|
| 1115 |
+
with gr.Tab("Image Search"):
|
| 1116 |
+
image_input = gr.Image(
|
| 1117 |
+
type="pil",
|
| 1118 |
+
label="Upload an image",
|
| 1119 |
+
sources=["upload", "clipboard"],
|
| 1120 |
+
height=400
|
| 1121 |
+
)
|
| 1122 |
+
image_gender_dropdown = gr.Dropdown(
|
| 1123 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 1124 |
+
label="Gender Filter (optional)"
|
| 1125 |
+
)
|
| 1126 |
+
image_search_btn = gr.Button("Search by Image", elem_classes="search-btn")
|
| 1127 |
+
|
| 1128 |
+
gallery = gr.Gallery(label="Search Results", columns=6, height=None)
|
| 1129 |
+
load_more_btn = gr.Button("Load More")
|
| 1130 |
+
|
| 1131 |
+
# --- UI State Holders ---
|
| 1132 |
+
search_offset = gr.State(0)
|
| 1133 |
+
current_query = gr.State("")
|
| 1134 |
+
current_image = gr.State(None)
|
| 1135 |
+
current_gender = gr.State("")
|
| 1136 |
+
shown_results = gr.State([])
|
| 1137 |
+
shown_ids = gr.State(set())
|
| 1138 |
+
|
| 1139 |
+
# --- Unified Search Function ---
|
| 1140 |
+
def unified_search(q, uploaded_image, a, offset, gender_ui):
|
| 1141 |
+
start = 0
|
| 1142 |
+
end = 12
|
| 1143 |
+
filters = extract_intent_from_openai(q) if q.strip() else {}
|
| 1144 |
+
gender_override = gender_ui if gender_ui else filters.get("gender")
|
| 1145 |
+
|
| 1146 |
+
if uploaded_image is not None:
|
| 1147 |
+
results = search_by_image(uploaded_image, a, start, end)
|
| 1148 |
+
elif q.strip():
|
| 1149 |
+
results = search_fashion(q, a, start, end, gender_override)
|
| 1150 |
+
else:
|
| 1151 |
+
results = []
|
| 1152 |
+
|
| 1153 |
+
seen_ids = {r[1] for r in results}
|
| 1154 |
+
return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 1155 |
+
|
| 1156 |
+
# --- Text Search Click Handler ---
|
| 1157 |
+
text_search_btn.click(
|
| 1158 |
+
unified_search,
|
| 1159 |
+
inputs=[query, gr.State(None), alpha, search_offset, gender_dropdown],
|
| 1160 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1161 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1162 |
|
| 1163 |
+
# --- Voice Search Click Handler ---
|
| 1164 |
+
voice_search_btn.click(
|
| 1165 |
+
handle_voice_search,
|
| 1166 |
+
inputs=[voice_input, alpha, search_offset, voice_gender_dropdown],
|
| 1167 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1168 |
+
)
|
| 1169 |
|
| 1170 |
+
# --- Image Search Click Handler ---
|
| 1171 |
+
image_search_btn.click(
|
| 1172 |
+
unified_search,
|
| 1173 |
+
inputs=[gr.State(""), image_input, alpha, search_offset, image_gender_dropdown],
|
| 1174 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 1175 |
+
)
|
| 1176 |
|
| 1177 |
+
# --- Load More Button Function ---
|
| 1178 |
+
def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 1179 |
+
start = offset
|
| 1180 |
+
end = offset + 12
|
| 1181 |
+
gender_override = gender_ui
|
| 1182 |
+
|
| 1183 |
+
if img is not None:
|
| 1184 |
+
new_results = search_by_image(img, a, start, end)
|
| 1185 |
+
elif q.strip():
|
| 1186 |
+
new_results = search_fashion(q, a, start, end, gender_override)
|
| 1187 |
+
else:
|
| 1188 |
+
new_results = []
|
| 1189 |
+
|
| 1190 |
+
filtered_new = []
|
| 1191 |
+
new_ids = set()
|
| 1192 |
+
for item in new_results:
|
| 1193 |
+
img_obj, caption = item
|
| 1194 |
+
if caption not in prev_ids:
|
| 1195 |
+
filtered_new.append(item)
|
| 1196 |
+
new_ids.add(caption)
|
| 1197 |
+
|
| 1198 |
+
combined = prev_results + filtered_new
|
| 1199 |
+
updated_ids = prev_ids.union(new_ids)
|
| 1200 |
+
|
| 1201 |
+
return combined, end, combined, updated_ids
|
| 1202 |
+
|
| 1203 |
+
load_more_btn.click(
|
| 1204 |
+
load_more_fn,
|
| 1205 |
+
inputs=[alpha, search_offset, current_query, current_image, current_gender, shown_results, shown_ids],
|
| 1206 |
+
outputs=[gallery, search_offset, shown_results, shown_ids]
|
| 1207 |
+
)
|
| 1208 |
|
| 1209 |
demo.launch()
|
| 1210 |
|