bivav commited on
Commit
b79d615
·
1 Parent(s): f072d11

Expose demo for Gradio Spaces hot reload and update main launch logic

Browse files
Files changed (2) hide show
  1. app.py +6 -1
  2. test_app.py +7 -0
app.py CHANGED
@@ -218,6 +218,11 @@ def create_app():
218
  return demo
219
 
220
 
 
 
 
 
 
221
  if __name__ == "__main__":
222
  if not LOGIN_PASSWORD:
223
  print(
@@ -225,4 +230,4 @@ if __name__ == "__main__":
225
  "authentication and anyone who can reach it can spend endpoint quota."
226
  )
227
 
228
- create_app().launch(auth=verify_login if LOGIN_PASSWORD else None)
 
218
  return demo
219
 
220
 
221
+ # Bound at module scope on purpose: Hugging Face Spaces' hot reloader looks up
222
+ # `demo` in __main__ after every reload and fails if the name is missing.
223
+ demo = create_app()
224
+
225
+
226
  if __name__ == "__main__":
227
  if not LOGIN_PASSWORD:
228
  print(
 
230
  "authentication and anyone who can reach it can spend endpoint quota."
231
  )
232
 
233
+ demo.launch(auth=verify_login if LOGIN_PASSWORD else None)
test_app.py CHANGED
@@ -321,3 +321,10 @@ def test_stream_completion_raises_on_malformed_chunk(monkeypatch):
321
 
322
  with pytest.raises(ValueError):
323
  list(app.stream_completion([], "m"))
 
 
 
 
 
 
 
 
321
 
322
  with pytest.raises(ValueError):
323
  list(app.stream_completion([], "m"))
324
+
325
+
326
+ def test_module_exposes_demo_for_spaces_hot_reload():
327
+ """Spaces' reloader does getattr(__main__, "demo") after every reload."""
328
+ import gradio as gr
329
+
330
+ assert isinstance(app.demo, gr.Blocks)