spagestic commited on
Commit
51b6df5
·
1 Parent(s): 55f84e8

Refactor Gradio node cleanup and enhance OAuth integration. Updated _cleanup_stale_gradio_nodes to handle multiple patterns for orphaned processes. Improved demo assignment in _launch_with_oauth for better hot reload compatibility with Gradio Spaces.

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -2,6 +2,7 @@
2
  import os
3
  import signal
4
  import subprocess
 
5
  import time
6
  import warnings
7
  from pathlib import Path
@@ -28,6 +29,7 @@ from ui import server_api
28
  ASSETS_DIR = Path(__file__).resolve().parent / "assets"
29
 
30
  app = Server(title="Borderless - Immigration Research Agent")
 
31
  demo = app
32
 
33
 
@@ -126,22 +128,29 @@ app.mount("/assets", StaticFiles(directory=str(ASSETS_DIR)), name="assets")
126
 
127
  def _cleanup_stale_gradio_nodes() -> None:
128
  """Terminate orphaned Gradio Node SSR workers from prior restarts."""
129
- try:
130
- result = subprocess.run(
131
- ["pgrep", "-f", "gradio/templates/node/build"],
132
- capture_output=True,
133
- text=True,
134
- check=False,
135
- )
136
- except FileNotFoundError:
137
- return
138
-
139
- for pid_str in result.stdout.split():
140
- pid = int(pid_str)
141
  try:
142
- os.kill(pid, signal.SIGTERM)
143
- except ProcessLookupError:
144
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
 
147
  def _launch_with_oauth(**kwargs: Any):
@@ -151,8 +160,13 @@ def _launch_with_oauth(**kwargs: Any):
151
  for fn, api_kwargs in app._deferred_apis:
152
  gr_api(fn=fn, **api_kwargs)
153
 
154
- os.environ["GRADIO_SERVER_MODE_ENABLED"] = "1"
155
- return blocks.launch(_app=app, **kwargs)
 
 
 
 
 
156
 
157
 
158
  app.launch = _launch_with_oauth # type: ignore[method-assign]
 
2
  import os
3
  import signal
4
  import subprocess
5
+ import sys
6
  import time
7
  import warnings
8
  from pathlib import Path
 
29
  ASSETS_DIR = Path(__file__).resolve().parent / "assets"
30
 
31
  app = Server(title="Borderless - Immigration Research Agent")
32
+ # Replaced with the OAuth Blocks instance in _launch_with_oauth (Gradio hot reload).
33
  demo = app
34
 
35
 
 
128
 
129
  def _cleanup_stale_gradio_nodes() -> None:
130
  """Terminate orphaned Gradio Node SSR workers from prior restarts."""
131
+ patterns = (
132
+ "gradio/templates/node/build",
133
+ "gradio/templates/node/build/server/entry.js",
134
+ )
135
+ for pattern in patterns:
 
 
 
 
 
 
 
136
  try:
137
+ result = subprocess.run(
138
+ ["pgrep", "-f", pattern],
139
+ capture_output=True,
140
+ text=True,
141
+ check=False,
142
+ )
143
+ except FileNotFoundError:
144
+ return
145
+
146
+ for pid_str in result.stdout.split():
147
+ if not pid_str.strip():
148
+ continue
149
+ pid = int(pid_str)
150
+ try:
151
+ os.kill(pid, signal.SIGTERM)
152
+ except ProcessLookupError:
153
+ pass
154
 
155
 
156
  def _launch_with_oauth(**kwargs: Any):
 
160
  for fn, api_kwargs in app._deferred_apis:
161
  gr_api(fn=fn, **api_kwargs)
162
 
163
+ os.environ["GRADIO_SERVER_MODE_ENABLED"] = "1"
164
+ # Gradio Spaces hot reload matches __main__.demo to the launched Blocks.
165
+ main = sys.modules.get("__main__")
166
+ if main is not None:
167
+ main.demo = blocks
168
+ globals()["demo"] = blocks
169
+ return blocks.launch(_app=app, **kwargs)
170
 
171
 
172
  app.launch = _launch_with_oauth # type: ignore[method-assign]