File size: 1,957 Bytes
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
"""
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