





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


__lowerCamelCase :  Dict										=  logging.get_logger(__name__)



class       A__     ( __snake_case    ):
      _UpperCAmelCase			:Tuple							=     ['audio_values', 'audio_mask']





      def __init__(				self ,							A_=2048 ,							A_=1 ,							A_=[16, 16] ,							A_=128 ,							A_=4_4100 ,							A_=86 ,							A_=2048 ,							A_=0.0 ,							**A_ ,							):
       '''simple docstring'''




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

       UpperCamelCase			:      Optional[int] 			=     spectrogram_length
       UpperCamelCase			:      Dict 			=     num_channels
       UpperCamelCase			:      Optional[Any] 			=     patch_size
       UpperCamelCase			:      str 			=     feature_size // self.patch_size[1]
       UpperCamelCase			:      List[str] 			=     n_fft
       UpperCamelCase			:      int 			=     sampling_rate // hop_length_to_sampling_rate
       UpperCamelCase			:      Optional[int] 			=     sampling_rate
       UpperCamelCase			:      int 			=     padding_value
       UpperCamelCase			:      str 			=     mel_filter_bank(
           num_frequency_bins=1 + n_fft // 2 ,							num_mel_filters=A_ ,							min_frequency=0.0 ,							max_frequency=2_20_50.0 ,							sampling_rate=A_ ,							norm="slaney" ,							mel_scale="slaney" ,							).T





      def  __UpperCamelCase(				self ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     spectrogram(
           A_ ,							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 ,							)
       UpperCamelCase			:      List[Any] 			=     log_spec[:, :-1]
       UpperCamelCase			:      Optional[int] 			=     log_spec - 20.0
       UpperCamelCase			:      str 			=     np.clip(log_spec / 40.0 ,							-2.0 ,							0.0     ) + 1.0
       return log_spec





      def __call__(				self ,							A_ ,							A_ = None ,							A_ = True ,							A_ = None ,							A_ = False ,							A_ = False ,							**A_ ,							):
       '''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."     )

       UpperCamelCase			:      Optional[int] 			=     isinstance(A_ ,							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			:      Union[str, Any] 			=     is_batched_numpy or (
           isinstance(A_ ,							(list, tuple)     ) and (isinstance(raw_speech[0] ,							(np.ndarray, tuple, list)     ))
       )
       if is_batched:
        UpperCamelCase			:      int 			=     [np.asarray([speech] ,							dtype=np.floataa     ).T for speech in raw_speech]
       elif not is_batched and not isinstance(A_ ,							np.ndarray     ):
        UpperCamelCase			:      str 			=     np.asarray(A_ ,							dtype=np.floataa     )
       elif isinstance(A_ ,							np.ndarray     ) and raw_speech.dtype is np.dtype(np.floataa     ):
        UpperCamelCase			:      List[Any] 			=     raw_speech.astype(np.floataa     )
       # always return batch
       if not is_batched:
        UpperCamelCase			:      Tuple 			=     [np.asarray([raw_speech]     ).T]

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

       # Create audio attention mask
       UpperCamelCase			:      List[str] 			=     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:
        UpperCamelCase			:      str 			=     [
            (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
        ]
        UpperCamelCase			:      Tuple 			=     np.array(A_     ).astype(np.floataa     )

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

       # return as BatchFeature
       if return_attention_mask:
        UpperCamelCase			:      Optional[Any] 			=     {"audio_values": padded_audio_features, "audio_mask": audio_mask}
       else:
        UpperCamelCase			:      int 			=     {"audio_values": padded_audio_features}

       UpperCamelCase			:      Any 			=     BatchFeature(data=A_ ,							tensor_type=A_     )
       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__     ( __snake_case    ):
      _UpperCAmelCase			:Optional[int]							=     ['image_processor', 'tokenizer']
      _UpperCAmelCase			:Tuple							=     'BlipImageProcessor'
      _UpperCAmelCase			:Optional[int]							=     'AutoTokenizer'





      def __init__(				self ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      str 			=     False
       super().__init__(A_ ,							A_     )
       UpperCamelCase			:      str 			=     self.image_processor





      def __call__(				self ,							A_ = None ,							A_ = None ,							A_ = True ,							A_ = False ,							A_ = None ,							A_ = None ,							A_ = 0 ,							A_ = None ,							A_ = None ,							A_ = False ,							A_ = False ,							A_ = False ,							A_ = False ,							A_ = False ,							A_ = True ,							A_ = None ,							**A_ ,							):
       '''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:
        UpperCamelCase			:      int 			=     self.tokenizer
        UpperCamelCase			:      Optional[int] 			=     self.tokenizer(
            text=A_ ,							add_special_tokens=A_ ,							padding=A_ ,							truncation=A_ ,							max_length=A_ ,							stride=A_ ,							pad_to_multiple_of=A_ ,							return_attention_mask=A_ ,							return_overflowing_tokens=A_ ,							return_special_tokens_mask=A_ ,							return_offsets_mapping=A_ ,							return_token_type_ids=A_ ,							return_length=A_ ,							verbose=A_ ,							return_tensors=A_ ,							**A_ ,							)
        return text_encoding

       # add pixel_values
       UpperCamelCase			:      int 			=     self.image_processor(A_ ,							return_tensors=A_     )

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

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

       return encoding_image_processor





      def  __UpperCamelCase(				self ,							*A_ ,							**A_     ):
       '''simple docstring'''




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





      def  __UpperCamelCase(				self ,							*A_ ,							**A_     ):
       '''simple docstring'''




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





      @property
      # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.tokenizer.model_input_names
       UpperCamelCase			:      int 			=     self.image_processor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )
