Kokoro-FastAPI/api/src/services/tts_service.py

656 lines
29 KiB
Python
Raw Normal View History

"""TTS service using model and voice managers."""
2025-02-09 18:32:17 -07:00
import asyncio
import os
import tempfile
2025-02-09 18:32:17 -07:00
import time
from typing import AsyncGenerator, List, Optional, Tuple, Union
2025-02-12 15:06:11 +00:00
from ..inference.base import AudioChunk
2025-01-13 20:15:46 -07:00
import numpy as np
import torch
2025-02-09 18:32:17 -07:00
from kokoro import KPipeline
from loguru import logger
2025-01-09 18:41:44 -07:00
from ..core.config import settings
2025-02-09 18:32:17 -07:00
from ..inference.kokoro_v1 import KokoroV1
from ..inference.model_manager import get_manager as get_model_manager
from ..inference.voice_manager import get_manager as get_voice_manager
2025-01-13 20:15:46 -07:00
from .audio import AudioNormalizer, AudioService
from .text_processing import tokenize
2025-02-09 18:32:17 -07:00
from .text_processing.text_processor import process_text_chunk, smart_split
2025-02-11 19:09:35 -05:00
from ..structures.schemas import NormalizationOptions
class TTSService:
"""Text-to-speech service."""
# Limit concurrent chunk processing
_chunk_semaphore = asyncio.Semaphore(4)
def __init__(self, output_dir: str = None):
2025-01-25 05:25:13 -07:00
"""Initialize service."""
self.output_dir = output_dir
self.model_manager = None
self._voice_manager = None
@classmethod
2025-02-09 18:32:17 -07:00
async def create(cls, output_dir: str = None) -> "TTSService":
2025-01-25 05:25:13 -07:00
"""Create and initialize TTSService instance."""
service = cls(output_dir)
service.model_manager = await get_model_manager()
service._voice_manager = await get_voice_manager()
return service
2025-01-25 05:25:13 -07:00
async def _process_chunk(
self,
chunk_text: str,
tokens: List[int],
voice_name: str,
voice_path: str,
2025-01-25 05:25:13 -07:00
speed: float,
output_format: Optional[str] = None,
is_first: bool = False,
is_last: bool = False,
normalizer: Optional[AudioNormalizer] = None,
lang_code: Optional[str] = None,
2025-02-12 17:13:56 +00:00
return_timestamps: Optional[bool] = False,
) -> AsyncGenerator[Tuple[Union[np.ndarray, bytes],AudioChunk], Tuple[None,None]]:
"""Process tokens into audio."""
2025-01-25 05:25:13 -07:00
async with self._chunk_semaphore:
try:
# Handle stream finalization
if is_last:
# Skip format conversion for raw audio mode
if not output_format:
yield np.array([], dtype=np.float32)
return
2025-02-12 17:13:56 +00:00
result, chunk_data = await AudioService.convert_audio(
2025-02-12 15:06:11 +00:00
AudioChunk(np.array([0], dtype=np.float32)), # Dummy data for type checking
24000,
output_format,
speed,
"",
is_first_chunk=False,
normalizer=normalizer,
2025-02-09 18:32:17 -07:00
is_last_chunk=True,
)
2025-02-12 17:13:56 +00:00
yield result, chunk_data
return
2025-02-09 18:32:17 -07:00
# Skip empty chunks
if not tokens and not chunk_text:
return
# Get backend
backend = self.model_manager.get_backend()
2025-01-25 05:25:13 -07:00
# Generate audio using pre-warmed model
if isinstance(backend, KokoroV1):
# For Kokoro V1, pass text and voice info with lang_code
2025-02-11 22:32:10 -05:00
async for chunk_data in self.model_manager.generate(
chunk_text,
(voice_name, voice_path),
speed=speed,
2025-02-09 18:32:17 -07:00
lang_code=lang_code,
2025-02-12 17:13:56 +00:00
return_timestamps=return_timestamps,
):
# For streaming, convert to bytes
if output_format:
try:
2025-02-11 22:32:10 -05:00
converted, chunk_data = await AudioService.convert_audio(
chunk_data,
24000,
output_format,
speed,
chunk_text,
is_first_chunk=is_first,
2025-02-09 18:32:17 -07:00
is_last_chunk=is_last,
normalizer=normalizer,
)
2025-02-12 17:13:56 +00:00
yield converted, chunk_data
except Exception as e:
logger.error(f"Failed to convert audio: {str(e)}")
else:
2025-02-11 22:32:10 -05:00
chunk_data = await AudioService.trim_audio(chunk_data,
chunk_text,
speed,
is_last,
normalizer)
2025-02-12 17:13:56 +00:00
yield chunk_data.audio, chunk_data
else:
2025-02-12 15:06:11 +00:00
# For legacy backends, load voice tensor
2025-02-09 18:32:17 -07:00
voice_tensor = await self._voice_manager.load_voice(
voice_name, device=backend.device
)
2025-02-12 17:13:56 +00:00
chunk_data = await self.model_manager.generate(
tokens, voice_tensor, speed=speed, return_timestamps=return_timestamps
)
2025-02-09 18:32:17 -07:00
2025-02-12 17:13:56 +00:00
if chunk_data.audio is None:
logger.error("Model generated None for audio chunk")
return
2025-02-09 18:32:17 -07:00
2025-02-12 17:13:56 +00:00
if len(chunk_data.audio) == 0:
logger.error("Model generated empty audio chunk")
return
2025-02-09 18:32:17 -07:00
# For streaming, convert to bytes
if output_format:
try:
2025-02-12 17:13:56 +00:00
converted, chunk_data = await AudioService.convert_audio(
chunk_data,
24000,
output_format,
speed,
chunk_text,
is_first_chunk=is_first,
normalizer=normalizer,
2025-02-09 18:32:17 -07:00
is_last_chunk=is_last,
)
2025-02-12 17:13:56 +00:00
yield converted, chunk_data
except Exception as e:
logger.error(f"Failed to convert audio: {str(e)}")
else:
2025-02-12 17:13:56 +00:00
trimmed = await AudioService.trim_audio(chunk_data,
chunk_text,
speed,
is_last,
normalizer)
2025-02-12 17:13:56 +00:00
yield trimmed.audio, trimmed
2025-01-25 05:25:13 -07:00
except Exception as e:
logger.error(f"Failed to process tokens: {str(e)}")
async def _get_voice_path(self, voice: str) -> Tuple[str, str]:
"""Get voice path, handling combined voices.
2025-02-09 18:32:17 -07:00
Args:
voice: Voice name or combined voice names (e.g., 'af_jadzia+af_jessica')
2025-02-09 18:32:17 -07:00
Returns:
Tuple of (voice name to use, voice path to use)
2025-02-09 18:32:17 -07:00
Raises:
RuntimeError: If voice not found
"""
try:
# Check if it's a combined voice
if "+" in voice:
# Split on + but preserve any parentheses
voice_parts = []
weights = []
for part in voice.split("+"):
part = part.strip()
if not part:
continue
# Extract voice name and weight if present
if "(" in part and ")" in part:
voice_name = part.split("(")[0].strip()
weight = float(part.split("(")[1].split(")")[0])
else:
voice_name = part
weight = 1.0
voice_parts.append(voice_name)
weights.append(weight)
if len(voice_parts) < 2:
raise RuntimeError(f"Invalid combined voice name: {voice}")
2025-02-09 18:32:17 -07:00
# Normalize weights to sum to 1
total_weight = sum(weights)
2025-02-09 18:32:17 -07:00
weights = [w / total_weight for w in weights]
# Load and combine voices
voice_tensors = []
for v, w in zip(voice_parts, weights):
path = await self._voice_manager.get_voice_path(v)
if not path:
raise RuntimeError(f"Voice not found: {v}")
logger.debug(f"Loading voice tensor from: {path}")
voice_tensor = torch.load(path, map_location="cpu")
voice_tensors.append(voice_tensor * w)
2025-02-09 18:32:17 -07:00
# Sum the weighted voice tensors
2025-02-09 18:32:17 -07:00
logger.debug(
f"Combining {len(voice_tensors)} voice tensors with weights {weights}"
)
combined = torch.sum(torch.stack(voice_tensors), dim=0)
2025-02-09 18:32:17 -07:00
# Save combined tensor
temp_dir = tempfile.gettempdir()
combined_path = os.path.join(temp_dir, f"{voice}.pt")
logger.debug(f"Saving combined voice to: {combined_path}")
torch.save(combined, combined_path)
2025-02-09 18:32:17 -07:00
return voice, combined_path
else:
# Single voice
path = await self._voice_manager.get_voice_path(voice)
if not path:
raise RuntimeError(f"Voice not found: {voice}")
logger.debug(f"Using single voice path: {path}")
return voice, path
except Exception as e:
logger.error(f"Failed to get voice path: {e}")
raise
2025-01-04 17:54:54 -07:00
async def generate_audio_stream(
2025-01-09 18:41:44 -07:00
self,
text: str,
voice: str,
speed: float = 1.0,
2025-01-09 18:41:44 -07:00
output_format: str = "wav",
lang_code: Optional[str] = None,
2025-02-12 17:13:56 +00:00
normalization_options: Optional[NormalizationOptions] = NormalizationOptions(),
return_timestamps: Optional[bool] = False,
) -> AsyncGenerator[Tuple[bytes,AudioChunk], None]:
2025-01-25 05:25:13 -07:00
"""Generate and stream audio chunks."""
stream_normalizer = AudioNormalizer()
chunk_index = 0
2025-02-09 18:32:17 -07:00
2025-01-04 17:54:54 -07:00
try:
# Get backend
backend = self.model_manager.get_backend()
# Get voice path, handling combined voices
voice_name, voice_path = await self._get_voice_path(voice)
logger.debug(f"Using voice path: {voice_path}")
# Use provided lang_code or determine from voice name
pipeline_lang_code = lang_code if lang_code else voice[:1].lower()
2025-02-09 18:32:17 -07:00
logger.info(
f"Using lang_code '{pipeline_lang_code}' for voice '{voice_name}' in audio stream"
)
# Process text in chunks with smart splitting
2025-02-11 19:09:35 -05:00
async for chunk_text, tokens in smart_split(text,normalization_options=normalization_options):
try:
# Process audio for chunk
2025-02-12 17:13:56 +00:00
async for result, chunk_data in self._process_chunk(
chunk_text, # Pass text for Kokoro V1
2025-02-09 18:32:17 -07:00
tokens, # Pass tokens for legacy backends
voice_name, # Pass voice name
voice_path, # Pass voice path
2025-01-25 05:25:13 -07:00
speed,
output_format,
is_first=(chunk_index == 0),
is_last=False, # We'll update the last chunk later
normalizer=stream_normalizer,
2025-02-09 18:32:17 -07:00
lang_code=pipeline_lang_code, # Pass lang_code
):
if result is not None:
2025-02-12 17:13:56 +00:00
yield result,chunk_data
chunk_index += 1
else:
2025-02-09 18:32:17 -07:00
logger.warning(
f"No audio generated for chunk: '{chunk_text[:100]}...'"
)
except Exception as e:
2025-02-09 18:32:17 -07:00
logger.error(
f"Failed to process audio for chunk: '{chunk_text[:100]}...'. Error: {str(e)}"
)
continue
# Only finalize if we successfully processed at least one chunk
if chunk_index > 0:
try:
# Empty tokens list to finalize audio
2025-02-12 17:13:56 +00:00
async for result,chunk_data in self._process_chunk(
"", # Empty text
[], # Empty tokens
voice_name,
voice_path,
speed,
output_format,
is_first=False,
is_last=True, # Signal this is the last chunk
normalizer=stream_normalizer,
2025-02-09 18:32:17 -07:00
lang_code=pipeline_lang_code, # Pass lang_code
):
if result is not None:
2025-02-12 17:13:56 +00:00
yield result, chunk_data
except Exception as e:
logger.error(f"Failed to finalize audio stream: {str(e)}")
except Exception as e:
logger.error(f"Error in phoneme audio generation: {str(e)}")
2025-02-12 15:06:11 +00:00
raise e
2025-01-25 05:25:13 -07:00
async def generate_audio(
2025-02-09 18:32:17 -07:00
self,
text: str,
voice: str,
speed: float = 1.0,
return_timestamps: bool = False,
lang_code: Optional[str] = None,
2025-02-12 17:13:56 +00:00
) -> Tuple[Tuple[np.ndarray,AudioChunk]]:
2025-01-25 05:25:13 -07:00
"""Generate complete audio for text using streaming internally."""
start_time = time.time()
chunks = []
word_timestamps = []
2025-02-09 18:32:17 -07:00
2025-01-25 05:25:13 -07:00
try:
# Get backend and voice path
backend = self.model_manager.get_backend()
voice_name, voice_path = await self._get_voice_path(voice)
2025-01-25 05:25:13 -07:00
if isinstance(backend, KokoroV1):
# Use provided lang_code or determine from voice name
pipeline_lang_code = lang_code if lang_code else voice[:1].lower()
2025-02-09 18:32:17 -07:00
logger.info(
f"Using lang_code '{pipeline_lang_code}' for voice '{voice_name}' in text chunking"
)
# Get pipelines from backend for proper device management
try:
# Initialize quiet pipeline for text chunking
text_chunks = []
current_offset = 0.0 # Track time offset for timestamps
2025-02-09 18:32:17 -07:00
logger.debug("Splitting text into chunks...")
# Use backend's pipeline management
for result in backend._get_pipeline(pipeline_lang_code)(text):
if result.graphemes and result.phonemes:
text_chunks.append((result.graphemes, result.phonemes))
logger.debug(f"Split text into {len(text_chunks)} chunks")
2025-02-09 18:32:17 -07:00
# Process each chunk
2025-02-09 18:32:17 -07:00
for chunk_idx, (chunk_text, chunk_phonemes) in enumerate(
text_chunks
):
logger.debug(
f"Processing chunk {chunk_idx + 1}/{len(text_chunks)}: '{chunk_text[:50]}...'"
)
# Use backend's pipeline for generation
for result in backend._get_pipeline(pipeline_lang_code)(
2025-02-09 18:32:17 -07:00
chunk_text, voice=voice_path, speed=speed
):
# Collect audio chunks
if result.audio is not None:
chunks.append(result.audio.numpy())
2025-02-09 18:32:17 -07:00
# Process timestamps for this chunk
2025-02-09 18:32:17 -07:00
if (
return_timestamps
and hasattr(result, "tokens")
and result.tokens
):
logger.debug(
f"Processing chunk timestamps with {len(result.tokens)} tokens"
)
if result.pred_dur is not None:
try:
# Join timestamps for this chunk's tokens
2025-02-09 18:32:17 -07:00
KPipeline.join_timestamps(
result.tokens, result.pred_dur
)
# Add timestamps with offset
for token in result.tokens:
2025-02-09 18:32:17 -07:00
if not all(
hasattr(token, attr)
for attr in [
"text",
"start_ts",
"end_ts",
]
):
continue
if not token.text or not token.text.strip():
continue
2025-02-09 18:32:17 -07:00
# Apply offset to timestamps
2025-02-09 18:32:17 -07:00
start_time = (
float(token.start_ts) + current_offset
)
end_time = (
float(token.end_ts) + current_offset
)
word_timestamps.append(
{
"word": str(token.text).strip(),
"start_time": start_time,
"end_time": end_time,
}
)
logger.debug(
f"Added timestamp for word '{token.text}': {start_time:.3f}s - {end_time:.3f}s"
)
# Update offset for next chunk based on pred_dur
2025-02-09 18:32:17 -07:00
chunk_duration = (
float(result.pred_dur.sum()) / 80
) # Convert frames to seconds
current_offset = max(
current_offset + chunk_duration, end_time
)
logger.debug(
f"Updated time offset to {current_offset:.3f}s"
)
except Exception as e:
2025-02-09 18:32:17 -07:00
logger.error(
f"Failed to process timestamps for chunk: {e}"
)
logger.debug(
f"Processing timestamps with pred_dur shape: {result.pred_dur.shape}"
)
try:
# Join timestamps for this chunk's tokens
2025-02-09 18:32:17 -07:00
KPipeline.join_timestamps(
result.tokens, result.pred_dur
)
logger.debug(
"Successfully joined timestamps for chunk"
)
except Exception as e:
2025-02-09 18:32:17 -07:00
logger.error(
f"Failed to join timestamps for chunk: {e}"
)
continue
2025-02-09 18:32:17 -07:00
# Convert tokens to timestamps
for token in result.tokens:
try:
# Skip tokens without required attributes
2025-02-09 18:32:17 -07:00
if not all(
hasattr(token, attr)
for attr in ["text", "start_ts", "end_ts"]
):
logger.debug(
f"Skipping token missing attributes: {dir(token)}"
)
continue
2025-02-09 18:32:17 -07:00
# Get and validate text
2025-02-09 18:32:17 -07:00
text = (
str(token.text).strip()
if token.text is not None
else ""
)
if not text:
logger.debug("Skipping empty token")
continue
2025-02-09 18:32:17 -07:00
# Get and validate timestamps
2025-02-09 18:32:17 -07:00
start_ts = getattr(token, "start_ts", None)
end_ts = getattr(token, "end_ts", None)
if start_ts is None or end_ts is None:
2025-02-09 18:32:17 -07:00
logger.debug(
f"Skipping token with None timestamps: {text}"
)
continue
2025-02-09 18:32:17 -07:00
# Convert timestamps to float
try:
start_time = float(start_ts)
end_time = float(end_ts)
except (TypeError, ValueError):
2025-02-09 18:32:17 -07:00
logger.debug(
f"Skipping token with invalid timestamps: {text}"
)
continue
2025-02-09 18:32:17 -07:00
# Add timestamp
2025-02-09 18:32:17 -07:00
word_timestamps.append(
{
"word": text,
"start_time": start_time,
"end_time": end_time,
}
)
logger.debug(
f"Added timestamp for word '{text}': {start_time:.3f}s - {end_time:.3f}s"
)
except Exception as e:
logger.warning(f"Error processing token: {e}")
continue
except Exception as e:
logger.error(f"Failed to process text with pipeline: {e}")
raise RuntimeError(f"Pipeline processing failed: {e}")
if not chunks:
raise ValueError("No audio chunks were generated successfully")
# Combine chunks
audio = np.concatenate(chunks) if len(chunks) > 1 else chunks[0]
processing_time = time.time() - start_time
2025-02-09 18:32:17 -07:00
if return_timestamps:
# Validate timestamps before returning
if not word_timestamps:
logger.warning("No valid timestamps were generated")
else:
# Sort timestamps by start time to ensure proper order
2025-02-09 18:32:17 -07:00
word_timestamps.sort(key=lambda x: x["start_time"])
# Validate timestamp sequence
for i in range(1, len(word_timestamps)):
2025-02-09 18:32:17 -07:00
prev = word_timestamps[i - 1]
curr = word_timestamps[i]
2025-02-09 18:32:17 -07:00
if curr["start_time"] < prev["end_time"]:
logger.warning(
f"Overlapping timestamps detected: '{prev['word']}' ({prev['start_time']:.3f}-{prev['end_time']:.3f}) and '{curr['word']}' ({curr['start_time']:.3f}-{curr['end_time']:.3f})"
)
logger.debug(
f"Returning {len(word_timestamps)} word timestamps"
)
logger.debug(
f"First timestamp: {word_timestamps[0]['word']} at {word_timestamps[0]['start_time']:.3f}s"
)
logger.debug(
f"Last timestamp: {word_timestamps[-1]['word']} at {word_timestamps[-1]['end_time']:.3f}s"
)
return audio, processing_time, word_timestamps
return audio, processing_time
2025-01-25 05:25:13 -07:00
else:
# For legacy backends
async for chunk in self.generate_audio_stream(
2025-02-09 18:32:17 -07:00
text,
voice,
speed, # Default to WAV for raw audio
):
if chunk is not None:
chunks.append(chunk)
if not chunks:
raise ValueError("No audio chunks were generated successfully")
# Combine chunks
audio = np.concatenate(chunks) if len(chunks) > 1 else chunks[0]
processing_time = time.time() - start_time
2025-02-09 18:32:17 -07:00
if return_timestamps:
2025-02-09 18:32:17 -07:00
return (
audio,
processing_time,
[],
) # Empty timestamps for legacy backends
return audio, processing_time
2025-01-25 05:25:13 -07:00
except Exception as e:
logger.error(f"Error in audio generation: {str(e)}")
raise
async def combine_voices(self, voices: List[str]) -> torch.Tensor:
"""Combine multiple voices.
2025-02-09 18:32:17 -07:00
Returns:
Combined voice tensor
"""
return await self._voice_manager.combine_voices(voices)
2025-01-09 18:41:44 -07:00
async def list_voices(self) -> List[str]:
2025-01-25 05:25:13 -07:00
"""List available voices."""
return await self._voice_manager.list_voices()
async def generate_from_phonemes(
self,
phonemes: str,
voice: str,
speed: float = 1.0,
2025-02-09 18:32:17 -07:00
lang_code: Optional[str] = None,
) -> Tuple[np.ndarray, float]:
"""Generate audio directly from phonemes.
2025-02-09 18:32:17 -07:00
Args:
phonemes: Phonemes in Kokoro format
voice: Voice name
speed: Speed multiplier
lang_code: Optional language code override
2025-02-09 18:32:17 -07:00
Returns:
Tuple of (audio array, processing time)
"""
start_time = time.time()
try:
# Get backend and voice path
backend = self.model_manager.get_backend()
voice_name, voice_path = await self._get_voice_path(voice)
if isinstance(backend, KokoroV1):
# For Kokoro V1, use generate_from_tokens with raw phonemes
result = None
# Use provided lang_code or determine from voice name
pipeline_lang_code = lang_code if lang_code else voice[:1].lower()
2025-02-09 18:32:17 -07:00
logger.info(
f"Using lang_code '{pipeline_lang_code}' for voice '{voice_name}' in phoneme pipeline"
)
try:
# Use backend's pipeline management
2025-02-09 18:32:17 -07:00
for r in backend._get_pipeline(
pipeline_lang_code
).generate_from_tokens(
tokens=phonemes, # Pass raw phonemes string
voice=voice_path,
2025-02-09 18:32:17 -07:00
speed=speed,
):
if r.audio is not None:
result = r
break
except Exception as e:
logger.error(f"Failed to generate from phonemes: {e}")
raise RuntimeError(f"Phoneme generation failed: {e}")
2025-02-09 18:32:17 -07:00
if result is None or result.audio is None:
raise ValueError("No audio generated")
2025-02-09 18:32:17 -07:00
processing_time = time.time() - start_time
return result.audio.numpy(), processing_time
else:
2025-02-09 18:32:17 -07:00
raise ValueError(
"Phoneme generation only supported with Kokoro V1 backend"
)
except Exception as e:
logger.error(f"Error in phoneme audio generation: {str(e)}")
raise