






from importlib import import_module

from .logging import get_logger


A        =			get_logger(__name__)


class 							__a							:





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   attrs or []
						if module is not None:
										for key in module.__dict__:
														if key in attrs or not key.startswith('__'       ):
																		setattr(self ,    UpperCamelCase__ ,    getattr(UpperCamelCase__ ,    UpperCamelCase__       )       )
						SCREAMING_SNAKE_CASE_   :	Any													=   module._original_module if isinstance(UpperCamelCase__ ,    _PatchedModuleObj       ) else module






class 							__a							:





		'''simple docstring'''



		UpperCAmelCase__      :				List[Any]				    =						[]


		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   obj
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   target
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   new
						SCREAMING_SNAKE_CASE_   :	int													=   target.split('.'       )[0]
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   {}
						SCREAMING_SNAKE_CASE_   :	Any													=   attrs or []


		def __enter__(						self       ):
						*SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	List[str]													=   self.target.split('.'       )

						# Patch modules:
						# it's used to patch attributes of submodules like "os.path.join";
						# in this case we need to patch "os" and "os.path"

						for i in range(len(UpperCamelCase__       )       ):
										try:
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   import_module('.'.join(submodules[: i + 1]       )       )
										except ModuleNotFoundError:
														continue
										# We iterate over all the globals in self.obj in case we find "os" or "os.path"
										for attr in self.obj.__dir__():
														SCREAMING_SNAKE_CASE_   :	str													=   getattr(self.obj ,    UpperCamelCase__       )
														# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
														# This allows to patch renamed modules like "from os import path as ospath".
														if obj_attr is submodule or (
														    (isinstance(UpperCamelCase__ ,    _PatchedModuleObj       ) and obj_attr._original_module is submodule)
														):
																		SCREAMING_SNAKE_CASE_   :	Optional[int]													=   obj_attr
																		# patch at top level
																		setattr(self.obj ,    UpperCamelCase__ ,    _PatchedModuleObj(UpperCamelCase__ ,    attrs=self.attrs       )       )
																		SCREAMING_SNAKE_CASE_   :	List[Any]													=   getattr(self.obj ,    UpperCamelCase__       )
																		# construct lower levels patches
																		for key in submodules[i + 1 :]:
																						setattr(UpperCamelCase__ ,    UpperCamelCase__ ,    _PatchedModuleObj(getattr(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ) ,    attrs=self.attrs       )       )
																						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   getattr(UpperCamelCase__ ,    UpperCamelCase__       )
																		# finally set the target attribute
																		setattr(UpperCamelCase__ ,    UpperCamelCase__ ,    self.new       )

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

						if submodules:  # if it's an attribute of a submodule like "os.path.join"
										try:
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   getattr(import_module('.'.join(UpperCamelCase__       )       ) ,    UpperCamelCase__       )
										except (AttributeError, ModuleNotFoundError):
														return
										# We iterate over all the globals in self.obj in case we find "os.path.join"
										for attr in self.obj.__dir__():
														# We don't check for the name of the global, but rather if its value *is* "os.path.join".
														# This allows to patch renamed attributes like "from os.path import join as pjoin".
														if getattr(self.obj ,    UpperCamelCase__       ) is attr_value:
																		SCREAMING_SNAKE_CASE_   :	Optional[int]													=   getattr(self.obj ,    UpperCamelCase__       )
																		setattr(self.obj ,    UpperCamelCase__ ,    self.new       )
						elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
										SCREAMING_SNAKE_CASE_   :	Dict													=   globals()['__builtins__'][target_attr]
										setattr(self.obj ,    UpperCamelCase__ ,    self.new       )
						else:
										raise RuntimeError(F'''Tried to patch attribute {target_attr} instead of a submodule.'''       )


		def __exit__(						self ,    *UpperCamelCase__       ):
						for attr in list(self.original       ):
										setattr(self.obj ,    UpperCamelCase__ ,    self.original.pop(UpperCamelCase__       )       )


		def   __snake_case			(						self       ):
						self.__enter__()
						self._active_patches.append(self       )






		def   __snake_case			(						self       ):
						try:
										self._active_patches.remove(self       )
						except ValueError:
										# If the patch hasn't been started this will fail
										return None

						return self.__exit__()







import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


A        =			logging.get_logger(__name__)

A        =			{'vocab_file': 'spm_char.model'}

A        =			{
    'vocab_file': {
        'microsoft/speecht5_asr': 'https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model',
        'microsoft/speecht5_tts': 'https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model',
        'microsoft/speecht5_vc': 'https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model',
    }
}

