




'''simple docstring'''



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 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						mock.Mock()
             SCREAMING_SNAKE_CASE   :      Tuple    					=						500
             SCREAMING_SNAKE_CASE   :      int    					=						{}
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						HTTPError
             SCREAMING_SNAKE_CASE   :      Tuple    					=						{}

             # Download this model to make sure it's in the cache.
             SCREAMING_SNAKE_CASE   :      Optional[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=A  ) as mock_head:
                   SCREAMING_SNAKE_CASE   :      int    					=						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  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						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  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						TOKEN
             HfFolder.save_token(A  )







       @classmethod
       def        UpperCamelCase_							(      cls  ):







             '''simple docstring'''







             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  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						WavaVecaFeatureExtractor.from_pretrained(A  )
             feature_extractor.push_to_hub('test-feature-extractor',							use_auth_token=self._token  )

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						WavaVecaFeatureExtractor.from_pretrained(F"{USER}/test-feature-extractor"  )
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(A,							getattr(A,							A  )  )

             # 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(
                       A,							repo_id='test-feature-extractor',							push_to_hub=A,							use_auth_token=self._token  )

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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						WavaVecaFeatureExtractor.from_pretrained(A  )
             feature_extractor.push_to_hub('valid_org/test-feature-extractor',							use_auth_token=self._token  )

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

             # 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(
                       A,							repo_id='valid_org/test-feature-extractor-org',							push_to_hub=A,							use_auth_token=self._token  )

             SCREAMING_SNAKE_CASE   :      str    					=						WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'  )
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(A,							getattr(A,							A  )  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             CustomFeatureExtractor.register_for_auto_class()
             SCREAMING_SNAKE_CASE   :      str    					=						CustomFeatureExtractor.from_pretrained(A  )

             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'},							)

             SCREAMING_SNAKE_CASE   :      Any    					=						AutoFeatureExtractor.from_pretrained(
                 F"{USER}/test-dynamic-feature-extractor",							trust_remote_code=A  )
             # 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'  )








'''simple docstring'''



from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


UpperCamelCase_    	=				logging.get_logger(__name__)


UpperCamelCase_    	=				TypeVar("DatasetType", Dataset, IterableDataset)




def   lowercase__(			__UpperCamelCase:					List[DatasetType]      ,__UpperCamelCase:					Optional[List[float]] = None      ,__UpperCamelCase:					Optional[int] = None      ,__UpperCamelCase:					Optional[DatasetInfo] = None      ,__UpperCamelCase:					Optional[NamedSplit] = None      ,__UpperCamelCase:					Literal["first_exhausted", "all_exhausted"] = "first_exhausted"      ,):

      """simple docstring"""






      from .arrow_dataset import Dataset
      from .iterable_dataset import IterableDataset

      if not datasets:
            raise ValueError('Unable to interleave an empty list of datasets.'    )
      for i, dataset in enumerate(__UpperCamelCase    ):
            if not isinstance(__UpperCamelCase      ,(Dataset, IterableDataset)    ):
                  if isinstance(__UpperCamelCase      ,(DatasetDict, IterableDatasetDict)    ):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'    )
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCamelCase    )}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase    )    )}']"    )
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase    ).__name__}."    )
            if i == 0:
                  SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						(
                      (Dataset, IterableDataset) if isinstance(__UpperCamelCase      ,__UpperCamelCase    ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCamelCase      ,__UpperCamelCase    ):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."    )
      if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
            raise ValueError(f"{stopping_strategy} is not supported. Please enter a valid stopping_strategy."    )
      if dataset_type is Dataset:
            return _interleave_map_style_datasets(
                __UpperCamelCase      ,__UpperCamelCase      ,__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,stopping_strategy=__UpperCamelCase    )
      else:
            return _interleave_iterable_datasets(
                __UpperCamelCase      ,__UpperCamelCase      ,__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,stopping_strategy=__UpperCamelCase    )





def   lowercase__(			__UpperCamelCase:					List[DatasetType]      ,__UpperCamelCase:					Optional[DatasetInfo] = None      ,__UpperCamelCase:					Optional[NamedSplit] = None      ,__UpperCamelCase:					int = 0      ,):

      """simple docstring"""






      if not dsets:
            raise ValueError('Unable to concatenate an empty list of datasets.'    )
      for i, dataset in enumerate(__UpperCamelCase    ):
            if not isinstance(__UpperCamelCase      ,(Dataset, IterableDataset)    ):
                  if isinstance(__UpperCamelCase      ,(DatasetDict, IterableDatasetDict)    ):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'    )
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCamelCase    )}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase    )    )}']"    )
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase    ).__name__}."    )
            if i == 0:
                  SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      int    					=						(
                      (Dataset, IterableDataset) if isinstance(__UpperCamelCase      ,__UpperCamelCase    ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCamelCase      ,__UpperCamelCase    ):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."    )
      if dataset_type is Dataset:
            return _concatenate_map_style_datasets(__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,axis=__UpperCamelCase    )
      else:
            return _concatenate_iterable_datasets(__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,axis=__UpperCamelCase    )


