Skip to content

A TTS that fits in your CPU (and pocket)

Python
11.5 KB
Raw Blame
"""Turning a text prompt into the chunks the model generates one at a time.

The model is trained on single sentences, so long inputs are split on sentence
boundaries and regrouped into chunks that fit `max_tokens`.
"""

import logging
import re

from pocket_tts.modules.text_conditioner import Tokenizer

logger = logging.getLogger(__name__)


def prepare_text_prompt(
    text: str,
    pad_with_spaces_for_short_inputs: bool,
    remove_semicolons: bool,
    append_terminal_punctuation: bool = True,
    capitalize_first_letter: bool = True,
    replace_characters: dict[str, str] | None = None,
) -> tuple[str, int]:
    text = text.strip()
    if replace_characters:
        text = " ".join(text.translate(str.maketrans(replace_characters)).split())
        # Deleted quotes leave '"Hi?", she said' as 'Hi?, she said', which reads as a sentence end
        # followed by a stray comma; keep the sentence mark only.
        text = re.sub(r"([.!?\u2026])\s*[,;:]", r"\1", text)
    if text == "":
        raise ValueError("Text prompt cannot be empty")
    text = text.replace("\n", " ").replace("\r", " ").replace("  ", " ")
    if remove_semicolons:
        text = text.replace(";", ",")
    number_of_words = len(text.split())
    if number_of_words <= 4:
        frames_after_eos_guess = 3
    else:
        frames_after_eos_guess = 1

    # Make sure it starts with an uppercase letter, after any opening quotes or brackets
    # ('"hello," she said' -> '"Hello," she said'). Only meaningful for orthographies that
    # have case; see Config.capitalize_first_letter.
    if capitalize_first_letter:
        start = len(text) - len(text.lstrip(_OPENERS))
        if start < len(text) and not text[start].isupper():
            text = text[:start] + text[start].upper() + text[start + 1 :]

    if append_terminal_punctuation:
        text = _ensure_terminal_punctuation(text)

    # The model does not perform well when there are very few tokens, so
    # we can add empty spaces at the beginning to increase the token count.
    if pad_with_spaces_for_short_inputs and len(text.split()) < 5:
        text = " " * 8 + text

    return text, frames_after_eos_guess


_TERMINAL_PUNCTUATION = ".!?\u2026"
# A segment ending on a decimal number, as in "... a fee of $822.95,", "... is 3.14." or, with a
# decimal comma, "... de 822,95 €,"
_ENDS_WITH_DECIMAL_NUMBER = re.compile(r"\d[.,]\d+\W*$")
_WEAK_PUNCTUATION = ",;:-\u2013\u2014"
_CLOSERS = "\"'\u201d\u2019)]\u00bb"
_OPENERS = "\"'\u201c\u2018([\u00ab"


def _ensure_terminal_punctuation(text: str) -> str:
    """Make sure the prompt ends with sentence-final punctuation.

    The model is trained on sentences that end with a period, a question mark
    or an exclamation mark. Without one, the last word is often mispronounced
    or repeated. Text that already ends with one, possibly followed by closing
    quotes or brackets, is left alone. A trailing comma, colon or dash is
    replaced by a period. Anything else gets a period appended after the
    closing quote or bracket: the sentence splitter only recognizes a bare
    period token, and a quote left alone after a period would become a chunk
    of its own.
    """
    core = text.rstrip(_CLOSERS + " ")
    closers = text[len(core) :].strip()
    if not core or core[-1] in _TERMINAL_PUNCTUATION:
        return text
    if core[-1] in _WEAK_PUNCTUATION:
        return core.rstrip(_WEAK_PUNCTUATION + " ") + "." + closers
    return text + "."


# Abbreviations that always precede a name or an example, never end a sentence.
_TITLES = {"Mr.", "Mrs.", "Ms.", "Dr.", "Prof.", "Rev.", "vs.", "e.g.", "i.e."}


