YanTianlong commited on
Commit
6e4cd3f
·
1 Parent(s): 77fda11

Run Space preparation in background

Browse files
Files changed (1) hide show
  1. app.py +48 -7
app.py CHANGED
@@ -25,7 +25,9 @@ COMFY_HOST = "127.0.0.1"
25
  COMFY_PORT = "8188"
26
 
27
  COMFY_PROCESS: subprocess.Popen | None = None
 
28
  BOOTSTRAPPED = False
 
29
 
30
 
31
  def gpu_status_lines() -> list[str]:
@@ -90,6 +92,13 @@ def run_bootstrap(lines: list[str], *, allow_heavy: bool = True) -> None:
90
  def ensure_comfy(lines: list[str], *, timeout: float = 240) -> dict[str, Any]:
91
  global COMFY_PROCESS
92
 
 
 
 
 
 
 
 
93
  run_bootstrap(lines, allow_heavy=False)
94
 
95
  try:
@@ -261,14 +270,44 @@ def voxcpm_tts_workflow(prefix: str) -> dict[str, Any]:
261
 
262
 
263
  def prepare_runtime() -> str:
 
 
264
  lines = ["VoiceGate runtime preparation"]
265
- started = time.time()
266
- try:
267
- run_bootstrap(lines, allow_heavy=True)
268
- lines.append(f"elapsed_sec={time.time() - started:.1f}")
269
- lines.append(f"comfy_dir_exists={(COMFY_DIR / 'main.py').exists()}")
270
- except Exception as exc:
271
- lines.append(f"error={type(exc).__name__}: {exc}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  return "\n".join(lines)
273
 
274
 
@@ -344,12 +383,14 @@ with gr.Blocks(title="VoiceGate") as demo:
344
  gr.Markdown("# VoiceGate")
345
  with gr.Row():
346
  prepare_run = gr.Button("Prepare")
 
347
  gpu_run = gr.Button("GPU")
348
  comfy_run = gr.Button("ComfyUI")
349
  melband_run = gr.Button("MelBand")
350
  voxcpm_run = gr.Button("VoxCPM TTS")
351
  output = gr.Textbox(label="Status", lines=28)
352
  prepare_run.click(fn=prepare_runtime, outputs=output)
 
353
  gpu_run.click(fn=gpu_smoke_test, outputs=output)
354
  comfy_run.click(fn=comfy_runtime_test, outputs=output)
355
  melband_run.click(fn=melband_gpu_test, outputs=output)
 
25
  COMFY_PORT = "8188"
26
 
27
  COMFY_PROCESS: subprocess.Popen | None = None
28
+ PREPARE_PROCESS: subprocess.Popen | None = None
29
  BOOTSTRAPPED = False
30
+ BOOTSTRAP_LOG = Path("/tmp/voicegate_bootstrap.log")
31
 
32
 
33
  def gpu_status_lines() -> list[str]:
 
92
  def ensure_comfy(lines: list[str], *, timeout: float = 240) -> dict[str, Any]:
93
  global COMFY_PROCESS
94
 
95
+ if PREPARE_PROCESS is not None:
96
+ returncode = PREPARE_PROCESS.poll()
97
+ if returncode is None:
98
+ raise RuntimeError("Runtime preparation is still running. Check Prepare Status first.")
99
+ if returncode != 0:
100
+ raise RuntimeError(f"Runtime preparation failed with return code {returncode}.")
101
+
102
  run_bootstrap(lines, allow_heavy=False)
103
 
104
  try:
 
270
 
271
 
272
  def prepare_runtime() -> str:
273
+ global PREPARE_PROCESS
274
+
275
  lines = ["VoiceGate runtime preparation"]
276
+ if PREPARE_PROCESS is not None and PREPARE_PROCESS.poll() is None:
277
+ lines.append(f"prepare=already_running pid={PREPARE_PROCESS.pid}")
278
+ return "\n".join(lines)
279
+ BOOTSTRAP_LOG.parent.mkdir(parents=True, exist_ok=True)
280
+ log = BOOTSTRAP_LOG.open("ab")
281
+ command = [sys.executable, str(ROOT / "scripts" / "bootstrap_comfy.py")]
282
+ PREPARE_PROCESS = subprocess.Popen(
283
+ command,
284
+ cwd=ROOT,
285
+ stdout=log,
286
+ stderr=subprocess.STDOUT,
287
+ )
288
+ lines.append(f"prepare=started pid={PREPARE_PROCESS.pid}")
289
+ lines.append(f"log={BOOTSTRAP_LOG}")
290
+ return "\n".join(lines)
291
+
292
+
293
+ def prepare_status() -> str:
294
+ global BOOTSTRAPPED
295
+
296
+ lines = ["VoiceGate runtime preparation status"]
297
+ if PREPARE_PROCESS is None:
298
+ lines.append("prepare=not_started")
299
+ else:
300
+ returncode = PREPARE_PROCESS.poll()
301
+ if returncode is None:
302
+ lines.append(f"prepare=running pid={PREPARE_PROCESS.pid}")
303
+ else:
304
+ lines.append(f"prepare=finished returncode={returncode}")
305
+ if returncode == 0 and (COMFY_DIR / "main.py").exists():
306
+ BOOTSTRAPPED = True
307
+ lines.append(f"comfy_dir_exists={(COMFY_DIR / 'main.py').exists()}")
308
+ if BOOTSTRAP_LOG.exists():
309
+ lines.append("bootstrap_log_tail:")
310
+ lines.extend(BOOTSTRAP_LOG.read_text(encoding="utf-8", errors="replace").splitlines()[-80:])
311
  return "\n".join(lines)
312
 
313
 
 
383
  gr.Markdown("# VoiceGate")
384
  with gr.Row():
385
  prepare_run = gr.Button("Prepare")
386
+ prepare_status_run = gr.Button("Prepare Status")
387
  gpu_run = gr.Button("GPU")
388
  comfy_run = gr.Button("ComfyUI")
389
  melband_run = gr.Button("MelBand")
390
  voxcpm_run = gr.Button("VoxCPM TTS")
391
  output = gr.Textbox(label="Status", lines=28)
392
  prepare_run.click(fn=prepare_runtime, outputs=output)
393
+ prepare_status_run.click(fn=prepare_status, outputs=output)
394
  gpu_run.click(fn=gpu_smoke_test, outputs=output)
395
  comfy_run.click(fn=comfy_runtime_test, outputs=output)
396
  melband_run.click(fn=melband_gpu_test, outputs=output)