


'''simple docstring'''






import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

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

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


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
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


__A   :    Any											= get_tests_dir("fixtures/dummy_feature_extractor_config.json")
__A   :    Union[str, Any]											= get_tests_dir("fixtures/vocab.json")
__A   :    Optional[int]											= get_tests_dir("fixtures")






class   __snake_case			(  unittest.TestCase):







    """simple docstring"""



    lowercase  					=   ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou']

    def __lowercase  (  self       :    Dict						)  ->    int:
      lowerCAmelCase_     : List[Any]     =  0

    def __lowercase  (  self       :    Dict						)  ->    int:
      lowerCAmelCase_     : Tuple     =  AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""						)
      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    Any						)  ->    int:
      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCAmelCase_     : Any     =  WavaVecaConfig()
        lowerCAmelCase_     : str     =  AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""						)

        # save in new folder
        model_config.save_pretrained(lowerCamelCase						)
        processor.save_pretrained(lowerCamelCase						)

        lowerCAmelCase_     : Dict     =  AutoProcessor.from_pretrained(lowerCamelCase						)

      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    str						)  ->    Tuple:
      with tempfile.TemporaryDirectory() as tmpdirname:
        # copy relevant files
        copyfile(lowerCamelCase       ,		os.path.join(lowerCamelCase       ,		lowerCamelCase						)						)
        copyfile(lowerCamelCase       ,		os.path.join(lowerCamelCase       ,		"""vocab.json"""						)						)

        lowerCAmelCase_     : Optional[Any]     =  AutoProcessor.from_pretrained(lowerCamelCase						)

      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    str						)  ->    Union[str, Any]:
      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCAmelCase_     : Dict     =  WavaVecaFeatureExtractor()
        lowerCAmelCase_     : Tuple     =  AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""						)

        lowerCAmelCase_     : Union[str, Any]     =  WavaVecaProcessor(lowerCamelCase       ,		lowerCamelCase						)

        # save in new folder
        processor.save_pretrained(lowerCamelCase						)

        # drop `processor_class` in tokenizer
        with open(os.path.join(lowerCamelCase       ,		lowerCamelCase						)       ,		"""r"""						) as f:
          lowerCAmelCase_     : List[str]     =  json.load(lowerCamelCase						)
          config_dict.pop("""processor_class"""						)

        with open(os.path.join(lowerCamelCase       ,		lowerCamelCase						)       ,		"""w"""						) as f:
          f.write(json.dumps(lowerCamelCase						)						)

        lowerCAmelCase_     : List[str]     =  AutoProcessor.from_pretrained(lowerCamelCase						)

      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    Dict						)  ->    List[Any]:
      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCAmelCase_     : Dict     =  WavaVecaFeatureExtractor()
        lowerCAmelCase_     : Optional[int]     =  AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""						)

        lowerCAmelCase_     : List[Any]     =  WavaVecaProcessor(lowerCamelCase       ,		lowerCamelCase						)

        # save in new folder
        processor.save_pretrained(lowerCamelCase						)

        # drop `processor_class` in feature extractor
        with open(os.path.join(lowerCamelCase       ,		lowerCamelCase						)       ,		"""r"""						) as f:
          lowerCAmelCase_     : int     =  json.load(lowerCamelCase						)
          config_dict.pop("""processor_class"""						)

        with open(os.path.join(lowerCamelCase       ,		lowerCamelCase						)       ,		"""w"""						) as f:
          f.write(json.dumps(lowerCamelCase						)						)

        lowerCAmelCase_     : List[Any]     =  AutoProcessor.from_pretrained(lowerCamelCase						)

      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    List[str]						)  ->    List[str]:
      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCAmelCase_     : str     =  WavaVecaConfig(processor_class="""Wav2Vec2Processor"""						)
        model_config.save_pretrained(lowerCamelCase						)
        # copy relevant files
        copyfile(lowerCamelCase       ,		os.path.join(lowerCamelCase       ,		"""vocab.json"""						)						)
        # create emtpy sample processor
        with open(os.path.join(lowerCamelCase       ,		lowerCamelCase						)       ,		"""w"""						) as f:
          f.write("""{}"""						)

        lowerCAmelCase_     : Optional[int]     =  AutoProcessor.from_pretrained(lowerCamelCase						)

      self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

    def __lowercase  (  self       :    Any						)  ->    Any:
      # If remote code is not set, we will time out when asking whether to load the model.
      with self.assertRaises(lowerCamelCase						):
        lowerCAmelCase_     : Union[str, Any]     =  AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""						)
      # If remote code is disabled, we can't load this config.
      with self.assertRaises(lowerCamelCase						):
        lowerCAmelCase_     : Optional[int]     =  AutoProcessor.from_pretrained(
            """hf-internal-testing/test_dynamic_processor"""       ,		trust_remote_code=lowerCamelCase						)

      lowerCAmelCase_     : Optional[int]     =  AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""       ,		trust_remote_code=lowerCamelCase						)
      self.assertTrue(processor.special_attribute_present						)
      self.assertEqual(processor.__class__.__name__       ,		"""NewProcessor"""						)

      lowerCAmelCase_     : str     =  processor.feature_extractor
      self.assertTrue(feature_extractor.special_attribute_present						)
      self.assertEqual(feature_extractor.__class__.__name__       ,		"""NewFeatureExtractor"""						)

      lowerCAmelCase_     : Union[str, Any]     =  processor.tokenizer
      self.assertTrue(tokenizer.special_attribute_present						)
      if is_tokenizers_available():
        self.assertEqual(tokenizer.__class__.__name__       ,		"""NewTokenizerFast"""						)

        # Test we can also load the slow version
        lowerCAmelCase_     : str     =  AutoProcessor.from_pretrained(
            """hf-internal-testing/test_dynamic_processor"""       ,		trust_remote_code=lowerCamelCase       ,		use_fast=lowerCamelCase						)
        lowerCAmelCase_     : Optional[int]     =  new_processor.tokenizer
        self.assertTrue(new_tokenizer.special_attribute_present						)
        self.assertEqual(new_tokenizer.__class__.__name__       ,		"""NewTokenizer"""						)
      else:
        self.assertEqual(tokenizer.__class__.__name__       ,		"""NewTokenizer"""						)

    def __lowercase  (  self       :    List[str]						)  ->    Optional[int]:
      try:
        AutoConfig.register("""custom"""       ,		lowerCamelCase						)
        AutoFeatureExtractor.register(lowerCamelCase       ,		lowerCamelCase						)
        AutoTokenizer.register(lowerCamelCase       ,		slow_tokenizer_class=lowerCamelCase						)
        AutoProcessor.register(lowerCamelCase       ,		lowerCamelCase						)
        # Trying to register something existing in the Transformers library will raise an error
        with self.assertRaises(lowerCamelCase						):
          AutoProcessor.register(lowerCamelCase       ,		lowerCamelCase						)

        # Now that the config is registered, it can be used as any other config with the auto-API
        lowerCAmelCase_     : List[Any]     =  CustomFeatureExtractor.from_pretrained(lowerCamelCase						)

        with tempfile.TemporaryDirectory() as tmp_dir:
          lowerCAmelCase_     : List[Any]     =  os.path.join(lowerCamelCase       ,		"""vocab.txt"""						)
          with open(lowerCamelCase       ,		"""w"""       ,		encoding="""utf-8"""						) as vocab_writer:
            vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]						)						)
          lowerCAmelCase_     : int     =  CustomTokenizer(lowerCamelCase						)

        lowerCAmelCase_     : Union[str, Any]     =  CustomProcessor(lowerCamelCase       ,		lowerCamelCase						)

        with tempfile.TemporaryDirectory() as tmp_dir:
          processor.save_pretrained(lowerCamelCase						)
          lowerCAmelCase_     : Any     =  AutoProcessor.from_pretrained(lowerCamelCase						)
          self.assertIsInstance(lowerCamelCase       ,		lowerCamelCase						)

      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]
        if CustomConfig in TOKENIZER_MAPPING._extra_content:
          del TOKENIZER_MAPPING._extra_content[CustomConfig]
        if CustomConfig in PROCESSOR_MAPPING._extra_content:
          del PROCESSOR_MAPPING._extra_content[CustomConfig]

    def __lowercase  (  self       :    Optional[Any]						)  ->    List[str]:






      class   __snake_case			(  _SCREAMING_SNAKE_CASE):







          """simple docstring"""



          lowercase  					=   False







      class   __snake_case			(  _SCREAMING_SNAKE_CASE):







          """simple docstring"""



          lowercase  					=   False







      class   __snake_case			(  _SCREAMING_SNAKE_CASE):







          """simple docstring"""



          lowercase  					=   'AutoFeatureExtractor'
          lowercase  					=   'AutoTokenizer'
          lowercase  					=   False

      try:
        AutoConfig.register("""custom"""       ,		lowerCamelCase						)
        AutoFeatureExtractor.register(lowerCamelCase       ,		lowerCamelCase						)
        AutoTokenizer.register(lowerCamelCase       ,		slow_tokenizer_class=lowerCamelCase						)
        AutoProcessor.register(lowerCamelCase       ,		lowerCamelCase						)
        # If remote code is not set, the default is to use local classes.
        lowerCAmelCase_     : Union[str, Any]     =  AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""						)
        self.assertEqual(processor.__class__.__name__       ,		"""NewProcessor"""						)
        self.assertFalse(processor.special_attribute_present						)
        self.assertFalse(processor.feature_extractor.special_attribute_present						)
        self.assertFalse(processor.tokenizer.special_attribute_present						)

        # If remote code is disabled, we load the local ones.
        lowerCAmelCase_     : Optional[Any]     =  AutoProcessor.from_pretrained(
            """hf-internal-testing/test_dynamic_processor"""       ,		trust_remote_code=lowerCamelCase						)
        self.assertEqual(processor.__class__.__name__       ,		"""NewProcessor"""						)
        self.assertFalse(processor.special_attribute_present						)
        self.assertFalse(processor.feature_extractor.special_attribute_present						)
        self.assertFalse(processor.tokenizer.special_attribute_present						)

        # If remote is enabled, we load from the Hub.
        lowerCAmelCase_     : str     =  AutoProcessor.from_pretrained(
            """hf-internal-testing/test_dynamic_processor"""       ,		trust_remote_code=lowerCamelCase						)
        self.assertEqual(processor.__class__.__name__       ,		"""NewProcessor"""						)
        self.assertTrue(processor.special_attribute_present						)
        self.assertTrue(processor.feature_extractor.special_attribute_present						)
        self.assertTrue(processor.tokenizer.special_attribute_present						)

      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]
        if CustomConfig in TOKENIZER_MAPPING._extra_content:
          del TOKENIZER_MAPPING._extra_content[CustomConfig]
        if CustomConfig in PROCESSOR_MAPPING._extra_content:
          del PROCESSOR_MAPPING._extra_content[CustomConfig]

    def __lowercase  (  self       :    Any						)  ->    Tuple:
      lowerCAmelCase_     : Optional[Any]     =  AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-bert"""						)
      self.assertEqual(processor.__class__.__name__       ,		"""BertTokenizerFast"""						)





    def __lowercase  (  self       :    Optional[int]						)  ->    Optional[Any]:
      lowerCAmelCase_     : int     =  AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-convnext"""						)
      self.assertEqual(processor.__class__.__name__       ,		"""ConvNextImageProcessor"""						)







