#!/usr/bin/env python3 """ Hugging Face Space app for Second Brain AI Assistant. Direct implementation for HF Space deployment. """ import os import sys from pathlib import Path # Add the current directory and src directory to Python path sys.path.append('.') sys.path.append('src') from second_brain_online.application.agents import get_agent from second_brain_online.application.ui import CustomGradioUI # Try to import opik_utils, but handle import errors gracefully try: from second_brain_online import opik_utils OPIK_UTILS_AVAILABLE = True except ImportError as e: print(f"⚠️ OPIK utils not available: {e}") opik_utils = None OPIK_UTILS_AVAILABLE = False def main(): """Main function for Hugging Face Space deployment.""" # Set default values for HF Spaces retriever_config_path = os.getenv("RETRIEVER_CONFIG_PATH", "configs/compute_rag_vector_index_openai_contextual.yaml") print("🚀 Starting Second Brain AI Assistant...") print(f"📁 Using retriever config: {retriever_config_path}") # Debug: Check environment variables print("🔍 Environment variables check:") print(f" COMET_API_KEY: {'SET' if os.getenv('COMET_API_KEY') else 'NOT SET'}") print(f" COMET_PROJECT: {os.getenv('COMET_PROJECT', 'NOT SET')}") print(f" OPENAI_API_KEY: {'SET' if os.getenv('OPENAI_API_KEY') else 'NOT SET'}") print(f" MONGODB_URI: {'SET' if os.getenv('MONGODB_URI') else 'NOT SET'}") # Configure OPIK (optional - will use default project if COMET_PROJECT not set) if OPIK_UTILS_AVAILABLE: try: opik_utils.configure() print("✅ OPIK configured successfully") except Exception as e: print(f"⚠️ OPIK configuration failed: {e}") else: print("⚠️ OPIK utils not available, skipping configuration") try: # Initialize agent agent = get_agent(retriever_config_path=Path(retriever_config_path)) # Pass the AgentWrapper directly so it uses our custom run() method with extraction logic # Launch custom UI CustomGradioUI(agent).launch( server_name="0.0.0.0", server_port=7860, share=False ) except Exception as e: print(f"❌ Error starting the application: {e}") raise if __name__ == "__main__": main()