AndrewRqy commited on
Commit
9233b44
·
1 Parent(s): 7c97976

Define demo at module scope so HF Space's hot-reload launcher applies theme+css

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -4408,16 +4408,17 @@ def build_app() -> gr.Blocks:
4408
  input_background_fill="#251d2e",
4409
  )
4410
 
 
 
 
 
 
4411
  with gr.Blocks(
 
 
4412
  title="The Apprentice",
4413
  analytics_enabled=False,
4414
  ) as demo:
4415
- # Gradio 6 moved theme + css from Blocks() to launch() (verified by the
4416
- # boot warning + TypeError chain on 2026-06-15). We stash both on the
4417
- # demo object so main() can hand them to launch() without threading
4418
- # extra positional args through.
4419
- demo._apprentice_theme = _apprentice_theme
4420
- demo._apprentice_css = composed_css
4421
  state = gr.State(initial_state)
4422
  trial_substate = gr.State(TRIAL_STATE_AWAIT)
4423
 
@@ -4982,29 +4983,31 @@ def run_self_test() -> int:
4982
  # ---------------------------------------------------------------------------
4983
  # Entry point
4984
  # ---------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4985
 
4986
 
4987
  def main() -> int:
4988
  if "--self-test" in sys.argv:
4989
  return run_self_test()
4990
- demo = build_app()
4991
- # Off-Brand sweep: suppress the "Use via API" sidebar + analytics
4992
- # so the rendered page doesn't leak Gradio chrome. ``quiet=True``
4993
- # would suppress the "Running on http://..." console banner, which
4994
- # is useful info — leave it on.
4995
  demo.launch(
4996
  server_name="0.0.0.0",
4997
  server_port=7860,
4998
- # BISECT 1: theme + css temporarily disabled to see if the custom
4999
- # theme or the 2000 lines of bespoke CSS is what's breaking Gradio 6's
5000
- # iframe rendering. If the page renders without these, we know which
5001
- # layer to fix. Once we find it, restore both.
5002
- # theme=getattr(demo, "_apprentice_theme", None),
5003
- # css=getattr(demo, "_apprentice_css", None),
5004
- # Gradio 6 enables SSR by default (a Node.js proxy in front of the
5005
- # Python ASGI app). SSR hydration fails inside HF's Space iframe and
5006
- # leaves the App tab blank — same symptom that hit us on Gradio 5.
5007
- # Force classic client-rendering so the iframe loads cleanly.
5008
  ssr_mode=False,
5009
  allowed_paths=[str(_HERE / "assets")],
5010
  show_error=True,
 
4408
  input_background_fill="#251d2e",
4409
  )
4410
 
4411
+ # Gradio 6 prefers theme + css on launch(), but HF Space's launcher
4412
+ # imports the module and runs its own launch() with defaults — anything
4413
+ # we set on our own launch() call is bypassed there. So we pass these
4414
+ # to gr.Blocks() despite the deprecation warning; that's the only path
4415
+ # Gradio actually reads on HF Space.
4416
  with gr.Blocks(
4417
+ theme=_apprentice_theme,
4418
+ css=composed_css,
4419
  title="The Apprentice",
4420
  analytics_enabled=False,
4421
  ) as demo:
 
 
 
 
 
 
4422
  state = gr.State(initial_state)
4423
  trial_substate = gr.State(TRIAL_STATE_AWAIT)
4424
 
 
4983
  # ---------------------------------------------------------------------------
4984
  # Entry point
4985
  # ---------------------------------------------------------------------------
4986
+ #
4987
+ # On HF Spaces, Gradio's launcher imports app.py as a module and looks for a
4988
+ # top-level ``demo`` variable to serve. If it doesn't find one, it falls back
4989
+ # to its own ``.launch()`` with DEFAULT settings — which means everything we
4990
+ # pass to ``demo.launch(...)`` below would be ignored (no theme, no css, no
4991
+ # ssr_mode=False, no allowed_paths). So we build the demo at module scope.
4992
+ #
4993
+ # To keep --self-test fast (it doesn't need a Blocks instance, and building
4994
+ # one base64-encodes ~30 sprites), we skip the eager build when the flag is
4995
+ # present.
4996
+
4997
+ if "--self-test" in sys.argv:
4998
+ demo = None
4999
+ else:
5000
+ demo = build_app()
5001
 
5002
 
5003
  def main() -> int:
5004
  if "--self-test" in sys.argv:
5005
  return run_self_test()
5006
+ # Local entry point — uses the module-level ``demo`` so the local run
5007
+ # matches the HF Space behavior exactly.
 
 
 
5008
  demo.launch(
5009
  server_name="0.0.0.0",
5010
  server_port=7860,
 
 
 
 
 
 
 
 
 
 
5011
  ssr_mode=False,
5012
  allowed_paths=[str(_HERE / "assets")],
5013
  show_error=True,