Spaces:
Running
Running
File size: 12,492 Bytes
8f8a746 | 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 | """
VoiceCloneClient — a thin router/facade over available TTS providers.
Selects a provider per-request based on:
1. A forced provider (if the caller supplies one).
2. The canonical LANGUAGE_CATALOG hint (`chatterbox` / `minimax`).
3. Availability (is_configured + supports_language).
Handles: retry-once on transient errors, user-readable error messages,
and language discovery merged across all providers.
"""
from __future__ import annotations
import logging
import time
import wave
from pathlib import Path
from typing import Optional
from app.config import LANGUAGE_CATALOG, Settings, get_settings
from app.errors import (
ConfigError,
ProviderHTTPError,
ProviderTimeoutError,
ProviderUnavailableError,
ReferenceAudioError,
UnsupportedLanguageError,
VoiceCloneError,
)
from app.providers import ChatterboxProvider, MinimaxProvider
from app.providers.base import GenerationResult, HealthStatus, TTSProvider
log = logging.getLogger(__name__)
TRANSIENT_STATUS_CODES = {408, 429, 500, 502, 503, 504}
class VoiceCloneClient:
"""High-level orchestrator used by Gradio + tests."""
def __init__(
self,
settings: Settings | None = None,
chatterbox: Optional[TTSProvider] = None,
minimax: Optional[TTSProvider] = None,
):
self.settings = settings or get_settings()
self.chatterbox: TTSProvider = chatterbox or ChatterboxProvider(settings=self.settings)
self.minimax: TTSProvider = minimax or MinimaxProvider(settings=self.settings)
# ------------------------------------------------------------------
# Language discovery
# ------------------------------------------------------------------
def discover_languages(self) -> dict[str, dict[str, str]]:
"""
Return the languages we will actually expose in the UI.
Starts from LANGUAGE_CATALOG, then filters to those the configured
provider(s) can actually serve. Cantonese is enforced as mandatory:
if no configured provider supports `yue`, we log a loud warning.
"""
chatterbox_langs = set(self.chatterbox.supported_languages()) \
if self.chatterbox.is_configured() else set()
minimax_langs = set(self.minimax.supported_languages()) \
if self.minimax.is_configured() else set()
available: dict[str, dict[str, str]] = {}
for code, meta in LANGUAGE_CATALOG.items():
if meta["provider_hint"] == "chatterbox" and code in chatterbox_langs:
available[code] = meta
elif meta["provider_hint"] == "minimax" and code in minimax_langs:
available[code] = meta
elif code in chatterbox_langs or code in minimax_langs:
# Catalog says X but in practice either provider has it.
available[code] = meta
if "yue" not in available:
log.warning(
"Cantonese (yue) is not available in the current configuration. "
"Configure MINIMAX_API_KEY or another yue-capable backend."
)
return available
def cantonese_is_available(self) -> bool:
return "yue" in self.discover_languages()
# ------------------------------------------------------------------
# Health
# ------------------------------------------------------------------
def health(self) -> dict[str, HealthStatus]:
return {
"chatterbox": self.chatterbox.health(),
"minimax": self.minimax.health(),
}
def any_provider_reachable(self) -> bool:
return any(h.reachable for h in self.health().values())
# ------------------------------------------------------------------
# Reference audio validation
# ------------------------------------------------------------------
def validate_reference_audio(self, path: Path) -> tuple[bool, str]:
"""Return (ok, message). Message includes guidance for short clips."""
if not path.exists():
return False, "Reference audio file not found."
size_mb = path.stat().st_size / 1_000_000
if size_mb > self.settings.max_reference_mb:
return False, (
f"Reference audio is {size_mb:.1f} MB which exceeds the "
f"{self.settings.max_reference_mb} MB limit."
)
try:
with wave.open(str(path), "rb") as w:
duration = w.getnframes() / float(w.getframerate() or 1)
except wave.Error as e:
return False, (
f"Reference audio is not a valid WAV file ({e}). "
"Please upload a PCM WAV at 16 kHz or higher."
)
if duration < self.settings.min_reference_seconds:
return False, (
f"Reference audio is only {duration:.1f}s — minimum is "
f"{self.settings.min_reference_seconds:.0f}s."
)
if duration > self.settings.max_reference_seconds:
return False, (
f"Reference audio is {duration:.1f}s — trim to "
f"≤ {self.settings.max_reference_seconds:.0f}s for best results."
)
if duration < self.settings.recommended_reference_seconds_min:
return True, (
f"Clip is {duration:.1f}s. Recommended is "
f"{self.settings.recommended_reference_seconds_min:.0f}–"
f"{self.settings.recommended_reference_seconds_max:.0f}s "
f"for a cleaner clone."
)
return True, f"Reference OK ({duration:.1f}s)."
# ------------------------------------------------------------------
# Provider routing
# ------------------------------------------------------------------
def _pick_provider(self, language_code: str,
force: Optional[str] = None) -> TTSProvider:
if force == "chatterbox":
if not self.chatterbox.is_configured():
raise ConfigError("Chatterbox backend not configured.")
return self.chatterbox
if force == "minimax":
if not self.minimax.is_configured():
raise ConfigError("MiniMax backend not configured.")
return self.minimax
meta = LANGUAGE_CATALOG.get(language_code)
hint = meta["provider_hint"] if meta else None
# First try the catalog hint. If the hinted provider is configured,
# prefer it even if `/languages` discovery failed transiently — the
# catalog is our source of truth for what each provider *can* do.
if hint == "minimax" and self.minimax.is_configured():
return self.minimax
if hint == "chatterbox" and self.chatterbox.is_configured():
return self.chatterbox
# Fallback: any configured provider that supports the language.
if self.chatterbox.is_configured() and self.chatterbox.supports_language(language_code):
return self.chatterbox
if self.minimax.is_configured() and self.minimax.supports_language(language_code):
return self.minimax
raise UnsupportedLanguageError(
f"No configured provider supports language '{language_code}'. "
f"Check CHATTERBOX_API_BASE_URL or MINIMAX_API_KEY."
)
# ------------------------------------------------------------------
# Generation
# ------------------------------------------------------------------
def clone_voice(
self,
*,
reference_audio_path: Path,
text: str,
language_code: str,
exaggeration: Optional[float] = None,
cfg_weight: Optional[float] = None,
temperature: Optional[float] = None,
force_provider: Optional[str] = None,
) -> GenerationResult:
if not text or not text.strip():
raise VoiceCloneError("Please enter some text to synthesise.")
ok, msg = self.validate_reference_audio(reference_audio_path)
if not ok:
raise ReferenceAudioError(msg)
exag = exaggeration if exaggeration is not None else self.settings.default_exaggeration
cfg = cfg_weight if cfg_weight is not None else self.settings.default_cfg_weight
tmp = temperature if temperature is not None else self.settings.default_temperature
# Clamp to documented ranges.
exag = max(self.settings.exaggeration_range[0],
min(self.settings.exaggeration_range[1], exag))
cfg = max(self.settings.cfg_weight_range[0],
min(self.settings.cfg_weight_range[1], cfg))
tmp = max(self.settings.temperature_range[0],
min(self.settings.temperature_range[1], tmp))
provider = self._pick_provider(language_code, force=force_provider)
attempt = 0
last_exc: Exception | None = None
max_attempts = 2 if self.settings.request_retry_once else 1
while attempt < max_attempts:
attempt += 1
try:
return provider.generate(
text=text,
reference_audio_path=reference_audio_path,
language_code=language_code,
exaggeration=exag,
cfg_weight=cfg,
temperature=tmp,
)
except ProviderTimeoutError as e:
last_exc = e
log.warning("provider timeout (attempt %d/%d): %s",
attempt, max_attempts, e)
except ProviderUnavailableError as e:
last_exc = e
log.warning("provider unavailable (attempt %d/%d): %s",
attempt, max_attempts, e)
except ProviderHTTPError as e:
last_exc = e
if e.status_code in TRANSIENT_STATUS_CODES:
log.warning("transient HTTP %d (attempt %d/%d): %s",
e.status_code, attempt, max_attempts, e)
else:
# Non-transient: surface immediately.
if self.settings.log_provider_errors:
log.error("provider error body: %s", e.provider_response)
raise
if attempt < max_attempts:
time.sleep(1.0)
assert last_exc is not None
if self.settings.log_provider_errors and hasattr(last_exc, "provider_response"):
log.error("final provider error body: %s",
getattr(last_exc, "provider_response", None))
raise last_exc
def language_tour(
self,
*,
reference_audio_path: Path,
text: str,
language_codes: Optional[list[str]] = None,
exaggeration: Optional[float] = None,
cfg_weight: Optional[float] = None,
) -> list[tuple[str, Optional[GenerationResult], Optional[str]]]:
"""
Clone the same voice across multiple languages.
Returns a list of (language_code, result_or_None, error_or_None).
Never raises — individual failures are captured per-language.
"""
langs = language_codes or list(self.discover_languages().keys())
out: list[tuple[str, Optional[GenerationResult], Optional[str]]] = []
for code in langs:
try:
res = self.clone_voice(
reference_audio_path=reference_audio_path,
text=text,
language_code=code,
exaggeration=exaggeration,
cfg_weight=cfg_weight,
)
out.append((code, res, None))
except VoiceCloneError as e:
out.append((code, None, str(e)))
return out
# ------------------------------------------------------------------
# Cleanup
# ------------------------------------------------------------------
def cleanup_temp(self, max_age_seconds: int = 3600) -> int:
"""Delete temp WAVs older than max_age_seconds. Returns count deleted."""
now = time.time()
count = 0
if not self.settings.temp_dir.exists():
return 0
for p in self.settings.temp_dir.glob("*.wav"):
try:
if now - p.stat().st_mtime > max_age_seconds:
p.unlink()
count += 1
except OSError:
continue
return count
|