polats commited on
Commit
d011d06
·
1 Parent(s): 8000b92

Retry transient VoxCPM accelerator errors

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -19,6 +19,7 @@ import asyncio
19
  import json as _json
20
  import os
21
  import threading
 
22
 
23
  # ZeroGPU requires the spaces shim to be imported before torch. Locally, or on
24
  # non-ZeroGPU hardware, this falls back to a no-op decorator.
@@ -412,24 +413,40 @@ async def qwen_tts(request: Request):
412
  return Response(wav, media_type="audio/wav", headers={"Cache-Control": "no-store"})
413
 
414
 
415
- def _voxcpm_tts(text, instruct):
416
  from gradio_client import Client
417
  client = Client(VOXCPM_SPACE, token=HF_TOKEN or None)
418
- result = client.predict(text, instruct or "A clear, natural voice at a moderate pace.", api_name="/synthesize")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  path = result[0] if isinstance(result, (tuple, list)) else result
420
  with open(os.fspath(path), "rb") as f:
421
  return f.read()
422
 
423
 
424
  def _voxcpm_clone(text, ref_audio_b64, ref_text, instruct):
425
- from gradio_client import Client
426
- client = Client(VOXCPM_SPACE, token=HF_TOKEN or None)
427
- result = client.predict(
428
  text,
429
  ref_audio_b64,
430
  ref_text or "",
431
  instruct or "",
432
- api_name="/clone",
433
  )
434
  path = result[0] if isinstance(result, (tuple, list)) else result
435
  with open(os.fspath(path), "rb") as f:
 
19
  import json as _json
20
  import os
21
  import threading
22
+ import time
23
 
24
  # ZeroGPU requires the spaces shim to be imported before torch. Locally, or on
25
  # non-ZeroGPU hardware, this falls back to a no-op decorator.
 
413
  return Response(wav, media_type="audio/wav", headers={"Cache-Control": "no-store"})
414
 
415
 
416
+ def _voxcpm_predict(api_name, *args):
417
  from gradio_client import Client
418
  client = Client(VOXCPM_SPACE, token=HF_TOKEN or None)
419
+ last_err = None
420
+ for attempt in range(3):
421
+ try:
422
+ return client.predict(*args, api_name=api_name)
423
+ except Exception as e: # noqa: BLE001
424
+ last_err = e
425
+ msg = str(e).lower()
426
+ if attempt == 2 or not any(s in msg for s in ("accelerator", "queue", "gpu", "timeout", "temporarily")):
427
+ raise
428
+ time.sleep(1.5 * (attempt + 1))
429
+ raise last_err
430
+
431
+
432
+ def _voxcpm_tts(text, instruct):
433
+ result = _voxcpm_predict(
434
+ "/synthesize",
435
+ text,
436
+ instruct or "A clear, natural voice at a moderate pace.",
437
+ )
438
  path = result[0] if isinstance(result, (tuple, list)) else result
439
  with open(os.fspath(path), "rb") as f:
440
  return f.read()
441
 
442
 
443
  def _voxcpm_clone(text, ref_audio_b64, ref_text, instruct):
444
+ result = _voxcpm_predict(
445
+ "/clone",
 
446
  text,
447
  ref_audio_b64,
448
  ref_text or "",
449
  instruct or "",
 
450
  )
451
  path = result[0] if isinstance(result, (tuple, list)) else result
452
  with open(os.fspath(path), "rb") as f: