Spaces:
Runtime error
Runtime error
Anusha806
commited on
Commit
·
c1181ca
1
Parent(s):
8227357
commit24
Browse files
app.py
CHANGED
|
@@ -596,27 +596,156 @@ def search_by_image(uploaded_image, alpha=0.5, start=0, end=12):
|
|
| 596 |
return imgs_with_captions
|
| 597 |
|
| 598 |
# ------------------- UI -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
custom_css = """
|
| 600 |
-
.search-btn {
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
.gr-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
"""
|
| 606 |
|
| 607 |
with gr.Blocks(css=custom_css) as demo:
|
| 608 |
-
gr.Markdown("
|
| 609 |
|
| 610 |
with gr.Row(equal_height=True):
|
| 611 |
with gr.Column(scale=5, elem_classes="query-slider"):
|
| 612 |
-
query = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 613 |
alpha = gr.Slider(0, 1, value=0.5, label="Hybrid Weight (alpha: 0=sparse, 1=dense)")
|
| 614 |
-
gender_dropdown = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
| 615 |
with gr.Column(scale=1):
|
| 616 |
-
image_input = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
search_btn = gr.Button("Search", elem_classes="search-btn")
|
| 619 |
-
gallery = gr.Gallery(label="Search Results", columns=
|
| 620 |
load_more_btn = gr.Button("Load More")
|
| 621 |
|
| 622 |
search_offset = gr.State(0)
|
|
@@ -642,8 +771,11 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 642 |
seen_ids = {r[1] for r in results}
|
| 643 |
return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 644 |
|
| 645 |
-
search_btn.click(
|
| 646 |
-
|
|
|
|
|
|
|
|
|
|
| 647 |
|
| 648 |
def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 649 |
start = offset
|
|
@@ -670,8 +802,11 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 670 |
|
| 671 |
return combined, end, combined, updated_ids
|
| 672 |
|
| 673 |
-
load_more_btn.click(
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
| 675 |
|
| 676 |
gr.Markdown("🧠 Powered by OpenAI + Hybrid AI Fashion Search")
|
| 677 |
|
|
|
|
| 596 |
return imgs_with_captions
|
| 597 |
|
| 598 |
# ------------------- UI -------------------
|
| 599 |
+
# custom_css = """
|
| 600 |
+
# .search-btn { width: 100%; }
|
| 601 |
+
# .gr-row { gap: 8px !important; }
|
| 602 |
+
# .query-slider > div { margin-bottom: 4px !important; }
|
| 603 |
+
# .gr-gallery-item { width: 256px !important; height: 256px !important; }
|
| 604 |
+
# .gr-gallery-item img { width: 100% !important; height: 100% !important; object-fit: cover !important; }
|
| 605 |
+
# """
|
| 606 |
+
|
| 607 |
+
# with gr.Blocks(css=custom_css) as demo:
|
| 608 |
+
# gr.Markdown("# 🛍️ Fashion Product Hybrid Search (with GPT-4 powered query parsing)")
|
| 609 |
+
|
| 610 |
+
# with gr.Row(equal_height=True):
|
| 611 |
+
# with gr.Column(scale=5, elem_classes="query-slider"):
|
| 612 |
+
# query = gr.Textbox(label="Enter your fashion search query", placeholder="e.g., black sneakers for women")
|
| 613 |
+
# alpha = gr.Slider(0, 1, value=0.5, label="Hybrid Weight (alpha: 0=sparse, 1=dense)")
|
| 614 |
+
# gender_dropdown = gr.Dropdown(["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"], label="Gender Filter (optional)")
|
| 615 |
+
# with gr.Column(scale=1):
|
| 616 |
+
# image_input = gr.Image(type="pil", label="Upload an image (optional)", sources=["upload", "clipboard"], height=256, width=356)
|
| 617 |
+
|
| 618 |
+
# search_btn = gr.Button("Search", elem_classes="search-btn")
|
| 619 |
+
# gallery = gr.Gallery(label="Search Results", columns=6, height=None)
|
| 620 |
+
# load_more_btn = gr.Button("Load More")
|
| 621 |
+
|
| 622 |
+
# search_offset = gr.State(0)
|
| 623 |
+
# current_query = gr.State("")
|
| 624 |
+
# current_image = gr.State(None)
|
| 625 |
+
# current_gender = gr.State("")
|
| 626 |
+
# shown_results = gr.State([])
|
| 627 |
+
# shown_ids = gr.State(set())
|
| 628 |
+
|
| 629 |
+
# def unified_search(q, uploaded_image, a, offset, gender_ui):
|
| 630 |
+
# start = 0
|
| 631 |
+
# end = 12
|
| 632 |
+
# filters = extract_intent_from_openai(q) if q.strip() else {}
|
| 633 |
+
# gender_override = gender_ui if gender_ui else filters.get("gender")
|
| 634 |
+
|
| 635 |
+
# if uploaded_image is not None:
|
| 636 |
+
# results = search_by_image(uploaded_image, a, start, end)
|
| 637 |
+
# elif q.strip():
|
| 638 |
+
# results = search_fashion(q, a, start, end, gender_override)
|
| 639 |
+
# else:
|
| 640 |
+
# results = []
|
| 641 |
+
|
| 642 |
+
# seen_ids = {r[1] for r in results}
|
| 643 |
+
# return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 644 |
+
|
| 645 |
+
# search_btn.click(unified_search, inputs=[query, image_input, alpha, search_offset, gender_dropdown],
|
| 646 |
+
# outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids])
|
| 647 |
+
|
| 648 |
+
# def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 649 |
+
# start = offset
|
| 650 |
+
# end = offset + 12
|
| 651 |
+
# gender_override = gender_ui
|
| 652 |
+
|
| 653 |
+
# if img is not None:
|
| 654 |
+
# new_results = search_by_image(img, a, start, end)
|
| 655 |
+
# elif q.strip():
|
| 656 |
+
# new_results = search_fashion(q, a, start, end, gender_override)
|
| 657 |
+
# else:
|
| 658 |
+
# new_results = []
|
| 659 |
+
|
| 660 |
+
# filtered_new = []
|
| 661 |
+
# new_ids = set()
|
| 662 |
+
# for item in new_results:
|
| 663 |
+
# img_obj, caption = item
|
| 664 |
+
# if caption not in prev_ids:
|
| 665 |
+
# filtered_new.append(item)
|
| 666 |
+
# new_ids.add(caption)
|
| 667 |
+
|
| 668 |
+
# combined = prev_results + filtered_new
|
| 669 |
+
# updated_ids = prev_ids.union(new_ids)
|
| 670 |
+
|
| 671 |
+
# return combined, end, combined, updated_ids
|
| 672 |
+
|
| 673 |
+
# load_more_btn.click(load_more_fn, inputs=[alpha, search_offset, current_query, current_image, current_gender, shown_results, shown_ids],
|
| 674 |
+
# outputs=[gallery, search_offset, shown_results, shown_ids])
|
| 675 |
+
|
| 676 |
+
# gr.Markdown("🧠 Powered by OpenAI + Hybrid AI Fashion Search")
|
| 677 |
+
|
| 678 |
+
# demo.launch()
|
| 679 |
+
import gradio as gr
|
| 680 |
+
|
| 681 |
custom_css = """
|
| 682 |
+
.search-btn {
|
| 683 |
+
width: 100%;
|
| 684 |
+
}
|
| 685 |
+
|
| 686 |
+
.gr-row {
|
| 687 |
+
gap: 8px !important;
|
| 688 |
+
flex-wrap: wrap;
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
+
.query-slider > div {
|
| 692 |
+
margin-bottom: 4px !important;
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
.gr-gallery-item {
|
| 696 |
+
width: 100% !important;
|
| 697 |
+
max-width: 256px !important;
|
| 698 |
+
height: auto !important;
|
| 699 |
+
margin: auto;
|
| 700 |
+
}
|
| 701 |
+
|
| 702 |
+
.gr-gallery-item img {
|
| 703 |
+
width: 100% !important;
|
| 704 |
+
height: auto !important;
|
| 705 |
+
object-fit: cover !important;
|
| 706 |
+
}
|
| 707 |
+
|
| 708 |
+
@media (max-width: 768px) {
|
| 709 |
+
.gr-column {
|
| 710 |
+
width: 100% !important;
|
| 711 |
+
flex: 100% !important;
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
.search-btn {
|
| 715 |
+
font-size: 16px;
|
| 716 |
+
padding: 10px;
|
| 717 |
+
}
|
| 718 |
+
|
| 719 |
+
.gr-gallery-item {
|
| 720 |
+
max-width: 100% !important;
|
| 721 |
+
}
|
| 722 |
+
}
|
| 723 |
"""
|
| 724 |
|
| 725 |
with gr.Blocks(css=custom_css) as demo:
|
| 726 |
+
gr.Markdown("## 🛍️ Fashion Product Hybrid Search (with GPT-4 powered query parsing)")
|
| 727 |
|
| 728 |
with gr.Row(equal_height=True):
|
| 729 |
with gr.Column(scale=5, elem_classes="query-slider"):
|
| 730 |
+
query = gr.Textbox(
|
| 731 |
+
label="Enter your fashion search query",
|
| 732 |
+
placeholder="e.g., black sneakers for women"
|
| 733 |
+
)
|
| 734 |
alpha = gr.Slider(0, 1, value=0.5, label="Hybrid Weight (alpha: 0=sparse, 1=dense)")
|
| 735 |
+
gender_dropdown = gr.Dropdown(
|
| 736 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 737 |
+
label="Gender Filter (optional)"
|
| 738 |
+
)
|
| 739 |
with gr.Column(scale=1):
|
| 740 |
+
image_input = gr.Image(
|
| 741 |
+
type="pil",
|
| 742 |
+
label="Upload an image (optional)",
|
| 743 |
+
sources=["upload", "clipboard"],
|
| 744 |
+
height="auto"
|
| 745 |
+
)
|
| 746 |
|
| 747 |
search_btn = gr.Button("Search", elem_classes="search-btn")
|
| 748 |
+
gallery = gr.Gallery(label="Search Results", columns="auto", height=None, allow_preview=True)
|
| 749 |
load_more_btn = gr.Button("Load More")
|
| 750 |
|
| 751 |
search_offset = gr.State(0)
|
|
|
|
| 771 |
seen_ids = {r[1] for r in results}
|
| 772 |
return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 773 |
|
| 774 |
+
search_btn.click(
|
| 775 |
+
unified_search,
|
| 776 |
+
inputs=[query, image_input, alpha, search_offset, gender_dropdown],
|
| 777 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 778 |
+
)
|
| 779 |
|
| 780 |
def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 781 |
start = offset
|
|
|
|
| 802 |
|
| 803 |
return combined, end, combined, updated_ids
|
| 804 |
|
| 805 |
+
load_more_btn.click(
|
| 806 |
+
load_more_fn,
|
| 807 |
+
inputs=[alpha, search_offset, current_query, current_image, current_gender, shown_results, shown_ids],
|
| 808 |
+
outputs=[gallery, search_offset, shown_results, shown_ids]
|
| 809 |
+
)
|
| 810 |
|
| 811 |
gr.Markdown("🧠 Powered by OpenAI + Hybrid AI Fashion Search")
|
| 812 |
|