


'''simple docstring'''


from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
     from ...feature_extraction_utils import FeatureExtractionMixin
     from ...tokenization_utils_base import PreTrainedTokenizerBase
     from ...utils import TensorType

__magic_name__			=      logging.get_logger(__name__)

__magic_name__			=      {
    'openai/whisper-base': 'https://huggingface.co/openai/whisper-base/resolve/main/config.json',
}

# fmt: off
__magic_name__			=      [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377,
    1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211,
    4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786,
    11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791,
    17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409,
    34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361
]
__magic_name__			=      [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627,
    3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647,
    7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793,
    14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675,
    22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865,
    42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362
]



class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  a_  					=							"""whisper"""
  a_  					=							["""past_key_values"""]
  a_  					=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}






  def __init__(							self :       str	,_a :       List[str]=51865	,_a :       Optional[Any]=80	,_a :       List[Any]=6	,_a :       int=4	,_a :       Optional[int]=6	,_a :       int=4	,_a :       Optional[Any]=1536	,_a :       Union[str, Any]=1536	,_a :       Union[str, Any]=0.0	,_a :       List[Any]=0.0	,_a :       Tuple=50257	,_a :       Optional[int]=True	,_a :       Dict=True	,_a :       int="gelu"	,_a :       Dict=256	,_a :       Optional[Any]=0.0	,_a :       Optional[Any]=0.0	,_a :       Union[str, Any]=0.0	,_a :       Optional[Any]=0.02	,_a :       Dict=False	,_a :       Tuple=1500	,_a :       Dict=448	,_a :       Tuple=50256	,_a :       Tuple=50256	,_a :       Optional[int]=50256	,_a :       Optional[int]=None	,_a :       Tuple=[220, 50256]	,_a :       Dict=False	,_a :       List[str]=256	,_a :       Optional[int]=False	,_a :       int=0.05	,_a :       Any=10	,_a :       int=2	,_a :       int=0.0	,_a :       Union[str, Any]=10	,_a :       Optional[int]=0	,_a :       int=7	,**_a :       Optional[int]	,):

        '''simple docstring'''




        A_						:  Union[str, Any]          =				vocab_size
        A_						:  List[str]          =				num_mel_bins
        A_						:  Tuple          =				d_model
        A_						:  List[str]          =				encoder_layers
        A_						:  List[Any]          =				encoder_attention_heads
        A_						:  Dict          =				decoder_layers
        A_						:  Any          =				decoder_attention_heads
        A_						:  Union[str, Any]          =				decoder_ffn_dim
        A_						:  Optional[Any]          =				encoder_ffn_dim
        A_						:  Union[str, Any]          =				dropout
        A_						:  Tuple          =				attention_dropout
        A_						:  Tuple          =				activation_dropout
        A_						:  str          =				activation_function
        A_						:  Union[str, Any]          =				init_std
        A_						:  Optional[Any]          =				encoder_layerdrop
        A_						:  List[Any]          =				decoder_layerdrop
        A_						:  Tuple          =				use_cache
        A_						:  Optional[Any]          =				encoder_layers
        A_						:  List[str]          =				scale_embedding  # scale factor will be sqrt(d_model) if True
        A_						:  Optional[int]          =				max_source_positions
        A_						:  str          =				max_target_positions

        # Audio Classification-specific parameters. Feel free to ignore for other classes.
        A_						:  Tuple          =				classifier_proj_size
        A_						:  Tuple          =				use_weighted_layer_sum

        # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
        A_						:  Any          =				apply_spec_augment
        A_						:  Optional[int]          =				mask_time_prob
        A_						:  Optional[int]          =				mask_time_length
        A_						:  Optional[int]          =				mask_time_min_masks
        A_						:  Optional[Any]          =				mask_feature_prob
        A_						:  Optional[Any]          =				mask_feature_length
        A_						:  Any          =				mask_feature_min_masks

        A_						:  List[str]          =				median_filter_width

        super().__init__(
            pad_token_id=_a	,bos_token_id=_a	,eos_token_id=_a	,is_encoder_decoder=_a	,decoder_start_token_id=_a	,suppress_tokens=_a	,begin_suppress_tokens=_a	,**_a	,)







