




'''simple docstring'''





import numpy as np  # noqa: E402

from ...configuration_utils import ConfigMixin, register_to_config
from ...schedulers.scheduling_utils import SchedulerMixin


try:
  import librosa  # noqa: E402

  a		:  Union[str, Any]   			=			True
  a		:  List[str]   			=			""""""
except Exception as e:
  a		:  int   			=			False
  a		:  str   			=			(
      F"""Cannot import librosa because {e}. Make sure to correctly install librosa to be able to install it."""
  )


from PIL import Image  # noqa: E402




class     UpperCamelCase					(  UpperCAmelCase__			,		UpperCAmelCase__					):
  a    :List[str]      					=				'mel_config.json'



  @register_to_config
  def __init__( self		,    __lowerCAmelCase = 256		,    __lowerCAmelCase = 256		,    __lowerCAmelCase = 2_2050		,    __lowerCAmelCase = 2048		,    __lowerCAmelCase = 512		,    __lowerCAmelCase = 80		,    __lowerCAmelCase = 32		,    ):







      '''simple docstring'''


      _a							:       Optional[int]       					=			hop_length
      _a							:       List[str]       					=			sample_rate
      _a							:       Any       					=			n_fft
      _a							:       List[str]       					=			top_db
      _a							:       Union[str, Any]       					=			n_iter
      self.set_resolution(lowerCamelCase__		,    lowerCamelCase__   )
      _a							:       Union[str, Any]       					=			None

      if not _librosa_can_be_imported:
          raise ValueError(_import_error   )



  def 						a_       ( self		,    __lowerCAmelCase		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Union[str, Any]       					=			x_res
      _a							:       Any       					=			y_res
      _a							:       List[str]       					=			self.y_res
      _a							:       Optional[int]       					=			self.x_res * self.hop_length - 1



  def 						a_       ( self		,    __lowerCAmelCase = None		,    __lowerCAmelCase = None   ):







      '''simple docstring'''


      if audio_file is not None:
          _a							:       List[Any]       					=			librosa.load(lowerCamelCase__		,    mono=lowerCamelCase__		,    sr=self.sr   )
      else:
          _a							:       Tuple       					=			raw_audio

      # Pad with silence if necessary.
      if len(self.audio   ) < self.x_res * self.hop_length:
          _a							:       Tuple       					=			np.concatenate([self.audio, np.zeros((self.x_res * self.hop_length - len(self.audio   ),)   )]   )



  def 						a_       ( self   ):







      '''simple docstring'''


      return len(self.audio   ) // self.slice_size



  def 						a_       ( self		,    __lowerCAmelCase = 0   ):







      '''simple docstring'''


      return self.audio[self.slice_size * slice : self.slice_size * (slice + 1)]



  def 						a_       ( self   ):







      '''simple docstring'''


      return self.sr



  def 						a_       ( self		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Optional[Any]       					=			librosa.feature.melspectrogram(
          y=self.get_audio_slice(lowerCamelCase__   )		,    sr=self.sr		,    n_fft=self.n_fft		,    hop_length=self.hop_length		,    n_mels=self.n_mels   )
      _a							:       int       					=			librosa.power_to_db(lowerCamelCase__		,    ref=np.max		,    top_db=self.top_db   )
      _a							:       int       					=			(((log_S + self.top_db) * 255 / self.top_db).clip(0		,    255   ) + 0.5).astype(np.uinta   )
      _a							:       Optional[Any]       					=			Image.fromarray(lowerCamelCase__   )
      return image



  def 						a_       ( self		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Optional[int]       					=			np.frombuffer(image.tobytes()		,    dtype="""uint8"""   ).reshape((image.height, image.width)   )
      _a							:       Tuple       					=			bytedata.astype("""float"""   ) * self.top_db / 255 - self.top_db
      _a							:       Optional[int]       					=			librosa.db_to_power(lowerCamelCase__   )
      _a							:       Tuple       					=			librosa.feature.inverse.mel_to_audio(
          lowerCamelCase__		,    sr=self.sr		,    n_fft=self.n_fft		,    hop_length=self.hop_length		,    n_iter=self.n_iter   )
      return audio








'''simple docstring'''





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

from tokenizers import processors

from ...tokenization_utils import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
  from .tokenization_mbartaa import MBartaaTokenizer
else:
  a		:  str   			=			None


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

a		:  Dict   			=			{'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''}

a		:  Optional[Any]   			=			{
    '''vocab_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model'''
        ),
    },
    '''tokenizer_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/tokenizer.json'''
        ),
    },
}

a		:  Any   			=			{
    '''facebook/mbart-large-50-one-to-many-mmt''': 1_0_2_4,
}

# fmt: off
a		:  Dict   			=			['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN''', '''af_ZA''', '''az_AZ''', '''bn_IN''', '''fa_IR''', '''he_IL''', '''hr_HR''', '''id_ID''', '''ka_GE''', '''km_KH''', '''mk_MK''', '''ml_IN''', '''mn_MN''', '''mr_IN''', '''pl_PL''', '''ps_AF''', '''pt_XX''', '''sv_SE''', '''sw_KE''', '''ta_IN''', '''te_IN''', '''th_TH''', '''tl_XX''', '''uk_UA''', '''ur_PK''', '''xh_ZA''', '''gl_ES''', '''sl_SI''']