def _is_inner_punctuation(
    list_of_tokens: list[int], segment_start_idx: int, tokenizer: Tokenizer
) -> bool:
    """Return True when the punctuation right before segment_start_idx does not end a sentence or
    clause: it is followed by a letter or digit without a space ("3.14", "3,14" with a decimal
    comma, "1,000", "4 p.m.", "www.kyutai.org", "[email protected]"), or it ends a title that
    precedes a name ("Dr. Miller", "e.g. this").

    Only the tokens next to the boundary are decoded, so checking every sentence boundary
    stays linear in the text length. Decoding the period together with the next token keeps
    the space between them: in "I counted to 3. 14 people were there." the period ends a
    sentence.
    """
    after = tokenizer.decode(list_of_tokens[segment_start_idx - 1 : segment_start_idx + 1])
    if len(after) >= 2 and after[1].isalnum():
        return True
    window_start = max(0, segment_start_idx - 6)
    words = tokenizer.decode(list_of_tokens[window_start:segment_start_idx]).split()
    # The first word of the window may be cut, unless the window starts the text.
    return bool(words) and words[-1] in _TITLES and (len(words) > 1 or window_start == 0)


# A closing quote can span several byte tokens; bound the lookahead so the check stays linear.
_MAX_CLOSER_TOKENS = 8
# Characters allowed after the punctuation while skipping closers: U+FFFD is an incomplete closer.
_CLOSERS_OR_PARTIAL = _CLOSERS + "\ufffd"


def _skip_closers(list_of_tokens: list[int], segment_start_idx: int, tokenizer: Tokenizer) -> int:
    """Return where the segment starting at segment_start_idx should really start: after the
    closing quotes and brackets that directly follow the punctuation, as in "“Stop.”" or
    "(quietly.)", so that they stay with their sentence instead of starting the next segment.

    A closer like the curly quote is made of byte tokens that decode to U+FFFD on their own, so
    the tokens are decoded together with the punctuation and the start only moves past complete
    closers. A closer preceded by a space opens the next sentence and is left alone.
    """
    punctuation = tokenizer.decode(list_of_tokens[segment_start_idx - 1 : segment_start_idx])
    start = segment_start_idx
    stop = min(len(list_of_tokens), segment_start_idx + _MAX_CLOSER_TOKENS)
    for end in range(segment_start_idx + 1, stop + 1):
        window = tokenizer.decode(list_of_tokens[segment_start_idx - 1 : end])
        after = window[len(punctuation) :]
        if not after or any(c not in _CLOSERS_OR_PARTIAL for c in after):
            break
        if "\ufffd" not in after:
            start = end
    return start


def _find_boundary_indices(
    list_of_tokens: list[int],
    boundary_tokens: list[int],
    tokenizer: Tokenizer | None = None,
    skip_inner_punctuation: bool = False,
) -> list[int]:
    """Find token indices where text should be split based on boundary tokens.

    Returns a list of boundary positions used to slice segments. Each consecutive
    pair (indices[i], indices[i+1]) defines one segment. The first element is
    always 0 and the last is always len(list_of_tokens).
    """
    boundary_set = set(boundary_tokens)
    indices = [0]
    previous_was_boundary = False
    for idx, token in enumerate(list_of_tokens):
        if token in boundary_set:
            previous_was_boundary = True
        else:
            if previous_was_boundary:
                if (
                    skip_inner_punctuation
                    and tokenizer is not None
                    and _is_inner_punctuation(list_of_tokens, idx, tokenizer)
                ):
                    previous_was_boundary = False
                    continue
                start = idx if tokenizer is None else _skip_closers(list_of_tokens, idx, tokenizer)
                if start < len(list_of_tokens):
                    indices.append(start)
            previous_was_boundary = False
    indices.append(len(list_of_tokens))
    return indices


