cazyundee commited on
Commit
5e3fbbf
·
verified ·
1 Parent(s): d0e8976

Fix: inject routes via background thread after launch

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -458,27 +458,37 @@ def _benchmark_test_handler():
458
 
459
 
460
  # ---------------------------------------------------------------------------
461
- # Monkey-patch: intercept Gradio server creation to inject routes.
462
  # ---------------------------------------------------------------------------
463
 
464
- _orig_routes = gr.routes.App.__init__
465
-
466
-
467
- def _patched_init(self, *args, **kwargs):
468
- _orig_routes(self, *args, **kwargs)
469
- _log("Gradio server __init__ patched - adding routes")
470
- try:
471
- self.add_api_route("/respite/resources", _resources_handler, methods=["GET"])
472
- self.add_api_route("/respite/specs", _specs_handler, methods=["GET"])
473
- self.add_api_route("/respite/search", _search_handler, methods=["GET"])
474
- self.add_api_route("/respite/benchmark", _benchmark_handler, methods=["GET"])
475
- self.add_api_route("/respite/benchmark/test", _benchmark_test_handler, methods=["GET"])
476
- _log("Routes added via __init__ patch")
477
- except Exception as e:
478
- _log(f"Route patching failed: {e}")
 
 
 
 
 
 
 
 
 
479
 
480
 
481
- gr.routes.App.__init__ = _patched_init
 
482
 
483
  # Disable the Gradio Spaces file watcher thread which crashes because
484
  # importing `spaces` in a new thread after CUDA init fails.
 
458
 
459
 
460
  # ---------------------------------------------------------------------------
461
+ # After launch, inject routes into the running server.
462
  # ---------------------------------------------------------------------------
463
 
464
+ import atexit
465
+
466
+ def _inject_routes_after_launch():
467
+ """Wait for Gradio server to start, then add our routes."""
468
+ for attempt in range(30):
469
+ time.sleep(2)
470
+ server = getattr(demo, 'server', None)
471
+ if server is None:
472
+ continue
473
+ fa_app = getattr(server, 'app', None)
474
+ if fa_app is None:
475
+ continue
476
+ _log(f"Injecting routes into running server (attempt {attempt+1})...")
477
+ try:
478
+ fa_app.add_api_route("/respite/resources", _resources_handler, methods=["GET"])
479
+ fa_app.add_api_route("/respite/specs", _specs_handler, methods=["GET"])
480
+ fa_app.add_api_route("/respite/search", _search_handler, methods=["GET"])
481
+ fa_app.add_api_route("/respite/benchmark", _benchmark_handler, methods=["GET"])
482
+ fa_app.add_api_route("/respite/benchmark/test", _benchmark_test_handler, methods=["GET"])
483
+ _log(f"Routes injected into running server on attempt {attempt+1}")
484
+ return
485
+ except Exception as e:
486
+ _log(f"Route injection attempt {attempt+1} failed: {e}")
487
+ _log("Failed to inject routes after 30 attempts")
488
 
489
 
490
+ _thread = threading.Thread(target=_inject_routes_after_launch, daemon=True)
491
+ _thread.start()
492
 
493
  # Disable the Gradio Spaces file watcher thread which crashes because
494
  # importing `spaces` in a new thread after CUDA init fails.