class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  @property
  def        _a							(							self :       Union[str, Any] ):

        '''simple docstring'''




        A_						:  Optional[int]          =				OrderedDict(
            [
                ("""input_features""", {0: """batch""", 1: """feature_size""", 2: """encoder_sequence"""}),
            ] )
        if self.use_past:
              A_						:  Optional[int]          =				{0: """batch"""}
        else:
              A_						:  Optional[Any]          =				{0: """batch""", 1: """decoder_sequence"""}

        if self.use_past:
              self.fill_with_past_key_values_(_a	,direction="""inputs""" )

        return common_inputs






  def        _a							(							self :       Dict	,_a :       Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]	,_a :       int = -1	,_a :       int = -1	,_a :       bool = False	,_a :       Optional["TensorType"] = None	,_a :       int = 22050	,_a :       float = 5.0	,_a :       int = 220	,):

        '''simple docstring'''




        A_						:  int          =				OrderedDict()
        A_						:  Dict          =				OnnxConfig.generate_dummy_inputs(
            self	,preprocessor=preprocessor.feature_extractor	,batch_size=_a	,framework=_a	,sampling_rate=_a	,time_duration=_a	,frequency=_a	,)
        A_						:  int          =				encoder_inputs["""input_features"""].shape[2]
        A_						:  Union[str, Any]          =				encoder_sequence_length // 2 if self.use_past else seq_length

        A_						:  int          =				super().generate_dummy_inputs(
            preprocessor.tokenizer	,_a	,_a	,_a	,_a )

        A_						:  Optional[Any]          =				encoder_inputs.pop("""input_features""" )
        A_						:  Dict          =				decoder_inputs.pop("""decoder_input_ids""" )

        if "past_key_values" in decoder_inputs:
              A_						:  List[Any]          =				decoder_inputs.pop("""past_key_values""" )

        return dummy_inputs






  @property
  def        _a							(							self :       Any ):

        '''simple docstring'''




        return 1e-3







'''simple docstring'''


import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


__magic_name__			=      logging.get_logger(__name__)



