






"""simple docstring"""





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


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






class 	a		(   __snake_case						):
    SCREAMING_SNAKE_CASE		:	List[str]		=					["""input_features""", """attention_mask"""]






    def __init__(			self	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				List[str]=80			,		__SCREAMING_SNAKE_CASE	:				Optional[Any]=16000			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]=80			,		__SCREAMING_SNAKE_CASE	:				Any=0.0			,		__SCREAMING_SNAKE_CASE	:				List[Any]=True			,		__SCREAMING_SNAKE_CASE	:				int=True			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]=True			,		**__SCREAMING_SNAKE_CASE	:				Any			,		)	->						Any:
      super().__init__(feature_size=__SCREAMING_SNAKE_CASE			,		sampling_rate=__SCREAMING_SNAKE_CASE			,		padding_value=__SCREAMING_SNAKE_CASE			,		**__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		num_mel_bins
      lowerCamelCase_       	=		do_ceptral_normalize
      lowerCamelCase_       	=		normalize_means
      lowerCamelCase_       	=		normalize_vars
      lowerCamelCase_       	=		True






    def   UpperCamelCase			(			self	:				List[str]			,		__SCREAMING_SNAKE_CASE	:				np.ndarray			,		)	->						np.ndarray:
      lowerCamelCase_       	=		waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
      lowerCamelCase_       	=		torch.from_numpy(__SCREAMING_SNAKE_CASE   ).unsqueeze(0   )
      lowerCamelCase_       	=		ta_kaldi.fbank(__SCREAMING_SNAKE_CASE			,		num_mel_bins=self.num_mel_bins			,		sample_frequency=self.sampling_rate   )
      return features.numpy()






    @staticmethod
    def   UpperCamelCase			(			__SCREAMING_SNAKE_CASE	:				np.ndarray			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Optional[bool] = True			,		__SCREAMING_SNAKE_CASE	:				Optional[bool] = True			,		__SCREAMING_SNAKE_CASE	:				float = 0.0			,		)	->						np.ndarray:
      # make sure we normalize float32 arrays
      if normalize_means:
        lowerCamelCase_       	=		x[:input_length].mean(axis=0   )
        lowerCamelCase_       	=		np.subtract(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE   )
      if normalize_vars:
        lowerCamelCase_       	=		x[:input_length].std(axis=0   )
        lowerCamelCase_       	=		np.divide(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE   )

      if input_length < x.shape[0]:
        lowerCamelCase_       	=		padding_value

      # make sure array is in float32
      lowerCamelCase_       	=		x.astype(np.floataa   )

      return x






    def   UpperCamelCase			(			self	:				Optional[Any]			,		__SCREAMING_SNAKE_CASE	:				List[np.ndarray]			,		__SCREAMING_SNAKE_CASE	:				Optional[np.ndarray] = None   )	->						List[np.ndarray]:
      lowerCamelCase_       	=		attention_mask.sum(-1   ) if attention_mask is not None else [x.shape[0] for x in input_features]
      return [
          self.utterance_cmvn(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE			,		self.normalize_means			,		self.normalize_vars			,		self.padding_value   )
          for x, n in zip(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE   )
      ]






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

      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_       	=		isinstance(__SCREAMING_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_       	=		is_batched_numpy or (
          isinstance(__SCREAMING_SNAKE_CASE			,		(list, tuple)   ) and (isinstance(raw_speech[0]			,		(np.ndarray, tuple, list)   ))
      )

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

      # always return batch
      if not is_batched:
        lowerCamelCase_       	=		[raw_speech]

      # extract fbank features
      lowerCamelCase_       	=		[self._extract_fbank_features(__SCREAMING_SNAKE_CASE   ) for waveform in raw_speech]

      # convert into correct format for padding
      lowerCamelCase_       	=		BatchFeature({'input_features': features}   )

      lowerCamelCase_       	=		self.pad(
          __SCREAMING_SNAKE_CASE			,		padding=__SCREAMING_SNAKE_CASE			,		max_length=__SCREAMING_SNAKE_CASE			,		truncation=__SCREAMING_SNAKE_CASE			,		pad_to_multiple_of=__SCREAMING_SNAKE_CASE			,		return_attention_mask=__SCREAMING_SNAKE_CASE			,		**__SCREAMING_SNAKE_CASE			,		)

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

      lowerCamelCase_       	=		padded_inputs.get('attention_mask'   )
      if attention_mask is not None:
        lowerCamelCase_       	=		[np.asarray(__SCREAMING_SNAKE_CASE			,		dtype=np.intaa   ) for array in attention_mask]

      # Utterance-level cepstral mean and variance normalization
      if self.do_ceptral_normalize:
        lowerCamelCase_       	=		(
            np.array(__SCREAMING_SNAKE_CASE			,		dtype=np.intaa   )
            if self._get_padding_strategies(__SCREAMING_SNAKE_CASE			,		max_length=__SCREAMING_SNAKE_CASE   ) is not PaddingStrategy.DO_NOT_PAD
            else None
        )
        lowerCamelCase_       	=		self.normalize(
            padded_inputs['input_features']			,		attention_mask=__SCREAMING_SNAKE_CASE   )

      if return_tensors is not None:
        lowerCamelCase_       	=		padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE   )

      return padded_inputs












"""simple docstring"""





from transformers import DistilBertTokenizer, DistilBertTokenizerFast
from transformers.testing_utils import require_tokenizers, slow

from ..bert.test_tokenization_bert import BertTokenizationTest






@require_tokenizers
class 	a		(   __snake_case						):
    SCREAMING_SNAKE_CASE		:	Optional[Any]		=					DistilBertTokenizer
    SCREAMING_SNAKE_CASE		:	Optional[Any]		=					DistilBertTokenizerFast
    SCREAMING_SNAKE_CASE		:	int		=					True






    @slow
    def   UpperCamelCase			(			self	:				Optional[int]   )	->						int:
      lowerCamelCase_       	=		DistilBertTokenizer.from_pretrained('distilbert-base-uncased'   )

      lowerCamelCase_       	=		tokenizer.encode('sequence builders'			,		add_special_tokens=__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		tokenizer.encode('multi-sequence build'			,		add_special_tokens=__SCREAMING_SNAKE_CASE   )

      lowerCamelCase_       	=		tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE   )

      assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
      assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
          tokenizer.sep_token_id
      ]




