






import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


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

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__UpperCAmelCase								=						get_tests_dir('fixtures')



class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		def 					__UpperCAmelCase			(							self   )	->							int:
							# A mock response for an HTTP head request to emulate server down
							UpperCAmelCase_    :      Optional[int]            =						mock.Mock()
							UpperCAmelCase_    :      Any            =						5_0_0
							UpperCAmelCase_    :      str            =						{}
							UpperCAmelCase_    :      Any            =						HTTPError
							UpperCAmelCase_    :      List[str]            =						{}

							# Download this model to make sure it's in the cache.
							UpperCAmelCase_    :      Union[str, Any]            =						WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'   )
							# Under the mock environment we get a 500 error when trying to reach the model.
							with mock.patch('requests.Session.request'				,		return_value=_UpperCamelCase   ) as mock_head:
												UpperCAmelCase_    :      Tuple            =						WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'   )
												# This check we did call the fake head request
												mock_head.assert_called()



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							# This test is for deprecated behavior and can be removed in v5
							UpperCAmelCase_    :      int            =						WavaVecaFeatureExtractor.from_pretrained(
							    'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'   )





@is_staging_test
class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		@classmethod
		def 					__UpperCAmelCase			(							cls   )	->							Tuple:
							UpperCAmelCase_    :      Any            =						TOKEN
							HfFolder.save_token(_UpperCamelCase   )



		@classmethod
		def 					__UpperCAmelCase			(							cls   )	->							Optional[int]:
							try:
												delete_repo(token=cls._token				,		repo_id='test-feature-extractor'   )
							except HTTPError:
												pass

							try:
												delete_repo(token=cls._token				,		repo_id='valid_org/test-feature-extractor-org'   )
							except HTTPError:
												pass

							try:
												delete_repo(token=cls._token				,		repo_id='test-dynamic-feature-extractor'   )
							except HTTPError:
												pass



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							UpperCAmelCase_    :      Any            =						WavaVecaFeatureExtractor.from_pretrained(_UpperCamelCase   )
							feature_extractor.push_to_hub('test-feature-extractor'				,		use_auth_token=self._token   )

							UpperCAmelCase_    :      List[Any]            =						WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"   )
							for k, v in feature_extractor.__dict__.items():
												self.assertEqual(_UpperCamelCase				,		getattr(_UpperCamelCase				,		_UpperCamelCase   )   )

							# Reset repo
							delete_repo(token=self._token				,		repo_id='test-feature-extractor'   )

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
												feature_extractor.save_pretrained(
												    _UpperCamelCase				,		repo_id='test-feature-extractor'				,		push_to_hub=_UpperCamelCase				,		use_auth_token=self._token   )

							UpperCAmelCase_    :      Optional[Any]            =						WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"   )
							for k, v in feature_extractor.__dict__.items():
												self.assertEqual(_UpperCamelCase				,		getattr(_UpperCamelCase				,		_UpperCamelCase   )   )



		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							UpperCAmelCase_    :      Union[str, Any]            =						WavaVecaFeatureExtractor.from_pretrained(_UpperCamelCase   )
							feature_extractor.push_to_hub('valid_org/test-feature-extractor'				,		use_auth_token=self._token   )

							UpperCAmelCase_    :      Dict            =						WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'   )
							for k, v in feature_extractor.__dict__.items():
												self.assertEqual(_UpperCamelCase				,		getattr(_UpperCamelCase				,		_UpperCamelCase   )   )

							# Reset repo
							delete_repo(token=self._token				,		repo_id='valid_org/test-feature-extractor'   )

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
												feature_extractor.save_pretrained(
												    _UpperCamelCase				,		repo_id='valid_org/test-feature-extractor-org'				,		push_to_hub=_UpperCamelCase				,		use_auth_token=self._token   )

							UpperCAmelCase_    :      List[str]            =						WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'   )
							for k, v in feature_extractor.__dict__.items():
												self.assertEqual(_UpperCamelCase				,		getattr(_UpperCamelCase				,		_UpperCamelCase   )   )



		def 					__UpperCAmelCase			(							self   )	->							int:
							CustomFeatureExtractor.register_for_auto_class()
							UpperCAmelCase_    :      Optional[int]            =						CustomFeatureExtractor.from_pretrained(_UpperCamelCase   )

							feature_extractor.push_to_hub('test-dynamic-feature-extractor'				,		use_auth_token=self._token   )

							# This has added the proper auto_map field to the config
							self.assertDictEqual(
							    feature_extractor.auto_map				,		{'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor'}				,		)

							UpperCAmelCase_    :      List[Any]            =						AutoFeatureExtractor.from_pretrained(
							    f"{USER}/test-dynamic-feature-extractor"				,		trust_remote_code=_UpperCamelCase   )
							# Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
							self.assertEqual(new_feature_extractor.__class__.__name__				,		'CustomFeatureExtractor'   )














