nz-nz commited on
Commit
847a58e
·
verified ·
1 Parent(s): 0bc2fd8

Clean up launch (remove diagnostics); document CPU-for-stub / ZeroGPU-for-real-model

Browse files
Files changed (1) hide show
  1. server.py +15 -32
server.py CHANGED
@@ -24,6 +24,7 @@ from __future__ import annotations
24
 
25
  import os
26
  import tempfile
 
27
  import time
28
  import uuid
29
  from collections import OrderedDict
@@ -338,36 +339,18 @@ async def index():
338
 
339
 
340
  # HF runs `python server.py` (README app_file) and proxies port 7860. `gradio.Server`
341
- # launches at MODULE TOP LEVEL; on a Space gradio binds 0.0.0.0:7860 automatically
342
- # and launch() blocks to keep this single long-lived server alive. This mirrors
343
- # gradio's reference `Server` example, which runs the same way on a ZeroGPU Space.
344
- # (Locally, set GRADIO_SERVER_PORT to use a different port.)
345
  #
346
- # IMPORTANT: gradio is pinned to 6.10.0 on purpose. On newer gradio (6.17.x) a
347
- # custom gradio.Server breaks under the Space's hot-reloader the app prints
348
- # "Running on 7860" but the process exits and the Space dies with RUNTIME_ERROR.
349
- # 6.10.0 is the version gradio's own ZeroGPU Server example ships, and it still
350
- # allows huggingface-hub<1.0 (needed by the real model). Keep the README
351
- # sdk_version and requirements*.txt gradio pins in lockstep.
352
- import sys as _sys
353
-
354
-
355
- def _log(msg):
356
- print(f"[recall] {msg}", flush=True)
357
- _sys.stderr.flush()
358
-
359
-
360
- _log("before launch")
361
- try:
362
- _ret = app.launch(show_error=True, prevent_thread_lock=True)
363
- _log(f"launch returned: {type(_ret)}")
364
- except Exception as _e: # noqa: BLE001
365
- import traceback
366
-
367
- traceback.print_exc()
368
- _log(f"launch raised: {type(_e).__name__}: {_e}")
369
-
370
- _log("holding main thread open")
371
- import threading as _threading
372
-
373
- _threading.Event().wait()
 
24
 
25
  import os
26
  import tempfile
27
+ import threading
28
  import time
29
  import uuid
30
  from collections import OrderedDict
 
339
 
340
 
341
  # HF runs `python server.py` (README app_file) and proxies port 7860. `gradio.Server`
342
+ # launches at MODULE TOP LEVEL; on a Space gradio binds 0.0.0.0:7860 automatically.
343
+ # We launch non-blocking and hold the main thread ourselves so the (daemon) uvicorn
344
+ # server keeps serving. (Locally, set GRADIO_SERVER_PORT to use a different port.)
 
345
  #
346
+ # Two deploy gotchas, both learned the hard way:
347
+ # * gradio is pinned to 6.10.0. On 6.17.x a custom gradio.Server doesn't stay up
348
+ # under the Space runtime. 6.10.0 is the version gradio's own `Server` example
349
+ # ships, and it still allows huggingface-hub<1.0 (needed by the real model).
350
+ # * Stub mode registers no `@spaces.GPU` function, which makes a ZeroGPU Space's
351
+ # `spaces` reload server error out and flip the Space to RUNTIME_ERROR even
352
+ # though uvicorn serves fine. So the stub demo runs on CPU-basic hardware. Once
353
+ # the real model (with `@spaces.GPU`) is wired, switch hardware back to ZeroGPU.
354
+ # Keep the README sdk_version and requirements*.txt gradio pins in lockstep.
355
+ app.launch(show_error=True, prevent_thread_lock=True)
356
+ threading.Event().wait()