



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


lowerCAmelCase_   						=   logging.get_logger(__name__)
class        __A       (	A_    ):







		'''simple docstring'''





		lowerCAmelCase			:					int   	=				["input_features", "attention_mask"]







		def __init__(	self						:  int			,_snake_case						:  Optional[int]=80			,_snake_case						:  int=16_000			,_snake_case						:  List[Any]=80			,_snake_case						:  Any=0.0			,_snake_case						:  Tuple=True			,_snake_case						:  int=True			,_snake_case						:  int=True			,**_snake_case						:  Tuple			,)					->		Any:






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Dict			,_snake_case						:  np.ndarray			,)					->		np.ndarray:






				"""simple docstring"""



				lowercase__   :    Dict				=			waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
				lowercase__   :    Union[str, Any]				=			torch.from_numpy(_snake_case						).unsqueeze(0						)
				lowercase__   :    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:
						lowercase__   :    Optional[Any]				=			x[:input_length].mean(axis=0						)
						lowercase__   :    List[Any]				=			np.subtract(_snake_case			,_snake_case						)
				if normalize_vars:
						lowercase__   :    str				=			x[:input_length].std(axis=0						)
						lowercase__   :    Optional[int]				=			np.divide(_snake_case			,_snake_case						)

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

				# make sure array is in float32
				lowercase__   :    List[Any]				=			x.astype(np.floataa						)

				return x







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






				"""simple docstring"""



				lowercase__   :    Union[str, 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						:  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						:  Any			,)					->		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.'''						)

				lowercase__   :    List[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}"""						)
				lowercase__   :    Dict				=			is_batched_numpy or (
				    isinstance(_snake_case			,(list, tuple)						) and (isinstance(raw_speech[0]			,(np.ndarray, tuple, list)						))
				)

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

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

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

				# convert into correct format for padding
				lowercase__   :    Union[str, Any]				=			BatchFeature({'''input_features''': features}						)

				lowercase__   :    List[Any]				=			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
				lowercase__   :    Any				=			padded_inputs.get('''input_features'''						)
				if isinstance(input_features[0]			,_snake_case						):
						lowercase__   :    List[str]				=			[np.asarray(_snake_case			,dtype=np.floataa						) for feature in input_features]

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

				# Utterance-level cepstral mean and variance normalization
				if self.do_ceptral_normalize:
						lowercase__   :    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
						)
						lowercase__   :    List[str]				=			self.normalize(
						    padded_inputs['''input_features''']			,attention_mask=_snake_case						)

				if return_tensors is not None:
						lowercase__   :    Optional[int]				=			padded_inputs.convert_to_tensors(_snake_case						)

				return padded_inputs





"""simple docstring"""







from ...configuration_utils import PretrainedConfig


lowerCAmelCase_   						=   {
    'google/tapas-base-finetuned-sqa': (
        'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wtq': (
        'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wikisql-supervised': (
        'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-tabfact': (
        'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json'
    ),
}
class        __A       (	A_    ):







		'''simple docstring'''





		lowerCAmelCase			:					str   	=				"tapas"







		def __init__(	self						:  List[Any]			,_snake_case						:  Dict=30_522			,_snake_case						:  Union[str, Any]=768			,_snake_case						:  int=12			,_snake_case						:  Union[str, Any]=12			,_snake_case						:  Union[str, Any]=3_072			,_snake_case						:  List[Any]="gelu"			,_snake_case						:  Optional[int]=0.1			,_snake_case						:  Tuple=0.1			,_snake_case						:  List[Any]=1_024			,_snake_case						:  Any=[3, 256, 256, 2, 256, 256, 10]			,_snake_case						:  List[Any]=0.02			,_snake_case						:  Union[str, Any]=1e-12			,_snake_case						:  str=0			,_snake_case						:  Any=10.0			,_snake_case						:  int=0			,_snake_case						:  Optional[Any]=1.0			,_snake_case						:  List[str]=None			,_snake_case						:  Tuple=1.0			,_snake_case						:  Tuple=False			,_snake_case						:  List[Any]=None			,_snake_case						:  int=1.0			,_snake_case						:  List[Any]=1.0			,_snake_case						:  Optional[int]=False			,_snake_case						:  Optional[int]=False			,_snake_case						:  Optional[int]="ratio"			,_snake_case						:  Any=None			,_snake_case						:  Union[str, Any]=None			,_snake_case						:  List[str]=64			,_snake_case						:  Optional[Any]=32			,_snake_case						:  Optional[Any]=False			,_snake_case						:  Optional[int]=True			,_snake_case						:  Dict=False			,_snake_case						:  Tuple=False			,_snake_case						:  int=True			,_snake_case						:  List[str]=False			,_snake_case						:  Dict=None			,_snake_case						:  Optional[int]=None			,**_snake_case						:  int			,)					->		List[Any]:






				"""simple docstring"""



				super().__init__(pad_token_id=_snake_case			,**_snake_case						)

				# BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
				lowercase__   :    Optional[int]				=			vocab_size
				lowercase__   :    List[str]				=			hidden_size
				lowercase__   :    Any				=			num_hidden_layers
				lowercase__   :    Optional[Any]				=			num_attention_heads
				lowercase__   :    Optional[int]				=			hidden_act
				lowercase__   :    List[Any]				=			intermediate_size
				lowercase__   :    List[Any]				=			hidden_dropout_prob
				lowercase__   :    Dict				=			attention_probs_dropout_prob
				lowercase__   :    str				=			max_position_embeddings
				lowercase__   :    Dict				=			type_vocab_sizes
				lowercase__   :    Optional[Any]				=			initializer_range
				lowercase__   :    Dict				=			layer_norm_eps

				# Fine-tuning task hyperparameters
				lowercase__   :    Any				=			positive_label_weight
				lowercase__   :    int				=			num_aggregation_labels
				lowercase__   :    List[str]				=			aggregation_loss_weight
				lowercase__   :    Optional[int]				=			use_answer_as_supervision
				lowercase__   :    Optional[Any]				=			answer_loss_importance
				lowercase__   :    Union[str, Any]				=			use_normalized_answer_loss
				lowercase__   :    str				=			huber_loss_delta
				lowercase__   :    str				=			temperature
				lowercase__   :    int				=			aggregation_temperature
				lowercase__   :    List[Any]				=			use_gumbel_for_cells
				lowercase__   :    Tuple				=			use_gumbel_for_aggregation
				lowercase__   :    Union[str, Any]				=			average_approximation_function
				lowercase__   :    Union[str, Any]				=			cell_selection_preference
				lowercase__   :    Any				=			answer_loss_cutoff
				lowercase__   :    List[Any]				=			max_num_rows
				lowercase__   :    str				=			max_num_columns
				lowercase__   :    int				=			average_logits_per_cell
				lowercase__   :    str				=			select_one_column
				lowercase__   :    str				=			allow_empty_column_selection
				lowercase__   :    Any				=			init_cell_selection_weights_to_zero
				lowercase__   :    Optional[int]				=			reset_position_index_per_cell
				lowercase__   :    Union[str, Any]				=			disable_per_token_loss

				# Aggregation hyperparameters
				lowercase__   :    Optional[Any]				=			aggregation_labels
				lowercase__   :    List[Any]				=			no_aggregation_label_index

				if isinstance(self.aggregation_labels			,_snake_case						):
						lowercase__   :    Union[str, Any]				=			{int(_snake_case						): v for k, v in aggregation_labels.items()}
