






import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__a      :       Any				  =   get_tests_dir("fixtures")
__a      :       int				  =   get_tests_dir("fixtures/dummy_feature_extractor_config.json")
__a      :       Optional[int]				  =   get_tests_dir("fixtures/dummy-config.json")


class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         __A         =	0




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



         __A         =	AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""				)
         self.assertIsInstance(UpperCamelCase_ ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_				)
         self.assertIsInstance(UpperCamelCase_ ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



         with tempfile.TemporaryDirectory() as tmpdirname:
             __A         =	WavaVecaConfig()

             # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
             __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_				).to_dict()

             config_dict.pop("""feature_extractor_type"""				)
             __A         =	WavaVecaFeatureExtractor(**UpperCamelCase_				)

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

             __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_				)

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

         self.assertIsInstance(UpperCamelCase_ ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_				)
         self.assertIsInstance(UpperCamelCase_ ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



         with self.assertRaisesRegex(
             UpperCamelCase_ ,	"""bert-base is not a local folder and is not a valid model identifier"""				):
             __A         =	AutoFeatureExtractor.from_pretrained("""bert-base"""				)




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



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

         __A         =	AutoFeatureExtractor.from_pretrained(
             """hf-internal-testing/test_dynamic_feature_extractor""" ,	trust_remote_code=UpperCamelCase_				)
         self.assertEqual(feature_extractor.__class__.__name__ ,	"""NewFeatureExtractor"""				)

         # Test feature extractor can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
             feature_extractor.save_pretrained(UpperCamelCase_				)
             __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_ ,	trust_remote_code=UpperCamelCase_				)
         self.assertEqual(reloaded_feature_extractor.__class__.__name__ ,	"""NewFeatureExtractor"""				)




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



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

             # Now that the config is registered, it can be used as any other config with the auto-API
             __A         =	CustomFeatureExtractor.from_pretrained(UpperCamelCase_				)
             with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(UpperCamelCase_				)
                 __A         =	AutoFeatureExtractor.from_pretrained(UpperCamelCase_				)
                 self.assertIsInstance(UpperCamelCase_ ,	UpperCamelCase_				)

         finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                 del CONFIG_MAPPING._extra_content["custom"]
             if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                 del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         class       __lowercase      (	lowercase_  ):







              '''simple docstring'''
              SCREAMING_SNAKE_CASE   						=				True

         try:
             AutoConfig.register("""custom""" ,	UpperCamelCase_				)
             AutoFeatureExtractor.register(UpperCamelCase_ ,	UpperCamelCase_				)
             # If remote code is not set, the default is to use local
             __A         =	AutoFeatureExtractor.from_pretrained(
                 """hf-internal-testing/test_dynamic_feature_extractor"""				)
             self.assertEqual(feature_extractor.__class__.__name__ ,	"""NewFeatureExtractor"""				)
             self.assertTrue(feature_extractor.is_local				)

             # If remote code is disabled, we load the local one.
             __A         =	AutoFeatureExtractor.from_pretrained(
                 """hf-internal-testing/test_dynamic_feature_extractor""" ,	trust_remote_code=UpperCamelCase_				)
             self.assertEqual(feature_extractor.__class__.__name__ ,	"""NewFeatureExtractor"""				)
             self.assertTrue(feature_extractor.is_local				)

             # If remote is enabled, we load from the Hub
             __A         =	AutoFeatureExtractor.from_pretrained(
                 """hf-internal-testing/test_dynamic_feature_extractor""" ,	trust_remote_code=UpperCamelCase_				)
             self.assertEqual(feature_extractor.__class__.__name__ ,	"""NewFeatureExtractor"""				)
             self.assertTrue(not hasattr(UpperCamelCase_ ,	"""is_local"""				)				)

         finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                 del CONFIG_MAPPING._extra_content["custom"]
             if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                 del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]












from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      ImageTextPipelineOutput,
      UniDiffuserPipeline,
  )
else:
  from .modeling_text_decoder import UniDiffuserTextDecoder
  from .modeling_uvit import UniDiffuserModel, UTransformeraDModel
  from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline




