

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      :					Tuple					    =      get_tests_dir('''fixtures''')
_lowerCamelCase      :					int					    =      get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
_lowerCamelCase      :					Optional[int]					    =      get_tests_dir('''fixtures/dummy-config.json''')





class        lowerCAmelCase__  (					unittest.TestCase			):



       '''simple docstring'''







       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =0

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''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(lowercase__     ).to_dict()

                   config_dict.pop('''feature_extractor_type'''     )
                   __A    =WavaVecaFeatureExtractor(**lowercase__     )

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

                   __A    =AutoFeatureExtractor.from_pretrained(lowercase__     )

                   # 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(lowercase__ ,   lowercase__     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             with self.assertRaisesRegex(
                 lowercase__ ,   '''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     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             with self.assertRaises(lowercase__     ):
                   __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(lowercase__     ):
                   __A    =AutoFeatureExtractor.from_pretrained(
                       '''hf-internal-testing/test_dynamic_feature_extractor''' ,   trust_remote_code=lowercase__     )

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

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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

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

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







             '''simple docstring'''



             class        lowerCAmelCase__  (					__magic_name__			):



                    '''simple docstring'''







                    lowercase_						=    True

             try:
                   AutoConfig.register('''custom''' ,   lowercase__     )
                   AutoFeatureExtractor.register(lowercase__ ,   lowercase__     )
                   # 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=lowercase__     )
                   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=lowercase__     )
                   self.assertEqual(feature_extractor.__class__.__name__ ,   '''NewFeatureExtractor'''     )
                   self.assertTrue(not hasattr(lowercase__ ,   '''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 collections import deque





def      A__					(			__A  :						Optional[Any]			)					->Tuple:
      __A    =len(__A			)
      __A    =deque()
      __A    =[False for _ in range(__A			)]
      __A    =[-1 for _ in range(__A			)]
      __A    =index_of[:]

      def strong_connect(__A  :						Union[str, Any]     ,				__A  :						int     ,				__A  :						Optional[int]			):
            __A    =index  # the number when this node is seen
            __A    =index  # lowest rank node reachable from here
            index += 1
            stack.append(__A			)
            __A    =True

            for w in g[v]:
                  if index_of[w] == -1:
                        __A    =strong_connect(__A     ,				__A     ,				__A			)
                        __A    =(
                            lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
                        )
                  elif on_stack[w]:
                        __A    =(
                            lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
                        )

            if lowlink_of[v] == index_of[v]:
                  __A    =[]
                  __A    =stack.pop()
                  __A    =False
                  component.append(__A			)
                  while w != v:
                        __A    =stack.pop()
                        __A    =False
                        component.append(__A			)
                  components.append(__A			)
            return index

      __A    =[]
      for v in range(__A			):
            if index_of[v] == -1:
                  strong_connect(__A     ,				0     ,				__A			)

      return components





def      A__					(			__A  :						List[Any]     ,				__A  :						Optional[int]			)					->Tuple:
      __A    =[[] for _ in range(__A			)]
      for u, v in edges:
            g[u].append(__A			)
      return g


if __name__ == "__main__":
   # Test
   _lowerCamelCase      :					int					    =      7
   _lowerCamelCase      :					Tuple					    =      [0, 0, 1, 2, 3, 3, 4, 4, 6]
   _lowerCamelCase      :					Dict					    =      [1, 3, 2, 0, 1, 4, 5, 6, 5]
   _lowerCamelCase      :					Optional[Any]					    =      [(u, v) for u, v in zip(source, target)]
   _lowerCamelCase      :					Dict					    =      create_graph(n_vertices, edges)

   assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)


