File size: 2,637 Bytes
c4e64ca
 
 
 
 
 
 
 
286b6f5
c4e64ca
 
 
 
 
f1c4fa9
c4e64ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286b6f5
f1c4fa9
 
 
 
 
 
286b6f5
f1c4fa9
c4e64ca
 
 
286b6f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4e64ca
286b6f5
 
 
 
 
 
c4e64ca
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
"""
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 = (
        "<iframe id='tester-frame' title='LibraxisAI Responses API Tester' "
        "style=\"width:100%;height:90vh;border:none;background:#0b0b0c;\" "
        f"srcdoc=\"{escaped}\"></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()