


'''simple docstring'''



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 			a       (	unittest.TestCase ):


     """simple docstring"""






     def 				lowerCamelCase__				(       self  :	Optional[Any]    )		->							Optional[int]:
          __UpperCAmelCase			:				Optional[Any]													=    0

     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							Dict:
          __UpperCAmelCase			:				Optional[int]													=    AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''    )
          self.assertIsInstance(snake_case							,				snake_case    )

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

               __UpperCAmelCase			:				Any													=    AutoImageProcessor.from_pretrained(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

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

               __UpperCAmelCase			:				int													=    AutoImageProcessor.from_pretrained(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

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

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

               # remove image_processor_type to make sure config.json alone is enough to load image processor locally
               __UpperCAmelCase			:				int													=    AutoImageProcessor.from_pretrained(snake_case    ).to_dict()

               config_dict.pop('''image_processor_type'''    )
               __UpperCAmelCase			:				Tuple													=    CLIPImageProcessor(**snake_case    )

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

               __UpperCAmelCase			:				Dict													=    AutoImageProcessor.from_pretrained(snake_case    )

               # make sure private variable is not incorrectly saved
               __UpperCAmelCase			:				Union[str, Any]													=    json.loads(config.to_json_string()    )
               self.assertTrue('''_processor_class''' not in dict_as_saved    )

          self.assertIsInstance(snake_case							,				snake_case    )

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

               __UpperCAmelCase			:				Any													=    AutoImageProcessor.from_pretrained(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     def 				lowerCamelCase__				(       self  :	int    )		->							str:
          with self.assertRaisesRegex(
              snake_case							,				'''clip-base is not a local folder and is not a valid model identifier'''    ):
               __UpperCAmelCase			:				Any													=    AutoImageProcessor.from_pretrained('''clip-base'''    )

     def 				lowerCamelCase__				(       self  :	Dict    )		->							int:
          with self.assertRaisesRegex(
              snake_case							,				r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''    ):
               __UpperCAmelCase			:				Tuple													=    AutoImageProcessor.from_pretrained(snake_case							,				revision='''aaaaaa'''    )

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

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

          __UpperCAmelCase			:				Optional[Any]													=    AutoImageProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_image_processor'''							,				trust_remote_code=snake_case    )
          self.assertEqual(image_processor.__class__.__name__							,				'''NewImageProcessor'''    )

          # Test image processor can be reloaded.
          with tempfile.TemporaryDirectory() as tmp_dir:
               image_processor.save_pretrained(snake_case    )
               __UpperCAmelCase			:				Optional[int]													=    AutoImageProcessor.from_pretrained(snake_case							,				trust_remote_code=snake_case    )
          self.assertEqual(reloaded_image_processor.__class__.__name__							,				'''NewImageProcessor'''    )

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

               with tempfile.TemporaryDirectory() as tmpdirname:
                    __UpperCAmelCase			:				int													=    Path(snake_case    ) / '''preprocessor_config.json'''
                    __UpperCAmelCase			:				Optional[int]													=    Path(snake_case    ) / '''config.json'''
                    json.dump(
                        {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}							,				open(snake_case							,				'''w'''    )							,				)
                    json.dump({'''model_type''': '''clip'''}							,				open(snake_case							,				'''w'''    )    )

                    __UpperCAmelCase			:				List[str]													=    CustomImageProcessor.from_pretrained(snake_case    )

               # 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(snake_case    )
                    __UpperCAmelCase			:				int													=    AutoImageProcessor.from_pretrained(snake_case    )
                    self.assertIsInstance(snake_case							,				snake_case    )

          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  :	List[str]    )		->							List[str]:



          class 			a       (	_a ):


               """simple docstring"""






               SCREAMING_SNAKE_CASE :				Dict							     =     True

          try:
               AutoConfig.register('''custom'''							,				snake_case    )
               AutoImageProcessor.register(snake_case							,				snake_case    )
               # If remote code is not set, the default is to use local
               __UpperCAmelCase			:				Dict													=    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.
               __UpperCAmelCase			:				str													=    AutoImageProcessor.from_pretrained(
                   '''hf-internal-testing/test_dynamic_image_processor'''							,				trust_remote_code=snake_case    )
               self.assertEqual(image_processor.__class__.__name__							,				'''NewImageProcessor'''    )
               self.assertTrue(image_processor.is_local    )

               # If remote is enabled, we load from the Hub
               __UpperCAmelCase			:				Any													=    AutoImageProcessor.from_pretrained(
                   '''hf-internal-testing/test_dynamic_image_processor'''							,				trust_remote_code=snake_case    )
               self.assertEqual(image_processor.__class__.__name__							,				'''NewImageProcessor'''    )
               self.assertTrue(not hasattr(snake_case							,				'''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]



'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__UpperCAmelCase    :Optional[Any]				       =     {
    "configuration_layoutlmv2": ["LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMv2Config"],
    "processing_layoutlmv2": ["LayoutLMv2Processor"],
    "tokenization_layoutlmv2": ["LayoutLMv2Tokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase    :Any				       =     ["LayoutLMv2TokenizerFast"]

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase    :int				       =     ["LayoutLMv2FeatureExtractor"]
      __UpperCAmelCase    :Optional[int]				       =     ["LayoutLMv2ImageProcessor"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase    :List[Any]				       =     [
          "LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST",
          "LayoutLMv2ForQuestionAnswering",
          "LayoutLMv2ForSequenceClassification",
          "LayoutLMv2ForTokenClassification",
          "LayoutLMv2Layer",
          "LayoutLMv2Model",
          "LayoutLMv2PreTrainedModel",
      ]

if TYPE_CHECKING:
      from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
      from .processing_layoutlmva import LayoutLMvaProcessor
      from .tokenization_layoutlmva import LayoutLMvaTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_layoutlmva import (
                LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
                LayoutLMvaForQuestionAnswering,
                LayoutLMvaForSequenceClassification,
                LayoutLMvaForTokenClassification,
                LayoutLMvaLayer,
                LayoutLMvaModel,
                LayoutLMvaPreTrainedModel,
            )
else:
      import sys

      __UpperCAmelCase    :Any				       =     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)