






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__       =     get_tests_dir("""fixtures""")
a__       =     get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
a__       =     get_tests_dir("""fixtures/dummy-config.json""")


class    _lowerCAmelCase			(		unittest.TestCase      ):



				"""simple docstring"""



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											snake_case__	  =       0



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    Any):






											'''simple docstring'''







											snake_case__	  =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__)
											self.assertIsInstance(UpperCamelCase__			,		UpperCamelCase__)



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







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

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

																		config_dict.pop("""feature_extractor_type""")
																		snake_case__	  =       WavaVecaFeatureExtractor(**UpperCamelCase__)

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

																		snake_case__	  =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__)

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

											self.assertIsInstance(UpperCamelCase__			,		UpperCamelCase__)



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__)
											self.assertIsInstance(UpperCamelCase__			,		UpperCamelCase__)



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    Any):






											'''simple docstring'''







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

											snake_case__	  =       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__)
																		snake_case__	  =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__			,		trust_remote_code=UpperCamelCase__)
											self.assertEqual(reloaded_feature_extractor.__class__.__name__			,		"""NewFeatureExtractor""")



				def 					__magic_name__							(      self    :    Optional[int]):






											'''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
																		snake_case__	  =       CustomFeatureExtractor.from_pretrained(UpperCamelCase__)
																		with tempfile.TemporaryDirectory() as tmp_dir:
																									feature_extractor.save_pretrained(UpperCamelCase__)
																									snake_case__	  =       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 					__magic_name__							(      self    :    List[Any]):






											'''simple docstring'''







											class    _lowerCAmelCase			(		lowercase_      ):



															"""simple docstring"""



															_lowercase				:   Optional[Any]      =  True

											try:
																		AutoConfig.register("""custom"""			,		UpperCamelCase__)
																		AutoFeatureExtractor.register(UpperCamelCase__			,		UpperCamelCase__)
																		# If remote code is not set, the default is to use local
																		snake_case__	  =       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.
																		snake_case__	  =       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
																		snake_case__	  =       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 collections.abc import Callable

def _UpperCAmelCase   (	a						:	Callable[[float], float]  ,					a						:	float  ,					a						:	float ):
							snake_case__	  =       a
							snake_case__	  =       b
							if function(a ) == 0:  # one of the a or b is a root for the function
														return a
							elif function(a ) == 0:
														return b
							elif (
							    function(a ) * function(a ) > 0
							):  # if none of these are root and they are both positive or negative,
														# then this algorithm can't find the root
														raise ValueError("""could not find root in given interval.""" )
							else:
														snake_case__	  =       start + (end - start) / 2.0
														while abs(start - mid ) > 10**-7:  # until precisely equals to 10^-7
																					if function(a ) == 0:
																												return mid
																					elif function(a ) * function(a ) < 0:
																												snake_case__	  =       mid
																					else:
																												snake_case__	  =       mid
																					snake_case__	  =       start + (end - start) / 2.0
														return mid

def _UpperCAmelCase   (	a						:	float ):
							return x**3 - 2 * x - 5


if __name__ == "__main__":
	print(bisection(f, 1, 1_0_0_0))

	import doctest

	doctest.testmod()
