Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import gradio as gr | |
| from dotenv import load_dotenv | |
| # Add the current directory to the path so we can import from src | |
| sys.path.append(".") | |
| # Load environment variables | |
| load_dotenv() | |
| # Ensure OpenAI API key is available | |
| api_key = os.environ.get("OPENAI_API_KEY") | |
| if not api_key: | |
| print("Warning: OPENAI_API_KEY environment variable not found.") | |
| print("OpenAI API calls will fail unless the key is provided.") | |
| else: | |
| print("OpenAI API key loaded successfully.") | |
| # Import our custom modules | |
| from src.utils.styles import get_custom_css | |
| from src.utils.ui import create_interface, create_fallback_ui | |
| from src.utils.resource_manager import ResourceManager, check_data_files | |
| from src.utils.query_handler import process_query_with_status | |
| # Check if data files exist and load them | |
| data_ready = check_data_files() | |
| # Create the global resource manager | |
| resource_manager = ResourceManager() | |
| # Try importing modules | |
| director = None | |
| CHAT_MODEL = None | |
| try: | |
| # We'll import these in the specific functions where needed | |
| data_ready = data_ready and os.path.exists('src/agents/agent_director.py') | |
| except ImportError as e: | |
| print(f"Error importing modules: {e}") | |
| print("This may be caused by missing data files or module dependencies.") | |
| data_ready = False | |
| # Get the custom CSS | |
| custom_css = get_custom_css() | |
| # Define a wrapper function for processing queries | |
| def process_query_wrapper(query, top_k=50, debug=False, progress=None): | |
| """Wrapper function to process queries with the resource manager.""" | |
| try: | |
| # Get the director from the resource manager | |
| director = resource_manager.get_director() | |
| return process_query_with_status(query, top_k, debug, progress, director) | |
| except Exception as e: | |
| import traceback | |
| print(f"Error in process_query_wrapper: {e}") | |
| traceback.print_exc() | |
| return (f""" | |
| <div class='status-error'> | |
| <strong>ERROR:</strong> An unexpected error occurred: {str(e)} | |
| <p>Please try again or contact support if the problem persists.</p> | |
| </div> | |
| """, "") | |
| # Launch the app | |
| if __name__ == "__main__": | |
| # Create and launch the appropriate interface based on data readiness | |
| if data_ready: | |
| demo = create_interface(custom_css, process_query_wrapper) | |
| else: | |
| demo = create_fallback_ui(custom_css) | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| debug=True, | |
| favicon_path=None # Clear the favicon to prevent theme inheritance issues | |
| ) |