

'''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


UpperCamelCase_							=					logging.get_logger(__name__)





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						['input_features', 'is_longer']







				def __init__(					self    :		Tuple		,				UpperCAmelCase__    :		List[Any]=64		,				UpperCAmelCase__    :		str=48000		,				UpperCAmelCase__    :		List[Any]=480		,				UpperCAmelCase__    :		List[str]=10		,				UpperCAmelCase__    :		Union[str, Any]=1024		,				UpperCAmelCase__    :		Union[str, Any]=0.0		,				UpperCAmelCase__    :		List[Any]=False		,				UpperCAmelCase__    :		float = 0		,				UpperCAmelCase__    :		float = 14000		,				UpperCAmelCase__    :		int = None		,				UpperCAmelCase__    :		str = "fusion"		,				UpperCAmelCase__    :		str = "repeatpad"		,				**UpperCAmelCase__    :		int		,				):
										'''simple docstring'''
										super().__init__(
										    feature_size=UpperCAmelCase__		,				sampling_rate=UpperCAmelCase__		,				padding_value=UpperCAmelCase__		,				return_attention_mask=UpperCAmelCase__		,				**UpperCAmelCase__		,				)
										lowercase   :       Tuple								=top_db
										lowercase   :       List[str]								=truncation
										lowercase   :       int								=padding
										lowercase   :       Tuple								=fft_window_size
										lowercase   :       Union[str, Any]								=(fft_window_size >> 1) + 1
										lowercase   :       List[Any]								=hop_length
										lowercase   :       Dict								=max_length_s
										lowercase   :       int								=max_length_s * sampling_rate
										lowercase   :       Union[str, Any]								=sampling_rate
										lowercase   :       int								=frequency_min
										lowercase   :       List[Any]								=frequency_max
										lowercase   :       Dict								=mel_filter_bank(
										    num_frequency_bins=self.nb_frequency_bins		,				num_mel_filters=UpperCAmelCase__		,				min_frequency=UpperCAmelCase__		,				max_frequency=UpperCAmelCase__		,				sampling_rate=UpperCAmelCase__		,				norm=UpperCAmelCase__		,				mel_scale='''htk'''		,				)
										lowercase   :       Optional[int]								=mel_filter_bank(
										    num_frequency_bins=self.nb_frequency_bins		,				num_mel_filters=UpperCAmelCase__		,				min_frequency=UpperCAmelCase__		,				max_frequency=UpperCAmelCase__		,				sampling_rate=UpperCAmelCase__		,				norm='''slaney'''		,				mel_scale='''slaney'''		,				)







				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=copy.deepcopy(self.__dict__      )
										lowercase   :       List[str]								=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    lowerCamelCase_  (					self    :		Any		,				UpperCAmelCase__    :		np.array		,				UpperCAmelCase__    :		Optional[np.array] = None      ):
										'''simple docstring'''
										lowercase   :       int								=spectrogram(
										    UpperCAmelCase__		,				window_function(self.fft_window_size		,				'''hann'''      )		,				frame_length=self.fft_window_size		,				hop_length=self.hop_length		,				power=2.0		,				mel_filters=UpperCAmelCase__		,				log_mel='''dB'''		,				)
										return log_mel_spectrogram.T







				def    lowerCamelCase_  (					self    :		Dict		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		str      ):
										'''simple docstring'''
										lowercase   :       Any								=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
																lowercase   :       Optional[Any]								=[0]
										if len(ranges[2]      ) == 0:
																# if the audio is too short, we just use the first chunk
																lowercase   :       List[Any]								=[0]
										# randomly choose index for each part
										lowercase   :       Any								=np.random.choice(ranges[0]      )
										lowercase   :       Any								=np.random.choice(ranges[1]      )
										lowercase   :       str								=np.random.choice(ranges[2]      )

										lowercase   :       List[Any]								=mel[idx_front : idx_front + chunk_frames, :]
										lowercase   :       Any								=mel[idx_middle : idx_middle + chunk_frames, :]
										lowercase   :       List[Any]								=mel[idx_back : idx_back + chunk_frames, :]

										lowercase   :       List[str]								=torch.tensor(mel[None, None, :]      )
										lowercase   :       Tuple								=torch.nn.functional.interpolate(
										    UpperCAmelCase__		,				size=[chunk_frames, 64]		,				mode='''bilinear'''		,				align_corners=UpperCAmelCase__      )
										lowercase   :       Union[str, Any]								=mel_shrink[0][0].numpy()
										lowercase   :       Dict								=np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]		,				axis=0      )
										return mel_fusion







				def    lowerCamelCase_  (					self    :		Union[str, Any]		,				UpperCAmelCase__    :		np.array		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Optional[int]      ):
										'''simple docstring'''
										if waveform.shape[0] > max_length:
																if truncation == "rand_trunc":
																						lowercase   :       Any								=True
																						# random crop to max_length (for compatibility) -> this should be handled by self.pad
																						lowercase   :       int								=len(UpperCAmelCase__      ) - max_length
																						lowercase   :       Any								=np.random.randint(0		,				overflow + 1      )
																						lowercase   :       int								=waveform[idx : idx + max_length]
																						lowercase   :       List[str]								=self._np_extract_fbank_features(UpperCAmelCase__		,				self.mel_filters_slaney      )[None, :]
																elif truncation == "fusion":
																						lowercase   :       Optional[Any]								=self._np_extract_fbank_features(UpperCAmelCase__		,				self.mel_filters      )
																						lowercase   :       Tuple								=max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
																						lowercase   :       str								=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.
																												lowercase   :       List[str]								=np.stack([mel, mel, mel, mel]		,				axis=0      )
																												lowercase   :       List[Any]								=False
																						else:
																												lowercase   :       Optional[int]								=self._random_mel_fusion(UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__      )
																												lowercase   :       Dict								=True
																else:
																						raise NotImplementedError(F'''data_truncating {truncation} not implemented'''      )

										else:
																lowercase   :       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":
																												lowercase   :       Any								=int(max_length / len(UpperCAmelCase__      )      )
																												lowercase   :       List[Any]								=np.stack(np.tile(UpperCAmelCase__		,				n_repeat + 1      )      )[:max_length]
																						if padding == "repeatpad":
																												lowercase   :       str								=int(max_length / len(UpperCAmelCase__      )      )
																												lowercase   :       Any								=np.stack(np.tile(UpperCAmelCase__		,				UpperCAmelCase__      )      )
																						lowercase   :       Union[str, Any]								=np.pad(UpperCAmelCase__		,				(0, max_length - waveform.shape[0])		,				mode='''constant'''		,				constant_values=0      )

																if truncation == "fusion":
																						lowercase   :       List[Any]								=self._np_extract_fbank_features(UpperCAmelCase__		,				self.mel_filters      )
																						lowercase   :       Any								=np.stack([input_mel, input_mel, input_mel, input_mel]		,				axis=0      )
																else:
																						lowercase   :       Optional[int]								=self._np_extract_fbank_features(UpperCAmelCase__		,				self.mel_filters_slaney      )[None, :]

										return input_mel, longer







				def __call__(					self    :		Union[str, Any]		,				UpperCAmelCase__    :		Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		,				UpperCAmelCase__    :		str = None		,				UpperCAmelCase__    :		Optional[str] = None		,				UpperCAmelCase__    :		Optional[int] = None		,				UpperCAmelCase__    :		Optional[int] = None		,				UpperCAmelCase__    :		Optional[Union[str, TensorType]] = None		,				**UpperCAmelCase__    :		str		,				):
										'''simple docstring'''
										lowercase   :       Union[str, Any]								=truncation if truncation is not None else self.truncation
										lowercase   :       str								=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.'''      )

										lowercase   :       Dict								=isinstance(UpperCAmelCase__		,				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}'''      )
										lowercase   :       Union[str, Any]								=is_batched_numpy or (
										    isinstance(UpperCAmelCase__		,				(list, tuple)      ) and (isinstance(raw_speech[0]		,				(np.ndarray, tuple, list)      ))
										)

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

										# always return batch
										if not is_batched:
																lowercase   :       Dict								=[np.asarray(UpperCAmelCase__      )]

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

										lowercase   :       int								=[]
										lowercase   :       List[Any]								=[]
										for mel, longer in padded_inputs:
																input_mel.append(UpperCAmelCase__      )
																is_longer.append(UpperCAmelCase__      )

										if truncation == "fusion" and sum(UpperCAmelCase__      ) == 0:
																# if no audio is longer than 10s, then randomly select one audio to be longer
																lowercase   :       int								=np.random.randint(0		,				len(UpperCAmelCase__      )      )
																lowercase   :       Tuple								=True

										if isinstance(input_mel[0]		,				UpperCAmelCase__      ):
																lowercase   :       List[str]								=[np.asarray(UpperCAmelCase__		,				dtype=np.floataa      ) for feature in input_mel]

										# is_longer is a list of bool
										lowercase   :       List[Any]								=[[longer] for longer in is_longer]

										lowercase   :       Dict								={'''input_features''': input_mel, '''is_longer''': is_longer}
										lowercase   :       List[str]								=BatchFeature(UpperCAmelCase__      )

										if return_tensors is not None:
																lowercase   :       Any								=input_features.convert_to_tensors(UpperCAmelCase__      )

										return input_features








'''simple docstring'''





import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
							from ... import PreTrainedTokenizerBase, TensorType

UpperCamelCase_							=					logging.get_logger(__name__)





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						'vision-encoder-decoder'
				lowerCamelCase_							=						True







				def __init__(					self    :		Optional[int]		,				**UpperCAmelCase__    :		Tuple      ):
										'''simple docstring'''
										super().__init__(**UpperCAmelCase__      )
										if "encoder" not in kwargs or "decoder" not in kwargs:
																raise ValueError(
																    F'''A configuraton of type {self.model_type} cannot be instantiated because '''
																    F'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}'''      )

										lowercase   :       Optional[Any]								=kwargs.pop('''encoder'''      )
										lowercase   :       List[Any]								=encoder_config.pop('''model_type'''      )
										lowercase   :       List[str]								=kwargs.pop('''decoder'''      )
										lowercase   :       Dict								=decoder_config.pop('''model_type'''      )

										lowercase   :       Union[str, Any]								=AutoConfig.for_model(UpperCAmelCase__		,				**UpperCAmelCase__      )
										lowercase   :       List[str]								=AutoConfig.for_model(UpperCAmelCase__		,				**UpperCAmelCase__      )
										lowercase   :       str								=True







				@classmethod
				def    lowerCamelCase_  (					cls    :		List[str]		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		PretrainedConfig		,				**UpperCAmelCase__    :		Optional[int]      ):
										'''simple docstring'''
										logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''      )
										lowercase   :       int								=True
										lowercase   :       Optional[Any]								=True

										return cls(encoder=encoder_config.to_dict()		,				decoder=decoder_config.to_dict()		,				**UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       int								=copy.deepcopy(self.__dict__      )
										lowercase   :       Union[str, Any]								=self.encoder.to_dict()
										lowercase   :       Union[str, Any]								=self.decoder.to_dict()
										lowercase   :       int								=self.__class__.model_type
										return output





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						version.parse('1.11'			)







				@property
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										return OrderedDict(
										    [
										        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
										    ]      )







				@property
				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										return 1E-4







				@property
				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										return OrderedDict({'''last_hidden_state''': {0: '''batch''', 1: '''encoder_sequence'''}}      )





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):







				@property
				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       List[str]								=OrderedDict()
										lowercase   :       Tuple								={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
										lowercase   :       Optional[int]								={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
										lowercase   :       int								={0: '''batch''', 1: '''encoder_sequence'''}

										return common_inputs







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		"PreTrainedTokenizerBase"		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		bool = False		,				UpperCAmelCase__    :		Optional["TensorType"] = None		,				):
										'''simple docstring'''
										import torch

										lowercase   :       Optional[Any]								=OrderedDict()

										lowercase   :       List[Any]								=super().generate_dummy_inputs(
										    UpperCAmelCase__		,				batch_size=UpperCAmelCase__		,				seq_length=UpperCAmelCase__		,				is_pair=UpperCAmelCase__		,				framework=UpperCAmelCase__      )

										lowercase		,  lowercase   :       Optional[int]								=dummy_input['''input_ids'''].shape
										lowercase   :       Union[str, Any]								=(batch, encoder_sequence, self._config.encoder_hidden_size)
										lowercase   :       List[str]								=dummy_input.pop('''input_ids'''      )
										lowercase   :       Tuple								=dummy_input.pop('''attention_mask'''      )
										lowercase   :       Union[str, Any]								=torch.zeros(UpperCAmelCase__      )

										return common_inputs





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):







				@property
				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										pass







				def    lowerCamelCase_  (					self    :		List[Any]		,				UpperCAmelCase__    :		PretrainedConfig      ):
										'''simple docstring'''
										return VisionEncoderDecoderEncoderOnnxConfig(UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		str = "default"      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=encoder_config.hidden_size
										return VisionEncoderDecoderDecoderOnnxConfig(UpperCAmelCase__		,				UpperCAmelCase__      )





