Skip to content

Commit 750fabc

Browse files
fix(model-unload): lazy reload on next request after /dev/unload
Add ensure_backend() to ModelManager which reinitialises the backend and reloads the model if /dev/unload was called. All three get_backend() call sites in tts_service now await ensure_backend() first, so the first TTS request after an unload reloads the model automatically rather than raising RuntimeError: Backend not initialized. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent 3b2cfee commit 750fabc

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎api/src/inference/model_manager.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ async def initialize_with_warmup(self, voice_manager) -> tuple[str, str, int]:
101101
except Exception as e:
102102
raise RuntimeError(f"Warmup failed: {e}")
103103

104+
async def ensure_backend(self) -> None:
105+
"""Reload the backend if it was unloaded via /dev/unload."""
106+
if not self._backend:
107+
await self.initialize()
108+
await self.load_model(self._config.pytorch_kokoro_v1_file)
109+
104110
def get_backend(self) -> BaseModelBackend:
105111
"""Get initialized backend.
106112

‎api/src/services/tts_service.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def _process_chunk(
8888
if not tokens and not chunk_text:
8989
return
9090

91-
# Get backend
91+
await self.model_manager.ensure_backend()
9292
backend = self.model_manager.get_backend()
9393

9494
# Generate audio using pre-warmed model
@@ -273,7 +273,7 @@ async def generate_audio_stream(
273273
chunk_index = 0
274274
current_offset = 0.0
275275
try:
276-
# Get backend
276+
await self.model_manager.ensure_backend()
277277
backend = self.model_manager.get_backend()
278278

279279
# Get voice path, handling combined voices
@@ -466,7 +466,7 @@ async def generate_from_phonemes(
466466
"""
467467
start_time = time.time()
468468
try:
469-
# Get backend and voice path
469+
await self.model_manager.ensure_backend()
470470
backend = self.model_manager.get_backend()
471471
voice_name, voice_path = await self._get_voices_path(voice)
472472

0 commit comments

Comments
 (0)