




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

import numpy as np
import torch

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


lowerCamelCase    :		Tuple			   =       logging.get_logger(__name__)







class   A(						_UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['input_features', 'is_longer']

 def __init__(    self :  Optional[Any]						,							A_ :  Optional[int]=64						,							A_ :  Dict=48000						,							A_ :  int=480						,							A_ :  Any=10						,							A_ :  List[str]=1024						,							A_ :  List[Any]=0.0						,							A_ :  Optional[int]=False						,							A_ :  float = 0						,							A_ :  float = 14000						,							A_ :  int = None						,							A_ :  str = "fusion"						,							A_ :  str = "repeatpad"						,							**A_ :  Optional[Any]						,							)       ->     Optional[int]:

       """simple docstring"""






       super().__init__(
           feature_size=_UpperCAmelCase						,							sampling_rate=_UpperCAmelCase						,							padding_value=_UpperCAmelCase						,							return_attention_mask=_UpperCAmelCase						,							**_UpperCAmelCase						,							)
       lowerCamelCase_							=   top_db
       lowerCamelCase_							=   truncation
       lowerCamelCase_							=   padding
       lowerCamelCase_							=   fft_window_size
       lowerCamelCase_							=   (fft_window_size >> 1) + 1
       lowerCamelCase_							=   hop_length
       lowerCamelCase_							=   max_length_s
       lowerCamelCase_							=   max_length_s * sampling_rate
       lowerCamelCase_							=   sampling_rate
       lowerCamelCase_							=   frequency_min
       lowerCamelCase_							=   frequency_max
       lowerCamelCase_							=   mel_filter_bank(
           num_frequency_bins=self.nb_frequency_bins						,							num_mel_filters=_UpperCAmelCase						,							min_frequency=_UpperCAmelCase						,							max_frequency=_UpperCAmelCase						,							sampling_rate=_UpperCAmelCase						,							norm=_UpperCAmelCase						,							mel_scale='htk'						,							)
       lowerCamelCase_							=   mel_filter_bank(
           num_frequency_bins=self.nb_frequency_bins						,							num_mel_filters=_UpperCAmelCase						,							min_frequency=_UpperCAmelCase						,							max_frequency=_UpperCAmelCase						,							sampling_rate=_UpperCAmelCase						,							norm='slaney'						,							mel_scale='slaney'						,							)

 def     a__			(    self :  Optional[int]  )       ->     List[Any]:

       """simple docstring"""






       lowerCamelCase_							=   copy.deepcopy(self.__dict__  )
       lowerCamelCase_							=   self.__class__.__name__
       if "mel_filters" in output:
             del output["mel_filters"]
       if "mel_filters_slaney" in output:
             del output["mel_filters_slaney"]
       return output

 def     a__			(    self :  Tuple						,							A_ :  np.array						,							A_ :  Optional[np.array] = None  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   spectrogram(
           _UpperCAmelCase						,							window_function(self.fft_window_size						,							'hann'  )						,							frame_length=self.fft_window_size						,							hop_length=self.hop_length						,							power=2.0						,							mel_filters=_UpperCAmelCase						,							log_mel='dB'						,							)
       return log_mel_spectrogram.T

 def     a__			(    self :  Optional[Any]						,							A_ :  List[str]						,							A_ :  Union[str, Any]						,							A_ :  Dict  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_							=   np.array_split(list(range(0						,							total_frames - chunk_frames + 1  )  )						,							3  )
       if len(ranges[1]  ) == 0:
             # if the audio is too short, we just use the first chunk
             lowerCamelCase_							=   [0]
       if len(ranges[2]  ) == 0:
             # if the audio is too short, we just use the first chunk
             lowerCamelCase_							=   [0]
       # randomly choose index for each part
       lowerCamelCase_							=   np.random.choice(ranges[0]  )
       lowerCamelCase_							=   np.random.choice(ranges[1]  )
       lowerCamelCase_							=   np.random.choice(ranges[2]  )

       lowerCamelCase_							=   mel[idx_front : idx_front + chunk_frames, :]
       lowerCamelCase_							=   mel[idx_middle : idx_middle + chunk_frames, :]
       lowerCamelCase_							=   mel[idx_back : idx_back + chunk_frames, :]

       lowerCamelCase_							=   torch.tensor(mel[None, None, :]  )
       lowerCamelCase_							=   torch.nn.functional.interpolate(
           _UpperCAmelCase						,							size=[chunk_frames, 64]						,							mode='bilinear'						,							align_corners=_UpperCAmelCase  )
       lowerCamelCase_							=   mel_shrink[0][0].numpy()
       lowerCamelCase_							=   np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]						,							axis=0  )
       return mel_fusion

 def     a__			(    self :  Union[str, Any]						,							A_ :  np.array						,							A_ :  Optional[int]						,							A_ :  str						,							A_ :  str  )       ->     str:

       """simple docstring"""






       if waveform.shape[0] > max_length:
             if truncation == "rand_trunc":
                   lowerCamelCase_							=   True
                   # random crop to max_length (for compatibility) -> this should be handled by self.pad
                   lowerCamelCase_							=   len(_UpperCAmelCase  ) - max_length
                   lowerCamelCase_							=   np.random.randint(0						,							overflow + 1  )
                   lowerCamelCase_							=   waveform[idx : idx + max_length]
                   lowerCamelCase_							=   self._np_extract_fbank_features(_UpperCAmelCase						,							self.mel_filters_slaney  )[None, :]
             elif truncation == "fusion":
                   lowerCamelCase_							=   self._np_extract_fbank_features(_UpperCAmelCase						,							self.mel_filters  )
                   lowerCamelCase_							=   max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
                   lowerCamelCase_							=   mel.shape[0]
                   if chunk_frames == total_frames:
                         # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
                         # In this case, we just use the whole audio.
                         lowerCamelCase_							=   np.stack([mel, mel, mel, mel]						,							axis=0  )
                         lowerCamelCase_							=   False
                   else:
                         lowerCamelCase_							=   self._random_mel_fusion(_UpperCAmelCase						,							_UpperCAmelCase						,							_UpperCAmelCase  )
                         lowerCamelCase_							=   True
             else:
                   raise NotImplementedError(f"""data_truncating {truncation} not implemented"""  )

       else:
             lowerCamelCase_							=   False
             # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
             if waveform.shape[0] < max_length:
                   if padding == "repeat":
                         lowerCamelCase_							=   int(max_length / len(_UpperCAmelCase  )  )
                         lowerCamelCase_							=   np.stack(np.tile(_UpperCAmelCase						,							n_repeat + 1  )  )[:max_length]
                   if padding == "repeatpad":
                         lowerCamelCase_							=   int(max_length / len(_UpperCAmelCase  )  )
                         lowerCamelCase_							=   np.stack(np.tile(_UpperCAmelCase						,							_UpperCAmelCase  )  )
                   lowerCamelCase_							=   np.pad(_UpperCAmelCase						,							(0, max_length - waveform.shape[0])						,							mode='constant'						,							constant_values=0  )

             if truncation == "fusion":
                   lowerCamelCase_							=   self._np_extract_fbank_features(_UpperCAmelCase						,							self.mel_filters  )
                   lowerCamelCase_							=   np.stack([input_mel, input_mel, input_mel, input_mel]						,							axis=0  )
             else:
                   lowerCamelCase_							=   self._np_extract_fbank_features(_UpperCAmelCase						,							self.mel_filters_slaney  )[None, :]

       return input_mel, longer





 def __call__(    self :  str						,							A_ :  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]						,							A_ :  str = None						,							A_ :  Optional[str] = None						,							A_ :  Optional[int] = None						,							A_ :  Optional[int] = None						,							A_ :  Optional[Union[str, TensorType]] = None						,							**A_ :  Tuple						,							)       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   truncation if truncation is not None else self.truncation
       lowerCamelCase_							=   padding if padding else self.padding

       if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                   raise ValueError(
                       f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
                       f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
                       f""" was sampled 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(_UpperCAmelCase						,							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(_UpperCAmelCase						,							(list, tuple)  ) and (isinstance(raw_speech[0]						,							(np.ndarray, tuple, list)  ))
       )

       if is_batched:
             lowerCamelCase_							=   [np.asarray(_UpperCAmelCase						,							dtype=np.floataa  ) for speech in raw_speech]
       elif not is_batched and not isinstance(_UpperCAmelCase						,							np.ndarray  ):
             lowerCamelCase_							=   np.asarray(_UpperCAmelCase						,							dtype=np.floataa  )
       elif isinstance(_UpperCAmelCase						,							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(_UpperCAmelCase  )]

       # convert to mel spectrogram, truncate and pad if needed.
       lowerCamelCase_							=   [
           self._get_input_mel(_UpperCAmelCase						,							max_length if max_length else self.nb_max_samples						,							_UpperCAmelCase						,							_UpperCAmelCase  )
           for waveform in raw_speech
       ]

       lowerCamelCase_							=   []
       lowerCamelCase_							=   []
       for mel, longer in padded_inputs:
             input_mel.append(_UpperCAmelCase  )
             is_longer.append(_UpperCAmelCase  )

       if truncation == "fusion" and sum(_UpperCAmelCase  ) == 0:
             # if no audio is longer than 10s, then randomly select one audio to be longer
             lowerCamelCase_							=   np.random.randint(0						,							len(_UpperCAmelCase  )  )
             lowerCamelCase_							=   True

       if isinstance(input_mel[0]						,							_UpperCAmelCase  ):
             lowerCamelCase_							=   [np.asarray(_UpperCAmelCase						,							dtype=np.floataa  ) for feature in input_mel]

       # is_longer is a list of bool
       lowerCamelCase_							=   [[longer] for longer in is_longer]

       lowerCamelCase_							=   {'input_features': input_mel, 'is_longer': is_longer}
       lowerCamelCase_							=   BatchFeature(_UpperCAmelCase  )

       if return_tensors is not None:
             lowerCamelCase_							=   input_features.convert_to_tensors(_UpperCAmelCase  )

       return input_features






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import ChineseCLIPImageProcessor







class   A(						unittest.TestCase       ):



 '''simple docstring'''


 def __init__(    self :  str						,							A_ :  int						,							A_ :  Any=7						,							A_ :  Tuple=3						,							A_ :  Union[str, Any]=18						,							A_ :  Tuple=30						,							A_ :  Union[str, Any]=400						,							A_ :  Optional[int]=True						,							A_ :  List[Any]=None						,							A_ :  Dict=True						,							A_ :  Union[str, Any]=None						,							A_ :  Optional[int]=True						,							A_ :  str=[0.48145466, 0.4578275, 0.40821073]						,							A_ :  Tuple=[0.26862954, 0.26130258, 0.27577711]						,							A_ :  Any=True						,							)       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   size if size is not None else {'height': 224, 'width': 224}
       lowerCamelCase_							=   crop_size if crop_size is not None else {'height': 18, 'width': 18}
       lowerCamelCase_							=   parent
       lowerCamelCase_							=   batch_size
       lowerCamelCase_							=   num_channels
       lowerCamelCase_							=   image_size
       lowerCamelCase_							=   min_resolution
       lowerCamelCase_							=   max_resolution
       lowerCamelCase_							=   do_resize
       lowerCamelCase_							=   size
       lowerCamelCase_							=   do_center_crop
       lowerCamelCase_							=   crop_size
       lowerCamelCase_							=   do_normalize
       lowerCamelCase_							=   image_mean
       lowerCamelCase_							=   image_std
       lowerCamelCase_							=   do_convert_rgb

 def     a__			(    self :  Optional[int]  )       ->     Tuple:

       """simple docstring"""






       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_center_crop": self.do_center_crop,
           "crop_size": self.crop_size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_convert_rgb": self.do_convert_rgb,
       }





 def     a__			(    self :  Any						,							A_ :  Any=False						,							A_ :  Dict=False						,							A_ :  str=False  )       ->     Union[str, Any]:

       """simple docstring"""






       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       if equal_resolution:
             lowerCamelCase_							=   []
             for i in range(self.batch_size  ):
                   image_inputs.append(
                       np.random.randint(
                           255						,							size=(self.num_channels, self.max_resolution, self.max_resolution)						,							dtype=np.uinta  )  )
       else:
             lowerCamelCase_							=   []
             for i in range(self.batch_size  ):
                   lowerCamelCase_					,    lowerCamelCase_							=   np.random.choice(np.arange(self.min_resolution						,							self.max_resolution  )						,							2  )
                   image_inputs.append(np.random.randint(255						,							size=(self.num_channels, width, height)						,							dtype=np.uinta  )  )

       if not numpify and not torchify:
             # PIL expects the channel dimension as last dimension
             lowerCamelCase_							=   [Image.fromarray(np.moveaxis(A_						,							0						,							-1  )  ) for x in image_inputs]

       if torchify:
             lowerCamelCase_							=   [torch.from_numpy(A_  ) for x in image_inputs]

       return image_inputs







@require_torch
@require_vision
class   A(						UpperCamelCase							,				unittest.TestCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						ChineseCLIPImageProcessor if is_vision_available() else None

 def     a__			(    self :  int  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   ChineseCLIPImageProcessingTester(self						,							do_center_crop=A_  )

 @property
 def     a__			(    self :  str  )       ->     List[Any]:

       """simple docstring"""






       return self.image_processor_tester.prepare_image_processor_dict()

 def     a__			(    self :  int  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       self.assertTrue(hasattr(A_						,							'do_resize'  )  )
       self.assertTrue(hasattr(A_						,							'size'  )  )
       self.assertTrue(hasattr(A_						,							'do_center_crop'  )  )
       self.assertTrue(hasattr(A_						,							'center_crop'  )  )
       self.assertTrue(hasattr(A_						,							'do_normalize'  )  )
       self.assertTrue(hasattr(A_						,							'image_mean'  )  )
       self.assertTrue(hasattr(A_						,							'image_std'  )  )
       self.assertTrue(hasattr(A_						,							'do_convert_rgb'  )  )

 def     a__			(    self :  Any  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class.from_dict(self.image_processor_dict  )
       self.assertEqual(image_processor.size						,							{'height': 224, 'width': 224}  )
       self.assertEqual(image_processor.crop_size						,							{'height': 18, 'width': 18}  )

       lowerCamelCase_							=   self.image_processing_class.from_dict(self.image_processor_dict						,							size=42						,							crop_size=84  )
       self.assertEqual(image_processor.size						,							{'shortest_edge': 42}  )
       self.assertEqual(image_processor.crop_size						,							{'height': 84, 'width': 84}  )

 def     a__			(    self :  List[Any]  )       ->     List[Any]:

       """simple docstring"""






       pass

 def     a__			(    self :  str  )       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       # create random PIL images
       lowerCamelCase_							=   self.image_processor_tester.prepare_inputs(equal_resolution=A_  )
       for image in image_inputs:
             self.assertIsInstance(A_						,							Image.Image  )

       # Test not batched input
       lowerCamelCase_							=   image_processing(image_inputs[0]						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)

       # Test batched
       lowerCamelCase_							=   image_processing(A_						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)

 def     a__			(    self :  List[str]  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       # create random numpy tensors
       lowerCamelCase_							=   self.image_processor_tester.prepare_inputs(equal_resolution=A_						,							numpify=A_  )
       for image in image_inputs:
             self.assertIsInstance(A_						,							np.ndarray  )

       # Test not batched input
       lowerCamelCase_							=   image_processing(image_inputs[0]						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)

       # Test batched
       lowerCamelCase_							=   image_processing(A_						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)





 def     a__			(    self :  str  )       ->     Dict:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       # create random PyTorch tensors
       lowerCamelCase_							=   self.image_processor_tester.prepare_inputs(equal_resolution=A_						,							torchify=A_  )
       for image in image_inputs:
             self.assertIsInstance(A_						,							torch.Tensor  )

       # Test not batched input
       lowerCamelCase_							=   image_processing(image_inputs[0]						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)

       # Test batched
       lowerCamelCase_							=   image_processing(A_						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)







@require_torch
@require_vision
class   A(						UpperCamelCase							,				unittest.TestCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						ChineseCLIPImageProcessor if is_vision_available() else None

 def     a__			(    self :  Dict  )       ->     int:

       """simple docstring"""






       lowerCamelCase_							=   ChineseCLIPImageProcessingTester(self						,							num_channels=4						,							do_center_crop=A_  )
       lowerCamelCase_							=   3

 @property
 def     a__			(    self :  Any  )       ->     int:

       """simple docstring"""






       return self.image_processor_tester.prepare_image_processor_dict()

 def     a__			(    self :  Dict  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       self.assertTrue(hasattr(A_						,							'do_resize'  )  )
       self.assertTrue(hasattr(A_						,							'size'  )  )
       self.assertTrue(hasattr(A_						,							'do_center_crop'  )  )
       self.assertTrue(hasattr(A_						,							'center_crop'  )  )
       self.assertTrue(hasattr(A_						,							'do_normalize'  )  )
       self.assertTrue(hasattr(A_						,							'image_mean'  )  )
       self.assertTrue(hasattr(A_						,							'image_std'  )  )
       self.assertTrue(hasattr(A_						,							'do_convert_rgb'  )  )

 def     a__			(    self :  Tuple  )       ->     List[str]:

       """simple docstring"""






       pass





 def     a__			(    self :  Union[str, Any]  )       ->     int:

       """simple docstring"""






       lowerCamelCase_							=   self.image_processing_class(**self.image_processor_dict  )
       # create random PIL images
       lowerCamelCase_							=   self.image_processor_tester.prepare_inputs(equal_resolution=A_  )
       for image in image_inputs:
             self.assertIsInstance(A_						,							Image.Image  )

       # Test not batched input
       lowerCamelCase_							=   image_processing(image_inputs[0]						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               1,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)

       # Test batched
       lowerCamelCase_							=   image_processing(A_						,							return_tensors='pt'  ).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               self.image_processor_tester.batch_size,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )						,							)



