





import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->Optional[int]:
         lowerCamelCase__       :						Tuple        =							0







       def 						__lowerCamelCase   (				self					:     List[str]      )       ->int:
         lowerCamelCase__       :						int        =							AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''      )
         self.assertIsInstance(A		, A      )







       def 						__lowerCamelCase   (				self					:     Optional[Any]      )       ->Optional[Any]:
         with tempfile.TemporaryDirectory() as tmpdirname:
           lowerCamelCase__       :						Tuple        =							Path(A      ) / '''preprocessor_config.json'''
           lowerCamelCase__       :						Union[str, Any]        =							Path(A      ) / '''config.json'''
           json.dump(
               {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(A		, '''w'''      )		, )
           json.dump({'''model_type''': '''clip'''}		, open(A		, '''w'''      )      )

           lowerCamelCase__       :						Dict        =							AutoImageProcessor.from_pretrained(A      )
           self.assertIsInstance(A		, A      )







       def 						__lowerCamelCase   (				self					:     str      )       ->Union[str, Any]:
         # Ensure we can load the image processor from the feature extractor config
         with tempfile.TemporaryDirectory() as tmpdirname:
           lowerCamelCase__       :						str        =							Path(A      ) / '''preprocessor_config.json'''
           lowerCamelCase__       :						Optional[int]        =							Path(A      ) / '''config.json'''
           json.dump(
               {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		, open(A		, '''w'''      )		, )
           json.dump({'''model_type''': '''clip'''}		, open(A		, '''w'''      )      )

           lowerCamelCase__       :						str        =							AutoImageProcessor.from_pretrained(A      )
           self.assertIsInstance(A		, A      )







       def 						__lowerCamelCase   (				self					:     List[str]      )       ->Optional[Any]:
         with tempfile.TemporaryDirectory() as tmpdirname:
           lowerCamelCase__       :						List[Any]        =							CLIPConfig()

           # Create a dummy config file with image_proceesor_type
           lowerCamelCase__       :						Optional[Any]        =							Path(A      ) / '''preprocessor_config.json'''
           lowerCamelCase__       :						str        =							Path(A      ) / '''config.json'''
           json.dump(
               {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(A		, '''w'''      )		, )
           json.dump({'''model_type''': '''clip'''}		, open(A		, '''w'''      )      )

           # remove image_processor_type to make sure config.json alone is enough to load image processor locally
           lowerCamelCase__       :						str        =							AutoImageProcessor.from_pretrained(A      ).to_dict()

           config_dict.pop('''image_processor_type'''      )
           lowerCamelCase__       :						Tuple        =							CLIPImageProcessor(**A      )

           # save in new folder
           model_config.save_pretrained(A      )
           config.save_pretrained(A      )

           lowerCamelCase__       :						Union[str, Any]        =							AutoImageProcessor.from_pretrained(A      )

           # make sure private variable is not incorrectly saved
           lowerCamelCase__       :						int        =							json.loads(config.to_json_string()      )
           self.assertTrue('''_processor_class''' not in dict_as_saved      )

         self.assertIsInstance(A		, A      )







       def 						__lowerCamelCase   (				self					:     List[str]      )       ->Optional[Any]:
         with tempfile.TemporaryDirectory() as tmpdirname:
           lowerCamelCase__       :						str        =							Path(A      ) / '''preprocessor_config.json'''
           json.dump(
               {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(A		, '''w'''      )		, )

           lowerCamelCase__       :						List[str]        =							AutoImageProcessor.from_pretrained(A      )
           self.assertIsInstance(A		, A      )







       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->Dict:
         with self.assertRaisesRegex(
             A		, '''clip-base is not a local folder and is not a valid model identifier'''      ):
           lowerCamelCase__       :						List[str]        =							AutoImageProcessor.from_pretrained('''clip-base'''      )







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->str:
         with self.assertRaisesRegex(
             A		, R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''      ):
           lowerCamelCase__       :						Optional[Any]        =							AutoImageProcessor.from_pretrained(A		, revision='''aaaaaa'''      )







       def 						__lowerCamelCase   (				self					:     Dict      )       ->Optional[Any]:
         with self.assertRaisesRegex(
             A		, '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''		, ):
           lowerCamelCase__       :						int        =							AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''      )







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->List[str]:
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(A      ):
           lowerCamelCase__       :						Dict        =							AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''      )
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(A      ):
           lowerCamelCase__       :						Dict        =							AutoImageProcessor.from_pretrained(
               '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=A      )

         lowerCamelCase__       :						Tuple        =							AutoImageProcessor.from_pretrained(
             '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=A      )
         self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''      )

         # Test image processor can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
           image_processor.save_pretrained(A      )
           lowerCamelCase__       :						Union[str, Any]        =							AutoImageProcessor.from_pretrained(A		, trust_remote_code=A      )
         self.assertEqual(reloaded_image_processor.__class__.__name__		, '''NewImageProcessor'''      )







       def 						__lowerCamelCase   (				self					:     int      )       ->List[Any]:
         try:
           AutoConfig.register('''custom'''		, A      )
           AutoImageProcessor.register(A		, A      )
           # Trying to register something existing in the Transformers library will raise an error
           with self.assertRaises(A      ):
             AutoImageProcessor.register(A		, A      )

           with tempfile.TemporaryDirectory() as tmpdirname:
             lowerCamelCase__       :						Any        =							Path(A      ) / '''preprocessor_config.json'''
             lowerCamelCase__       :						Union[str, Any]        =							Path(A      ) / '''config.json'''
             json.dump(
                 {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		, open(A		, '''w'''      )		, )
             json.dump({'''model_type''': '''clip'''}		, open(A		, '''w'''      )      )

             lowerCamelCase__       :						Tuple        =							CustomImageProcessor.from_pretrained(A      )

           # Now that the config is registered, it can be used as any other config with the auto-API
           with tempfile.TemporaryDirectory() as tmp_dir:
             image_processor.save_pretrained(A      )
             lowerCamelCase__       :						Optional[Any]        =							AutoImageProcessor.from_pretrained(A      )
             self.assertIsInstance(A		, A      )

         finally:
           if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
             del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Dict:


         class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
                _UpperCAmelCase				:  Optional[Any]			   =   True

         try:
           AutoConfig.register('''custom'''		, A      )
           AutoImageProcessor.register(A		, A      )
           # If remote code is not set, the default is to use local
           lowerCamelCase__       :						List[Any]        =							AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''      )
           self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''      )
           self.assertTrue(image_processor.is_local      )

           # If remote code is disabled, we load the local one.
           lowerCamelCase__       :						Any        =							AutoImageProcessor.from_pretrained(
               '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=A      )
           self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''      )
           self.assertTrue(image_processor.is_local      )

           # If remote is enabled, we load from the Hub
           lowerCamelCase__       :						int        =							AutoImageProcessor.from_pretrained(
               '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=A      )
           self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''      )
           self.assertTrue(not hasattr(A		, '''is_local'''      )      )

         finally:
           if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
             del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]










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 :   List[str]  			=  logging.get_logger(__name__)


class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
       _UpperCAmelCase				:  Any			   =   ["audio_values", "audio_mask"]







       def __init__(				self					:     Any		, A					:     Union[str, Any]=2_0_4_8		, A					:     Any=1		, A					:     int=[1_6, 1_6]		, A					:     Any=1_2_8		, A					:     List[Any]=4_4_1_0_0		, A					:     Dict=8_6		, A					:     Dict=2_0_4_8		, A					:     str=0.0		, **A					:     Union[str, Any]		, )       ->List[Any]:
          super().__init__(
              feature_size=A		, sampling_rate=A		, padding_value=A		, **A		, )

          lowerCamelCase__       :						Dict        =							spectrogram_length
          lowerCamelCase__       :						Optional[int]        =							num_channels
          lowerCamelCase__       :						str        =							patch_size
          lowerCamelCase__       :						Any        =							feature_size // self.patch_size[1]
          lowerCamelCase__       :						Union[str, Any]        =							n_fft
          lowerCamelCase__       :						Union[str, Any]        =							sampling_rate // hop_length_to_sampling_rate
          lowerCamelCase__       :						Optional[Any]        =							sampling_rate
          lowerCamelCase__       :						Optional[Any]        =							padding_value
          lowerCamelCase__       :						Union[str, Any]        =							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 						__lowerCamelCase   (				self					:     List[Any]		, A					:     np.array      )       ->np.ndarray:
          lowerCamelCase__       :						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		, )
          lowerCamelCase__       :						Any        =							log_spec[:, :-1]
          lowerCamelCase__       :						int        =							log_spec - 20.0
          lowerCamelCase__       :						int        =							np.clip(log_spec / 40.0		, -2.0		, 0.0      ) + 1.0
          return log_spec







       def __call__(				self					:     Union[str, Any]		, A					:     Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		, A					:     Optional[Union[str, TensorType]] = None		, A					:     Optional[bool] = True		, A					:     Optional[int] = None		, A					:     bool = False		, A					:     bool = False		, **A					:     str		, )       ->BatchFeature:

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

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

          # Create audio attention mask
          lowerCamelCase__       :						Optional[Any]        =							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__       :						List[Any]        =							[
                 (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__       :						List[str]        =							np.array(A      ).astype(np.floataa      )

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

          # return as BatchFeature
          if return_attention_mask:
             lowerCamelCase__       :						Optional[int]        =							{'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask}
          else:
             lowerCamelCase__       :						Tuple        =							{'''audio_values''': padded_audio_features}

          lowerCamelCase__       :						Union[str, Any]        =							BatchFeature(data=A		, tensor_type=A      )
          return encoded_inputs



