""" Gradio UI Server Entry Point Starts the Gradio web interface for UI element detection. IMPORTANT: The API server must be running for this to work! Usage: # Terminal 1: Start API server python app_api.py # Terminal 2: Start UI server python app_ui.py The UI will be available at: - Gradio Interface: http://localhost:7860 Configuration: Set environment variables to customize: - CU1_API_URL: API endpoint (default: http://localhost:8000) - GRADIO_SERVER_NAME: Server host (default: 0.0.0.0) - GRADIO_SERVER_PORT: Server port (default: 7860) - GRADIO_SHARE: Enable sharing (default: false) """ import os os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' from ui.gradio_interface import create_gradio_interface def main(): """Start the Gradio UI server""" api_url = os.getenv("CU1_API_URL", "http://localhost:8000") print("=" * 70) print("🎨 CU-1 UI Element Detector - Gradio UI") print("=" * 70) print("\n⚠️ IMPORTANT: Make sure the API server is running!") print(" If not started, run in another terminal:") print(" python app_api.py") print(f"\n🔗 API Connection: {api_url}") print(" Change with: export CU1_API_URL=http://your-api:8000") print("\n📱 Gradio Interface: http://localhost:7860") print("\n🏗️ Architecture:") print(" This UI is a CLIENT of the API (service-oriented)") print(" All detection logic runs in the API server") print(" UI communicates via HTTP/REST") print("=" * 70 + "\n") demo = create_gradio_interface() # Read configuration from environment server_name = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0") port_env = os.getenv("GRADIO_SERVER_PORT") or os.getenv("PORT") server_port = int(port_env) if port_env and port_env.isdigit() else 7860 share_env = os.getenv("GRADIO_SHARE", "false").lower() share = share_env in {"1", "true", "yes", "y"} try: demo.queue().launch( server_name=server_name, server_port=server_port, share=share ) except OSError as e: if "Cannot find empty port" in str(e): print(f"\n⚠️ Port {server_port} is busy, trying to find a free port...") demo.queue().launch( server_name=server_name, server_port=None, # Auto-select free port share=share ) else: raise if __name__ == "__main__": main()