import os import sys from pathlib import Path # Add src to the Python path # HF Spaces has the repo content in the root. # Our package is in src/iks_rag sys.path.insert(0, "src") print("🚀 Initializing IKS RAG System on HuggingFace Spaces...") # Check environment print(f"Python Version: {sys.version}") print(f"CWD: {os.getcwd()}") # Import after path is set try: from iks_rag.ui.gradio_app import create_interface from iks_rag.rag_system import create_rag_system print("✅ Successfully imported iks_rag modules") except ImportError as e: print(f"❌ ERROR: Failed to import iks_rag modules: {e}") print(f"Python path: {sys.path}") # Create a simple fallback UI if imports fail to show the error on the page import gradio as gr demo = gr.Interface(fn=lambda x: f"Import Error: {e}", inputs="text", outputs="text") demo.launch() sys.exit(1) # Make sure Gemini key is loaded from space secrets api_key = os.environ.get("GOOGLE_API_KEY", "") if not api_key: print("⚠️ WARNING: GOOGLE_API_KEY not found in environment variables!") print("Please set GOOGLE_API_KEY in the 'Settings > Secrets' section of your Space.") # Create RAG system using our default config config_path = "configs/rag/default.yaml" if not os.path.exists(config_path): print(f"❌ ERROR: Config file not found at {config_path}") else: print(f"✅ Found config at {config_path}") try: rag_system = create_rag_system(config_path) print("✅ RAG system initialized") except Exception as e: print(f"❌ ERROR initializing RAG system: {e}") rag_system = None # Check data and load if rag_system: docs_path = "data/documents" if not os.path.exists(docs_path) or not os.listdir(docs_path): print(f"⚠️ WARNING: No documents found in {docs_path}") else: print(f"📚 Loading documents from {docs_path}...") try: rag_system.load_documents() stats = rag_system.get_stats() print(f"✅ Loaded {stats['documents_loaded']} document chunks into ChromaDB") except Exception as e: print(f"❌ ERROR loading documents: {str(e)}") # Launch the Gradio Interface print("🌐 Launching Gradio Interface...") if rag_system: demo = create_interface(rag_system) else: import gradio as gr demo = gr.Interface(fn=lambda x: "RAG System failed to initialize", inputs="text", outputs="text") if __name__ == "__main__": demo.launch()