class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  a_  					=							["""input_features""", """is_longer"""]






  def __init__(							self :       Dict	,_a :       Optional[int]=64	,_a :       List[Any]=48000	,_a :       str=480	,_a :       Optional[Any]=10	,_a :       Optional[int]=1024	,_a :       Tuple=0.0	,_a :       str=False	,_a :       float = 0	,_a :       float = 14000	,_a :       int = None	,_a :       str = "fusion"	,_a :       str = "repeatpad"	,**_a :       Tuple	,):

        '''simple docstring'''




        super().__init__(
            feature_size=_a	,sampling_rate=_a	,padding_value=_a	,return_attention_mask=_a	,**_a	,)
        A_						:  Tuple          =				top_db
        A_						:  Tuple          =				truncation
        A_						:  Optional[Any]          =				padding
        A_						:  Optional[int]          =				fft_window_size
        A_						:  Dict          =				(fft_window_size >> 1) + 1
        A_						:  Any          =				hop_length
        A_						:  List[Any]          =				max_length_s
        A_						:  Tuple          =				max_length_s * sampling_rate
        A_						:  Tuple          =				sampling_rate
        A_						:  Optional[int]          =				frequency_min
        A_						:  Tuple          =				frequency_max
        A_						:  Tuple          =				mel_filter_bank(
            num_frequency_bins=self.nb_frequency_bins	,num_mel_filters=_a	,min_frequency=_a	,max_frequency=_a	,sampling_rate=_a	,norm=_a	,mel_scale="""htk"""	,)
        A_						:  Dict          =				mel_filter_bank(
            num_frequency_bins=self.nb_frequency_bins	,num_mel_filters=_a	,min_frequency=_a	,max_frequency=_a	,sampling_rate=_a	,norm="""slaney"""	,mel_scale="""slaney"""	,)






  def        _a							(							self :       int ):

        '''simple docstring'''




        A_						:  int          =				copy.deepcopy(self.__dict__ )
        A_						:  Tuple          =				self.__class__.__name__
        if "mel_filters" in output:
              del output["mel_filters"]
        if "mel_filters_slaney" in output:
              del output["mel_filters_slaney"]
        return output






  def        _a							(							self :       Dict	,_a :       np.array	,_a :       Optional[np.array] = None ):

        '''simple docstring'''




        A_						:  List[str]          =				spectrogram(
            _a	,window_function(self.fft_window_size	,"""hann""" )	,frame_length=self.fft_window_size	,hop_length=self.hop_length	,power=2.0	,mel_filters=_a	,log_mel="""dB"""	,)
        return log_mel_spectrogram.T






  def        _a							(							self :       Optional[int]	,_a :       Dict	,_a :       Optional[Any]	,_a :       Optional[int] ):

        '''simple docstring'''




        A_						:  Dict          =				np.array_split(list(range(0	,total_frames - chunk_frames + 1 ) )	,3 )
        if len(ranges[1] ) == 0:
              # if the audio is too short, we just use the first chunk
              A_						:  List[Any]          =				[0]
        if len(ranges[2] ) == 0:
              # if the audio is too short, we just use the first chunk
              A_						:  int          =				[0]
        # randomly choose index for each part
        A_						:  List[str]          =				np.random.choice(ranges[0] )
        A_						:  int          =				np.random.choice(ranges[1] )
        A_						:  Optional[int]          =				np.random.choice(ranges[2] )

        A_						:  Tuple          =				mel[idx_front : idx_front + chunk_frames, :]
        A_						:  Dict          =				mel[idx_middle : idx_middle + chunk_frames, :]
        A_						:  Dict          =				mel[idx_back : idx_back + chunk_frames, :]

        A_						:  Optional[int]          =				torch.tensor(mel[None, None, :] )
        A_						:  Dict          =				torch.nn.functional.interpolate(
            _a	,size=[chunk_frames, 64]	,mode="""bilinear"""	,align_corners=_a )
        A_						:  str          =				mel_shrink[0][0].numpy()
        A_						:  Tuple          =				np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]	,axis=0 )
        return mel_fusion






  def        _a							(							self :       Dict	,_a :       np.array	,_a :       Optional[Any]	,_a :       int	,_a :       Dict ):

        '''simple docstring'''




        if waveform.shape[0] > max_length:
              if truncation == "rand_trunc":
                    A_						:  Dict          =				True
                    # random crop to max_length (for compatibility) -> this should be handled by self.pad
                    A_						:  Tuple          =				len(_a ) - max_length
                    A_						:  Optional[int]          =				np.random.randint(0	,overflow + 1 )
                    A_						:  List[Any]          =				waveform[idx : idx + max_length]
                    A_						:  Optional[Any]          =				self._np_extract_fbank_features(_a	,self.mel_filters_slaney )[None, :]
              elif truncation == "fusion":
                    A_						:  Dict          =				self._np_extract_fbank_features(_a	,self.mel_filters )
                    A_						:  Tuple          =				max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
                    A_						:  Optional[int]          =				mel.shape[0]
                    if chunk_frames == total_frames:
                          # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
                          # In this case, we just use the whole audio.
                          A_						:  Optional[int]          =				np.stack([mel, mel, mel, mel]	,axis=0 )
                          A_						:  str          =				False
                    else:
                          A_						:  str          =				self._random_mel_fusion(_a	,_a	,_a )
                          A_						:  Optional[Any]          =				True
              else:
                    raise NotImplementedError(f'data_truncating {truncation} not implemented' )

        else:
              A_						:  Optional[int]          =				False
              # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
              if waveform.shape[0] < max_length:
                    if padding == "repeat":
                          A_						:  int          =				int(max_length / len(_a ) )
                          A_						:  Any          =				np.stack(np.tile(_a	,n_repeat + 1 ) )[:max_length]
                    if padding == "repeatpad":
                          A_						:  List[str]          =				int(max_length / len(_a ) )
                          A_						:  Optional[Any]          =				np.stack(np.tile(_a	,_a ) )
                    A_						:  Any          =				np.pad(_a	,(0, max_length - waveform.shape[0])	,mode="""constant"""	,constant_values=0 )

              if truncation == "fusion":
                    A_						:  List[Any]          =				self._np_extract_fbank_features(_a	,self.mel_filters )
                    A_						:  Optional[Any]          =				np.stack([input_mel, input_mel, input_mel, input_mel]	,axis=0 )
              else:
                    A_						:  Union[str, Any]          =				self._np_extract_fbank_features(_a	,self.mel_filters_slaney )[None, :]

        return input_mel, longer






  def __call__(							self :       List[Any]	,_a :       Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]	,_a :       str = None	,_a :       Optional[str] = None	,_a :       Optional[int] = None	,_a :       Optional[int] = None	,_a :       Optional[Union[str, TensorType]] = None	,**_a :       Any	,):

        '''simple docstring'''




        A_						:  List[str]          =				truncation if truncation is not None else self.truncation
        A_						:  List[Any]          =				padding if padding else self.padding

        if sampling_rate is not None:
              if sampling_rate != self.sampling_rate:
                    raise ValueError(
                        f'The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a'
                        f' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input'
                        f' was sampled with {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.""" )

        A_						:  Any          =				isinstance(_a	,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}' )
        A_						:  int          =				is_batched_numpy or (
            isinstance(_a	,(list, tuple) ) and (isinstance(raw_speech[0]	,(np.ndarray, tuple, list) ))
        )

        if is_batched:
              A_						:  Optional[int]          =				[np.asarray(_a	,dtype=np.floataa ) for speech in raw_speech]
        elif not is_batched and not isinstance(_a	,np.ndarray ):
              A_						:  str          =				np.asarray(_a	,dtype=np.floataa )
        elif isinstance(_a	,np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ):
              A_						:  Tuple          =				raw_speech.astype(np.floataa )

        # always return batch
        if not is_batched:
              A_						:  Any          =				[np.asarray(_a )]

        # convert to mel spectrogram, truncate and pad if needed.
        A_						:  str          =				[
            self._get_input_mel(_a	,max_length if max_length else self.nb_max_samples	,_a	,_a )
            for waveform in raw_speech
        ]

        A_						:  int          =				[]
        A_						:  Any          =				[]
        for mel, longer in padded_inputs:
              input_mel.append(_a )
              is_longer.append(_a )

        if truncation == "fusion" and sum(_a ) == 0:
              # if no audio is longer than 10s, then randomly select one audio to be longer
              A_						:  List[Any]          =				np.random.randint(0	,len(_a ) )
              A_						:  List[str]          =				True

        if isinstance(input_mel[0]	,_a ):
              A_						:  Tuple          =				[np.asarray(_a	,dtype=np.floataa ) for feature in input_mel]

        # is_longer is a list of bool
        A_						:  List[str]          =				[[longer] for longer in is_longer]

        A_						:  Optional[Any]          =				{"""input_features""": input_mel, """is_longer""": is_longer}
        A_						:  int          =				BatchFeature(_a )

        if return_tensors is not None:
              A_						:  int          =				input_features.convert_to_tensors(_a )

        return input_features
