
"""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 AutoImageProcessor, ViTImageProcessor
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_image_processing import CustomImageProcessor  # noqa E402


SCREAMING_SNAKE_CASE_					     =	get_tests_dir('''fixtures''')
class       lowerCAmelCase_		(				unittest.TestCase ):



							'''simple docstring'''





							def    A__						( self				)       ->  int:
													# A mock response for an HTTP head request to emulate server down
													__lowerCAmelCase     =  mock.Mock()
													__lowerCAmelCase     =  500
													__lowerCAmelCase     =  {}
													__lowerCAmelCase     =  HTTPError
													__lowerCAmelCase     =  {}

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




							def    A__						( self				)       ->  Union[str, Any]:
													# This test is for deprecated behavior and can be removed in v5
													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained(
													    """https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json"""				)




							def    A__						( self				)       ->  Optional[Any]:
													with self.assertRaises(snake_case_				):
																			# config is in subfolder, the following should not work without specifying the subfolder
																			__lowerCAmelCase     =  AutoImageProcessor.from_pretrained("""hf-internal-testing/stable-diffusion-all-variants"""				)

													__lowerCAmelCase     =  AutoImageProcessor.from_pretrained(
													    """hf-internal-testing/stable-diffusion-all-variants"""    ,				subfolder="""feature_extractor"""				)

													self.assertIsNotNone(snake_case_				)
@is_staging_test
class       lowerCAmelCase_		(				unittest.TestCase ):



							'''simple docstring'''





							@classmethod
							def    A__						( cls				)       ->  Dict:
													__lowerCAmelCase     =  TOKEN
													HfFolder.save_token(snake_case_				)




							@classmethod
							def    A__						( cls				)       ->  Any:
													try:
																			delete_repo(token=cls._token    ,				repo_id="""test-image-processor"""				)
													except HTTPError:
																			pass

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

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




							def    A__						( self				)       ->  List[Any]:
													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained(snake_case_				)
													image_processor.push_to_hub("""test-image-processor"""    ,				use_auth_token=self._token				)

													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained(f"""{USER}/test-image-processor"""				)
													for k, v in image_processor.__dict__.items():
																			self.assertEqual(snake_case_    ,				getattr(snake_case_    ,				snake_case_				)				)

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

													# Push to hub via save_pretrained
													with tempfile.TemporaryDirectory() as tmp_dir:
																			image_processor.save_pretrained(
																			    snake_case_    ,				repo_id="""test-image-processor"""    ,				push_to_hub=snake_case_    ,				use_auth_token=self._token				)

													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained(f"""{USER}/test-image-processor"""				)
													for k, v in image_processor.__dict__.items():
																			self.assertEqual(snake_case_    ,				getattr(snake_case_    ,				snake_case_				)				)




							def    A__						( self				)       ->  Optional[Any]:
													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained(snake_case_				)
													image_processor.push_to_hub("""valid_org/test-image-processor"""    ,				use_auth_token=self._token				)

													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained("""valid_org/test-image-processor"""				)
													for k, v in image_processor.__dict__.items():
																			self.assertEqual(snake_case_    ,				getattr(snake_case_    ,				snake_case_				)				)

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

													# Push to hub via save_pretrained
													with tempfile.TemporaryDirectory() as tmp_dir:
																			image_processor.save_pretrained(
																			    snake_case_    ,				repo_id="""valid_org/test-image-processor-org"""    ,				push_to_hub=snake_case_    ,				use_auth_token=self._token				)

													__lowerCAmelCase     =  ViTImageProcessor.from_pretrained("""valid_org/test-image-processor-org"""				)
													for k, v in image_processor.__dict__.items():
																			self.assertEqual(snake_case_    ,				getattr(snake_case_    ,				snake_case_				)				)




							def    A__						( self				)       ->  str:
													CustomImageProcessor.register_for_auto_class()
													__lowerCAmelCase     =  CustomImageProcessor.from_pretrained(snake_case_				)

													image_processor.push_to_hub("""test-dynamic-image-processor"""    ,				use_auth_token=self._token				)

													# This has added the proper auto_map field to the config
													self.assertDictEqual(
													    image_processor.auto_map    ,				{"""AutoImageProcessor""": """custom_image_processing.CustomImageProcessor"""}    ,				)

													__lowerCAmelCase     =  AutoImageProcessor.from_pretrained(
													    f"""{USER}/test-dynamic-image-processor"""    ,				trust_remote_code=snake_case_				)
													# Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
													self.assertEqual(new_image_processor.__class__.__name__    ,				"""CustomImageProcessor"""				)


"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_					     =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_					     =	{
    '''alibaba-damo/mgp-str-base''': '''https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json''',
}
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''mgp-str'''




							def __init__( self    ,				snake_case_=[32, 128]    ,				snake_case_=4    ,				snake_case_=3    ,				snake_case_=27    ,				snake_case_=38    ,				snake_case_=50_257    ,				snake_case_=30_522    ,				snake_case_=768    ,				snake_case_=12    ,				snake_case_=12    ,				snake_case_=4.0    ,				snake_case_=True    ,				snake_case_=False    ,				snake_case_=1e-5    ,				snake_case_=0.0    ,				snake_case_=0.0    ,				snake_case_=0.0    ,				snake_case_=False    ,				snake_case_=0.02    ,				**snake_case_    ,				)       ->  List[Any]:
													super().__init__(**snake_case_				)

													__lowerCAmelCase     =  image_size
													__lowerCAmelCase     =  patch_size
													__lowerCAmelCase     =  num_channels
													__lowerCAmelCase     =  max_token_length
													__lowerCAmelCase     =  num_character_labels
													__lowerCAmelCase     =  num_bpe_labels
													__lowerCAmelCase     =  num_wordpiece_labels
													__lowerCAmelCase     =  hidden_size
													__lowerCAmelCase     =  num_hidden_layers
													__lowerCAmelCase     =  num_attention_heads
													__lowerCAmelCase     =  mlp_ratio
													__lowerCAmelCase     =  distilled
													__lowerCAmelCase     =  layer_norm_eps
													__lowerCAmelCase     =  drop_rate
													__lowerCAmelCase     =  qkv_bias
													__lowerCAmelCase     =  attn_drop_rate
													__lowerCAmelCase     =  drop_path_rate
													__lowerCAmelCase     =  output_aa_attentions
													__lowerCAmelCase     =  initializer_range
