




'''simple docstring'''





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


lowerCAmelCase_					    = get_tests_dir("""fixtures""")
lowerCAmelCase_					    = get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
lowerCAmelCase_					    = get_tests_dir("""fixtures/dummy-config.json""")






class  _lowerCAmelCase     (  unittest.TestCase	):

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[Any]		       =     0

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[int]		       =     AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''		)
        self.assertIsInstance(__A      ,				__A		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[str]		       =     AutoFeatureExtractor.from_pretrained(__A		)
        self.assertIsInstance(__A      ,				__A		)

      def 	__magic_name__(   self		):



        with tempfile.TemporaryDirectory() as tmpdirname:
          lowerCAmelCase__				:  Dict		       =     WavaVecaConfig()

          # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
          lowerCAmelCase__				:  Optional[Any]		       =     AutoFeatureExtractor.from_pretrained(__A		).to_dict()

          config_dict.pop('''feature_extractor_type'''		)
          lowerCAmelCase__				:  List[str]		       =     WavaVecaFeatureExtractor(**__A		)

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

          lowerCAmelCase__				:  Any		       =     AutoFeatureExtractor.from_pretrained(__A		)

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

        self.assertIsInstance(__A      ,				__A		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[Any]		       =     AutoFeatureExtractor.from_pretrained(__A		)
        self.assertIsInstance(__A      ,				__A		)

      def 	__magic_name__(   self		):



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

      def 	__magic_name__(   self		):



        with self.assertRaisesRegex(
            __A      ,				R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''		):
          lowerCAmelCase__				:  Optional[Any]		       =     AutoFeatureExtractor.from_pretrained(__A      ,				revision='''aaaaaa'''		)

      def 	__magic_name__(   self		):



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

      def 	__magic_name__(   self		):



        with self.assertRaises(__A		):
          lowerCAmelCase__				:  Union[str, Any]		       =     AutoFeatureExtractor.from_pretrained(
              '''hf-internal-testing/test_dynamic_feature_extractor'''		)
        # If remote code is disabled, we can't load this config.
        with self.assertRaises(__A		):
          lowerCAmelCase__				:  Optional[int]		       =     AutoFeatureExtractor.from_pretrained(
              '''hf-internal-testing/test_dynamic_feature_extractor'''      ,				trust_remote_code=__A		)

        lowerCAmelCase__				:  int		       =     AutoFeatureExtractor.from_pretrained(
            '''hf-internal-testing/test_dynamic_feature_extractor'''      ,				trust_remote_code=__A		)
        self.assertEqual(feature_extractor.__class__.__name__      ,				'''NewFeatureExtractor'''		)

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

      def 	__magic_name__(   self		):



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

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

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






        class  _lowerCAmelCase     (  UpperCamelCase__	):
              A__		     =		True

        try:
          AutoConfig.register('''custom'''      ,				__A		)
          AutoFeatureExtractor.register(__A      ,				__A		)
          # If remote code is not set, the default is to use local
          lowerCAmelCase__				:  Any		       =     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.
          lowerCAmelCase__				:  Optional[Any]		       =     AutoFeatureExtractor.from_pretrained(
              '''hf-internal-testing/test_dynamic_feature_extractor'''      ,				trust_remote_code=__A		)
          self.assertEqual(feature_extractor.__class__.__name__      ,				'''NewFeatureExtractor'''		)
          self.assertTrue(feature_extractor.is_local		)

          # If remote is enabled, we load from the Hub
          lowerCAmelCase__				:  Optional[int]		       =     AutoFeatureExtractor.from_pretrained(
              '''hf-internal-testing/test_dynamic_feature_extractor'''      ,				trust_remote_code=__A		)
          self.assertEqual(feature_extractor.__class__.__name__      ,				'''NewFeatureExtractor'''		)
          self.assertTrue(not hasattr(__A      ,				'''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 tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch






@require_torch
@require_tf
class  _lowerCAmelCase     (  _lowercase	):

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     SMALL_MODEL_IDENTIFIER
        lowerCAmelCase__				:  List[str]		       =     '''pt'''
        lowerCAmelCase__				:  int		       =     '''tf'''

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  Optional[Any]		       =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  str		       =     TFAutoModel.from_pretrained(self.test_model      ,				from_pt=__UpperCAmelCase		)
        model_tf.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  int		       =     '''mock_framework'''

        # Framework provided - return whatever the user provides
        lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model      ,				__UpperCAmelCase		)
        self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  List[str]		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
          with self.assertRaises(__UpperCAmelCase		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # PyTorch not in environment -> use TensorFlow
        lowerCAmelCase__				:  Union[str, Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  Optional[int]		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

        # Both in environment -> use PyTorch
        lowerCAmelCase__				:  Union[str, Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		), patch(
            '''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  List[Any]		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # Both not in environment -> raise error
        lowerCAmelCase__				:  Optional[int]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  Dict		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		), patch(
            '''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          with self.assertRaises(__UpperCAmelCase		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model		)