def _segments_from_boundaries(
    list_of_tokens: list[int], boundary_indices: list[int], tokenizer: Tokenizer
) -> list[tuple[int, str]]:
    """Decode token segments between boundary indices into (token_count, text) pairs."""
    segments = []
    for i in range(len(boundary_indices) - 1):
        start = boundary_indices[i]
        end = boundary_indices[i + 1]
        text = tokenizer.decode(list_of_tokens[start:end])
        segments.append((end - start, text))
    return segments


def split_into_best_sentences(
    tokenizer: Tokenizer,
    text_to_generate: str,
    max_tokens: int,
    pad_with_spaces_for_short_inputs: bool,
    remove_semicolons: bool,
    append_terminal_punctuation: bool = True,
    capitalize_first_letter: bool = True,
    replace_characters: dict[str, str] | None = None,
) -> list[str]:
    text_to_generate, _ = prepare_text_prompt(
        text_to_generate,
        pad_with_spaces_for_short_inputs,
        remove_semicolons,
        append_terminal_punctuation,
        capitalize_first_letter,
        replace_characters,
    )
    text_to_generate = text_to_generate.strip()
    tokens = tokenizer(text_to_generate)
    list_of_tokens = tokens[0].tolist()

    _, *end_of_sentence_tokens = tokenizer(".!...?")[0].tolist()
    sentence_boundaries = _find_boundary_indices(
        list_of_tokens, end_of_sentence_tokens, tokenizer, skip_inner_punctuation=True
    )
    nb_tokens_and_sentences = _segments_from_boundaries(
        list_of_tokens, sentence_boundaries, tokenizer
    )

    # Sub-split oversized sentences on commas, semicolons, and colons to prevent skipped words
    _, *fallback_tokens = tokenizer(",;:")[0].tolist()
    refined_segments = []
    for nb_tokens, text in nb_tokens_and_sentences:
        if nb_tokens <= max_tokens:
            refined_segments.append((nb_tokens, text))
        else:
            sub_tokens = tokenizer(text.strip())[0].tolist()
            sub_boundaries = _find_boundary_indices(
                sub_tokens, fallback_tokens, tokenizer, skip_inner_punctuation=True
            )
            sub_segments = _segments_from_boundaries(sub_tokens, sub_boundaries, tokenizer)
            if len(sub_segments) > 1:
                refined_segments.extend(sub_segments)
            else:
                refined_segments.append((nb_tokens, text))

    # Never end a chunk on a decimal number: "... $822.95," would become the chunk "... $822.95."
    # and the model takes the decimal point for the sentence end, stopping before the decimals.
    # Such a segment stays with the next one.
    glued_segments: list[tuple[int, str]] = []
    for nb_tokens, text in refined_segments:
        if glued_segments and _ENDS_WITH_DECIMAL_NUMBER.search(glued_segments[-1][1]):
            prev_tokens, prev_text = glued_segments[-1]
            glued_segments[-1] = (prev_tokens + nb_tokens, prev_text.rstrip() + " " + text.strip())
        else:
            glued_segments.append((nb_tokens, text))
    refined_segments = glued_segments

    max_nb_tokens_in_a_chunk = max_tokens
    chunks = []
    current_chunk = ""
    current_nb_of_tokens_in_chunk = 0
    for nb_tokens, sentence in refined_segments:
        if current_chunk == "":
            current_chunk = sentence
            current_nb_of_tokens_in_chunk = nb_tokens
            continue

        if current_nb_of_tokens_in_chunk + nb_tokens > max_nb_tokens_in_a_chunk:
            chunks.append(current_chunk.strip())
            current_chunk = sentence
            current_nb_of_tokens_in_chunk = nb_tokens
        else:
            current_chunk += " " + sentence
            current_nb_of_tokens_in_chunk += nb_tokens

    if current_chunk != "":
        chunks.append(current_chunk.strip())

    for chunk in chunks:
        chunk_tokens = tokenizer(chunk.strip())[0].tolist()
        if len(chunk_tokens) > max_tokens:
            logger.warning(
                "Chunk has %d tokens (max %d), generation may skip words: '%.50s...'",
                len(chunk_tokens),
                max_tokens,
                chunk,
            )

    return chunks