File size: 2,535 Bytes
77da9e2
bf5eae6
77da9e2
bf5eae6
 
 
 
77da9e2
bf5eae6
 
 
77da9e2
 
 
 
 
 
 
 
 
 
bf5eae6
77da9e2
bf5eae6
77da9e2
 
 
 
 
 
 
 
bf5eae6
77da9e2
 
bf5eae6
77da9e2
bf5eae6
77da9e2
 
bf5eae6
 
 
77da9e2
 
 
bf5eae6
77da9e2
bf5eae6
 
 
77da9e2
 
 
 
 
ff03012
77da9e2
bf5eae6
77da9e2
 
bf5eae6
77da9e2
 
 
 
bf5eae6
77da9e2
 
bf5eae6
77da9e2
 
 
 
bf5eae6
 
77da9e2
 
bf5eae6
 
 
77da9e2
 
 
 
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
79
80
81
82
83
84
85
86
"""
Unified Entry Point - Direct Mode for HuggingFace Spaces

Simplified architecture for HuggingFace Spaces:
    - Direct service access (no API subprocess)
    - Faster and more reliable
    - No HTTP overhead

For production with separated API/UI, use:
    - python app_api.py (API server)
    - python app_ui.py (UI client)

Usage:
    python app.py
"""

import os
os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1'

import sys

# Use shared UI components with DIRECT service access
from ui.shared_interface import create_interface
from ui.detection_wrapper import detect_with_service


# Configuration
UI_HOST = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0")
UI_PORT = int(os.getenv("GRADIO_SERVER_PORT", "7860"))


def main():
    """Main entry point - Direct service mode for HuggingFace Spaces"""
    
    print("=" * 70)
    print("🎯 CU-1 UI Element Detector - Direct Mode")
    print("=" * 70)
    print("\n📡 Architecture: Direct service access (optimized for HF Spaces)")
    print(f"   - Gradio UI: http://localhost:{UI_PORT}")
    print("\n🏗️  Benefits:")
    print("   - Faster (no HTTP overhead)")
    print("   - More reliable (no subprocess)")
    print("   - Simpler architecture")
    print("=" * 70 + "\n")
    
    try:
        # Create Gradio interface with DIRECT detection function
        demo = create_interface(
            detection_fn=detect_with_service,
            title_suffix="Direct Mode",
            show_api_info=False
        )
        
        print(f"\n🎨 Starting Gradio UI on http://localhost:{UI_PORT}...\n")
        
        # Launch Gradio with automatic port fallback
        # API is automatically exposed at /api/predict for HF Spaces
        try:
            demo.queue().launch(
                server_name=UI_HOST,
                server_port=UI_PORT,
                share=False
            )
        except OSError as e:
            if "Cannot find empty port" in str(e):
                print(f"⚠️  Port {UI_PORT} is busy, trying to find a free port...")
                demo.queue().launch(
                    server_name=UI_HOST,
                    server_port=None,  # Auto-select free port
                    share=False
                )
            else:
                raise
    except KeyboardInterrupt:
        print("\n\n🛑 Shutting down... Goodbye! 👋")
        sys.exit(0)
    except Exception as e:
        print(f"\n❌ Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()