





from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


_UpperCAmelCase             =							logging.get_logger(__name__)






class        snake_case_    (		__lowercase						):
  A_      							=   ['input_features', 'attention_mask']






  def __init__(							self			:					List[Any]       ,  _snake_case			:					Optional[int]=80       ,  _snake_case			:					str=16000       ,  _snake_case			:					Tuple=80       ,  _snake_case			:					int=0.0       ,  _snake_case			:					Dict=True       ,  _snake_case			:					Union[str, Any]=True       ,  _snake_case			:					str=True       ,  **_snake_case			:					int       ,  )->int:






   '''simple docstring'''

   super().__init__(feature_size=_snake_case       ,  sampling_rate=_snake_case       ,  padding_value=_snake_case       ,  **_snake_case      )
   __lowerCAmelCase					:	int      =       num_mel_bins
   __lowerCAmelCase					:	Union[str, Any]      =       do_ceptral_normalize
   __lowerCAmelCase					:	Union[str, Any]      =       normalize_means
   __lowerCAmelCase					:	Tuple      =       normalize_vars
   __lowerCAmelCase					:	Optional[Any]      =       True






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]       ,  _snake_case			:					np.ndarray       ,  )->np.ndarray:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
   __lowerCAmelCase					:	Tuple      =       torch.from_numpy(_snake_case      ).unsqueeze(0      )
   __lowerCAmelCase					:	Optional[int]      =       ta_kaldi.fbank(_snake_case       ,  num_mel_bins=self.num_mel_bins       ,  sample_frequency=self.sampling_rate      )
   return features.numpy()






  @staticmethod
  def 				UpperCAmelCase__						(							_snake_case			:					np.ndarray       ,  _snake_case			:					int       ,  _snake_case			:					Optional[bool] = True       ,  _snake_case			:					Optional[bool] = True       ,  _snake_case			:					float = 0.0       ,  )->np.ndarray:






   '''simple docstring'''

   if normalize_means:
    __lowerCAmelCase					:	Optional[Any]      =       x[:input_length].mean(axis=0      )
    __lowerCAmelCase					:	List[str]      =       np.subtract(_snake_case       ,  _snake_case      )
   if normalize_vars:
    __lowerCAmelCase					:	int      =       x[:input_length].std(axis=0      )
    __lowerCAmelCase					:	List[str]      =       np.divide(_snake_case       ,  _snake_case      )

   if input_length < x.shape[0]:
    __lowerCAmelCase					:	Union[str, Any]      =       padding_value

   # make sure array is in float32
   __lowerCAmelCase					:	Tuple      =       x.astype(np.floataa      )

   return x






  def 				UpperCAmelCase__						(							self			:					List[Any]       ,  _snake_case			:					List[np.ndarray]       ,  _snake_case			:					Optional[np.ndarray] = None      )->List[np.ndarray]:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       attention_mask.sum(-1      ) if attention_mask is not None else [x.shape[0] for x in input_features]
   return [
       self.utterance_cmvn(_snake_case       ,  _snake_case       ,  self.normalize_means       ,  self.normalize_vars       ,  self.padding_value      )
       for x, n in zip(_snake_case       ,  _snake_case      )
   ]






  def __call__(							self			:					Union[str, Any]       ,  _snake_case			:					Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]       ,  _snake_case			:					Union[bool, str, PaddingStrategy] = False       ,  _snake_case			:					Optional[int] = None       ,  _snake_case			:					bool = False       ,  _snake_case			:					Optional[int] = None       ,  _snake_case			:					Optional[Union[str, TensorType]] = None       ,  _snake_case			:					Optional[int] = None       ,  _snake_case			:					Optional[bool] = None       ,  **_snake_case			:					Tuple       ,  )->BatchFeature:






   '''simple docstring'''

   if sampling_rate is not None:
    if sampling_rate != self.sampling_rate:
     raise ValueError(
         F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of'''
         F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'''
         F''' {self.sampling_rate} and not {sampling_rate}.'''      )
   else:
    logger.warning(
        """It is strongly recommended to pass the `sampling_rate` argument to this function. """
        """Failing to do so can result in silent errors that might be hard to debug."""      )

   __lowerCAmelCase					:	Optional[Any]      =       isinstance(_snake_case       ,  np.ndarray      ) and len(raw_speech.shape      ) > 1
   if is_batched_numpy and len(raw_speech.shape      ) > 2:
    raise ValueError(F'''Only mono-channel audio is supported for input to {self}'''      )
   __lowerCAmelCase					:	Union[str, Any]      =       is_batched_numpy or (
       isinstance(_snake_case       ,  (list, tuple)      ) and (isinstance(raw_speech[0]       ,  (np.ndarray, tuple, list)      ))
   )

   if is_batched:
    __lowerCAmelCase					:	Any      =       [np.asarray(_snake_case       ,  dtype=np.floataa      ) for speech in raw_speech]
   elif not is_batched and not isinstance(_snake_case       ,  np.ndarray      ):
    __lowerCAmelCase					:	Any      =       np.asarray(_snake_case       ,  dtype=np.floataa      )
   elif isinstance(_snake_case       ,  np.ndarray      ) and raw_speech.dtype is np.dtype(np.floataa      ):
    __lowerCAmelCase					:	Optional[Any]      =       raw_speech.astype(np.floataa      )

   # always return batch
   if not is_batched:
    __lowerCAmelCase					:	Union[str, Any]      =       [raw_speech]

   # extract fbank features
   __lowerCAmelCase					:	str      =       [self._extract_fbank_features(_snake_case      ) for waveform in raw_speech]

   # convert into correct format for padding
   __lowerCAmelCase					:	Tuple      =       BatchFeature({"""input_features""": features}      )

   __lowerCAmelCase					:	int      =       self.pad(
       _snake_case       ,  padding=_snake_case       ,  max_length=_snake_case       ,  truncation=_snake_case       ,  pad_to_multiple_of=_snake_case       ,  return_attention_mask=_snake_case       ,  **_snake_case       ,  )

   # make sure list is in array format
   __lowerCAmelCase					:	str      =       padded_inputs.get("""input_features"""      )
   if isinstance(input_features[0]       ,  _snake_case      ):
    __lowerCAmelCase					:	str      =       [np.asarray(_snake_case       ,  dtype=np.floataa      ) for feature in input_features]

   __lowerCAmelCase					:	Union[str, Any]      =       padded_inputs.get("""attention_mask"""      )
   if attention_mask is not None:
    __lowerCAmelCase					:	int      =       [np.asarray(_snake_case       ,  dtype=np.intaa      ) for array in attention_mask]

   # Utterance-level cepstral mean and variance normalization
   if self.do_ceptral_normalize:
    __lowerCAmelCase					:	Union[str, Any]      =       (
        np.array(_snake_case       ,  dtype=np.intaa      )
        if self._get_padding_strategies(_snake_case       ,  max_length=_snake_case      ) is not PaddingStrategy.DO_NOT_PAD
        else None
    )
    __lowerCAmelCase					:	Tuple      =       self.normalize(
        padded_inputs["""input_features"""]       ,  attention_mask=_snake_case      )

   if return_tensors is not None:
    __lowerCAmelCase					:	List[Any]      =       padded_inputs.convert_to_tensors(_snake_case      )

   return padded_inputs






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


