






'''simple docstring'''







import warnings
from typing import Any, Dict, List, Optional, Union

import numpy as np

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


UpperCamelCase    :      List[Any]  			=		logging.get_logger(__name__)




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					['input_values', 'attention_mask']






		def __init__(      self   ,_lowerCAmelCase = 1   ,_lowerCAmelCase = 1_60_00   ,_lowerCAmelCase = 0.0   ,_lowerCAmelCase = False   ,_lowerCAmelCase = 80   ,_lowerCAmelCase = 16   ,_lowerCAmelCase = 64   ,_lowerCAmelCase = "hann_window"   ,_lowerCAmelCase = 1.0   ,_lowerCAmelCase = 80   ,_lowerCAmelCase = 76_00   ,_lowerCAmelCase = 1E-10   ,_lowerCAmelCase = 2   ,_lowerCAmelCase = True   ,**_lowerCAmelCase   ,):
								super().__init__(feature_size=_lowerCAmelCase   ,sampling_rate=_lowerCAmelCase   ,padding_value=_lowerCAmelCase   ,**_lowerCAmelCase       )
								lowerCamelCase__         =  do_normalize
								lowerCamelCase__         =  return_attention_mask

								lowerCamelCase__         =  num_mel_bins
								lowerCamelCase__         =  hop_length
								lowerCamelCase__         =  win_length
								lowerCamelCase__         =  win_function
								lowerCamelCase__         =  frame_signal_scale
								lowerCamelCase__         =  fmin
								lowerCamelCase__         =  fmax
								lowerCamelCase__         =  mel_floor
								lowerCamelCase__         =  reduction_factor

								lowerCamelCase__         =  win_length * sampling_rate // 10_00
								lowerCamelCase__         =  hop_length * sampling_rate // 10_00
								lowerCamelCase__         =  optimal_fft_length(self.sample_size       )
								lowerCamelCase__         =  (self.n_fft // 2) + 1

								lowerCamelCase__         =  window_function(window_length=self.sample_size   ,name=self.win_function   ,periodic=_lowerCAmelCase       )

								lowerCamelCase__         =  mel_filter_bank(
								    num_frequency_bins=self.n_freqs   ,num_mel_filters=self.num_mel_bins   ,min_frequency=self.fmin   ,max_frequency=self.fmax   ,sampling_rate=self.sampling_rate   ,norm="""slaney"""   ,mel_scale="""slaney"""   ,)

								if frame_signal_scale != 1.0:
														warnings.warn(
														    """The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers"""   ,_lowerCAmelCase   ,)
								if reduction_factor != 2.0:
														warnings.warn(
														    """The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers"""   ,_lowerCAmelCase   ,)






		@staticmethod
		# Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
		def 			UpperCamelCase_					(      _lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase = 0.0       ):
								if attention_mask is not None:
														lowerCamelCase__         =  np.array(_lowerCAmelCase   ,np.intaa       )
														lowerCamelCase__         =  []

														for vector, length in zip(_lowerCAmelCase   ,attention_mask.sum(-1       )       ):
																				lowerCamelCase__         =  (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7       )
																				if length < normed_slice.shape[0]:
																										lowerCamelCase__         =  padding_value

																				normed_input_values.append(_lowerCAmelCase       )
								else:
														lowerCamelCase__         =  [(x - x.mean()) / np.sqrt(x.var() + 1E-7       ) for x in input_values]

								return normed_input_values






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,):
								lowerCamelCase__         =  spectrogram(
								    _lowerCAmelCase   ,window=self.window   ,frame_length=self.sample_size   ,hop_length=self.sample_stride   ,fft_length=self.n_fft   ,mel_filters=self.mel_filters   ,mel_floor=self.mel_floor   ,log_mel="""log10"""   ,)
								return log_mel_spec.T






		def __call__(      self   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,**_lowerCAmelCase   ,):
								if audio is None and audio_target is None:
														raise ValueError("""You must provide either `audio` or `audio_target` values."""       )

								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 audio 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."""       )

								if audio is not None:
														lowerCamelCase__         =  self._process_audio(
														    _lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,**_lowerCAmelCase   ,)
								else:
														lowerCamelCase__         =  None

								if audio_target is not None:
														lowerCamelCase__         =  self._process_audio(
														    _lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,**_lowerCAmelCase   ,)

														if inputs is None:
																				return inputs_target
														else:
																				lowerCamelCase__         =  inputs_target["""input_values"""]
																				lowerCamelCase__         =  inputs_target.get("""attention_mask"""       )
																				if decoder_attention_mask is not None:
																										lowerCamelCase__         =  decoder_attention_mask

								return inputs






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = False   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,**_lowerCAmelCase   ,):
								lowerCamelCase__         =  isinstance(_lowerCAmelCase   ,np.ndarray       ) and len(speech.shape       ) > 1
								if is_batched_numpy and len(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(speech[0]   ,(np.ndarray, tuple, list)       ))
								)

								if is_batched:
														lowerCamelCase__         =  [np.asarray(_lowerCAmelCase   ,dtype=np.floataa       ) for speech in speech]
								elif not is_batched and not isinstance(_lowerCAmelCase   ,np.ndarray       ):
														lowerCamelCase__         =  np.asarray(_lowerCAmelCase   ,dtype=np.floataa       )
								elif isinstance(_lowerCAmelCase   ,np.ndarray       ) and speech.dtype is np.dtype(np.floataa       ):
														lowerCamelCase__         =  speech.astype(np.floataa       )

								# always return batch
								if not is_batched:
														lowerCamelCase__         =  [speech]

								# needed to make pad() work on spectrogram inputs
								lowerCamelCase__         =  self.feature_size

								# convert into correct format for padding
								if is_target:
														lowerCamelCase__         =  [self._extract_mel_features(_lowerCAmelCase       ) for waveform in speech]
														lowerCamelCase__         =  BatchFeature({"""input_values""": features}       )
														lowerCamelCase__         =  self.num_mel_bins
								else:
														lowerCamelCase__         =  BatchFeature({"""input_values""": speech}       )

								lowerCamelCase__         =  self.pad(
								    _lowerCAmelCase   ,padding=_lowerCAmelCase   ,max_length=_lowerCAmelCase   ,truncation=_lowerCAmelCase   ,pad_to_multiple_of=_lowerCAmelCase   ,return_attention_mask=_lowerCAmelCase   ,**_lowerCAmelCase   ,)

								lowerCamelCase__         =  feature_size_hack

								# convert input values to correct format
								lowerCamelCase__         =  padded_inputs["""input_values"""]
								if not isinstance(input_values[0]   ,np.ndarray       ):
														lowerCamelCase__         =  [np.asarray(_lowerCAmelCase   ,dtype=np.floataa       ) for array in input_values]
								elif (
								    not isinstance(_lowerCAmelCase   ,np.ndarray       )
								    and isinstance(input_values[0]   ,np.ndarray       )
								    and input_values[0].dtype is np.dtype(np.floataa       )
								):
														lowerCamelCase__         =  [array.astype(np.floataa       ) for array in input_values]
								elif isinstance(_lowerCAmelCase   ,np.ndarray       ) and input_values.dtype is np.dtype(np.floataa       ):
														lowerCamelCase__         =  input_values.astype(np.floataa       )

								# convert attention_mask to correct format
								lowerCamelCase__         =  padded_inputs.get("""attention_mask"""       )
								if attention_mask is not None:
														lowerCamelCase__         =  [np.asarray(_lowerCAmelCase   ,dtype=np.intaa       ) for array in attention_mask]

								# zero-mean and unit-variance normalization
								if not is_target and self.do_normalize:
														lowerCamelCase__         =  (
														    attention_mask
														    if self._get_padding_strategies(_lowerCAmelCase   ,max_length=_lowerCAmelCase       ) is not PaddingStrategy.DO_NOT_PAD
														    else None
														)
														lowerCamelCase__         =  self.zero_mean_unit_var_norm(
														    padded_inputs["""input_values"""]   ,attention_mask=_lowerCAmelCase   ,padding_value=self.padding_value       )

								if return_tensors is not None:
														lowerCamelCase__         =  padded_inputs.convert_to_tensors(_lowerCAmelCase       )

								return padded_inputs






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  super().to_dict()

								# Don't serialize these as they are derived from the other properties.
								lowerCamelCase__         =  ["""window""", """mel_filters""", """sample_size""", """sample_stride""", """n_fft""", """n_freqs"""]
								for name in names:
														if name in output:
																				del output[name]

								return output









'''simple docstring'''







import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast




@require_vision
class 		UpperCamelCase__    (unittest.TestCase			):




		'''simple docstring'''





		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  tempfile.mkdtemp()

								lowerCamelCase__         =  BlipImageProcessor()
								lowerCamelCase__         =  BertTokenizer.from_pretrained("""hf-internal-testing/tiny-random-BertModel"""       )

								lowerCamelCase__         =  BlipProcessor(_lowerCAmelCase   ,_lowerCAmelCase       )

								processor.save_pretrained(self.tmpdirname       )






		def 			UpperCamelCase_					(      self   ,**_lowerCAmelCase       ):
								return AutoProcessor.from_pretrained(self.tmpdirname   ,**_lowerCAmelCase       ).tokenizer






		def 			UpperCamelCase_					(      self   ,**_lowerCAmelCase       ):
								return AutoProcessor.from_pretrained(self.tmpdirname   ,**_lowerCAmelCase       ).image_processor






		def 			UpperCamelCase_					(      self       ):
								shutil.rmtree(self.tmpdirname       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  [np.random.randint(2_55   ,size=(3, 30, 4_00)   ,dtype=np.uinta       )]

								lowerCamelCase__         =  [Image.fromarray(np.moveaxis(_lowerCAmelCase   ,0   ,-1       )       ) for x in image_inputs]

								return image_inputs






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  BlipProcessor(tokenizer=self.get_tokenizer()   ,image_processor=self.get_image_processor()       )
								processor.save_pretrained(self.tmpdirname       )

								lowerCamelCase__         =  self.get_tokenizer(bos_token="""(BOS)"""   ,eos_token="""(EOS)"""       )
								lowerCamelCase__         =  self.get_image_processor(do_normalize=_lowerCAmelCase   ,padding_value=1.0       )

								lowerCamelCase__         =  BlipProcessor.from_pretrained(
								    self.tmpdirname   ,bos_token="""(BOS)"""   ,eos_token="""(EOS)"""   ,do_normalize=_lowerCAmelCase   ,padding_value=1.0       )

								self.assertEqual(processor.tokenizer.get_vocab()   ,tokenizer_add_kwargs.get_vocab()       )
								self.assertIsInstance(processor.tokenizer   ,_lowerCAmelCase       )

								self.assertEqual(processor.image_processor.to_json_string()   ,image_processor_add_kwargs.to_json_string()       )
								self.assertIsInstance(processor.image_processor   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.get_image_processor()
								lowerCamelCase__         =  self.get_tokenizer()

								lowerCamelCase__         =  BlipProcessor(tokenizer=_lowerCAmelCase   ,image_processor=_lowerCAmelCase       )

								lowerCamelCase__         =  self.prepare_image_inputs()

								lowerCamelCase__         =  image_processor(_lowerCAmelCase   ,return_tensors="""np"""       )
								lowerCamelCase__         =  processor(images=_lowerCAmelCase   ,return_tensors="""np"""       )

								for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum()   ,input_processor[key].sum()   ,delta=1E-2       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.get_image_processor()
								lowerCamelCase__         =  self.get_tokenizer()

								lowerCamelCase__         =  BlipProcessor(tokenizer=_lowerCAmelCase   ,image_processor=_lowerCAmelCase       )

								lowerCamelCase__         =  """lower newer"""

								lowerCamelCase__         =  processor(text=_lowerCAmelCase       )

								lowerCamelCase__         =  tokenizer(_lowerCAmelCase   ,return_token_type_ids=_lowerCAmelCase       )

								for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key]   ,encoded_processor[key]       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.get_image_processor()
								lowerCamelCase__         =  self.get_tokenizer()

								lowerCamelCase__         =  BlipProcessor(tokenizer=_lowerCAmelCase   ,image_processor=_lowerCAmelCase       )

								lowerCamelCase__         =  """lower newer"""
								lowerCamelCase__         =  self.prepare_image_inputs()

								lowerCamelCase__         =  processor(text=_lowerCAmelCase   ,images=_lowerCAmelCase       )

								self.assertListEqual(list(inputs.keys()       )   ,["""pixel_values""", """input_ids""", """attention_mask"""]       )

								# test if it raises when no input is passed
								with pytest.raises(_lowerCAmelCase       ):
														processor()






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.get_image_processor()
								lowerCamelCase__         =  self.get_tokenizer()

								lowerCamelCase__         =  BlipProcessor(tokenizer=_lowerCAmelCase   ,image_processor=_lowerCAmelCase       )

								lowerCamelCase__         =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								lowerCamelCase__         =  processor.batch_decode(_lowerCAmelCase       )
								lowerCamelCase__         =  tokenizer.batch_decode(_lowerCAmelCase       )

								self.assertListEqual(_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.get_image_processor()
								lowerCamelCase__         =  self.get_tokenizer()

								lowerCamelCase__         =  BlipProcessor(tokenizer=_lowerCAmelCase   ,image_processor=_lowerCAmelCase       )

								lowerCamelCase__         =  """lower newer"""
								lowerCamelCase__         =  self.prepare_image_inputs()

								lowerCamelCase__         =  processor(text=_lowerCAmelCase   ,images=_lowerCAmelCase       )

								# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
								self.assertListEqual(list(inputs.keys()       )   ,["""pixel_values""", """input_ids""", """attention_mask"""]       )

