""" LibraxisAI API Batch Tester — HF Space entrypoint. Serves the standalone multi-lane HTML tester via Gradio. Auth is optional and controlled via environment variables. """ from __future__ import annotations import inspect import os from pathlib import Path from typing import List, Tuple import gradio as gr import html as py_html BASE_DIR = Path(__file__).parent HTML_PATH = BASE_DIR / "api-tester.html" def load_html() -> str: html_env = os.getenv("API_TESTER_HTML") html_path = Path(html_env) if html_env else HTML_PATH if not html_path.exists(): raise FileNotFoundError(f"API tester HTML not found at {html_path}") return html_path.read_text(encoding="utf-8") def build_auth() -> List[Tuple[str, str]] | None: auth_list: list[tuple[str, str]] = [] user = os.getenv("GRADIO_USERNAME") pwd = os.getenv("GRADIO_PASSWORD") if user and pwd: auth_list.append((user, pwd)) auth_env = os.getenv("GRADIO_AUTH") if auth_env: for pair in auth_env.split(","): if ":" in pair: u, p = pair.split(":", 1) if u and p: auth_list.append((u, p)) return auth_list or None def build_demo(html: str) -> gr.Blocks: escaped = py_html.escape(html, quote=True) iframe = ( "" ) with gr.Blocks(title="LibraxisAI API Batch Tester") as demo: gr.HTML(iframe) return demo def launch_demo(demo: gr.Blocks, auth: List[Tuple[str, str]] | None) -> None: port = int(os.getenv("PORT", os.getenv("GRADIO_PORT", "7860"))) raw_kwargs = { "server_name": "0.0.0.0", "server_port": port, "max_threads": 8, "css": "body{background:#0b0b0c;}", } if auth: raw_kwargs["auth"] = auth launch_signature = inspect.signature(demo.launch).parameters launch_kwargs = {k: v for k, v in raw_kwargs.items() if k in launch_signature} if "footer_links" in launch_signature: # Replacement for show_api=False in newer Gradio versions. launch_kwargs["footer_links"] = ["gradio", "settings"] elif "show_api" in launch_signature: launch_kwargs["show_api"] = False if "inline" in launch_signature: launch_kwargs["inline"] = False demo.launch(**launch_kwargs) def main() -> None: launch_demo(demo, AUTH) HTML = load_html() AUTH = build_auth() demo = build_demo(HTML) if __name__ == "__main__": main()