File size: 3,202 Bytes
7dcdffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f660eb
7dcdffa
0f660eb
7dcdffa
 
 
0f660eb
 
 
 
 
 
7dcdffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
load_dotenv()
import argparse
import os
import subprocess
import sys
import logging
from src.webui.interface import theme_map, create_ui


# ---------------------------------------------------------------------------
# Playwright installer for Hugging Face Spaces & other CI environments
# ---------------------------------------------------------------------------


def _ensure_playwright():
    """Ensure Playwright and its Chromium browser are installed.

    Hugging Face Spaces (Gradio runtime) does not execute a `postBuild` hook
    when the Space type is *Gradio*, therefore we perform the browser
    download at runtime.  The operations below are idempotent: if the Python
    package or the browser binary is already present they complete
    immediately.  Any failure is logged but will not crash the Web-UI.
    """

    # 1) Guarantee the Python package exists (should already be present if it
    #    was declared in requirements.txt, but install defensively just in
    #    case).
    try:
        import playwright  # noqa: F401
    except ModuleNotFoundError:
        logging.info("Playwright package missing – installing via pip…")
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", "playwright"])
        except subprocess.CalledProcessError as err:
            logging.warning("Failed to pip-install Playwright: %s", err)
            return

    # 2) Ensure the Chromium browser binary is downloaded.  This command is
    #    cheap when the browser is already present.
    try:
        subprocess.run(
            ["playwright", "install", "--with-deps", "chromium"],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )
    except Exception as err:  # pylint: disable=broad-except
        logging.warning("Playwright install with deps failed: %s", err)
        # Fallback: try without system deps (may succeed in HF image)
        try:
            subprocess.run(["playwright", "install", "chromium"], check=True)
        except Exception as err2:  # pylint: disable=broad-except
            logging.error("Playwright browser installation ultimately failed: %s", err2)


# ---------------------------------------------------------------------------
#
#                                                                              
# ---------------------------------------------------------------------------


def main():
    _ensure_playwright()

    parser = argparse.ArgumentParser(description="Gradio WebUI for Browser Agent")

    # On Hugging Face Spaces the runtime port is provided via the PORT env var.
    default_port = int(os.getenv("PORT", 7860))

    parser.add_argument("--ip", type=str, default="0.0.0.0", help="IP address to bind to")
    parser.add_argument("--port", type=int, default=default_port, help="Port to listen on")
    parser.add_argument("--theme", type=str, default="Ocean", choices=theme_map.keys(), help="Theme to use for the UI")
    args = parser.parse_args()

    demo = create_ui(theme_name=args.theme)
    demo.queue().launch(server_name=args.ip, server_port=args.port)


if __name__ == '__main__':
    main()