_UpperCAmelCase             =							'▁'

_UpperCAmelCase             =							{'vocab_file': 'spiece.model'}

_UpperCAmelCase             =							{
    'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'}
}

_UpperCAmelCase             =							{
    'google/pegasus-xsum': 512,
}


_UpperCAmelCase             =							logging.get_logger(__name__)






class        snake_case_    (		__lowercase						):
  A_      							=   VOCAB_FILES_NAMES

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






  def __init__(							self			:					List[Any]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Union[str, Any]="<pad>"       ,  _snake_case			:					int="</s>"       ,  _snake_case			:					Any="<unk>"       ,  _snake_case			:					Union[str, Any]="<mask_2>"       ,  _snake_case			:					Any="<mask_1>"       ,  _snake_case			:					Optional[int]=None       ,  _snake_case			:					List[str]=103       ,  _snake_case			:					Optional[Dict[str, Any]] = None       ,  **_snake_case			:					Optional[int]       ,  )->None:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       offset
   if additional_special_tokens is not None:
    if not isinstance(_snake_case       ,  _snake_case      ):
     raise TypeError(
         F'''additional_special_tokens should be of type {type(_snake_case      )}, but is'''
         F''' {type(_snake_case      )}'''      )

    __lowerCAmelCase					:	List[str]      =       (
        ([mask_token_sent] + additional_special_tokens)
        if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
        else additional_special_tokens
    )
    # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
    additional_special_tokens_extended += [
        F'''<unk_{i}>''' for i in range(len(_snake_case      )       ,  self.offset - 1      )
    ]

    if len(set(_snake_case      )      ) != len(_snake_case      ):
     raise ValueError(
         """Please make sure that the provided additional_special_tokens do not contain an incorrectly"""
         F''' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.'''      )
    __lowerCAmelCase					:	Dict      =       additional_special_tokens_extended
   else:
    __lowerCAmelCase					:	Tuple      =       [mask_token_sent] if mask_token_sent is not None else []
    additional_special_tokens += [F'''<unk_{i}>''' for i in range(2       ,  self.offset      )]

   __lowerCAmelCase					:	Optional[int]      =       {} if sp_model_kwargs is None else sp_model_kwargs

   super().__init__(
       eos_token=_snake_case       ,  unk_token=_snake_case       ,  mask_token=_snake_case       ,  pad_token=_snake_case       ,  mask_token_sent=_snake_case       ,  offset=_snake_case       ,  additional_special_tokens=_snake_case       ,  sp_model_kwargs=self.sp_model_kwargs       ,  **_snake_case       ,  )
   __lowerCAmelCase					:	Optional[Any]      =       mask_token_sent
   __lowerCAmelCase					:	Any      =       vocab_file
   __lowerCAmelCase					:	Any      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
   self.sp_model.Load(_snake_case      )

   # add special tokens to encoder dict
   __lowerCAmelCase					:	Dict[int, str]      =       {
       0: self.pad_token,
       1: self.eos_token,
   }

   if self.mask_token_sent is not None:
    self.encoder.update(
        {
            2: self.mask_token_sent,
            3: self.mask_token,
        }      )

   if self.offset > 0:
    # entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102
    # mask_token_sent is already added to list -> so start at 1
    self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1       ,  self.offset - 1      )}      )

   __lowerCAmelCase					:	Dict[str, int]      =       {v: k for k, v in self.encoder.items()}






  @property
  def 				UpperCAmelCase__						(							self			:					str      )->int:






   '''simple docstring'''

   return len(self.sp_model      ) + self.offset






  def 				UpperCAmelCase__						(							self			:					Dict      )->Dict[str, int]:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       {self.convert_ids_to_tokens(_snake_case      ): i for i in range(self.vocab_size      )}
   vocab.update(self.added_tokens_encoder      )
   return vocab






  def __getstate__(							self			:					Optional[int]      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       self.__dict__.copy()
   __lowerCAmelCase					:	Union[str, Any]      =       None
   return state






  def __setstate__(							self			:					Any       ,  _snake_case			:					str      )->Any:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       d

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

   __lowerCAmelCase					:	Optional[int]      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
   self.sp_model.Load(self.vocab_file      )






  def 				UpperCAmelCase__						(							self			:					Dict       ,  _snake_case			:					str      )->List[str]:






   '''simple docstring'''

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






  def 				UpperCAmelCase__						(							self			:					Tuple       ,  _snake_case			:					str      )->int:






   '''simple docstring'''

   if token in self.decoder:
    return self.decoder[token]
   elif token in self.added_tokens_decoder:
    return self.added_tokens_decoder[token]
   __lowerCAmelCase					:	Any      =       self.sp_model.piece_to_id(_snake_case      )
   return sp_id + self.offset






  def 				UpperCAmelCase__						(							self			:					List[Any]       ,  _snake_case			:					int      )->str:






   '''simple docstring'''

   if index in self.encoder:
    return self.encoder[index]
   elif index in self.added_tokens_encoder:
    return self.added_tokens_encoder[index]
   else:
    __lowerCAmelCase					:	Optional[int]      =       self.sp_model.IdToPiece(index - self.offset      )
   return token






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]       ,  _snake_case			:					Optional[int]      )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       []
   __lowerCAmelCase					:	Dict      =       """"""
   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(_snake_case      ) + token
     __lowerCAmelCase					:	int      =       []
    else:
     current_sub_tokens.append(_snake_case      )
   out_string += self.sp_model.decode(_snake_case      )
   return out_string.strip()






  def 				UpperCAmelCase__						(							self			:					Optional[int]       ,  _snake_case			:					Dict=False      )->int:






   '''simple docstring'''

   return 1






  def 				UpperCAmelCase__						(							self			:					Tuple       ,  _snake_case			:					Tuple      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       set(self.all_special_ids      )  # call it once instead of inside list comp
   all_special_ids.remove(self.unk_token_id      )  # <unk> is only sometimes special

   return [1 if x in all_special_ids else 0 for x in seq]






  def 				UpperCAmelCase__						(							self			:					List[str]       ,  _snake_case			:					List       ,  _snake_case			:					Optional[List] = None       ,  _snake_case			:					bool = False      )->List[int]:






   '''simple docstring'''

   if already_has_special_tokens:
    return self._special_token_mask(_snake_case      )
   elif token_ids_a is None:
    return self._special_token_mask(_snake_case      ) + [1]
   else:
    return self._special_token_mask(token_ids_a + token_ids_a      ) + [1]






  def 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Tuple=None      )->List[int]:






   '''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			:					Any       ,  _snake_case			:					str       ,  _snake_case			:					Optional[str] = None      )->Tuple[str]:






   '''simple docstring'''

   if not os.path.isdir(_snake_case      ):
    logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''      )
    return
   __lowerCAmelCase					:	Optional[int]      =       os.path.join(
       _snake_case       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]      )

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

   return (out_vocab_file,)