esilver's picture
Fixed bugs
3b88add
raw
history blame
11 kB
import gradio as gr
from comparison import compare_ingredient_methods_ui
# Import from our UI modules
from ui_core import embeddings, get_css, load_examples
from ui_ingredient_matching import categorize_products
from ui_category_matching import categorize_products_by_category
from ui_hybrid_matching import categorize_products_with_voyage_reranking
from ui_expanded_matching import categorize_products_with_openai_reranking
def create_demo():
"""Create the Gradio interface"""
with gr.Blocks(css=get_css()) as demo:
gr.Markdown("# Product Categorization Tool\nAnalyze products by matching to ingredients or categories using AI embeddings.")
with gr.Tabs() as tabs:
# Original Ingredient Matching Tab
with gr.TabItem("Ingredient Embeddings"):
with gr.Row():
with gr.Column(scale=1):
# Input section
text_input = gr.Textbox(
lines=10,
placeholder="Enter product names, one per line",
label="Product Names"
)
input_controls = gr.Row()
with input_controls:
use_expansion = gr.Checkbox(
value=False,
label="Use Description Expansion",
info="Expand product descriptions using AI before matching"
)
top_n = gr.Slider(1, 25, 10, step=1, label="Top N Results")
confidence = gr.Slider(0.1, 0.9, 0.5, label="Similarity Threshold")
with gr.Row():
examples_btn = gr.Button("Load Examples", variant="secondary")
categorize_btn = gr.Button("Find Similar Ingredients", variant="primary")
with gr.Column(scale=1):
# Results section
text_output = gr.HTML(label="Similar Ingredients Results", elem_id="results-container")
# New Category Matching Tab
with gr.TabItem("Category Embeddings"):
with gr.Row():
with gr.Column(scale=1):
# Input section
category_text_input = gr.Textbox(
lines=10,
placeholder="Enter product names, one per line",
label="Product Names"
)
category_input_controls = gr.Row()
with category_input_controls:
category_use_expansion = gr.Checkbox(
value=False,
label="Use Description Expansion",
info="Expand product descriptions using AI before matching"
)
category_top_n = gr.Slider(1, 10, 5, step=1, label="Top N Categories")
category_confidence = gr.Slider(0.1, 0.9, 0.5, label="Matching Threshold")
with gr.Row():
category_examples_btn = gr.Button("Load Examples", variant="secondary")
match_categories_btn = gr.Button("Match to Categories", variant="primary")
with gr.Column(scale=1):
# Results section
category_output = gr.HTML(label="Category Matching Results", elem_id="results-container")
# Common function to create reranking UI tabs
def create_reranking_tab(tab_name, fn_name, default_match="ingredients"):
with gr.TabItem(tab_name):
with gr.Row():
with gr.Column(scale=1):
# Input section
tab_input = gr.Textbox(
lines=10,
placeholder="Enter product names, one per line",
label="Product Names"
)
with gr.Row():
tab_expansion = gr.Checkbox(
value=False,
label="Use Description Expansion",
info="Expand product descriptions using AI before matching"
)
tab_emb_top_n = gr.Slider(1, 50, 20, step=1, label="Embedding Top N Results")
tab_top_n = gr.Slider(1, 10, 5, step=1, label="Final Top N Results")
tab_confidence = gr.Slider(0.1, 0.9, 0.5, label="Matching Threshold")
tab_match_type = gr.Radio(
choices=["categories", "ingredients"],
value=default_match,
label="Match Type",
info="Choose whether to match against ingredients or categories"
)
with gr.Row():
tab_examples_btn = gr.Button("Load Examples", variant="secondary")
tab_match_btn = gr.Button(f"Match using {tab_name}", variant="primary")
with gr.Column(scale=1):
# Results section
tab_output = gr.HTML(label=f"{tab_name} Results", elem_id="results-container")
# Connect button events
tab_match_btn.click(
fn=fn_name,
inputs=[tab_input, gr.State(False), tab_expansion, tab_emb_top_n,
tab_top_n, tab_confidence, tab_match_type],
outputs=[tab_output],
)
tab_examples_btn.click(
fn=load_examples,
inputs=[],
outputs=tab_input
)
# Create the reranking tabs using the shared function
create_reranking_tab("Voyage AI Reranking", categorize_products_with_voyage_reranking, "categories")
create_reranking_tab("OpenAI Reranking", categorize_products_with_openai_reranking, "categories")
# New Comparison Tab
with gr.TabItem("Compare Methods"):
with gr.Row():
with gr.Column():
compare_product_input = gr.Textbox(
label="Enter product names (one per line)",
placeholder="4 Tbsp sweet pickle relish\nchocolate chips\nfresh parsley",
lines=5
)
with gr.Row():
compare_embedding_top_n = gr.Slider(
minimum=5, maximum=50, value=20, step=5,
label="Initial embedding candidates"
)
compare_final_top_n = gr.Slider(
minimum=1, maximum=10, value=3, step=1,
label="Final results per method"
)
compare_confidence_threshold = gr.Slider(
minimum=0.0, maximum=1.0, value=0.5, step=0.05,
label="Confidence threshold"
)
compare_match_type = gr.Radio(
choices=["categories", "ingredients"],
value="categories",
label="Match Type",
info="Choose whether to match against ingredients or categories"
)
# Add expansion checkbox
compare_expansion = gr.Checkbox(
value=False,
label="Use Description Expansion",
info="Expand product descriptions using AI before matching"
)
compare_btn = gr.Button("Compare Methods", variant="primary")
compare_examples_btn = gr.Button("Load Examples", variant="secondary")
with gr.Column():
comparison_output = gr.HTML(label="Results", elem_id="results-container")
# Connect the compare button
compare_btn.click(
fn=compare_ingredient_methods_ui,
inputs=[
compare_product_input,
compare_embedding_top_n,
compare_final_top_n,
compare_confidence_threshold,
compare_match_type,
compare_expansion
],
outputs=comparison_output
)
# Add examples button functionality
compare_examples_btn.click(
fn=load_examples,
inputs=[],
outputs=compare_product_input
)
# Connect buttons for ingredient matching
categorize_btn.click(
fn=categorize_products,
inputs=[text_input, gr.State(False), use_expansion, top_n, confidence],
outputs=[text_output],
)
# Connect buttons for category matching
match_categories_btn.click(
fn=categorize_products_by_category,
inputs=[category_text_input, gr.State(False), category_use_expansion, category_top_n, category_confidence],
outputs=[category_output],
)
# Examples buttons for the first two tabs
examples_btn.click(
fn=load_examples,
inputs=[],
outputs=text_input
)
category_examples_btn.click(
fn=load_examples, # Reuse the same examples
inputs=[],
outputs=category_text_input
)
gr.Markdown("Powered by Voyage AI embeddings • Built with Gradio")
return demo