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