@is_staging_test
class   __snake_case			(  unittest.TestCase):







    """simple docstring"""



    lowercase  					=   ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou']

    @classmethod
    def __lowercase  (  cls       :    str						)  ->    Dict:
      lowerCAmelCase_     : Tuple     =  TOKEN
      HfFolder.save_token(lowerCamelCase						)

    @classmethod
    def __lowercase  (  cls       :    List[Any]						)  ->    Dict:
      try:
        delete_repo(token=cls._token       ,		repo_id="""test-processor"""						)
      except HTTPError:
        pass

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

      try:
        delete_repo(token=cls._token       ,		repo_id="""test-dynamic-processor"""						)
      except HTTPError:
        pass

    def __lowercase  (  self       :    Tuple						)  ->    int:
      lowerCAmelCase_     : int     =  WavaVecaProcessor.from_pretrained(lowerCamelCase						)
      with tempfile.TemporaryDirectory() as tmp_dir:
        processor.save_pretrained(
            os.path.join(lowerCamelCase       ,		"""test-processor"""						)       ,		push_to_hub=lowerCamelCase       ,		use_auth_token=self._token						)

        lowerCAmelCase_     : int     =  WavaVecaProcessor.from_pretrained(F'{USER}/test-processor'						)
        for k, v in processor.feature_extractor.__dict__.items():
          self.assertEqual(lowerCamelCase       ,		getattr(new_processor.feature_extractor       ,		lowerCamelCase						)						)
        self.assertDictEqual(new_processor.tokenizer.get_vocab()       ,		processor.tokenizer.get_vocab()						)

    def __lowercase  (  self       :    List[str]						)  ->    int:
      lowerCAmelCase_     : Any     =  WavaVecaProcessor.from_pretrained(lowerCamelCase						)

      with tempfile.TemporaryDirectory() as tmp_dir:
        processor.save_pretrained(
            os.path.join(lowerCamelCase       ,		"""test-processor-org"""						)       ,		push_to_hub=lowerCamelCase       ,		use_auth_token=self._token       ,		organization="""valid_org"""       ,		)

        lowerCAmelCase_     : Optional[int]     =  WavaVecaProcessor.from_pretrained("""valid_org/test-processor-org"""						)
        for k, v in processor.feature_extractor.__dict__.items():
          self.assertEqual(lowerCamelCase       ,		getattr(new_processor.feature_extractor       ,		lowerCamelCase						)						)
        self.assertDictEqual(new_processor.tokenizer.get_vocab()       ,		processor.tokenizer.get_vocab()						)





    def __lowercase  (  self       :    Any						)  ->    Tuple:
      CustomFeatureExtractor.register_for_auto_class()
      CustomTokenizer.register_for_auto_class()
      CustomProcessor.register_for_auto_class()

      lowerCAmelCase_     : List[str]     =  CustomFeatureExtractor.from_pretrained(lowerCamelCase						)

      with tempfile.TemporaryDirectory() as tmp_dir:
        lowerCAmelCase_     : Optional[int]     =  os.path.join(lowerCamelCase       ,		"""vocab.txt"""						)
        with open(lowerCamelCase       ,		"""w"""       ,		encoding="""utf-8"""						) as vocab_writer:
          vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]						)						)
        lowerCAmelCase_     : Dict     =  CustomTokenizer(lowerCamelCase						)

      lowerCAmelCase_     : Optional[Any]     =  CustomProcessor(lowerCamelCase       ,		lowerCamelCase						)

      with tempfile.TemporaryDirectory() as tmp_dir:
        create_repo(F'{USER}/test-dynamic-processor'       ,		token=self._token						)
        lowerCAmelCase_     : Union[str, Any]     =  Repository(lowerCamelCase       ,		clone_from=F'{USER}/test-dynamic-processor'       ,		token=self._token						)
        processor.save_pretrained(lowerCamelCase						)

        # This has added the proper auto_map field to the feature extractor config
        self.assertDictEqual(
            processor.feature_extractor.auto_map       ,		{
                """AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor""",
                """AutoProcessor""": """custom_processing.CustomProcessor""",
            }       ,		)

        # This has added the proper auto_map field to the tokenizer config
        with open(os.path.join(lowerCamelCase       ,		"""tokenizer_config.json"""						)						) as f:
          lowerCAmelCase_     : int     =  json.load(lowerCamelCase						)
        self.assertDictEqual(
            tokenizer_config["""auto_map"""]       ,		{
                """AutoTokenizer""": ["""custom_tokenization.CustomTokenizer""", None],
                """AutoProcessor""": """custom_processing.CustomProcessor""",
            }       ,		)

        # The code has been copied from fixtures
        self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase       ,		"""custom_feature_extraction.py"""						)						)						)
        self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase       ,		"""custom_tokenization.py"""						)						)						)
        self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase       ,		"""custom_processing.py"""						)						)						)

        repo.push_to_hub()

      lowerCAmelCase_     : Optional[Any]     =  AutoProcessor.from_pretrained(F'{USER}/test-dynamic-processor'       ,		trust_remote_code=lowerCamelCase						)
      # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
      self.assertEqual(new_processor.__class__.__name__       ,		"""CustomProcessor"""						)







