
"""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__ (			_lowerCamelCase):
		A_		: List[str]            =			['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 ,	):
			super().__init__(feature_size=_SCREAMING_SNAKE_CASE ,	sampling_rate=_SCREAMING_SNAKE_CASE ,	padding_value=_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				)
			__lowerCAmelCase    :		List[Any]		    =   num_mel_bins
			__lowerCAmelCase    :		List[Any]		    =   do_ceptral_normalize
			__lowerCAmelCase    :		Any		    =   normalize_means
			__lowerCAmelCase    :		List[Any]		    =   normalize_vars
			__lowerCAmelCase    :		List[str]		    =   True

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	):
			__lowerCAmelCase    :		str		    =   waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
			__lowerCAmelCase    :		List[str]		    =   torch.from_numpy(_SCREAMING_SNAKE_CASE				).unsqueeze(0				)
			__lowerCAmelCase    :		Optional[Any]		    =   ta_kaldi.fbank(_SCREAMING_SNAKE_CASE ,	num_mel_bins=self.num_mel_bins ,	sample_frequency=self.sampling_rate				)
			return features.numpy()

		@staticmethod
		def 							__lowerCamelCase							(   _SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = 0.0 ,	):
			# make sure we normalize float32 arrays
			if normalize_means:
				__lowerCAmelCase    :		Any		    =   x[:input_length].mean(axis=0				)
				__lowerCAmelCase    :		int		    =   np.subtract(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)
			if normalize_vars:
				__lowerCAmelCase    :		Any		    =   x[:input_length].std(axis=0				)
				__lowerCAmelCase    :		Union[str, Any]		    =   np.divide(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)

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

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

			return x

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE = None				):
			__lowerCAmelCase    :		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 ,	):

			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    :		List[str]		    =   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    :		Any		    =   is_batched_numpy or (
			    isinstance(_SCREAMING_SNAKE_CASE ,	(list, tuple)				) and (isinstance(raw_speech[0] ,	(np.ndarray, tuple, list)				))
			)

			if is_batched:
				__lowerCAmelCase    :		Dict		    =   [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    :		List[str]		    =   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    :		str		    =   raw_speech.astype(np.floataa				)

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

			# extract fbank features
			__lowerCAmelCase    :		Any		    =   [self._extract_fbank_features(_SCREAMING_SNAKE_CASE				) for waveform in raw_speech]

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

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

			__lowerCAmelCase    :		str		    =   padded_inputs.get('attention_mask'				)
			if attention_mask is not None:
				__lowerCAmelCase    :		Any		    =   [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    :		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
				)
				__lowerCAmelCase    :		Tuple		    =   self.normalize(
				    padded_inputs['input_features'] ,	attention_mask=_SCREAMING_SNAKE_CASE				)

			if return_tensors is not None:
				__lowerCAmelCase    :		List[Any]		    =   padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE				)

			return padded_inputs

"""simple docstring"""







from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 				A__ (			_lowerCamelCase):
		A_		: Any            =			['image_processor', 'tokenizer']
		A_		: Optional[Any]            =			'BlipImageProcessor'
		A_		: int            =			'AutoTokenizer'

		def __init__(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		Union[str, Any]		    =   False
			super().__init__(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)
			__lowerCAmelCase    :		List[Any]		    =   self.image_processor

		def __call__(   self ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = 0 ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = None ,	**_SCREAMING_SNAKE_CASE ,	):
			if images is None and text is None:
				raise ValueError('You have to specify either images or text.'				)

			# Get only text
			if images is None:
				__lowerCAmelCase    :		Optional[int]		    =   self.tokenizer
				__lowerCAmelCase    :		List[str]		    =   self.tokenizer(
				    text=_SCREAMING_SNAKE_CASE ,	add_special_tokens=_SCREAMING_SNAKE_CASE ,	padding=_SCREAMING_SNAKE_CASE ,	truncation=_SCREAMING_SNAKE_CASE ,	max_length=_SCREAMING_SNAKE_CASE ,	stride=_SCREAMING_SNAKE_CASE ,	pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,	return_attention_mask=_SCREAMING_SNAKE_CASE ,	return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,	return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,	return_offsets_mapping=_SCREAMING_SNAKE_CASE ,	return_token_type_ids=_SCREAMING_SNAKE_CASE ,	return_length=_SCREAMING_SNAKE_CASE ,	verbose=_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE ,	)
				return text_encoding

			# add pixel_values
			__lowerCAmelCase    :		List[Any]		    =   self.image_processor(_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE				)

			if text is not None:
				__lowerCAmelCase    :		Optional[int]		    =   self.tokenizer(
				    text=_SCREAMING_SNAKE_CASE ,	add_special_tokens=_SCREAMING_SNAKE_CASE ,	padding=_SCREAMING_SNAKE_CASE ,	truncation=_SCREAMING_SNAKE_CASE ,	max_length=_SCREAMING_SNAKE_CASE ,	stride=_SCREAMING_SNAKE_CASE ,	pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,	return_attention_mask=_SCREAMING_SNAKE_CASE ,	return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,	return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,	return_offsets_mapping=_SCREAMING_SNAKE_CASE ,	return_token_type_ids=_SCREAMING_SNAKE_CASE ,	return_length=_SCREAMING_SNAKE_CASE ,	verbose=_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE ,	)
			else:
				__lowerCAmelCase    :		int		    =   None

			if text_encoding is not None:
				encoding_image_processor.update(_SCREAMING_SNAKE_CASE				)

			return encoding_image_processor

		def 							__lowerCamelCase							(   self ,	*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				):
			return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self ,	*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				):
			return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				)







		@property
		# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		int		    =   self.tokenizer.model_input_names
			__lowerCAmelCase    :		Union[str, Any]		    =   self.image_processor.model_input_names
			return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)