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





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= 0





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'   )
      self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with tempfile.TemporaryDirectory() as tmpdirname:
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "preprocessor_config.json"
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "config.json"
       json.dump(
           {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}			,  open(UpperCamelCase_			,  'w'   )			,  )
       json.dump({'model_type': 'clip'}			,  open(UpperCamelCase_			,  'w'   )   )

       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   )
       self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with tempfile.TemporaryDirectory() as tmpdirname:
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "preprocessor_config.json"
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "config.json"
       json.dump(
           {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}			,  open(UpperCamelCase_			,  'w'   )			,  )
       json.dump({'model_type': 'clip'}			,  open(UpperCamelCase_			,  'w'   )   )

       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   )
       self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with tempfile.TemporaryDirectory() as tmpdirname:
       __UpperCamelCase       						= CLIPConfig()

       # Create a dummy config file with image_proceesor_type
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "preprocessor_config.json"
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "config.json"
       json.dump(
           {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}			,  open(UpperCamelCase_			,  'w'   )			,  )
       json.dump({'model_type': 'clip'}			,  open(UpperCamelCase_			,  'w'   )   )

       # remove image_processor_type to make sure config.json alone is enough to load image processor locally
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   ).to_dict()

       config_dict.pop('image_processor_type'   )
       __UpperCamelCase       						= CLIPImageProcessor(**UpperCamelCase_   )

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

       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   )

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

      self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with tempfile.TemporaryDirectory() as tmpdirname:
       __UpperCamelCase       						= Path(UpperCamelCase_   ) / "preprocessor_config.json"
       json.dump(
           {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}			,  open(UpperCamelCase_			,  'w'   )			,  )

       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   )
       self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with self.assertRaisesRegex(
          UpperCamelCase_			,  'clip-base is not a local folder and is not a valid model identifier'   ):
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained('clip-base'   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with self.assertRaisesRegex(
          UpperCamelCase_			,  R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'   ):
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_			,  revision='aaaaaa'   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with self.assertRaisesRegex(
          UpperCamelCase_			,  'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'			,  ):
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model'   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      with self.assertRaises(UpperCamelCase_   ):
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'   )
      # If remote code is disabled, we can't load this config.
      with self.assertRaises(UpperCamelCase_   ):
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(
           'hf-internal-testing/test_dynamic_image_processor'			,  trust_remote_code=UpperCamelCase_   )

      __UpperCamelCase       						= AutoImageProcessor.from_pretrained(
          'hf-internal-testing/test_dynamic_image_processor'			,  trust_remote_code=UpperCamelCase_   )
      self.assertEqual(image_processor.__class__.__name__			,  'NewImageProcessor'   )

      # Test image processor can be reloaded.
      with tempfile.TemporaryDirectory() as tmp_dir:
       image_processor.save_pretrained(UpperCamelCase_   )
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_			,  trust_remote_code=UpperCamelCase_   )
      self.assertEqual(reloaded_image_processor.__class__.__name__			,  'NewImageProcessor'   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      try:
       AutoConfig.register('custom'			,  UpperCamelCase_   )
       AutoImageProcessor.register(UpperCamelCase_			,  UpperCamelCase_   )
       # Trying to register something existing in the Transformers library will raise an error
       with self.assertRaises(UpperCamelCase_   ):
        AutoImageProcessor.register(UpperCamelCase_			,  UpperCamelCase_   )

       with tempfile.TemporaryDirectory() as tmpdirname:
        __UpperCamelCase       						= Path(UpperCamelCase_   ) / "preprocessor_config.json"
        __UpperCamelCase       						= Path(UpperCamelCase_   ) / "config.json"
        json.dump(
            {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}			,  open(UpperCamelCase_			,  'w'   )			,  )
        json.dump({'model_type': 'clip'}			,  open(UpperCamelCase_			,  'w'   )   )

        __UpperCamelCase       						= CustomImageProcessor.from_pretrained(UpperCamelCase_   )

       # 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(UpperCamelCase_   )
        __UpperCamelCase       						= AutoImageProcessor.from_pretrained(UpperCamelCase_   )
        self.assertIsInstance(UpperCamelCase_			,  UpperCamelCase_   )

      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 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      class __lowerCAmelCase	(   __SCREAMING_SNAKE_CASE    ):
           lowercase		     =		True

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

       # If remote is enabled, we load from the Hub
       __UpperCamelCase       						= AutoImageProcessor.from_pretrained(
           'hf-internal-testing/test_dynamic_image_processor'			,  trust_remote_code=UpperCamelCase_   )
       self.assertEqual(image_processor.__class__.__name__			,  'NewImageProcessor'   )
       self.assertTrue(not hasattr(UpperCamelCase_			,  '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_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase			:       List[Any]									=				{
    "configuration_xlm_roberta": [
        "XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "XLMRobertaConfig",
        "XLMRobertaOnnxConfig",
    ],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase			:       Optional[Any]									=				["XLMRobertaTokenizer"]

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase			:       int									=				["XLMRobertaTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase			:       Optional[int]									=				[
          "XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
          "XLMRobertaForCausalLM",
          "XLMRobertaForMaskedLM",
          "XLMRobertaForMultipleChoice",
          "XLMRobertaForQuestionAnswering",
          "XLMRobertaForSequenceClassification",
          "XLMRobertaForTokenClassification",
          "XLMRobertaModel",
          "XLMRobertaPreTrainedModel",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase			:       List[str]									=				[
          "TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFXLMRobertaForCausalLM",
          "TFXLMRobertaForMaskedLM",
          "TFXLMRobertaForMultipleChoice",
          "TFXLMRobertaForQuestionAnswering",
          "TFXLMRobertaForSequenceClassification",
          "TFXLMRobertaForTokenClassification",
          "TFXLMRobertaModel",
          "TFXLMRobertaPreTrainedModel",
      ]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase			:       List[str]									=				[
          "FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
          "FlaxXLMRobertaForMaskedLM",
          "FlaxXLMRobertaForCausalLM",
          "FlaxXLMRobertaForMultipleChoice",
          "FlaxXLMRobertaForQuestionAnswering",
          "FlaxXLMRobertaForSequenceClassification",
          "FlaxXLMRobertaForTokenClassification",
          "FlaxXLMRobertaModel",
          "FlaxXLMRobertaPreTrainedModel",
      ]

if TYPE_CHECKING:
      from .configuration_xlm_roberta import (
          XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP,
          XLMRobertaConfig,
          XLMRobertaOnnxConfig,
      )

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_xlm_roberta import XLMRobertaTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_xlm_roberta import (
                XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
                XLMRobertaForCausalLM,
                XLMRobertaForMaskedLM,
                XLMRobertaForMultipleChoice,
                XLMRobertaForQuestionAnswering,
                XLMRobertaForSequenceClassification,
                XLMRobertaForTokenClassification,
                XLMRobertaModel,
                XLMRobertaPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_xlm_roberta import (
                TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFXLMRobertaForCausalLM,
                TFXLMRobertaForMaskedLM,
                TFXLMRobertaForMultipleChoice,
                TFXLMRobertaForQuestionAnswering,
                TFXLMRobertaForSequenceClassification,
                TFXLMRobertaForTokenClassification,
                TFXLMRobertaModel,
                TFXLMRobertaPreTrainedModel,
            )

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_xlm_roberta import (
                FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
                FlaxXLMRobertaForCausalLM,
                FlaxXLMRobertaForMaskedLM,
                FlaxXLMRobertaForMultipleChoice,
                FlaxXLMRobertaForQuestionAnswering,
                FlaxXLMRobertaForSequenceClassification,
                FlaxXLMRobertaForTokenClassification,
                FlaxXLMRobertaModel,
                FlaxXLMRobertaPreTrainedModel,
            )

else:
      import sys

      UpperCamelCase			:       Dict									=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



