


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





    """simple docstring"""






    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      List[Any]														=						0







    def  lowercase_							(				self      )							->	Dict:
         __lowerCamelCase  :      Dict														=						AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'      )
         self.assertIsInstance(UpperCamelCase_     ,	UpperCamelCase_      )







    def  lowercase_							(				self      )							->	Optional[int]:

         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase  :      Union[str, Any]														=						Path(UpperCamelCase_      ) / 'preprocessor_config.json'
              __lowerCamelCase  :      Optional[int]														=						Path(UpperCamelCase_      ) / 'config.json'
              json.dump(
                  {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}     ,	open(UpperCamelCase_     ,	'w'      )     ,	)
              json.dump({'model_type': 'clip'}     ,	open(UpperCamelCase_     ,	'w'      )      )

              __lowerCamelCase  :      Optional[int]														=						AutoImageProcessor.from_pretrained(UpperCamelCase_      )
              self.assertIsInstance(UpperCamelCase_     ,	UpperCamelCase_      )







    def  lowercase_							(				self      )							->	int:

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

              __lowerCamelCase  :      List[Any]														=						AutoImageProcessor.from_pretrained(UpperCamelCase_      )
              self.assertIsInstance(UpperCamelCase_     ,	UpperCamelCase_      )







    def  lowercase_							(				self      )							->	Dict:

         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase  :      Tuple														=						CLIPConfig()

              # Create a dummy config file with image_proceesor_type
              __lowerCamelCase  :      str														=						Path(UpperCamelCase_      ) / 'preprocessor_config.json'
              __lowerCamelCase  :      Tuple														=						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
              __lowerCamelCase  :      int														=						AutoImageProcessor.from_pretrained(UpperCamelCase_      ).to_dict()

              config_dict.pop('image_processor_type'      )
              __lowerCamelCase  :      Optional[Any]														=						CLIPImageProcessor(**UpperCamelCase_      )

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

              __lowerCamelCase  :      Any														=						AutoImageProcessor.from_pretrained(UpperCamelCase_      )

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

         self.assertIsInstance(UpperCamelCase_     ,	UpperCamelCase_      )







    def  lowercase_							(				self      )							->	Dict:

         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase  :      List[str]														=						Path(UpperCamelCase_      ) / 'preprocessor_config.json'
              json.dump(
                  {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}     ,	open(UpperCamelCase_     ,	'w'      )     ,	)

              __lowerCamelCase  :      Tuple														=						AutoImageProcessor.from_pretrained(UpperCamelCase_      )
              self.assertIsInstance(UpperCamelCase_     ,	UpperCamelCase_      )







    def  lowercase_							(				self      )							->	Any:

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







    def  lowercase_							(				self      )							->	int:

         with self.assertRaisesRegex(
             UpperCamelCase_     ,	r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'      ):
              __lowerCamelCase  :      Optional[int]														=						AutoImageProcessor.from_pretrained(UpperCamelCase_     ,	revision='aaaaaa'      )







    def  lowercase_							(				self      )							->	str:

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







    def  lowercase_							(				self      )							->	str:

         with self.assertRaises(UpperCamelCase_      ):
              __lowerCamelCase  :      Tuple														=						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_      ):
              __lowerCamelCase  :      Dict														=						AutoImageProcessor.from_pretrained(
                  'hf-internal-testing/test_dynamic_image_processor'     ,	trust_remote_code=UpperCamelCase_      )

         __lowerCamelCase  :      Optional[int]														=						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_      )
              __lowerCamelCase  :      List[Any]														=						AutoImageProcessor.from_pretrained(UpperCamelCase_     ,	trust_remote_code=UpperCamelCase_      )
         self.assertEqual(reloaded_image_processor.__class__.__name__     ,	'NewImageProcessor'      )







    def  lowercase_							(				self      )							->	str:

         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:
                   __lowerCamelCase  :      Tuple														=						Path(UpperCamelCase_      ) / 'preprocessor_config.json'
                   __lowerCamelCase  :      List[Any]														=						Path(UpperCamelCase_      ) / 'config.json'
                   json.dump(
                       {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}     ,	open(UpperCamelCase_     ,	'w'      )     ,	)
                   json.dump({'model_type': 'clip'}     ,	open(UpperCamelCase_     ,	'w'      )      )

                   __lowerCamelCase  :      Optional[int]														=						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_      )
                   __lowerCamelCase  :      Any														=						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  lowercase_							(				self      )							->	Dict:





         class   UpperCAmelCase_    (a_    ):





             """simple docstring"""






             lowerCamelCase    :   List[str]					=     True

         try:
              AutoConfig.register('custom'     ,	UpperCamelCase_      )
              AutoImageProcessor.register(UpperCamelCase_     ,	UpperCamelCase_      )
              # If remote code is not set, the default is to use local
              __lowerCamelCase  :      List[Any]														=						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.
              __lowerCamelCase  :      Optional[int]														=						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
              __lowerCamelCase  :      str														=						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_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case : int     		=		{}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : str     		=		["""NllbTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : Dict     		=		["""NllbTokenizerFast"""]


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb import NllbTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb_fast import NllbTokenizerFast

else:
 import sys

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