diff --git a/ai-hub/app/api/routes/tts.py b/ai-hub/app/api/routes/tts.py index 04af388..9c1d938 100644 --- a/ai-hub/app/api/routes/tts.py +++ b/ai-hub/app/api/routes/tts.py @@ -58,20 +58,37 @@ # instead of a meaningful error message. # Split into first chunk for latency, then send entire rest for smoothness all_text = request.text - separators = ['.', '?', '!', '\n', '。', '?', '!', ','] + separators = ['.', '?', '!', '\n', '。', '?', '!', ',', ';'] - # Find first separator within a reasonable limit - split_idx = -1 - chars_to_scan = min(len(all_text) - 1, 400) - for i in range(chars_to_scan, 50, -1): - if all_text[i] in separators: - split_idx = i + 1 - break - - if split_idx != -1 and split_idx < len(all_text): - chunks = [all_text[:split_idx], all_text[split_idx:]] - else: - chunks = [all_text] + # Intelligent Multi-Splitter: Break text into a sequence of sentences/clauses + # instead of just "First" and "Rest". + def split_text(text, max_chunk_size=200): + chunks = [] + while len(text) > 0: + if len(text) <= max_chunk_size: + chunks.append(text) + break + + # Find the best separator to split at + split_at = -1 + # Look for a separator within a reasonable sentence length + for i in range(min(len(text)-1, max_chunk_size), 20, -1): + if text[i] in separators: + split_at = i + 1 + break + + if split_at != -1: + chunks.append(text[:split_at].strip()) + text = text[split_at:].strip() + else: + # Forced split if no separator found + chunks.append(text[:max_chunk_size].strip()) + text = text[max_chunk_size:].strip() + return [c for c in chunks if c] + + chunks = split_text(all_text) + if not chunks: + raise HTTPException(status_code=400, detail="No text to synthesize.") provider = provider_override or services.tts_service.default_tts_provider if not chunks or not chunks[0].strip():