



"""simple docstring"""

import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


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

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


a_			   =					get_tests_dir('''fixtures''')







class    __lowercase     (					unittest.TestCase):







						"""simple docstring"""







						def 						__UpperCamelCase							(self       ):
											# A mock response for an HTTP head request to emulate server down
											snake_case_					:		Union[str, Any] 					=					mock.Mock()
											snake_case_					:		Union[str, Any] 					=					5_00
											snake_case_					:		str 					=					{}
											snake_case_					:		Dict 					=					HTTPError
											snake_case_					:		List[str] 					=					{}

											# Download this model to make sure it's in the cache.
											snake_case_					:		List[str] 					=					WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""       )
											# Under the mock environment we get a 500 error when trying to reach the model.
											with mock.patch("""requests.Session.request"""   ,		return_value=lowercase__       ) as mock_head:
																snake_case_					:		Union[str, Any] 					=					WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""       )
																# This check we did call the fake head request
																mock_head.assert_called()







						def 						__UpperCamelCase							(self       ):
											# This test is for deprecated behavior and can be removed in v5
											snake_case_					:		List[str] 					=					WavaVecaFeatureExtractor.from_pretrained(
											    """https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"""       )







@is_staging_test
class    __lowercase     (					unittest.TestCase):







						"""simple docstring"""







						@classmethod
						def 						__UpperCamelCase							(cls       ):
											snake_case_					:		List[Any] 					=					TOKEN
											HfFolder.save_token(lowercase__       )







						@classmethod
						def 						__UpperCamelCase							(cls       ):
											try:
																delete_repo(token=cls._token   ,		repo_id="""test-feature-extractor"""       )
											except HTTPError:
																pass

											try:
																delete_repo(token=cls._token   ,		repo_id="""valid_org/test-feature-extractor-org"""       )
											except HTTPError:
																pass

											try:
																delete_repo(token=cls._token   ,		repo_id="""test-dynamic-feature-extractor"""       )
											except HTTPError:
																pass







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					WavaVecaFeatureExtractor.from_pretrained(lowercase__       )
											feature_extractor.push_to_hub("""test-feature-extractor"""   ,		use_auth_token=self._token       )

											snake_case_					:		Optional[int] 					=					WavaVecaFeatureExtractor.from_pretrained(f'{USER}/test-feature-extractor'       )
											for k, v in feature_extractor.__dict__.items():
																self.assertEqual(lowercase__   ,		getattr(lowercase__   ,		lowercase__       )       )

											# Reset repo
											delete_repo(token=self._token   ,		repo_id="""test-feature-extractor"""       )

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																feature_extractor.save_pretrained(
																    lowercase__   ,		repo_id="""test-feature-extractor"""   ,		push_to_hub=lowercase__   ,		use_auth_token=self._token       )

											snake_case_					:		List[str] 					=					WavaVecaFeatureExtractor.from_pretrained(f'{USER}/test-feature-extractor'       )
											for k, v in feature_extractor.__dict__.items():
																self.assertEqual(lowercase__   ,		getattr(lowercase__   ,		lowercase__       )       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Union[str, Any] 					=					WavaVecaFeatureExtractor.from_pretrained(lowercase__       )
											feature_extractor.push_to_hub("""valid_org/test-feature-extractor"""   ,		use_auth_token=self._token       )

											snake_case_					:		Any 					=					WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor"""       )
											for k, v in feature_extractor.__dict__.items():
																self.assertEqual(lowercase__   ,		getattr(lowercase__   ,		lowercase__       )       )

											# Reset repo
											delete_repo(token=self._token   ,		repo_id="""valid_org/test-feature-extractor"""       )

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																feature_extractor.save_pretrained(
																    lowercase__   ,		repo_id="""valid_org/test-feature-extractor-org"""   ,		push_to_hub=lowercase__   ,		use_auth_token=self._token       )

											snake_case_					:		Union[str, Any] 					=					WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor-org"""       )
											for k, v in feature_extractor.__dict__.items():
																self.assertEqual(lowercase__   ,		getattr(lowercase__   ,		lowercase__       )       )







						def 						__UpperCamelCase							(self       ):
											CustomFeatureExtractor.register_for_auto_class()
											snake_case_					:		List[Any] 					=					CustomFeatureExtractor.from_pretrained(lowercase__       )

											feature_extractor.push_to_hub("""test-dynamic-feature-extractor"""   ,		use_auth_token=self._token       )

											# This has added the proper auto_map field to the config
											self.assertDictEqual(
											    feature_extractor.auto_map   ,		{"""AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor"""}   ,		)

											snake_case_					:		str 					=					AutoFeatureExtractor.from_pretrained(
											    f'{USER}/test-dynamic-feature-extractor'   ,		trust_remote_code=lowercase__       )
											# Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
											self.assertEqual(new_feature_extractor.__class__.__name__   ,		"""CustomFeatureExtractor"""       )









"""simple docstring"""

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Union[str, Any]							       =		["""image_processor""", """tokenizer"""]
						_A		:							str							       =		"""ChineseCLIPImageProcessor"""
						_A		:							Tuple							       =		("""BertTokenizer""", """BertTokenizerFast""")







						def __init__(self   ,		lowercase__=None   ,		lowercase__=None   ,		**lowercase__       ):
											snake_case_					:		Any 					=					None
											if "feature_extractor" in kwargs:
																warnings.warn(
																    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
																    """ instead."""   ,		lowercase__   ,		)
																snake_case_					:		Optional[Any] 					=					kwargs.pop("""feature_extractor"""       )

											snake_case_					:		str 					=					image_processor if image_processor is not None else feature_extractor
											if image_processor is None:
																raise ValueError("""You need to specify an `image_processor`."""       )
											if tokenizer is None:
																raise ValueError("""You need to specify a `tokenizer`."""       )

											super().__init__(lowercase__   ,		lowercase__       )
											snake_case_					:		Union[str, Any] 					=					self.image_processor







						def __call__(self   ,		lowercase__=None   ,		lowercase__=None   ,		lowercase__=None   ,		**lowercase__       ):

											if text is None and images is None:
																raise ValueError("""You have to specify either text or images. Both cannot be none."""       )

											if text is not None:
																snake_case_					:		Any 					=					self.tokenizer(lowercase__   ,		return_tensors=lowercase__   ,		**lowercase__       )

											if images is not None:
																snake_case_					:		Tuple 					=					self.image_processor(lowercase__   ,		return_tensors=lowercase__   ,		**lowercase__       )

											if text is not None and images is not None:
																snake_case_					:		List[Any] 					=					image_features.pixel_values
																return encoding
											elif text is not None:
																return encoding
											else:
																return BatchEncoding(data=dict(**lowercase__       )   ,		tensor_type=lowercase__       )







						def 						__UpperCamelCase							(self   ,		*lowercase__   ,		**lowercase__       ):
											return self.tokenizer.batch_decode(*lowercase__   ,		**lowercase__       )







						def 						__UpperCamelCase							(self   ,		*lowercase__   ,		**lowercase__       ):
											return self.tokenizer.decode(*lowercase__   ,		**lowercase__       )







						@property
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[int] 					=					self.tokenizer.model_input_names
											snake_case_					:		Union[str, Any] 					=					self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )







						@property
						def 						__UpperCamelCase							(self       ):
											warnings.warn(
											    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""   ,		lowercase__   ,		)
											return self.image_processor_class




