div0-space's picture
Embed tester HTML via srcdoc iframe to sandbox JS scope
f1c4fa9 verified
"""
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()