Spaces:
Sleeping
Sleeping
| """ | |
| Gradio Interface - UI Layer for Service-Oriented Mode | |
| This module provides a Gradio web interface that consumes the API via HTTP. | |
| Uses shared UI components to eliminate code duplication. | |
| Architecture: | |
| - Gradio UI is a CLIENT of the API | |
| - Uses requests library to call /detect endpoint | |
| - Handles image upload and response display | |
| - Provides user-friendly error messages | |
| """ | |
| import os | |
| os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' | |
| from functools import partial | |
| # Use shared UI components (eliminates code duplication) | |
| from ui.shared_interface import create_interface | |
| from ui.detection_wrapper import detect_with_api | |
| # API configuration | |
| API_BASE_URL = os.getenv("CU1_API_URL", "http://localhost:8000") | |
| def create_gradio_interface(): | |
| """Create and return the Gradio interface configured for API client mode""" | |
| # Create a detection function with API URL baked in | |
| detection_fn = partial(detect_with_api, api_url=API_BASE_URL) | |
| return create_interface( | |
| detection_fn=detection_fn, | |
| title_suffix="", | |
| show_api_info=True, | |
| api_url=API_BASE_URL | |
| ) | |
| if __name__ == "__main__": | |
| demo = create_gradio_interface() | |
| # Read configuration from environment with sensible defaults | |
| 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 None | |
| 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): | |
| demo.queue().launch( | |
| server_name=server_name, | |
| server_port=None, | |
| share=share | |
| ) | |
| else: | |
| raise | |