File size: 2,582 Bytes
e17a56e
 
 
 
d8d2e1a
 
 
 
 
 
e17a56e
a496a8e
 
 
 
 
 
 
 
5bc4687
 
 
 
 
 
 
 
3ac6e08
 
 
 
d8d2e1a
 
 
 
0560e17
 
d8d2e1a
 
 
 
8dd8c70
5bc4687
 
8dd8c70
5bc4687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e17a56e
 
 
d8d2e1a
 
5bc4687
d8d2e1a
5bc4687
d8d2e1a
8dd8c70
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
    )