""" 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()