import argparse import os import sys import gradio as gr from utils import load_embeddings from ui import create_demo, embeddings def main(): """Main entry point for the application""" parser = argparse.ArgumentParser(description='Run the Product Categorization web app') parser.add_argument('--embeddings', default='ingredient_embeddings_voyageai.pkl', help='Path to the ingredient embeddings pickle file') parser.add_argument('--share', action='store_true', help='Create a public link for sharing') args = parser.parse_args() # Check if embeddings file exists if not os.path.exists(args.embeddings): print(f"Error: Embeddings file {args.embeddings} not found!") print(f"Please ensure the file exists at {os.path.abspath(args.embeddings)}") sys.exit(1) # Load embeddings try: global embeddings embeddings_data = load_embeddings(args.embeddings) # Update the embeddings in the ui module import ui ui.embeddings = embeddings_data except Exception as e: print(f"Error loading embeddings: {e}") sys.exit(1) # Create and launch the interface demo = create_demo() # Register keyboard shortcuts for the interface demo.launch( share=args.share, # These are the keyboard shortcuts - use these options instead of custom JS show_api=False, # This sets the keyboard shortcuts at the Gradio level which is more reliable _js=""" document.addEventListener('keydown', function(e) { if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') { // Find visible buttons in the active tab const buttons = document.querySelectorAll('button.primary'); buttons.forEach(button => { // Check if the button is visible const rect = button.getBoundingClientRect(); if (rect.width > 0 && rect.height > 0 && getComputedStyle(button).display !== 'none') { // Click the first visible button button.click(); e.preventDefault(); return; } }); } }); """ ) if __name__ == "__main__": main()