Spaces:
Running on Zero
Running on Zero
Fix: inject routes via background thread after launch
Browse files
app.py
CHANGED
|
@@ -458,27 +458,37 @@ def _benchmark_test_handler():
|
|
| 458 |
|
| 459 |
|
| 460 |
# ---------------------------------------------------------------------------
|
| 461 |
-
#
|
| 462 |
# ---------------------------------------------------------------------------
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
_log("
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
|
| 480 |
|
| 481 |
-
|
|
|
|
| 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.
|