


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


UpperCamelCase__				:Tuple			   =	logging.get_logger(__name__)






class      A(  lowerCamelCase__		):

		"""simple docstring"""





		A    =							["input_features", "attention_mask"]
		def __init__( self					,				SCREAMING_SNAKE_CASE__=80					,				SCREAMING_SNAKE_CASE__=1_60_00					,				SCREAMING_SNAKE_CASE__=80					,				SCREAMING_SNAKE_CASE__=0.0					,				SCREAMING_SNAKE_CASE__=True					,				SCREAMING_SNAKE_CASE__=True					,				SCREAMING_SNAKE_CASE__=True					,				**SCREAMING_SNAKE_CASE__					,				)      ->	Tuple:

					"""simple docstring"""



					super().__init__(feature_size=SCREAMING_SNAKE_CASE__					,				sampling_rate=SCREAMING_SNAKE_CASE__					,				padding_value=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )
					_UpperCamelCase					:int					     =	num_mel_bins
					_UpperCamelCase					:Optional[Any]					     =	do_ceptral_normalize
					_UpperCamelCase					:List[str]					     =	normalize_means
					_UpperCamelCase					:Dict					     =	normalize_vars
					_UpperCamelCase					:Tuple					     =	True
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				)      ->	np.ndarray:

					"""simple docstring"""



					_UpperCamelCase					:Dict					     =	waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
					_UpperCamelCase					:Any					     =	torch.from_numpy(SCREAMING_SNAKE_CASE__   ).unsqueeze(0   )
					_UpperCamelCase					:int					     =	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__					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__ = True					,				SCREAMING_SNAKE_CASE__ = True					,				SCREAMING_SNAKE_CASE__ = 0.0					,				)      ->	np.ndarray:

					"""simple docstring"""



					if normalize_means:
								_UpperCamelCase					:Tuple					     =	x[:input_length].mean(axis=0   )
								_UpperCamelCase					:Any					     =	np.subtract(SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )
					if normalize_vars:
								_UpperCamelCase					:Any					     =	x[:input_length].std(axis=0   )
								_UpperCamelCase					:Any					     =	np.divide(SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )

					if input_length < x.shape[0]:
								_UpperCamelCase					:List[str]					     =	padding_value

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

					return x
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__ = None   )      ->	List[np.ndarray]:

					"""simple docstring"""



					_UpperCamelCase					:Optional[int]					     =	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					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__ = False					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = False					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = None					,				**SCREAMING_SNAKE_CASE__					,				)      ->	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.'''   )

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

					if is_batched:
								_UpperCamelCase					:Any					     =	[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   ):
								_UpperCamelCase					:Optional[Any]					     =	np.asarray(SCREAMING_SNAKE_CASE__					,				dtype=np.floataa   )
					elif isinstance(SCREAMING_SNAKE_CASE__					,				np.ndarray   ) and raw_speech.dtype is np.dtype(np.floataa   ):
								_UpperCamelCase					:str					     =	raw_speech.astype(np.floataa   )

					# always return batch
					if not is_batched:
								_UpperCamelCase					:List[Any]					     =	[raw_speech]

					# extract fbank features
					_UpperCamelCase					:Optional[Any]					     =	[self._extract_fbank_features(SCREAMING_SNAKE_CASE__   ) for waveform in raw_speech]

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

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

					_UpperCamelCase					:str					     =	padded_inputs.get('''attention_mask'''   )
					if attention_mask is not None:
								_UpperCamelCase					:str					     =	[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:
								_UpperCamelCase					:Union[str, Any]					     =	(
								    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
								)
								_UpperCamelCase					:Optional[int]					     =	self.normalize(
								    padded_inputs['''input_features''']					,				attention_mask=SCREAMING_SNAKE_CASE__   )

					if return_tensors is not None:
								_UpperCamelCase					:Any					     =	padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE__   )

					return padded_inputs





"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class      A(  lowerCamelCase__		):

		"""simple docstring"""





		A    =							["image_processor", "tokenizer"]
		A    =							"ViTImageProcessor"
		A    =							("CLIPTokenizer", "CLIPTokenizerFast")
		def __init__( self					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				**SCREAMING_SNAKE_CASE__   )      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:List[str]					     =	None
					if "feature_extractor" in kwargs:
								warnings.warn(
								    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
								    ''' instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
								_UpperCamelCase					:List[str]					     =	kwargs.pop('''feature_extractor'''   )

					_UpperCamelCase					:Optional[Any]					     =	image_processor if image_processor is not None else feature_extractor
					if image_processor is None:
								raise ValueError('''You need to specify an `image_processor`.'''   )
					if tokenizer is None:
								raise ValueError('''You need to specify a `tokenizer`.'''   )

					super().__init__(SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )
		def __call__( self					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				**SCREAMING_SNAKE_CASE__   )      ->	Tuple:

					"""simple docstring"""



					if text is None and visual_prompt is None and images is None:
								raise ValueError('''You have to specify either text, visual prompt or images.'''   )

					if text is not None and visual_prompt is not None:
								raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.'''   )

					if text is not None:
								_UpperCamelCase					:Dict					     =	self.tokenizer(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if visual_prompt is not None:
								_UpperCamelCase					:Optional[Any]					     =	self.image_processor(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if images is not None:
								_UpperCamelCase					:Union[str, Any]					     =	self.image_processor(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if visual_prompt is not None and images is not None:
								_UpperCamelCase					:List[str]					     =	{
								    '''pixel_values''': image_features.pixel_values,
								    '''conditional_pixel_values''': prompt_features.pixel_values,
								}
								return encoding
					elif text is not None and images is not None:
								_UpperCamelCase					:List[Any]					     =	image_features.pixel_values
								return encoding
					elif text is not None:
								return encoding
					elif visual_prompt is not None:
								_UpperCamelCase					:Optional[Any]					     =	{
								    '''conditional_pixel_values''': prompt_features.pixel_values,
								}
								return encoding
					else:
								return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE__   )					,				tensor_type=SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self					,				*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )      ->	Any:

					"""simple docstring"""



					return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self					,				*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )      ->	Tuple:

					"""simple docstring"""



					return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )
		@property
		def   _UpperCamelCase( self   )      ->	Tuple:

					"""simple docstring"""



					warnings.warn(
					    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
					return self.image_processor_class





		@property
		def   _UpperCamelCase( self   )      ->	Union[str, Any]:

					"""simple docstring"""



					warnings.warn(
					    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
					return self.image_processor

