
'''simple docstring'''



from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


UpperCamelCase__   :		Optional[int]											=	logging.get_logger(__name__)


if is_vision_available():
     import PIL

class  _a					(lowercase_):





       """simple docstring"""




       SCREAMING_SNAKE_CASE   =	['pixel_values']






       def __init__(				self							,				A__ = True							,				A__ = None							,				A__ = PILImageResampling.BICUBIC							,				A__ = True							,				A__ = None							,				A__ = True							,				A__ = 1 / 2_55							,				A__ = True							,				A__ = None							,				A__ = None							,				A__ = True							,				**A__							,				)     ->      None:
          super().__init__(**_lowercase   )
          _SCREAMING_SNAKE_CASE  			=					size if size is not None else {"""shortest_edge""": 2_24}
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase							,				default_to_square=_lowercase   )
          _SCREAMING_SNAKE_CASE  			=					crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase							,				default_to_square=_lowercase							,				param_name="""crop_size"""   )

          _SCREAMING_SNAKE_CASE  			=					do_resize
          _SCREAMING_SNAKE_CASE  			=					size
          _SCREAMING_SNAKE_CASE  			=					resample
          _SCREAMING_SNAKE_CASE  			=					do_center_crop
          _SCREAMING_SNAKE_CASE  			=					crop_size
          _SCREAMING_SNAKE_CASE  			=					do_rescale
          _SCREAMING_SNAKE_CASE  			=					rescale_factor
          _SCREAMING_SNAKE_CASE  			=					do_normalize
          _SCREAMING_SNAKE_CASE  			=					image_mean if image_mean is not None else OPENAI_CLIP_MEAN
          _SCREAMING_SNAKE_CASE  			=					image_std if image_std is not None else OPENAI_CLIP_STD
          _SCREAMING_SNAKE_CASE  			=					do_convert_rgb






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__ = PILImageResampling.BICUBIC							,				A__ = None							,				**A__							,				)     ->      np.ndarray:
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase							,				default_to_square=_lowercase   )
          if "shortest_edge" not in size:
             raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"   )
          _SCREAMING_SNAKE_CASE  			=					get_resize_output_image_size(_lowercase							,				size=size["""shortest_edge"""]							,				default_to_square=_lowercase   )
          return resize(_lowercase							,				size=_lowercase							,				resample=_lowercase							,				data_format=_lowercase							,				**_lowercase   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__ = None							,				**A__							,				)     ->      np.ndarray:
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase   )
          if "height" not in size or "width" not in size:
             raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}"   )
          return center_crop(_lowercase							,				size=(size["""height"""], size["""width"""])							,				data_format=_lowercase							,				**_lowercase   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__ = None							,				**A__							,				)     ->      Any:
          return rescale(_lowercase							,				scale=_lowercase							,				data_format=_lowercase							,				**_lowercase   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__ = None							,				**A__							,				)     ->      np.ndarray:
          return normalize(_lowercase							,				mean=_lowercase							,				std=_lowercase							,				data_format=_lowercase							,				**_lowercase   )






       def 							UpperCamelCase					(				self							,				A__							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = None							,				A__ = ChannelDimension.FIRST							,				**A__							,				)     ->      PIL.Image.Image:
          _SCREAMING_SNAKE_CASE  			=					do_resize if do_resize is not None else self.do_resize
          _SCREAMING_SNAKE_CASE  			=					size if size is not None else self.size
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase							,				param_name="""size"""							,				default_to_square=_lowercase   )
          _SCREAMING_SNAKE_CASE  			=					resample if resample is not None else self.resample
          _SCREAMING_SNAKE_CASE  			=					do_center_crop if do_center_crop is not None else self.do_center_crop
          _SCREAMING_SNAKE_CASE  			=					crop_size if crop_size is not None else self.crop_size
          _SCREAMING_SNAKE_CASE  			=					get_size_dict(_lowercase							,				param_name="""crop_size"""							,				default_to_square=_lowercase   )
          _SCREAMING_SNAKE_CASE  			=					do_rescale if do_rescale is not None else self.do_rescale
          _SCREAMING_SNAKE_CASE  			=					rescale_factor if rescale_factor is not None else self.rescale_factor
          _SCREAMING_SNAKE_CASE  			=					do_normalize if do_normalize is not None else self.do_normalize
          _SCREAMING_SNAKE_CASE  			=					image_mean if image_mean is not None else self.image_mean
          _SCREAMING_SNAKE_CASE  			=					image_std if image_std is not None else self.image_std
          _SCREAMING_SNAKE_CASE  			=					do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

          _SCREAMING_SNAKE_CASE  			=					make_list_of_images(_lowercase   )

          if not valid_images(_lowercase   ):
             raise ValueError(
                 """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                 """torch.Tensor, tf.Tensor or jax.ndarray."""   )

          if do_resize and size is None:
             raise ValueError("""Size must be specified if do_resize is True."""   )

          if do_center_crop and crop_size is None:
             raise ValueError("""Crop size must be specified if do_center_crop is True."""   )

          if do_rescale and rescale_factor is None:
             raise ValueError("""Rescale factor must be specified if do_rescale is True."""   )

          if do_normalize and (image_mean is None or image_std is None):
             raise ValueError("""Image mean and std must be specified if do_normalize is True."""   )

          # PIL RGBA images are converted to RGB
          if do_convert_rgb:
             _SCREAMING_SNAKE_CASE  			=					[convert_to_rgb(_lowercase   ) for image in images]

          # All transformations expect numpy arrays.
          _SCREAMING_SNAKE_CASE  			=					[to_numpy_array(_lowercase   ) for image in images]

          if do_resize:
             _SCREAMING_SNAKE_CASE  			=					[self.resize(image=_lowercase							,				size=_lowercase							,				resample=_lowercase   ) for image in images]

          if do_center_crop:
             _SCREAMING_SNAKE_CASE  			=					[self.center_crop(image=_lowercase							,				size=_lowercase   ) for image in images]

          if do_rescale:
             _SCREAMING_SNAKE_CASE  			=					[self.rescale(image=_lowercase							,				scale=_lowercase   ) for image in images]

          if do_normalize:
             _SCREAMING_SNAKE_CASE  			=					[self.normalize(image=_lowercase							,				mean=_lowercase							,				std=_lowercase   ) for image in images]

          _SCREAMING_SNAKE_CASE  			=					[to_channel_dimension_format(_lowercase							,				_lowercase   ) for image in images]

          _SCREAMING_SNAKE_CASE  			=					{"""pixel_values""": images}
          return BatchFeature(data=_lowercase							,				tensor_type=_lowercase   )









'''simple docstring'''

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

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


SCREAMING_SNAKE_CASE							:				int     	=			get_tests_dir("fixtures")
SCREAMING_SNAKE_CASE							:				str     	=			get_tests_dir("fixtures/dummy_feature_extractor_config.json")
SCREAMING_SNAKE_CASE							:				str     	=			get_tests_dir("fixtures/dummy-config.json")






class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				0



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							str:
											with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE_				=				WavaVecaConfig()

																		# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       ).to_dict()

																		config_dict.pop('feature_extractor_type'       )
																		SCREAMING_SNAKE_CASE_				=				WavaVecaFeatureExtractor(**_lowercase       )

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

																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )

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

											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							Any:
											with self.assertRaisesRegex(
											    _lowercase,     'bert-base is not a local folder and is not a valid model identifier'       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('bert-base'       )



				def   a__					(    self       )     ->							List[Any]:
											with self.assertRaisesRegex(
											    _lowercase,     R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase,     revision='aaaaaa'       )



				def   a__					(    self       )     ->							List[Any]:
											with self.assertRaisesRegex(
											    _lowercase,     'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.',     ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'       )



				def   a__					(    self       )     ->							List[str]:
											# If remote code is not set, we will time out when asking whether to load the model.
											with self.assertRaises(_lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor'       )
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(_lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )

											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
											self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )

											# Test feature extractor can be reloaded.
											with tempfile.TemporaryDirectory() as tmp_dir:
																		feature_extractor.save_pretrained(_lowercase       )
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase,     trust_remote_code=_lowercase       )
											self.assertEqual(reloaded_feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )



				def   a__					(    self       )     ->							int:
											try:
																		AutoConfig.register('custom',     _lowercase       )
																		AutoFeatureExtractor.register(_lowercase,     _lowercase       )
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(_lowercase       ):
																									AutoFeatureExtractor.register(_lowercase,     _lowercase       )

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		SCREAMING_SNAKE_CASE_				=				CustomFeatureExtractor.from_pretrained(_lowercase       )
																		with tempfile.TemporaryDirectory() as tmp_dir:
																									feature_extractor.save_pretrained(_lowercase       )
																									SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
																									self.assertIsInstance(_lowercase,     _lowercase       )

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





				def   a__					(    self       )     ->							str:






											class    snake_case     (      lowercase_		):






															"""simple docstring"""
															_a    	=     True

											try:
																		AutoConfig.register('custom',     _lowercase       )
																		AutoFeatureExtractor.register(_lowercase,     _lowercase       )
																		# If remote code is not set, the default is to use local
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor'       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(feature_extractor.is_local       )

																		# If remote code is disabled, we load the local one.
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(feature_extractor.is_local       )

																		# If remote is enabled, we load from the Hub
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(not hasattr(_lowercase,     'is_local'       )       )

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





