div0-space commited on
Commit
286b6f5
·
verified ·
1 Parent(s): f55ee23

gradio6: harden app launch for Spaces runtime

Browse files
Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -6,6 +6,7 @@ Auth is optional and controlled via environment variables.
6
  """
7
  from __future__ import annotations
8
 
 
9
  import os
10
  from pathlib import Path
11
  from typing import List, Tuple
@@ -41,31 +42,51 @@ def build_auth() -> List[Tuple[str, str]] | None:
41
  return auth_list or None
42
 
43
 
44
- def build_demo(html: str, auth: List[Tuple[str, str]] | None) -> gr.Blocks:
45
  escaped = py_html.escape(html, quote=True)
46
  iframe = (
47
  "<iframe id='tester-frame' title='LibraxisAI Responses API Tester' "
48
  "style=\"width:100%;height:90vh;border:none;background:#0b0b0c;\" "
49
  f"srcdoc=\"{escaped}\"></iframe>"
50
  )
51
- with gr.Blocks(title="LibraxisAI API Batch Tester", css="body{background:#0b0b0c;}") as demo:
52
  gr.HTML(iframe)
53
- port = int(os.getenv("PORT", os.getenv("GRADIO_PORT", "7860")))
54
- demo.launch(
55
- server_name="0.0.0.0",
56
- server_port=port,
57
- auth=auth,
58
- show_api=False,
59
- inline=False,
60
- max_threads=8,
61
- )
62
  return demo
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def main() -> None:
66
- html = load_html()
67
- auth = build_auth()
68
- build_demo(html, auth)
 
 
 
69
 
70
 
71
  if __name__ == "__main__":
 
6
  """
7
  from __future__ import annotations
8
 
9
+ import inspect
10
  import os
11
  from pathlib import Path
12
  from typing import List, Tuple
 
42
  return auth_list or None
43
 
44
 
45
+ def build_demo(html: str) -> gr.Blocks:
46
  escaped = py_html.escape(html, quote=True)
47
  iframe = (
48
  "<iframe id='tester-frame' title='LibraxisAI Responses API Tester' "
49
  "style=\"width:100%;height:90vh;border:none;background:#0b0b0c;\" "
50
  f"srcdoc=\"{escaped}\"></iframe>"
51
  )
52
+ with gr.Blocks(title="LibraxisAI API Batch Tester") as demo:
53
  gr.HTML(iframe)
 
 
 
 
 
 
 
 
 
54
  return demo
55
 
56
 
57
+ def launch_demo(demo: gr.Blocks, auth: List[Tuple[str, str]] | None) -> None:
58
+ port = int(os.getenv("PORT", os.getenv("GRADIO_PORT", "7860")))
59
+ raw_kwargs = {
60
+ "server_name": "0.0.0.0",
61
+ "server_port": port,
62
+ "max_threads": 8,
63
+ "css": "body{background:#0b0b0c;}",
64
+ }
65
+ if auth:
66
+ raw_kwargs["auth"] = auth
67
+
68
+ launch_signature = inspect.signature(demo.launch).parameters
69
+ launch_kwargs = {k: v for k, v in raw_kwargs.items() if k in launch_signature}
70
+
71
+ if "footer_links" in launch_signature:
72
+ # Replacement for show_api=False in newer Gradio versions.
73
+ launch_kwargs["footer_links"] = ["gradio", "settings"]
74
+ elif "show_api" in launch_signature:
75
+ launch_kwargs["show_api"] = False
76
+
77
+ if "inline" in launch_signature:
78
+ launch_kwargs["inline"] = False
79
+
80
+ demo.launch(**launch_kwargs)
81
+
82
+
83
  def main() -> None:
84
+ launch_demo(demo, AUTH)
85
+
86
+
87
+ HTML = load_html()
88
+ AUTH = build_auth()
89
+ demo = build_demo(HTML)
90
 
91
 
92
  if __name__ == "__main__":