A        =			{
    'microsoft/speecht5_asr': 1024,
    'microsoft/speecht5_tts': 1024,
    'microsoft/speecht5_vc': 1024,
}


class 							__a							(  __A						):





		'''simple docstring'''



		UpperCAmelCase__      :				str				    =						VOCAB_FILES_NAMES
		UpperCAmelCase__      :				List[str]				    =						PRETRAINED_VOCAB_FILES_MAP
		UpperCAmelCase__      :				Optional[int]				    =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		UpperCAmelCase__      :				Dict				    =						["""input_ids""", """attention_mask"""]


		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__="<s>" ,    UpperCamelCase__="</s>" ,    UpperCamelCase__="<unk>" ,    UpperCamelCase__="<pad>" ,    UpperCamelCase__ = None ,    **UpperCamelCase__ ,    ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   {} if sp_model_kwargs is None else sp_model_kwargs

						super().__init__(
						    bos_token=UpperCamelCase__ ,    eos_token=UpperCamelCase__ ,    unk_token=UpperCamelCase__ ,    pad_token=UpperCamelCase__ ,    sp_model_kwargs=self.sp_model_kwargs ,    **UpperCamelCase__ ,    )

						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   vocab_file

						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   spm.SentencePieceProcessor(**self.sp_model_kwargs       )
						self.sp_model.Load(UpperCamelCase__       )


		@property
		def   __snake_case			(						self       ):
						return self.sp_model.get_piece_size()


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[str]													=   {self.convert_ids_to_tokens(UpperCamelCase__       ): i for i in range(self.vocab_size       )}
						vocab.update(self.added_tokens_encoder       )
						return vocab


		def __getstate__(						self       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.__dict__.copy()
						SCREAMING_SNAKE_CASE_   :	List[str]													=   None
						return state


		def __setstate__(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   d

						# for backward compatibility
						if not hasattr(self ,    'sp_model_kwargs'       ):
										SCREAMING_SNAKE_CASE_   :	Any													=   {}

						SCREAMING_SNAKE_CASE_   :	List[Any]													=   spm.SentencePieceProcessor(**self.sp_model_kwargs       )
						self.sp_model.Load(self.vocab_file       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						return self.sp_model.encode(UpperCamelCase__ ,    out_type=UpperCamelCase__       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						return self.sp_model.piece_to_id(UpperCamelCase__       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.sp_model.IdToPiece(UpperCamelCase__       )
						return token


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	int													=   []
						SCREAMING_SNAKE_CASE_   :	int													=   ''
						for token in tokens:
										# make sure that special tokens are not decoded using sentencepiece model
										if token in self.all_special_tokens:
														out_string += self.sp_model.decode(UpperCamelCase__       ) + token
														SCREAMING_SNAKE_CASE_   :	Any													=   []
										else:
														current_sub_tokens.append(UpperCamelCase__       )
						out_string += self.sp_model.decode(UpperCamelCase__       )
						return out_string.strip()


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
						if token_ids_a is None:
										return token_ids_a + [self.eos_token_id]
						# We don't expect to process pairs, but leave the pair logic for API consistency
						return token_ids_a + token_ids_a + [self.eos_token_id]


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ = None ,    UpperCamelCase__ = False       ):
						if already_has_special_tokens:
										return super().get_special_tokens_mask(
										    token_ids_a=UpperCamelCase__ ,    token_ids_a=UpperCamelCase__ ,    already_has_special_tokens=UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   [1]
						if token_ids_a is None:
										return ([0] * len(UpperCamelCase__       )) + suffix_ones
						return ([0] * len(UpperCamelCase__       )) + ([0] * len(UpperCamelCase__       )) + suffix_ones






		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ = None       ):
						if not os.path.isdir(UpperCamelCase__       ):
										logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''       )
										return
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   os.path.join(
						    UpperCamelCase__ ,    (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )

						if os.path.abspath(self.vocab_file       ) != os.path.abspath(UpperCamelCase__       ) and os.path.isfile(self.vocab_file       ):
										copyfile(self.vocab_file ,    UpperCamelCase__       )
						elif not os.path.isfile(self.vocab_file       ):
										with open(UpperCamelCase__ ,    'wb'       ) as fi:
														SCREAMING_SNAKE_CASE_   :	Tuple													=   self.sp_model.serialized_model_proto()
														fi.write(UpperCamelCase__       )

						return (out_vocab_file,)