File size: 2,037 Bytes
c4e64ca
 
 
 
 
 
 
 
 
 
 
 
 
f1c4fa9
c4e64ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1c4fa9
 
 
 
 
 
c4e64ca
f1c4fa9
c4e64ca
 
 
 
 
 
 
a89e2d5
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
"""
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 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, auth: List[Tuple[str, str]] | None) -> 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", css="body{background:#0b0b0c;}") as demo:
        gr.HTML(iframe)
    port = int(os.getenv("PORT", os.getenv("GRADIO_PORT", "7860")))
    demo.launch(
        server_name="0.0.0.0",
        server_port=port,
        auth=auth,
        show_api=False,
        inline=False,
        max_threads=8,
    )
    return demo


def main() -> None:
    html = load_html()
    auth = build_auth()
    build_demo(html, auth)


if __name__ == "__main__":
    main()