Spaces:
Sleeping
Sleeping
import gradio as gr | |
from utils import SafeProgress | |
from embeddings import create_product_embeddings | |
from similarity import compute_similarities | |
from chicory_api import call_chicory_parser | |
from ui_core import embeddings, parse_input | |
from ui_formatters import format_categories_html, create_results_container | |
def categorize_products(product_input, is_file=False, top_n=10, confidence_threshold=0.5, progress=gr.Progress()): | |
"""Categorize products from text input or file""" | |
progress_tracker = SafeProgress(progress) | |
progress_tracker(0, desc="Starting...") | |
# Parse input | |
product_names, error = parse_input(product_input, is_file) | |
if error: | |
return error | |
# Validate embeddings are loaded | |
if not embeddings: | |
return "<div style='color: #d32f2f; font-weight: bold; padding: 20px;'>Error: No ingredient embeddings loaded. Please check that the embeddings file exists and is properly formatted.</div>" | |
# Create embeddings | |
progress_tracker(0.2, desc="Generating product embeddings...") | |
products_embeddings = create_product_embeddings(product_names, progress=progress) | |
if not products_embeddings: | |
return "<div style='color: #d32f2f; font-weight: bold; padding: 20px;'>Error: Failed to generate product embeddings. Please try again with different product names.</div>" | |
# Call Chicory Parser API | |
progress_tracker(0.5, desc="Calling Chicory Parser API...") | |
chicory_results = call_chicory_parser(product_names, progress=progress) | |
# Compute similarities | |
progress_tracker(0.7, desc="Computing similarities...") | |
all_similarities = compute_similarities(embeddings, products_embeddings) | |
# Format results | |
progress_tracker(0.9, desc="Formatting results...") | |
output_html = "<div style='font-family: Arial, sans-serif; max-width: 100%; overflow-x: auto;'>" | |
output_html += f"<p style='color: #555;'>Processing {len(product_names)} products.</p>" | |
for product, similarities in all_similarities.items(): | |
filtered_similarities = [(ingredient, score) for ingredient, score in similarities if score >= confidence_threshold] | |
top_similarities = filtered_similarities[:int(top_n)] | |
# Debug info for Chicory results | |
chicory_data = chicory_results.get(product, []) | |
output_html += format_categories_html(product, top_similarities, chicory_result=chicory_data) | |
output_html += "<hr style='margin: 15px 0; border: 0; border-top: 1px solid #eee;'>" | |
output_html += "</div>" | |
if not all_similarities: | |
output_html = "<div style='color: #d32f2f; font-weight: bold; padding: 20px;'>No results found. Please check your input or try different products.</div>" | |
progress_tracker(1.0, desc="Done!") | |
return output_html | |