import os import sys from dotenv import load_dotenv # Import load_dotenv import streamlit as st # Load environment variables from .env file at the very beginning load_dotenv() from utils import load_embeddings from ui import render_ui # Import the new Streamlit UI function # Set page config as the first Streamlit command st.set_page_config(layout="wide", page_title="Product Categorization Tool") import ui_core # Import ui_core to set embeddings # Path to the embeddings file EMBEDDINGS_PATH = "data/ingredient_embeddings_voyageai.pkl" # Use Streamlit's caching to load embeddings only once @st.cache_data def load_all_embeddings(path): """Loads embeddings from the specified path.""" if not os.path.exists(path): st.error(f"Error: Embeddings file {path} not found!") st.error(f"Please ensure the file exists at {os.path.abspath(path)}") st.stop() # Stop execution if file not found return None # Return None explicitly, although st.stop() halts try: embeddings_data = load_embeddings(path) return embeddings_data except Exception as e: st.error(f"Error loading embeddings: {e}") st.stop() return None # Load embeddings and make them available to UI modules embeddings_data = load_all_embeddings(EMBEDDINGS_PATH) if embeddings_data: # Pass the loaded embeddings to the ui_core module where other UI modules import it from ui_core.embeddings = embeddings_data # Render the Streamlit UI render_ui() else: # This part should ideally not be reached due to st.stop() in load_all_embeddings st.error("Failed to load embeddings. Application cannot start.") # Note: No __main__ block needed for Streamlit. # Streamlit apps are run using `streamlit run app.py`