-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts_api_server.py
More file actions
1219 lines (1025 loc) · 45.2 KB
/
Copy pathtts_api_server.py
File metadata and controls
1219 lines (1025 loc) · 45.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
TTS API Server - Gateway + Worker Architecture
The gateway runs on port 8100 and orchestrates:
- Text chunking and normalization
- Audio post-processing (de-reverb, high-pass, de-ess, trim, normalize, peak limit)
- Whisper verification (via whisper worker subprocess)
- Job tracking with recovery and cancellation
- Format conversion (wav/mp3/ogg/flac/m4a)
- Worker lifecycle management (spawn/kill/scale/health)
- Load balancing across multiple worker instances
- Multi-GPU support
Workers are separate subprocesses, each running one TTS model instance.
The gateway delegates inference to workers via HTTP.
Run with: uvicorn tts_api_server:app --host 0.0.0.0 --port 8100
"""
import asyncio
import os
import re
import sys
import threading
from pathlib import Path
# Bootstrap: ensure project root is on sys.path for sibling imports
_BASE_DIR = Path(__file__).parent.resolve()
if str(_BASE_DIR) not in sys.path:
sys.path.insert(0, str(_BASE_DIR))
import io
import uuid
import time
import base64
import logging
import soundfile as sf
from typing import Optional
from difflib import SequenceMatcher
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
import httpx
import numpy as np
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
from config import (
BASE_DIR, MODELS_DIR, VENVS_DIR, OUTPUT_DIR, FFMPEG_PATH,
DEFAULT_API_HOST, DEFAULT_API_PORT, JOBS_DIR, PROJECTS_OUTPUT,
VOICE_DIR, MAX_RETRIES, WHISPER_MODEL_SIZE, WHISPER_ENABLED,
WHISPER_DEFAULT_TOLERANCE, WHISPER_AVAILABLE_MODELS, MAX_INFERENCE_WORKERS,
WORKER_AUTO_SPAWN, WORKER_DEFAULT_DEVICE,
WORKER_PORT_MIN, WORKER_PORT_MAX,
setup_environment,
)
from text_utils import chunk_text_for_model
from audio_profiles import PROFILES
from audio_processing import post_process, verify_with_whisper
from audio_assembler import assemble_chunks, convert_format
from job_manager import JobManager
from worker_registry import WorkerRegistry
from worker_manager import WorkerManager
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
setup_environment()
OUTPUT_DIR.mkdir(exist_ok=True)
JOBS_DIR.mkdir(parents=True, exist_ok=True)
PROJECTS_OUTPUT.mkdir(parents=True, exist_ok=True)
VOICE_DIR.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Worker infrastructure
# ---------------------------------------------------------------------------
registry = WorkerRegistry(WORKER_PORT_MIN, WORKER_PORT_MAX)
worker_manager = WorkerManager(registry)
# ---------------------------------------------------------------------------
# App lifecycle
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Start health check loop on startup, kill all workers on shutdown."""
worker_manager.start_health_checks()
logger.info("Gateway started - worker health checks active")
yield
logger.info("Gateway shutting down - killing all workers...")
worker_manager.stop_health_checks()
await worker_manager.kill_all_workers()
logger.info("All workers stopped")
# ---------------------------------------------------------------------------
# App
# ---------------------------------------------------------------------------
app = FastAPI(
title="TTS API Gateway",
description="Gateway server orchestrating TTS workers with full pipeline",
version="3.0.0",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------------------------
# Shared state
# ---------------------------------------------------------------------------
job_manager = JobManager(JOBS_DIR)
executor = ThreadPoolExecutor(max_workers=MAX_INFERENCE_WORKERS)
# Track running job_id per model for cancellation
_running_jobs: dict[str, set[str]] = {} # model -> set of active job_ids
_running_jobs_lock = threading.Lock()
# Bark history prompt state: { job_id: history_data }
# Gateway manages this, passes to worker per-chunk, receives updated history back
_bark_history: dict[str, object] = {}
# Whisper worker reference (worker_id of the dedicated whisper worker)
_whisper_worker_id: str | None = None
# Prevent concurrent auto-spawns for the same model
_spawn_locks: dict[str, asyncio.Lock] = {}
_spawn_locks_lock = threading.Lock()
def _ts():
return time.strftime("%H:%M:%S")
# ============================================================
# Request models
# ============================================================
class PipelineTTSRequest(BaseModel):
text: str
voice: Optional[str] = None
reference_audio: Optional[str] = None
reference_text: Optional[str] = None
language: Optional[str] = "en"
speed: Optional[float] = 1.0
temperature: Optional[float] = 0.65
repetition_penalty: Optional[float] = 2.0
de_reverb: Optional[float] = 0.7
de_ess: Optional[float] = 0.0
tolerance: Optional[float] = 80.0
verify_whisper: Optional[bool] = False
whisper_model: Optional[str] = None
output_format: Optional[str] = "wav"
save_path: Optional[str] = None
skip_post_process: Optional[bool] = False
auto_retry: Optional[int] = None
device: Optional[str] = None
mode: Optional[str] = "cloned"
class CancelRequest(BaseModel):
job_id: Optional[str] = None
class SpawnRequest(BaseModel):
model: str
device: Optional[str] = None
class ScaleRequest(BaseModel):
count: int
device: Optional[str] = None
# ============================================================
# Health / discovery
# ============================================================
@app.get("/")
@app.get("/health")
async def health():
workers = registry.all_workers()
loaded_models = list(set(w.model for w in workers if w.status in ("ready", "busy")))
return {
"status": "ok",
"message": "TTS API Gateway running",
"loaded_models": loaded_models,
"worker_count": len(workers),
}
@app.get("/api/models")
async def list_models():
return {
"models": [
{"id": "chatterbox", "name": "Chatterbox TTS", "env": "chatterbox_env"},
{"id": "f5", "name": "F5-TTS", "env": "f5tts_env"},
{"id": "xtts", "name": "XTTS v2", "env": "coqui_env"},
{"id": "bark", "name": "Bark", "env": "coqui_env"},
{"id": "fish", "name": "Fish Speech", "env": "unified_env"},
{"id": "kokoro", "name": "Kokoro", "env": "unified_env"},
{"id": "dia", "name": "Dia", "env": "unified_env"},
{"id": "qwen", "name": "Qwen TTS", "env": "qwen3_env"},
{"id": "vibevoice", "name": "VibeVoice", "env": "vibevoice_env"},
{"id": "higgs", "name": "Higgs Audio", "env": "higgs_env"},
]
}
@app.get("/api/models/status")
async def models_status():
workers = registry.all_workers()
status = {}
for w in workers:
if w.model not in status:
status[w.model] = {"workers": [], "loaded": False}
status[w.model]["workers"].append({
"worker_id": w.worker_id,
"device": w.device,
"status": w.status,
})
if w.status in ("ready", "busy"):
status[w.model]["loaded"] = True
return {"models": status}
# ============================================================
# GPU / Device discovery
# ============================================================
@app.get("/api/devices")
async def list_devices():
devices = worker_manager.detect_devices()
return {"devices": devices}
# ============================================================
# Worker management endpoints
# ============================================================
@app.get("/api/workers")
async def list_workers():
return {"workers": registry.to_dict_list()}
@app.post("/api/workers/spawn")
async def spawn_worker(req: SpawnRequest):
try:
worker = await worker_manager.spawn_worker(req.model, req.device)
return {
"status": "spawned",
"worker_id": worker.worker_id,
"model": worker.model,
"port": worker.port,
"device": worker.device,
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/api/workers/{worker_id}")
async def delete_worker(worker_id: str):
killed = await worker_manager.kill_worker(worker_id)
if not killed:
raise HTTPException(status_code=404, detail=f"Worker '{worker_id}' not found")
return {"status": "killed", "worker_id": worker_id}
@app.post("/api/models/{model}/scale")
async def scale_model(model: str, req: ScaleRequest):
try:
workers = await worker_manager.scale_model(model, req.count, req.device)
return {
"status": "scaled",
"model": model,
"device": req.device or WORKER_DEFAULT_DEVICE,
"count": len(workers),
"workers": [w.worker_id for w in workers],
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ============================================================
# Model load/unload (now delegates to workers)
# ============================================================
@app.post("/api/models/{model}/load")
async def load_model(model: str, device: Optional[str] = None):
"""Load a model by spawning a worker for it."""
existing = registry.get_ready_workers(model)
if existing:
return {"status": "already_loaded", "model": model,
"workers": [w.worker_id for w in existing]}
try:
worker = await worker_manager.spawn_worker(model, device)
return {"status": "loaded", "model": model, "worker_id": worker.worker_id}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/models/{model}/unload")
async def unload_model(model: str):
"""Unload a model by killing all its workers."""
workers = registry.workers_for_model(model)
if not workers:
return {"status": "not_loaded", "model": model}
killed = 0
for w in workers:
if await worker_manager.kill_worker(w.worker_id):
killed += 1
return {"status": "unloaded", "model": model, "workers_killed": killed}
# ============================================================
# Whisper management
# ============================================================
@app.get("/api/whisper")
async def whisper_info():
global _whisper_worker_id
whisper_workers = registry.workers_for_model("whisper")
loaded_sizes = []
# Query the whisper worker for its loaded sizes
if whisper_workers:
for w in whisper_workers:
if w.status in ("ready", "busy"):
try:
async with httpx.AsyncClient(timeout=5.0) as client:
resp = await client.get(f"http://127.0.0.1:{w.port}/health")
if resp.status_code == 200:
loaded_sizes.append(w.worker_id)
except Exception:
pass
return {
"available_models": WHISPER_AVAILABLE_MODELS,
"default": WHISPER_MODEL_SIZE,
"loaded": loaded_sizes,
"enabled": WHISPER_ENABLED,
"whisper_workers": [w.worker_id for w in whisper_workers],
}
@app.post("/api/whisper/{size}/load")
async def load_whisper(size: str):
global _whisper_worker_id
if size not in WHISPER_AVAILABLE_MODELS:
raise HTTPException(
status_code=400,
detail=f"Invalid size. Choose from: {list(WHISPER_AVAILABLE_MODELS.keys())}"
)
# Spawn a whisper worker if none exists
whisper_workers = registry.workers_for_model("whisper")
if not whisper_workers:
try:
worker = await worker_manager.spawn_worker("whisper")
_whisper_worker_id = worker.worker_id
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to spawn whisper worker: {e}")
return {"status": "loaded", "size": size, "info": WHISPER_AVAILABLE_MODELS[size]}
@app.post("/api/whisper/{size}/unload")
async def unload_whisper(size: str):
# Kill whisper workers
whisper_workers = registry.workers_for_model("whisper")
for w in whisper_workers:
await worker_manager.kill_worker(w.worker_id)
return {"status": "unloaded", "size": size}
# ============================================================
# Job management endpoints
# ============================================================
@app.get("/api/jobs")
async def list_jobs():
return {"jobs": job_manager.list_jobs()}
@app.get("/api/jobs/{job_id}")
async def get_job(job_id: str):
job = job_manager.get_job(job_id)
if job is None:
raise HTTPException(status_code=404, detail="Job not found")
return job
@app.post("/api/jobs/{job_id}/recover")
async def recover_job(job_id: str):
job = job_manager.get_job(job_id)
if job is None:
raise HTTPException(status_code=404, detail="Job not found")
job_dir = Path(job.get("job_dir", JOBS_DIR / job_id))
recovered = job_manager.recover_job(job_dir)
if recovered is None:
raise HTTPException(status_code=400, detail="Job not recoverable (already complete or missing)")
model = recovered["model"]
if model in PROFILES:
executor.submit(_run_pipeline_recovery, model, recovered, job_dir)
return {"status": "recovering", "job_id": recovered["job_id"],
"resuming_from_chunk": recovered["chunks_completed"]}
raise HTTPException(status_code=400, detail=f"Recovery not supported for model: {model}")
# ============================================================
# Cancel endpoints
# ============================================================
@app.post("/api/tts/{model}/cancel")
async def cancel_model_job(model: str, body: CancelRequest = CancelRequest()):
if body.job_id:
# Cancel a specific job
if job_manager.request_cancel(body.job_id):
return {"status": "cancel_requested", "job_id": body.job_id}
return {"status": "job_not_found", "job_id": body.job_id}
# Cancel all running jobs for this model
with _running_jobs_lock:
active = list(_running_jobs.get(model, set()))
if active:
cancelled = [jid for jid in active if job_manager.request_cancel(jid)]
if cancelled:
return {"status": "cancel_requested", "job_ids": cancelled}
return {"status": "no_running_job", "model": model}
# ============================================================
# Pipeline TTS endpoints (backward compatible)
# ============================================================
@app.post("/api/tts/xtts")
async def tts_xtts(req: PipelineTTSRequest):
return await _handle_pipeline_request("xtts", req)
@app.post("/api/tts/fish")
async def tts_fish(req: PipelineTTSRequest):
return await _handle_pipeline_request("fish", req)
@app.post("/api/tts/kokoro")
async def tts_kokoro(req: PipelineTTSRequest):
return await _handle_pipeline_request("kokoro", req)
@app.post("/api/tts/bark")
async def tts_bark(req: PipelineTTSRequest):
return await _handle_pipeline_request("bark", req)
@app.post("/api/tts/chatterbox")
async def tts_chatterbox(req: PipelineTTSRequest):
return await _handle_pipeline_request("chatterbox", req)
@app.post("/api/tts/f5")
async def tts_f5(req: PipelineTTSRequest):
return await _handle_pipeline_request("f5", req)
@app.post("/api/tts/dia")
async def tts_dia(req: PipelineTTSRequest):
return await _handle_pipeline_request("dia", req)
@app.post("/api/tts/qwen")
async def tts_qwen(req: PipelineTTSRequest):
return await _handle_pipeline_request("qwen", req)
@app.post("/api/tts/vibevoice")
async def tts_vibevoice(req: PipelineTTSRequest):
return await _handle_pipeline_request("vibevoice", req)
@app.post("/api/tts/higgs")
async def tts_higgs(req: PipelineTTSRequest):
return await _handle_pipeline_request("higgs", req)
async def _handle_pipeline_request(model: str, req: PipelineTTSRequest):
"""Shared handler for all pipeline TTS requests."""
raw_text = req.text.strip()
if not raw_text:
raise HTTPException(status_code=400, detail="Missing text")
profile = PROFILES[model]
params = req.model_dump()
output_format = (req.output_format or "wav").lower()
max_retries = req.auto_retry if req.auto_retry is not None else MAX_RETRIES
# --- Recovery mode ---
if raw_text.lower() == "##recover##":
target = (req.save_path or "").strip()
if not target:
raise HTTPException(status_code=400, detail="Set save_path to the folder you want to recover")
job_dir = _resolve_job_dir(target)
recovered = job_manager.recover_job(job_dir)
if recovered is None:
raise HTTPException(status_code=400, detail="Job not recoverable or already finished")
executor.submit(_run_pipeline_recovery, model, recovered, job_dir)
return JSONResponse({
"status": "recovering",
"job_id": recovered["job_id"],
"resuming_from_chunk": recovered["chunks_completed"],
"total_chunks": recovered["total_chunks"],
})
# --- Ensure a worker exists for this model ---
worker = registry.pick_worker(model)
if not worker:
if WORKER_AUTO_SPAWN:
# Per-model lock prevents concurrent auto-spawns for the same model
with _spawn_locks_lock:
if model not in _spawn_locks:
_spawn_locks[model] = asyncio.Lock()
async with _spawn_locks[model]:
# Re-check after acquiring the lock — another request may have spawned one
worker = registry.pick_worker(model)
if not worker:
try:
device = req.device or WORKER_DEFAULT_DEVICE
worker = await worker_manager.spawn_worker(model, device)
except Exception as e:
raise HTTPException(
status_code=503,
detail=f"No worker available for '{model}' and auto-spawn failed: {e}"
)
else:
raise HTTPException(
status_code=503,
detail=f"No worker available for model '{model}'. Spawn one first via POST /api/workers/spawn"
)
# --- New job ---
text = raw_text
chunks = chunk_text_for_model(text, model)
save_path_input = (req.save_path or "").strip()
job_dir, stem = _resolve_output_paths(model, save_path_input)
job_dir.mkdir(parents=True, exist_ok=True)
job = job_manager.create_job(
model=model, text=text, chunks=chunks, params=params,
output_format=output_format, sample_rate=profile["sample_rate"],
stem=stem, job_dir=job_dir,
)
job_id = job["job_id"]
logger.info("[%s %s] New job %s: %d chunks, format=%s",
_ts(), model.upper(), job_id, len(chunks), output_format)
# Run pipeline in thread pool (blocking HTTP calls to workers)
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
executor,
_run_pipeline,
model, job_id, chunks, 0, params, profile, job_dir, stem,
output_format, max_retries, save_path_input,
)
return JSONResponse(result)
# ============================================================
# Pipeline core (runs in thread pool)
# ============================================================
def _run_pipeline(model: str, job_id: str, chunks: list[str],
start_from: int, params: dict, profile: dict,
job_dir: Path, stem: str, output_format: str,
max_retries: int, save_path_input: str) -> dict:
"""Execute the full TTS pipeline, delegating inference to workers via HTTP."""
logger.info("[%s %s] Pipeline thread started for job %s (thread=%s, chunks=%d)",
_ts(), model.upper(), job_id, threading.current_thread().name, len(chunks))
with _running_jobs_lock:
_running_jobs.setdefault(model, set()).add(job_id)
sr = profile["sample_rate"]
speed = float(params.get("speed", 1.0))
de_reverb = float(params.get("de_reverb", 0.7))
de_ess_raw = float(params.get("de_ess", 0.0))
de_ess = de_ess_raw / 100.0 if de_ess_raw > 1.0 else de_ess_raw
tolerance = float(params.get("tolerance", WHISPER_DEFAULT_TOLERANCE))
do_verify_whisper = params.get("verify_whisper", False)
skip_post_process = params.get("skip_post_process", False)
try:
# Bark: initialize history prompt state
bark_base_history = None
if model == "bark":
voice_preset = params.get("voice") or "v2/en_speaker_6"
bark_base_history = voice_preset
_bark_history[job_id] = bark_base_history
history_reset_every = profile.get("history_reset_every", 5)
for i in range(start_from, len(chunks)):
# Check cancellation
if job_manager.is_cancelled(job_id):
logger.info("[%s %s] Job %s cancelled at chunk %d",
_ts(), model.upper(), job_id, i)
job_manager.fail_job(job_id, "Cancelled by user")
return {"error": "Cancelled", "job_id": job_id, "cancelled_at_chunk": i}
chunk_text = chunks[i]
retry_count = 0
while True:
try:
# Bark: hybrid history - reset periodically
if model == "bark":
chunk_offset = i - start_from
if chunk_offset > 0 and chunk_offset % history_reset_every == 0:
logger.info("[%s BARK] Resetting history at chunk %d",
_ts(), i)
_bark_history[job_id] = bark_base_history
# --- Inference via worker ---
infer_params = dict(params)
if model == "bark":
infer_params["history_prompt"] = _bark_history.get(job_id)
raw_data, chunk_sr = _infer_via_worker(model, chunk_text, infer_params, job_id)
# Prepend front pad if configured
front_pad = profile.get("front_pad_sec", 0.0)
if front_pad > 0:
pad = np.zeros(int(chunk_sr * front_pad), dtype=np.float32)
raw_data = np.concatenate([pad, raw_data])
# Save raw chunk
tmp_wav = OUTPUT_DIR / f"raw_{model}_{i}_{uuid.uuid4().hex}.wav"
sf.write(str(tmp_wav), raw_data, chunk_sr, subtype="PCM_16")
# Post-process
if not skip_post_process:
post_process(str(tmp_wav), profile, speed, de_reverb, de_ess)
# Whisper verification
if do_verify_whisper:
whisper_size = params.get("whisper_model") or WHISPER_MODEL_SIZE
passed, sim, transcript = _verify_whisper_via_worker(
str(tmp_wav), chunk_text,
params.get("language", "en"),
tolerance, whisper_size,
)
job_manager.update_chunk(
job_id, i, status="success" if passed else "failed",
whisper_transcript=transcript,
whisper_similarity=sim,
verification_passed=passed,
)
if not passed:
raise ValueError(
f"Whisper verification failed (similarity={sim:.3f})"
)
# Move to final chunk location
data, _ = sf.read(str(tmp_wav))
duration_sec = len(data) / chunk_sr
chunk_wav = job_dir / f"chunk_{i:03d}.wav"
tmp_wav.replace(chunk_wav)
job_manager.update_chunk(
job_id, i, status="success",
duration=duration_sec,
audio_file=chunk_wav.name,
)
logger.info("[%s %s] Chunk %03d -> %.2fs (success)",
_ts(), model.upper(), i, duration_sec)
break # Success -> next chunk
except Exception as e:
retry_count += 1
error_msg = str(e) or "Unknown error"
if retry_count > max_retries:
logger.error("[%s %s] Chunk %03d failed after %d retries: %s",
_ts(), model.upper(), i, max_retries, error_msg)
job_manager.update_chunk(job_id, i, status="failed", error=error_msg)
job_manager.fail_job(
job_id, f"Chunk {i} failed after {max_retries} retries"
)
return {
"error": "generation_failed",
"reason": f"Chunk {i} failed after {max_retries} retries",
"failed_at_chunk": i,
"job_id": job_id,
"job_folder": str(job_dir.name),
"recover_command": f"##recover## (save_path: {job_dir.name})",
}
logger.warning("[%s %s] Chunk %03d retry %d/%d: %s",
_ts(), model.upper(), i, retry_count, max_retries,
error_msg)
time.sleep(1)
# --- Assembly ---
missing = [
f"chunk_{i:03d}.wav" for i in range(len(chunks))
if not (job_dir / f"chunk_{i:03d}.wav").exists()
]
if missing:
return {
"status": "incomplete",
"message": f"Missing {len(missing)} chunk(s)",
"missing_count": len(missing),
"job_id": job_id,
"job_folder": str(job_dir.name),
"recover_command": f"##recover## (save_path: {job_dir.name})",
}
chunk_files = [job_dir / f"chunk_{i:03d}.wav" for i in range(len(chunks))]
assembled_wav = OUTPUT_DIR / f"assembled_{uuid.uuid4().hex}.wav"
assemble_chunks(
chunk_files, str(assembled_wav), sr,
inter_pause=profile.get("inter_pause_sec", 0.25),
front_pad=profile.get("padding_sec", 0.5),
end_pad=profile.get("padding_sec", 0.5),
)
# Calculate duration from the assembled WAV (before format conversion,
# since sf.read cannot read mp3/ogg/m4a)
assembled_data, _ = sf.read(str(assembled_wav))
total_duration = len(assembled_data) / sr
del assembled_data
# Format conversion
final_filename = f"{stem}_final.{output_format}"
final_path = job_dir / final_filename
if output_format != "wav":
convert_format(str(assembled_wav), str(final_path), output_format, FFMPEG_PATH)
assembled_wav.unlink(missing_ok=True)
else:
assembled_wav.replace(final_path)
job_manager.complete_job(job_id, final_filename, total_duration)
logger.info("[%s %s] Job %s complete: %s (%.1fs)",
_ts(), model.upper(), job_id, final_filename, total_duration)
resp = {
"status": "completed",
"job_id": job_id,
"filename": final_filename,
"saved_to": str(final_path),
"sample_rate": sr,
"duration_sec": round(total_duration, 3),
"format": output_format,
}
if not save_path_input:
resp["audio_base64"] = base64.b64encode(final_path.read_bytes()).decode("utf-8")
return resp
except Exception as e:
error_str = str(e) or "Unknown error"
logger.error("[%s %s] Unexpected error: %s", _ts(), model.upper(), error_str)
job_manager.fail_job(job_id, error_str)
return {
"error": "generation_failed",
"reason": error_str,
"job_id": job_id,
"job_folder": str(job_dir.name),
}
finally:
with _running_jobs_lock:
active = _running_jobs.get(model, set())
active.discard(job_id)
if not active:
_running_jobs.pop(model, None)
_bark_history.pop(job_id, None)
job_manager.cleanup_cancel_flag(job_id)
def _run_pipeline_recovery(model: str, job: dict, job_dir: Path):
"""Resume a recovered job from where it left off."""
chunks = [c["text"] for c in job["chunks"]]
start_from = job["chunks_completed"]
params = job["parameters"]
profile = PROFILES[model]
output_format = job.get("output_format", "wav")
stem = job_dir.name
_run_pipeline(
model, job["job_id"], chunks, start_from, params,
profile, job_dir, stem, output_format, MAX_RETRIES, str(job_dir),
)
# ============================================================
# Worker-delegated inference
# ============================================================
def _finalize_worker(worker_id: str, timed_out: bool = False) -> None:
"""Check if a worker is still alive and mark ready or dead accordingly.
If timed_out=True, the worker is assumed stuck (e.g. hung CUDA generate())
and will be forcibly killed at the process level.
"""
worker_info = registry.get(worker_id)
if not worker_info:
return
if timed_out and worker_info.process and worker_info.process.poll() is None:
# Worker process is alive but not responding — kill it
logger.warning("Worker %s timed out — killing stuck process (pid=%s)",
worker_id, worker_info.process.pid)
try:
worker_info.process.terminate()
try:
worker_info.process.wait(timeout=5)
except Exception:
worker_info.process.kill()
worker_info.process.wait(timeout=5)
except Exception as e:
logger.error("Failed to kill stuck worker %s: %s", worker_id, e)
if worker_info.log_fh:
try:
worker_info.log_fh.close()
except Exception:
pass
worker_info.log_fh = None
worker_info.process = None
registry.unregister(worker_id)
registry.release_port(worker_info.port)
return
if worker_info.process and worker_info.process.poll() is not None:
logger.warning("Worker %s process died (exit code %d)",
worker_id, worker_info.process.returncode)
if worker_info.log_fh:
try:
worker_info.log_fh.close()
except Exception:
pass
worker_info.log_fh = None
registry.unregister(worker_id)
registry.release_port(worker_info.port)
else:
registry.mark_ready(worker_id)
def _infer_via_worker(model: str, text: str, params: dict,
job_id: str | None = None) -> tuple:
"""Send inference request to a worker and decode the response.
Retries with different workers on connection failures.
Returns (numpy_array, sample_rate).
"""
logger.info("[%s %s] _infer_via_worker called for job %s, text=%r",
_ts(), model.upper(), job_id, text[:80] + "..." if len(text) > 80 else text)
# Pre-serialize bark history (do once, reuse across retries)
send_params = dict(params)
if model == "bark" and "history_prompt" in send_params:
hp = send_params["history_prompt"]
if isinstance(hp, tuple) and len(hp) == 3 and hasattr(hp[0], 'dtype'):
encoded = []
for arr in hp:
buf = io.BytesIO()
np.save(buf, arr)
encoded.append(base64.b64encode(buf.getvalue()).decode("utf-8"))
send_params["history_prompt"] = {"_bark_b64": encoded}
max_worker_attempts = 3
last_error = None
tried_workers: set[str] = set()
for attempt in range(max_worker_attempts):
worker = registry.pick_worker(model)
if not worker:
break
# Don't retry the same worker that just failed
if worker.worker_id in tried_workers:
# Try to find a different worker
all_ready = registry.get_ready_workers(model)
untried = [w for w in all_ready if w.worker_id not in tried_workers]
if not untried:
break
worker = untried[0]
tried_workers.add(worker.worker_id)
worker_id = worker.worker_id
registry.mark_busy(worker_id, job_id)
infer_start = time.time()
try:
url = f"http://127.0.0.1:{worker.port}/infer"
# Large models may need longer for first inference (includes model loading)
infer_timeout = 900.0 if model in ("qwen", "higgs", "vibevoice", "dia", "fish") else 300.0
logger.info("[%s %s] Sending POST %s (worker=%s, timeout=%.0fs)",
_ts(), model.upper(), url, worker_id, infer_timeout)
with httpx.Client(timeout=infer_timeout) as client:
resp = client.post(url, json={"text": text, "params": send_params})
logger.info("[%s %s] Worker %s responded with status %d",
_ts(), model.upper(), worker_id, resp.status_code)
if resp.status_code != 200:
detail = resp.json().get("detail", resp.text) if resp.headers.get(
"content-type", "").startswith("application/json") else resp.text
raise RuntimeError(
f"Worker {worker_id} returned {resp.status_code}: {detail}")
data = resp.json()
audio_b64 = data["audio_b64"]
sample_rate = data["sample_rate"]
buf = io.BytesIO(base64.b64decode(audio_b64))
audio_array = np.load(buf, allow_pickle=False)
# For bark, capture updated history
if model == "bark" and "bark_history" in data and job_id:
history_parts = []
for part_b64 in data["bark_history"]:
hbuf = io.BytesIO(base64.b64decode(part_b64))
history_parts.append(np.load(hbuf, allow_pickle=False))
_bark_history[job_id] = tuple(history_parts)
# Success - mark worker ready
registry.mark_ready(worker_id)
return audio_array, sample_rate
except (httpx.ReadTimeout, httpx.PoolTimeout, httpx.WriteTimeout) as e:
# Inference timed out — worker is likely stuck in generate().
# Kill the process so it doesn't hold GPU memory forever.
elapsed = time.time() - infer_start
logger.error("Worker %s inference TIMED OUT after %.0fs (attempt %d/%d): %s",
worker_id, elapsed, attempt + 1, max_worker_attempts, e)
_finalize_worker(worker_id, timed_out=True)
last_error = f"Inference timed out after {int(elapsed)}s"
continue
except (httpx.ConnectError, httpx.ReadError, httpx.WriteError,
httpx.ConnectTimeout, httpx.RemoteProtocolError,
ConnectionError) as e:
# Worker connection failure - mark dead/ready, try next worker
logger.warning("Worker %s connection failed (attempt %d/%d): %s",
worker_id, attempt + 1, max_worker_attempts, e)
_finalize_worker(worker_id)
last_error = str(e)
continue
except Exception:
# Non-connection errors (inference failure, decode error) -
# finalize worker and re-raise for the outer retry loop
_finalize_worker(worker_id)
raise
if last_error:
raise RuntimeError(
f"All {len(tried_workers)} worker(s) for '{model}' failed: {last_error}")
raise RuntimeError(f"No ready worker available for model '{model}'")
def _verify_whisper_via_worker(audio_path: str, expected_text: str,
language: str, tolerance: float,
whisper_size: str) -> tuple:
"""Verify audio via whisper worker, falling back to local whisper.
Returns (passed: bool, similarity: float, transcript: str).
"""
# Try whisper worker first
whisper_workers = registry.get_ready_workers("whisper")
if whisper_workers:
worker = whisper_workers[0]
try:
with httpx.Client(timeout=60.0) as client:
resp = client.post(
f"http://127.0.0.1:{worker.port}/transcribe",
json={"audio_path": audio_path, "size": whisper_size},
)
if resp.status_code == 200:
result = resp.json()
transcript = result.get("text", "")
# Sanitize for comparison
clean_expected = expected_text.lower().strip()
clean_transcript = transcript.lower().strip()
sim = SequenceMatcher(None, clean_expected, clean_transcript).ratio()
passed = (sim * 100) >= tolerance
return passed, sim, transcript
except Exception as e:
logger.warning("Whisper worker failed, falling back to local: %s", e)