tokenization_vits.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright 2023 The Kakao Enterprise Authors, the MMS-TTS Authors and the HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tokenization class for VITS."""
  15. import json
  16. import os
  17. import re
  18. from typing import Any
  19. from ...tokenization_python import PreTrainedTokenizer
  20. from ...utils import is_phonemizer_available, is_uroman_available, logging
  21. if is_phonemizer_available():
  22. import phonemizer
  23. if is_uroman_available():
  24. import uroman as ur
  25. logger = logging.get_logger(__name__)
  26. VOCAB_FILES_NAMES = {"vocab_file": "vocab.json"}
  27. def has_non_roman_characters(input_string):
  28. # Find any character outside the ASCII range
  29. non_roman_pattern = re.compile(r"[^\x00-\x7F]")
  30. # Search the input string for non-Roman characters
  31. match = non_roman_pattern.search(input_string)
  32. has_non_roman = match is not None
  33. return has_non_roman
  34. class VitsTokenizer(PreTrainedTokenizer):
  35. """
  36. Construct a VITS tokenizer. Also supports MMS-TTS.
  37. This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
  38. this superclass for more information regarding those methods.
  39. Args:
  40. vocab_file (`str`):
  41. Path to the vocabulary file.
  42. language (`str`, *optional*):
  43. Language identifier.
  44. add_blank (`bool`, *optional*, defaults to `True`):
  45. Whether to insert token id 0 in between the other tokens.
  46. normalize (`bool`, *optional*, defaults to `True`):
  47. Whether to normalize the input text by removing all casing and punctuation.
  48. phonemize (`bool`, *optional*, defaults to `True`):
  49. Whether to convert the input text into phonemes.
  50. is_uroman (`bool`, *optional*, defaults to `False`):
  51. Whether the `uroman` Romanizer needs to be applied to the input text prior to tokenizing.
  52. """
  53. vocab_files_names = VOCAB_FILES_NAMES
  54. model_input_names = ["input_ids", "attention_mask"]
  55. def __init__(
  56. self,
  57. vocab_file,
  58. pad_token="<pad>",
  59. unk_token="<unk>",
  60. language=None,
  61. add_blank=True,
  62. normalize=True,
  63. phonemize=True,
  64. is_uroman=False,
  65. **kwargs,
  66. ) -> None:
  67. with open(vocab_file, encoding="utf-8") as vocab_handle:
  68. self.encoder = json.load(vocab_handle)
  69. self.decoder = {v: k for k, v in self.encoder.items()}
  70. self.language = language
  71. self.add_blank = add_blank
  72. self.normalize = normalize
  73. self.phonemize = phonemize
  74. self.is_uroman = is_uroman
  75. super().__init__(
  76. pad_token=pad_token,
  77. unk_token=unk_token,
  78. language=language,
  79. add_blank=add_blank,
  80. normalize=normalize,
  81. phonemize=phonemize,
  82. is_uroman=is_uroman,
  83. special_tokens_pattern="none",
  84. **kwargs,
  85. )
  86. @property
  87. def vocab_size(self):
  88. return len(self.encoder)
  89. def get_vocab(self):
  90. vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
  91. vocab.update(self.added_tokens_encoder)
  92. return vocab
  93. def normalize_text(self, input_string):
  94. """Lowercase the input string, respecting any special token ids that may be part or entirely upper-cased."""
  95. all_vocabulary = list(self.encoder.keys()) + list(self.added_tokens_encoder.keys())
  96. filtered_text = ""
  97. i = 0
  98. while i < len(input_string):
  99. found_match = False
  100. for word in all_vocabulary:
  101. if input_string[i : i + len(word)] == word:
  102. filtered_text += word
  103. i += len(word)
  104. found_match = True
  105. break
  106. if not found_match:
  107. filtered_text += input_string[i].lower()
  108. i += 1
  109. return filtered_text
  110. def _preprocess_char(self, text):
  111. """Special treatment of characters in certain languages"""
  112. if self.language == "ron":
  113. text = text.replace("ț", "ţ")
  114. return text
  115. def prepare_for_tokenization(
  116. self, text: str, is_split_into_words: bool = False, normalize: bool | None = None, **kwargs
  117. ) -> tuple[str, dict[str, Any]]:
  118. """
  119. Performs any necessary transformations before tokenization.
  120. This method should pop the arguments from kwargs and return the remaining `kwargs` as well. We test the
  121. `kwargs` at the end of the encoding process to be sure all the arguments have been used.
  122. Args:
  123. text (`str`):
  124. The text to prepare.
  125. is_split_into_words (`bool`, *optional*, defaults to `False`):
  126. Whether or not the input is already pre-tokenized (e.g., split into words). If set to `True`, the
  127. tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
  128. which it will tokenize.
  129. normalize (`bool`, *optional*, defaults to `None`):
  130. Whether or not to apply punctuation and casing normalization to the text inputs. Typically, VITS is
  131. trained on lower-cased and un-punctuated text. Hence, normalization is used to ensure that the input
  132. text consists only of lower-case characters.
  133. kwargs (`dict[str, Any]`, *optional*):
  134. Keyword arguments to use for the tokenization.
  135. Returns:
  136. `tuple[str, dict[str, Any]]`: The prepared text and the unused kwargs.
  137. """
  138. normalize = normalize if normalize is not None else self.normalize
  139. if normalize:
  140. # normalise for casing
  141. text = self.normalize_text(text)
  142. filtered_text = self._preprocess_char(text)
  143. if has_non_roman_characters(filtered_text) and self.is_uroman:
  144. if not is_uroman_available():
  145. logger.warning(
  146. "Text to the tokenizer contains non-Roman characters. To apply the `uroman` pre-processing "
  147. "step automatically, ensure the `uroman` Romanizer is installed with: `pip install uroman` "
  148. "Note `uroman` requires python version >= 3.10"
  149. "Otherwise, apply the Romanizer manually as per the instructions: https://github.com/isi-nlp/uroman"
  150. )
  151. else:
  152. uroman = ur.Uroman()
  153. filtered_text = uroman.romanize_string(filtered_text)
  154. if self.phonemize:
  155. if not is_phonemizer_available():
  156. raise ImportError("Please install the `phonemizer` Python package to use this tokenizer.")
  157. filtered_text = phonemizer.phonemize(
  158. filtered_text,
  159. language="en-us",
  160. backend="espeak",
  161. strip=True,
  162. preserve_punctuation=True,
  163. with_stress=True,
  164. )
  165. filtered_text = re.sub(r"\s+", " ", filtered_text)
  166. elif normalize:
  167. # strip any chars outside of the vocab (punctuation)
  168. filtered_text = "".join(list(filter(lambda char: char in self.encoder, filtered_text))).strip()
  169. return filtered_text, kwargs
  170. def _tokenize(self, text: str) -> list[str]:
  171. """Tokenize a string by inserting the `<pad>` token at the boundary between adjacent characters."""
  172. tokens = list(text)
  173. if self.add_blank:
  174. interspersed = [self._convert_id_to_token(0)] * (len(tokens) * 2 + 1)
  175. interspersed[1::2] = tokens
  176. tokens = interspersed
  177. return tokens
  178. def convert_tokens_to_string(self, tokens: list[str]) -> str:
  179. if self.add_blank and len(tokens) > 1:
  180. tokens = tokens[1::2]
  181. return "".join(tokens)
  182. def _convert_token_to_id(self, token):
  183. """Converts a token (str) in an id using the vocab."""
  184. if token in self.encoder:
  185. return self.encoder[token]
  186. return self.unk_token_id
  187. def _convert_id_to_token(self, index):
  188. """Converts an index (integer) in a token (str) using the vocab."""
  189. return self.decoder.get(index)
  190. def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str] | None:
  191. if not os.path.isdir(save_directory):
  192. logger.error(f"Vocabulary path ({save_directory}) should be a directory")
  193. return
  194. vocab_file = os.path.join(
  195. save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
  196. )
  197. with open(vocab_file, "w", encoding="utf-8") as f:
  198. f.write(json.dumps(self.encoder, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
  199. return (vocab_file,)
  200. __all__ = ["VitsTokenizer"]