
'''simple docstring'''


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


snake_case_			:	List[str] 	=       logging.get_logger(__name__)

snake_case_			:	Tuple 	=       {'vocab_file': 'spm_char.model'}

snake_case_			:	int 	=       {
    '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',
    }
}

snake_case_			:	Union[str, Any] 	=       {
    'microsoft/speecht5_asr': 1_024,
    'microsoft/speecht5_tts': 1_024,
    'microsoft/speecht5_vc': 1_024,
}


class     lowercase__						(	snake_case_ ):






   '''simple docstring'''







   _snake_case		 =				VOCAB_FILES_NAMES
   _snake_case		 =				PRETRAINED_VOCAB_FILES_MAP
   _snake_case		 =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   _snake_case		 =				['''input_ids''', '''attention_mask''']







   def __init__(      self			, lowerCamelCase__			, lowerCamelCase__="<s>"			, lowerCamelCase__="</s>"			, lowerCamelCase__="<unk>"			, lowerCamelCase__="<pad>"			, lowerCamelCase__ = None			, **lowerCamelCase__			, ):

      '''simple docstring'''







      UpperCamelCase	  =	{} if sp_model_kwargs is None else sp_model_kwargs

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

      UpperCamelCase	  =	vocab_file

      UpperCamelCase	  =	spm.SentencePieceProcessor(**self.sp_model_kwargs )
      self.sp_model.Load(_A )







   @property
   def    UpperCAmelCase					(      self ):

      '''simple docstring'''







      return self.sp_model.get_piece_size()







   def    UpperCAmelCase					(      self ):

      '''simple docstring'''







      UpperCamelCase	  =	{self.convert_ids_to_tokens(_A ): i for i in range(self.vocab_size )}
      vocab.update(self.added_tokens_encoder )
      return vocab







   def __getstate__(      self ):

      '''simple docstring'''







      UpperCamelCase	  =	self.__dict__.copy()
      UpperCamelCase	  =	None
      return state







   def __setstate__(      self			, lowerCamelCase__ ):

      '''simple docstring'''







      UpperCamelCase	  =	d

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

      UpperCamelCase	  =	spm.SentencePieceProcessor(**self.sp_model_kwargs )
      self.sp_model.Load(self.vocab_file )







   def    UpperCAmelCase					(      self			, lowerCamelCase__ ):

      '''simple docstring'''







      return self.sp_model.encode(_A			, out_type=_A )







   def    UpperCAmelCase					(      self			, lowerCamelCase__ ):

      '''simple docstring'''







      return self.sp_model.piece_to_id(_A )







   def    UpperCAmelCase					(      self			, lowerCamelCase__ ):

      '''simple docstring'''







      UpperCamelCase	  =	self.sp_model.IdToPiece(_A )
      return token







   def    UpperCAmelCase					(      self			, lowerCamelCase__ ):

      '''simple docstring'''







      UpperCamelCase	  =	[]
      UpperCamelCase	  =	''''''
      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(_A ) + token
            UpperCamelCase	  =	[]
         else:
            current_sub_tokens.append(_A )
      out_string += self.sp_model.decode(_A )
      return out_string.strip()







   def    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__=None ):

      '''simple docstring'''







      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    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__ = None			, lowerCamelCase__ = False ):

      '''simple docstring'''







      if already_has_special_tokens:
         return super().get_special_tokens_mask(
             token_ids_a=_A			, token_ids_a=_A			, already_has_special_tokens=_A )

      UpperCamelCase	  =	[1]
      if token_ids_a is None:
         return ([0] * len(_A )) + suffix_ones
      return ([0] * len(_A )) + ([0] * len(_A )) + suffix_ones







   def    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__ = None ):

      '''simple docstring'''







      if not os.path.isdir(_A ):
         logger.error(f'Vocabulary path ({save_directory}) should be a directory' )
         return
      UpperCamelCase	  =	os.path.join(
          _A			, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] )

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

      return (out_vocab_file,)













'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available


_UpperCamelCase						:						Tuple  =					{
    'configuration_audio_spectrogram_transformer': [
        'AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'ASTConfig',
    ]
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_UpperCamelCase						:						Optional[int]  =					[
						    'AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'ASTForAudioClassification',
						    'ASTModel',
						    'ASTPreTrainedModel',
						]

try:
						if not is_speech_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_UpperCamelCase						:						int  =					['ASTFeatureExtractor']

if TYPE_CHECKING:
						from .configuration_audio_spectrogram_transformer import (
						    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    ASTConfig,
						)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_audio_spectrogram_transformer import (
												    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    ASTForAudioClassification,
												    ASTModel,
												    ASTPreTrainedModel,
												)

						try:
												if not is_speech_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
						import sys

						_UpperCamelCase						:						List[str]  =					_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


