
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


lowerCamelCase_						     =					get_tests_dir('''fixtures''')




class      __A(    unittest.TestCase			):

  """simple docstring"""







  def 	UpperCAmelCase_ (self						):
       # A mock response for an HTTP head request to emulate server down
       UpperCamelCase__				=    mock.Mock()
       UpperCamelCase__				=    5_00
       UpperCamelCase__				=    {}
       UpperCamelCase__				=    HTTPError
       UpperCamelCase__				=    {}

       # Download this model to make sure it's in the cache.
       UpperCamelCase__				=    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__				=    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						):
       # This test is for deprecated behavior and can be removed in v5
       UpperCamelCase__				=    WavaVecaFeatureExtractor.from_pretrained(
           """https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"""						)






@is_staging_test
class      __A(    unittest.TestCase			):

  """simple docstring"""







  @classmethod
  def 	UpperCAmelCase_ (cls						):
       UpperCamelCase__				=    TOKEN
       HfFolder.save_token(UpperCamelCase_						)







  @classmethod
  def 	UpperCAmelCase_ (cls						):
       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						):
       UpperCamelCase__				=    WavaVecaFeatureExtractor.from_pretrained(UpperCamelCase_						)
       feature_extractor.push_to_hub("""test-feature-extractor"""	,      use_auth_token=self._token						)

       UpperCamelCase__				=    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__				=    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						):
       UpperCamelCase__				=    WavaVecaFeatureExtractor.from_pretrained(UpperCamelCase_						)
       feature_extractor.push_to_hub("""valid_org/test-feature-extractor"""	,      use_auth_token=self._token						)

       UpperCamelCase__				=    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__				=    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						):
       CustomFeatureExtractor.register_for_auto_class()
       UpperCamelCase__				=    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__				=    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"""						)







import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows






def 						__magic_name__			(   __a      :	Any						):  # picklable for multiprocessing



     '''simple docstring'''




     return i + 1






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def 						__magic_name__			(   ):



     '''simple docstring'''




     with parallel_backend("""spark"""						):
          assert ParallelBackendConfig.backend_name == "spark"

     UpperCamelCase__				=    [1, 2, 3]
     with pytest.raises(__a						):
          with parallel_backend("""unsupported backend"""						):
               map_nested(__a	,		__a	,		num_proc=2						)

     with pytest.raises(__a						):
          with parallel_backend("""unsupported backend"""						):
               map_nested(__a	,		__a	,		num_proc=-1						)






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("""num_proc"""	,		[2, -1]						)
def 						__magic_name__			(   __a      :	Optional[int]						):



     '''simple docstring'''




     UpperCamelCase__				=    [1, 2]
     UpperCamelCase__				=    {"""a""": 1, """b""": 2}
     UpperCamelCase__				=    {"""a""": [1, 2], """b""": [3, 4]}
     UpperCamelCase__				=    {"""a""": {"""1""": 1}, """b""": 2}
     UpperCamelCase__				=    {"""a""": 1, """b""": 2, """c""": 3, """d""": 4}
     UpperCamelCase__				=    [2, 3]
     UpperCamelCase__				=    {"""a""": 2, """b""": 3}
     UpperCamelCase__				=    {"""a""": [2, 3], """b""": [4, 5]}
     UpperCamelCase__				=    {"""a""": {"""1""": 2}, """b""": 3}
     UpperCamelCase__				=    {"""a""": 2, """b""": 3, """c""": 4, """d""": 5}

     with parallel_backend("""spark"""						):
          assert map_nested(__a	,		__a	,		num_proc=__a						) == expected_map_nested_sa
          assert map_nested(__a	,		__a	,		num_proc=__a						) == expected_map_nested_sa
          assert map_nested(__a	,		__a	,		num_proc=__a						) == expected_map_nested_sa
          assert map_nested(__a	,		__a	,		num_proc=__a						) == expected_map_nested_sa
          assert map_nested(__a	,		__a	,		num_proc=__a						) == expected_map_nested_sa





