





'''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


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


class   _lowercase						(							unittest.TestCase				):


   def 					snake_case				( self					):
       A       :       str							=						0


   def 					snake_case				( self					):
       A       :       int							=						AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''					)
       self.assertIsInstance(_UpperCAmelCase    ,   _UpperCAmelCase					)


   def 					snake_case				( self					):
       A       :       int							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase					)
       self.assertIsInstance(_UpperCAmelCase    ,   _UpperCAmelCase					)


   def 					snake_case				( self					):
       with tempfile.TemporaryDirectory() as tmpdirname:
           A       :       Any							=						WavaVecaConfig()

           # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
           A       :       List[str]							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase					).to_dict()

           config_dict.pop('''feature_extractor_type'''					)
           A       :       Optional[int]							=						WavaVecaFeatureExtractor(**_UpperCAmelCase					)

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

           A       :       Tuple							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase					)

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

       self.assertIsInstance(_UpperCAmelCase    ,   _UpperCAmelCase					)


   def 					snake_case				( self					):
       A       :       List[Any]							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase					)
       self.assertIsInstance(_UpperCAmelCase    ,   _UpperCAmelCase					)


   def 					snake_case				( self					):
       with self.assertRaisesRegex(
           _UpperCAmelCase    ,   '''bert-base is not a local folder and is not a valid model identifier'''					):
           A       :       str							=						AutoFeatureExtractor.from_pretrained('''bert-base'''					)


   def 					snake_case				( self					):
       with self.assertRaisesRegex(
           _UpperCAmelCase    ,   R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
           A       :       Optional[int]							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase    ,   revision='''aaaaaa'''					)


   def 					snake_case				( self					):
       with self.assertRaisesRegex(
           _UpperCAmelCase    ,   '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''    ,   ):
           A       :       Optional[Any]							=						AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''					)


   def 					snake_case				( self					):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(_UpperCAmelCase					):
           A       :       List[str]							=						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       :       Dict							=						AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''    ,   trust_remote_code=_UpperCAmelCase					)

       A       :       List[Any]							=						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       :       Union[str, Any]							=						AutoFeatureExtractor.from_pretrained(_UpperCAmelCase    ,   trust_remote_code=_UpperCAmelCase					)
       self.assertEqual(reloaded_feature_extractor.__class__.__name__    ,   '''NewFeatureExtractor'''					)


   def 					snake_case				( self					):
       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       :       Dict							=						CustomFeatureExtractor.from_pretrained(_UpperCAmelCase					)
           with tempfile.TemporaryDirectory() as tmp_dir:
               feature_extractor.save_pretrained(_UpperCAmelCase					)
               A       :       Optional[int]							=						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 					snake_case				( self					):


       class   _lowercase						(							a				):
          _UpperCamelCase       		=   True

       try:
           AutoConfig.register('''custom'''    ,   _UpperCAmelCase					)
           AutoFeatureExtractor.register(_UpperCAmelCase    ,   _UpperCAmelCase					)
           # If remote code is not set, the default is to use local
           A       :       int							=						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       :       List[str]							=						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       :       int							=						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]







'''simple docstring'''






def 							_lowerCamelCase(  UpperCamelCase__    : Optional[int]    ,       UpperCamelCase__    : str		)		->  Optional[int]:
    A       :       Optional[int]							=						0
    A       :       str							=						len(UpperCamelCase__		) - 1

    while left <= right:
        # avoid divided by 0 during interpolation
        if sorted_collection[left] == sorted_collection[right]:
            if sorted_collection[left] == item:
                return left
            else:
                return None

        A       :       Any							=						left + ((item - sorted_collection[left]) * (right - left)) // (
            sorted_collection[right] - sorted_collection[left]
        )

        # out of range check
        if point < 0 or point >= len(UpperCamelCase__		):
            return None

        A       :       Union[str, Any]							=						sorted_collection[point]
        if current_item == item:
            return point
        else:
            if point < left:
                A       :       Tuple							=						left
                A       :       Tuple							=						point
            elif point > right:
                A       :       Optional[Any]							=						right
                A       :       str							=						point
            else:
                if item < current_item:
                    A       :       Tuple							=						point - 1
                else:
                    A       :       Optional[Any]							=						point + 1
    return None
def 							_lowerCamelCase(  UpperCamelCase__    : Dict    ,       UpperCamelCase__    : str    ,       UpperCamelCase__    : Dict    ,       UpperCamelCase__    : List[str]		)		->  List[Any]:

    # avoid divided by 0 during interpolation
    if sorted_collection[left] == sorted_collection[right]:
        if sorted_collection[left] == item:
            return left
        else:
            return None

    A       :       Dict							=						left + ((item - sorted_collection[left]) * (right - left)) // (
        sorted_collection[right] - sorted_collection[left]
    )

    # out of range check
    if point < 0 or point >= len(UpperCamelCase__		):
        return None

    if sorted_collection[point] == item:
        return point
    elif point < left:
        return interpolation_search_by_recursion(UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__		)
    elif point > right:
        return interpolation_search_by_recursion(UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__		)
    else:
        if sorted_collection[point] > item:
            return interpolation_search_by_recursion(
                UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__    ,       point - 1		)
        else:
            return interpolation_search_by_recursion(
                UpperCamelCase__    ,       UpperCamelCase__    ,       point + 1    ,       UpperCamelCase__		)
def 							_lowerCamelCase(  UpperCamelCase__    : Optional[int]		)		->  List[Any]:
    if collection != sorted(UpperCamelCase__		):
        raise ValueError('''Collection must be ascending sorted'''		)
    return True


if __name__ == "__main__":
     import sys
     snake_case_     =	0
     if debug == 1:
          snake_case_     =	[10, 30, 40, 45, 50, 66, 77, 93]
          try:
               __assert_sorted(collection)
          except ValueError:
               sys.exit("""Sequence must be ascending sorted to apply interpolation search""")
          snake_case_     =	67

     snake_case_     =	interpolation_search(collection, target)
     if result is not None:
          print(f'''{target} found at positions: {result}''')
     else:
          print("""Not found""")