def      lowercase__      (     __snake_case    :							Dict  ):


					'''simple docstring'''


					if not head:
										return True
					# split the list to two parts
					UpperCAmelCase_       ,  UpperCAmelCase_    :      Any            =						head.next, head
					while fast and fast.next:
										UpperCAmelCase_    :      str            =						fast.next.next
										UpperCAmelCase_    :      Union[str, Any]            =						slow.next
					UpperCAmelCase_    :      int            =						slow.next
					UpperCAmelCase_    :      List[Any]            =						None  # Don't forget here! But forget still works!
					# reverse the second part
					UpperCAmelCase_    :      Tuple            =						None
					while second:
										UpperCAmelCase_    :      int            =						second.next
										UpperCAmelCase_    :      Any            =						node
										UpperCAmelCase_    :      Optional[Any]            =						second
										UpperCAmelCase_    :      Tuple            =						nxt
					# compare two parts
					# second part has the same or one less node
					while node:
										if node.val != head.val:
															return False
										UpperCAmelCase_    :      Optional[Any]            =						node.next
										UpperCAmelCase_    :      Dict            =						head.next
					return True




def      lowercase__      (     __snake_case    :							Union[str, Any]  ):


					'''simple docstring'''


					if not head or not head.next:
										return True

					# 1. Get the midpoint (slow)
					UpperCAmelCase_    :      Any            =						head
					while fast and fast.next:
										UpperCAmelCase_       ,  UpperCAmelCase_    :      Tuple            =						fast.next.next, slow.next

					# 2. Push the second half into the stack
					UpperCAmelCase_    :      List[str]            =						[slow.val]
					while slow.next:
										UpperCAmelCase_    :      List[str]            =						slow.next
										stack.append(slow.val  )

					# 3. Comparison
					while stack:
										if stack.pop() != cur.val:
															return False
										UpperCAmelCase_    :      int            =						cur.next

					return True






def      lowercase__      (     __snake_case    :							Dict  ):


					'''simple docstring'''


					if not head or not head.next:
										return True
					UpperCAmelCase_    :      Tuple            =						{}
					UpperCAmelCase_    :      int            =						0
					while head:
										if head.val in d:
															d[head.val].append(__snake_case  )
										else:
															UpperCAmelCase_    :      List[Any]            =						[pos]
										UpperCAmelCase_    :      Any            =						head.next
										pos += 1
					UpperCAmelCase_    :      Dict            =						pos - 1
					UpperCAmelCase_    :      Optional[int]            =						0
					for v in d.values():
										if len(__snake_case  ) % 2 != 0:
															middle += 1
										else:
															UpperCAmelCase_    :      int            =						0
															for i in range(0     ,       len(__snake_case  )  ):
																				if v[i] + v[len(__snake_case  ) - 1 - step] != checksum:
																									return False
																				step += 1
										if middle > 1:
															return False
					return True