class     UpperCamelCase					(  snake_case__					):
  a    :str      					=				VOCAB_FILES_NAMES
  a    :int      					=				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a    :Dict      					=				PRETRAINED_VOCAB_FILES_MAP
  a    :List[str]      					=				['input_ids', 'attention_mask']
  a    :Optional[Any]      					=				MBartaaTokenizer

  a    :List[int]      					=				[]
  a    :List[int]      					=				[]



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







      '''simple docstring'''


      _a							:       List[str]       					=			AddedToken(__lowerCAmelCase		,    lstrip=__lowerCAmelCase		,    rstrip=__lowerCAmelCase   ) if isinstance(__lowerCAmelCase		,    __lowerCAmelCase   ) else mask_token

      _a							:       List[str]       					=			kwargs.get("""additional_special_tokens"""		,    []   )
      kwargs["additional_special_tokens"] += [
          code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"]
      ]

      super().__init__(
          __lowerCAmelCase		,    src_lang=__lowerCAmelCase		,    tgt_lang=__lowerCAmelCase		,    tokenizer_file=__lowerCAmelCase		,    eos_token=__lowerCAmelCase		,    sep_token=__lowerCAmelCase		,    cls_token=__lowerCAmelCase		,    unk_token=__lowerCAmelCase		,    pad_token=__lowerCAmelCase		,    mask_token=__lowerCAmelCase		,    **__lowerCAmelCase		,    )

      _a							:       Optional[int]       					=			vocab_file
      _a							:       int       					=			False if not self.vocab_file else True

      _a							:       str       					=			{
          lang_code: self.convert_tokens_to_ids(__lowerCAmelCase   ) for lang_code in FAIRSEQ_LANGUAGE_CODES
      }

      _a							:       Dict       					=			src_lang if src_lang is not None else """en_XX"""
      _a							:       Tuple       					=			tgt_lang
      _a							:       Tuple       					=			self.lang_code_to_id[self._src_lang]
      self.set_src_lang_special_tokens(self._src_lang   )



  @property
  def 						a_       ( self   ):







      '''simple docstring'''


      return self._src_lang



  @src_lang.setter
  def 						a_       ( self		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       int       					=			new_src_lang
      self.set_src_lang_special_tokens(self._src_lang   )



  def 						a_       ( self		,    __lowerCAmelCase		,    __lowerCAmelCase = None   ):







      '''simple docstring'''


      if token_ids_a is None:
          return self.prefix_tokens + token_ids_a + self.suffix_tokens
      # We don't expect to process pairs, but leave the pair logic for API consistency
      return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens



  def 						a_       ( self		,    __lowerCAmelCase		,    __lowerCAmelCase = "en_XX"		,    __lowerCAmelCase = None		,    __lowerCAmelCase = "ro_RO"		,    **__lowerCAmelCase		,    ):







      '''simple docstring'''


      _a							:       List[Any]       					=			src_lang
      _a							:       Any       					=			tgt_lang
      return super().prepare_seqaseq_batch(__lowerCAmelCase		,    __lowerCAmelCase		,    **__lowerCAmelCase   )



  def 						a_       ( self   ):







      '''simple docstring'''


      return self.set_src_lang_special_tokens(self.src_lang   )



  def 						a_       ( self   ):







      '''simple docstring'''


      return self.set_tgt_lang_special_tokens(self.tgt_lang   )



  def 						a_       ( self		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Union[str, Any]       					=			self.convert_tokens_to_ids(__lowerCAmelCase   )
      _a							:       Optional[Any]       					=			[self.cur_lang_code_id]
      _a							:       int       					=			[self.eos_token_id]

      _a							:       Any       					=			self.convert_ids_to_tokens(self.prefix_tokens   )
      _a							:       Dict       					=			self.convert_ids_to_tokens(self.suffix_tokens   )

      _a							:       Union[str, Any]       					=			processors.TemplateProcessing(
          single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str		,    pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str		,    special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str		,    self.prefix_tokens + self.suffix_tokens   )   )		,    )



  def 						a_       ( self		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Optional[int]       					=			self.convert_tokens_to_ids(__lowerCAmelCase   )
      _a							:       List[str]       					=			[self.cur_lang_code_id]
      _a							:       Tuple       					=			[self.eos_token_id]

      _a							:       List[Any]       					=			self.convert_ids_to_tokens(self.prefix_tokens   )
      _a							:       int       					=			self.convert_ids_to_tokens(self.suffix_tokens   )

      _a							:       str       					=			processors.TemplateProcessing(
          single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str		,    pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str		,    special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str		,    self.prefix_tokens + self.suffix_tokens   )   )		,    )



  def 						a_       ( self		,    __lowerCAmelCase		,    __lowerCAmelCase		,    __lowerCAmelCase		,    __lowerCAmelCase		,    **__lowerCAmelCase   ):







      '''simple docstring'''


      if src_lang is None or tgt_lang is None:
          raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model"""   )
      _a							:       List[Any]       					=			src_lang
      _a							:       List[str]       					=			self(__lowerCAmelCase		,    add_special_tokens=__lowerCAmelCase		,    return_tensors=__lowerCAmelCase		,    **__lowerCAmelCase   )
      _a							:       Optional[Any]       					=			self.convert_tokens_to_ids(__lowerCAmelCase   )
      _a							:       Tuple       					=			tgt_lang_id
      return inputs



  def 						a_       ( self		,    __lowerCAmelCase		,    __lowerCAmelCase = None   ):







      '''simple docstring'''


      if not self.can_save_slow_tokenizer:
          raise ValueError(
              """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
              """tokenizer."""   )

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

      if os.path.abspath(self.vocab_file   ) != os.path.abspath(__lowerCAmelCase   ):
          copyfile(self.vocab_file		,    __lowerCAmelCase   )

      return (out_vocab_file,)


