





from math import ceil
from typing import List, Optional, Union

import numpy as np

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


_a 						=      logging.get_logger(__name__)






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						["""audio_values""", """audio_mask"""]





			def __init__(    self       ,  __lowerCAmelCase=2_0_4_8       ,  __lowerCAmelCase=1       ,  __lowerCAmelCase=[1_6, 1_6]       ,  __lowerCAmelCase=1_2_8       ,  __lowerCAmelCase=4_4_1_0_0       ,  __lowerCAmelCase=8_6       ,  __lowerCAmelCase=2_0_4_8       ,  __lowerCAmelCase=0.0       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				super().__init__(
				    feature_size=__lowerCAmelCase       ,  sampling_rate=__lowerCAmelCase       ,  padding_value=__lowerCAmelCase       ,  **__lowerCAmelCase       ,  )

				lowerCamelCase__								=		spectrogram_length
				lowerCamelCase__								=		num_channels
				lowerCamelCase__								=		patch_size
				lowerCamelCase__								=		feature_size // self.patch_size[1]
				lowerCamelCase__								=		n_fft
				lowerCamelCase__								=		sampling_rate // hop_length_to_sampling_rate
				lowerCamelCase__								=		sampling_rate
				lowerCamelCase__								=		padding_value
				lowerCamelCase__								=		mel_filter_bank(
				    num_frequency_bins=1 + n_fft // 2       ,  num_mel_filters=__lowerCAmelCase       ,  min_frequency=0.0       ,  max_frequency=2_2050.0       ,  sampling_rate=__lowerCAmelCase       ,  norm='''slaney'''       ,  mel_scale='''slaney'''       ,  ).T





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		spectrogram(
				    __lowerCAmelCase       ,  window_function(self.n_fft       ,  '''hann'''							)       ,  frame_length=self.n_fft       ,  hop_length=self.hop_length       ,  power=2.0       ,  mel_filters=self.mel_filters.T       ,  log_mel='''dB'''       ,  db_range=80.0       ,  )
				lowerCamelCase__								=		log_spec[:, :-1]
				lowerCamelCase__								=		log_spec - 20.0
				lowerCamelCase__								=		np.clip(log_spec / 40.0       ,  -2.0       ,  0.0							) + 1.0
				return log_spec





			def __call__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = True       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = False       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				if sampling_rate is not None:
					if sampling_rate != self.sampling_rate:
						raise ValueError(
						    '''This feature extractor is set to support sampling rate'''
						    F' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'
						    F' 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.'''							)

				lowerCamelCase__								=		isinstance(__lowerCAmelCase       ,  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__								=		is_batched_numpy or (
				    isinstance(__lowerCAmelCase       ,  (list, tuple)							) and (isinstance(raw_speech[0]       ,  (np.ndarray, tuple, list)							))
				)
				if is_batched:
					lowerCamelCase__								=		[np.asarray([speech]       ,  dtype=np.floataa							).T for speech in raw_speech]
				elif not is_batched and not isinstance(__lowerCAmelCase       ,  np.ndarray							):
					lowerCamelCase__								=		np.asarray(__lowerCAmelCase       ,  dtype=np.floataa							)
				elif isinstance(__lowerCAmelCase       ,  np.ndarray							) and raw_speech.dtype is np.dtype(np.floataa							):
					lowerCamelCase__								=		raw_speech.astype(np.floataa							)
				# always return batch
				if not is_batched:
					lowerCamelCase__								=		[np.asarray([raw_speech]							).T]

				# Convert audio signals to log mel spectrograms, truncate by time axis
				lowerCamelCase__								=		[
				    self._np_extract_fbank_features(waveform.squeeze()							).T[: self.spectrogram_length] for waveform in raw_speech
				]
				if isinstance(audio_features[0]       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		[np.asarray(__lowerCAmelCase       ,  dtype=np.floataa							) for feature in audio_features]

				# Create audio attention mask
				lowerCamelCase__								=		max(
				    [ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len for feature in audio_features]							)  # The maximum number of audio patches in a batch
				if return_attention_mask:
					lowerCamelCase__								=		[
					    (ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [1]
					    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [0]
					    for feature in audio_features
					]
					lowerCamelCase__								=		np.array(__lowerCAmelCase							).astype(np.floataa							)

				# convert into correct format for padding
				lowerCamelCase__								=		max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
				lowerCamelCase__								=		np.ones([len(__lowerCAmelCase							), 1, max_time_len, self.feature_size]							).astype(np.floataa							)
				lowerCamelCase__								=		padded_audio_features * self.padding_value
				for i in range(len(__lowerCAmelCase							)							):
					lowerCamelCase__								=		audio_features[i]
					lowerCamelCase__								=		feature

				# return as BatchFeature
				if return_attention_mask:
					lowerCamelCase__								=		{'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask}
				else:
					lowerCamelCase__								=		{'''audio_values''': padded_audio_features}

				lowerCamelCase__								=		BatchFeature(data=__lowerCAmelCase       ,  tensor_type=__lowerCAmelCase							)
				return encoded_inputs










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 ):

			'''simple docstring'''







			lowerCAmelCase_							     =						["""image_processor""", """tokenizer"""]
			lowerCAmelCase_							     =						"""BlipImageProcessor"""
			lowerCAmelCase_							     =						("""BertTokenizer""", """BertTokenizerFast""")





			def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		False
				super().__init__(__lowerCAmelCase       ,  __lowerCAmelCase							)
				lowerCamelCase__								=		self.image_processor





			def __call__(    self       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = True       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = 0       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = False       ,  __lowerCAmelCase = True       ,  __lowerCAmelCase = None       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				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__								=		self.tokenizer
					lowerCamelCase__								=		self.tokenizer(
					    text=__lowerCAmelCase       ,  add_special_tokens=__lowerCAmelCase       ,  padding=__lowerCAmelCase       ,  truncation=__lowerCAmelCase       ,  max_length=__lowerCAmelCase       ,  stride=__lowerCAmelCase       ,  pad_to_multiple_of=__lowerCAmelCase       ,  return_attention_mask=__lowerCAmelCase       ,  return_overflowing_tokens=__lowerCAmelCase       ,  return_special_tokens_mask=__lowerCAmelCase       ,  return_offsets_mapping=__lowerCAmelCase       ,  return_token_type_ids=__lowerCAmelCase       ,  return_length=__lowerCAmelCase       ,  verbose=__lowerCAmelCase       ,  return_tensors=__lowerCAmelCase       ,  **__lowerCAmelCase       ,  )
					return text_encoding

				# add pixel_values
				lowerCamelCase__								=		self.image_processor(__lowerCAmelCase       ,  return_tensors=__lowerCAmelCase							)

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

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

				return encoding_image_processor





			def 		__lowerCamelCase				(    self       ,  *__lowerCAmelCase       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				return self.tokenizer.batch_decode(*__lowerCAmelCase       ,  **__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self       ,  *__lowerCAmelCase       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				return self.tokenizer.decode(*__lowerCAmelCase       ,  **__lowerCAmelCase							)





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.tokenizer.model_input_names
				lowerCamelCase__								=		self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)