'''simple docstring'''






from __future__ import annotations







def 							UpperCamelCase_     (       A__   :  list				,       A__   :  int				,       A__   :  int				,       A__   :  int   ):
  '''simple docstring'''





  lowerCAmelCase_     : int     =  []
  lowerCAmelCase_,				lowerCAmelCase_     : Any     =  input_list[low:mid], input_list[mid : high + 1]
  while left and right:
    result.append((left if left[0] <= right[0] else right).pop(0   )   )
  lowerCAmelCase_     : Dict     =  result + left + right
  return input_list







def 							UpperCamelCase_     (       A__   :  list   ):
  '''simple docstring'''





  if len(A__   ) <= 1:
    return input_list
  lowerCAmelCase_     : str     =  list(A__   )

  # iteration for two-way merging
  lowerCAmelCase_     : Dict     =  2
  while p <= len(A__   ):
    # getting low, high and middle value for merge-sort of single list
    for i in range(0				,       len(A__   )				,       A__   ):
      lowerCAmelCase_     : Tuple     =  i
      lowerCAmelCase_     : List[str]     =  i + p - 1
      lowerCAmelCase_     : Optional[Any]     =  (low + high + 1) // 2
      lowerCAmelCase_     : List[str]     =  merge(A__				,       A__				,       A__				,       A__   )
    # final merge of last two parts
    if p * 2 >= len(A__   ):
      lowerCAmelCase_     : Union[str, Any]     =  i
      lowerCAmelCase_     : Dict     =  merge(A__				,       0				,       A__				,       len(A__   ) - 1   )
      break
    p *= 2

  return input_list


if __name__ == "__main__":
  __A   :    int											= input("Enter numbers separated by a comma:\n").strip()
  if user_input == "":
    __A   :    str											= []
  else:
    __A   :    Optional[Any]											= [int(item.strip()) for item in user_input.split(",")]
  print(iter_merge_sort(unsorted))



