File size: 2,666 Bytes
461adca
 
 
 
 
 
3e63a13
 
461adca
 
3e63a13
 
 
 
b79a815
 
 
 
 
 
 
 
 
 
 
 
22df219
 
 
 
 
461adca
 
 
 
 
22df219
461adca
 
 
ca73321
 
 
 
 
 
 
 
 
 
461adca
 
 
 
 
a09e8cb
22df219
a09e8cb
 
461adca
 
 
a09e8cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Hugging Face Spaces entry point for Legal Position AI Analyzer
"""
import os
import sys
import warnings
import logging
from pathlib import Path

# Suppress asyncio event loop __del__ warnings (known Python 3.11 issue in threaded envs)
warnings.filterwarnings("ignore", message=".*Invalid file descriptor.*")
logging.getLogger("asyncio").setLevel(logging.CRITICAL)

# Suppress "Exception ignored in: BaseEventLoop.__del__" on HF Spaces shutdown
# warnings.filterwarnings does NOT catch these — they go via sys.unraisablehook
_original_unraisablehook = sys.unraisablehook

def _suppress_asyncio_fd_errors(unraisable):
    if (unraisable.exc_type is ValueError and
            "Invalid file descriptor" in str(unraisable.exc_value)):
        return  # silently ignore
    _original_unraisablehook(unraisable)

sys.unraisablehook = _suppress_asyncio_fd_errors

def configure_runtime_environment() -> None:
    """Apply runtime defaults for Hugging Face Spaces / Gradio deployment."""
    os.environ.setdefault('GRADIO_SERVER_NAME', '0.0.0.0')
    os.environ.setdefault('GRADIO_SERVER_PORT', '7860')
    os.environ.setdefault('UVICORN_LOOP', 'asyncio')


# Add project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
configure_runtime_environment()

# Import and launch interface
from interface import create_gradio_interface
from main import initialize_components

# Initialize search components at module level (required for HF Spaces)
# On HF Spaces, __main__ block never runs, so this must be called here.
print("Initializing search components...")
_init_ok = initialize_components()
if _init_ok:
    print("Search components initialized successfully!")
else:
    print("[WARNING] Search components initialization failed. Search functionality will be limited.")

# Create Gradio interface (at module level for HF Spaces)
demo = create_gradio_interface()

if __name__ == "__main__":
    print("🚀 Starting Legal Position AI Analyzer...")

    # Detect if running on HF Spaces or locally
    is_hf_space = os.environ.get('SPACE_ID') is not None
    
    # Must call launch() explicitly — Gradio 6 does not auto-launch.
    # ssr_mode=False avoids the "shareable link" error on HF Spaces containers.
    if is_hf_space:
        # On HF Spaces, use fixed port 7860
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            show_error=True,
            ssr_mode=False,
        )
    else:
        # Locally, let Gradio find an available port
        demo.launch(
            share=False,
            show_error=True,
        )