


import copy
import tempfile
import unittest

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

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test





class 					A							(    unittest.TestCase					):


   @parameterized.expand([(None,), ("foo.json",)]		)
   def       lowercase_				(self					:						Optional[Any]	,  __UpperCAmelCase					:						Optional[Any]		)						->    List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE	,  temperature=0.7	,  length_penalty=1.0	,  bad_words_ids=[[1, 2, 3], [4, 5]]	,  )
         with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(_SCREAMING_SNAKE_CASE	,  config_name=_SCREAMING_SNAKE_CASE		)
               UpperCAmelCase__								=  GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE	,  config_name=_SCREAMING_SNAKE_CASE		)

         # Checks parameters that were specified
         self.assertEqual(loaded_config.do_sample	,  _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.temperature	,  0.7		)
         self.assertEqual(loaded_config.length_penalty	,  1.0		)
         self.assertEqual(loaded_config.bad_words_ids	,  [[1, 2, 3], [4, 5]]		)

         # Checks parameters that were not specified (defaults)
         self.assertEqual(loaded_config.top_k	,  5_0		)
         self.assertEqual(loaded_config.max_length	,  2_0		)
         self.assertEqual(loaded_config.max_time	,  _SCREAMING_SNAKE_CASE		)


   def       lowercase_				(self					:						Optional[int]		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  AutoConfig.from_pretrained("gpt2"		)
         UpperCAmelCase__								=  GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         UpperCAmelCase__								=  GenerationConfig()

         # The generation config has loaded a few non-default parameters from the model config
         self.assertNotEqual(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)

         # One of those parameters is eos_token_id -- check if it matches
         self.assertNotEqual(generation_config_from_model.eos_token_id	,  default_generation_config.eos_token_id		)
         self.assertEqual(generation_config_from_model.eos_token_id	,  model_config.eos_token_id		)


   def       lowercase_				(self					:						Optional[Any]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig()
         UpperCAmelCase__								=  {
             "max_new_tokens": 1_0_2_4,
             "foo": "bar",
         }
         UpperCAmelCase__								=  copy.deepcopy(_SCREAMING_SNAKE_CASE		)
         UpperCAmelCase__								=  generation_config.update(**_SCREAMING_SNAKE_CASE		)

         # update_kwargs was not modified (no side effects)
         self.assertEqual(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)

         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(generation_config.max_new_tokens	,  1_0_2_4		)

         # `.update()` returns a dictionary of unused kwargs
         self.assertEqual(_SCREAMING_SNAKE_CASE	,  {"foo": "bar"}		)


   def       lowercase_				(self					:						Tuple		)						->    str:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig()
         UpperCAmelCase__								=  "bar"

         with tempfile.TemporaryDirectory("test-generation-config"		) as tmp_dir:
               generation_config.save_pretrained(_SCREAMING_SNAKE_CASE		)

               UpperCAmelCase__								=  GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(new_config.foo	,  "bar"		)

         UpperCAmelCase__								=  GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         assert not hasattr(_SCREAMING_SNAKE_CASE	,  "foo"		)  # no new kwargs should be initialized if from config





   def       lowercase_				(self					:						Optional[Any]		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig()
         self.assertEqual(default_config.temperature	,  1.0		)
         self.assertEqual(default_config.do_sample	,  _SCREAMING_SNAKE_CASE		)
         self.assertEqual(default_config.num_beams	,  1		)

         UpperCAmelCase__								=  GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE	,  temperature=0.7	,  length_penalty=1.0	,  bad_words_ids=[[1, 2, 3], [4, 5]]	,  )
         self.assertEqual(config.temperature	,  0.7		)
         self.assertEqual(config.do_sample	,  _SCREAMING_SNAKE_CASE		)
         self.assertEqual(config.num_beams	,  1		)

         with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(_SCREAMING_SNAKE_CASE		)
               UpperCAmelCase__								=  GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE	,  temperature=1.0		)

         self.assertEqual(loaded_config.temperature	,  1.0		)
         self.assertEqual(loaded_config.do_sample	,  _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.num_beams	,  1		)  # default value





@is_staging_test
class 					A							(    unittest.TestCase					):


   @classmethod
   def       lowercase_				(cls					:						Any		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  TOKEN
         HfFolder.save_token(_SCREAMING_SNAKE_CASE		)


   @classmethod
   def       lowercase_				(cls					:						str		)						->    Dict:
         """simple docstring"""



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

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


   def       lowercase_				(self					:						List[str]		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE	,  temperature=0.7	,  length_penalty=1.0	,  )
         config.push_to_hub("test-generation-config"	,  use_auth_token=self._token		)

         UpperCAmelCase__								=  GenerationConfig.from_pretrained(f"""{USER}/test-generation-config"""		)
         for k, v in config.to_dict().items():
               if k != "transformers_version":
                     self.assertEqual(_SCREAMING_SNAKE_CASE	,  getattr(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token	,  repo_id="test-generation-config"		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(
                   _SCREAMING_SNAKE_CASE	,  repo_id="test-generation-config"	,  push_to_hub=_SCREAMING_SNAKE_CASE	,  use_auth_token=self._token		)

         UpperCAmelCase__								=  GenerationConfig.from_pretrained(f"""{USER}/test-generation-config"""		)
         for k, v in config.to_dict().items():
               if k != "transformers_version":
                     self.assertEqual(_SCREAMING_SNAKE_CASE	,  getattr(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)		)





   def       lowercase_				(self					:						Dict		)						->    Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE	,  temperature=0.7	,  length_penalty=1.0	,  )
         config.push_to_hub("valid_org/test-generation-config-org"	,  use_auth_token=self._token		)

         UpperCAmelCase__								=  GenerationConfig.from_pretrained("valid_org/test-generation-config-org"		)
         for k, v in config.to_dict().items():
               if k != "transformers_version":
                     self.assertEqual(_SCREAMING_SNAKE_CASE	,  getattr(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token	,  repo_id="valid_org/test-generation-config-org"		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(
                   _SCREAMING_SNAKE_CASE	,  repo_id="valid_org/test-generation-config-org"	,  push_to_hub=_SCREAMING_SNAKE_CASE	,  use_auth_token=self._token		)

         UpperCAmelCase__								=  GenerationConfig.from_pretrained("valid_org/test-generation-config-org"		)
         for k, v in config.to_dict().items():
               if k != "transformers_version":
                     self.assertEqual(_SCREAMING_SNAKE_CASE	,  getattr(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE		)		)




from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml





class 					A							(    yaml.SafeLoader					):


   def       lowercase_				(self					:						Tuple	,  __UpperCAmelCase					:						str		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  [self.constructed_objects[key_node] for key_node, _ in node.value]
         UpperCAmelCase__								=  [tuple(__UpperCAmelCase		) if isinstance(__UpperCAmelCase	,  __UpperCAmelCase		) else key for key in keys]
         UpperCAmelCase__								=  Counter(__UpperCAmelCase		)
         UpperCAmelCase__								=  [key for key in counter if counter[key] > 1]
         if duplicate_keys:
               raise TypeError(f"""Got duplicate yaml keys: {duplicate_keys}"""		)





   def       lowercase_				(self					:						Dict	,  __UpperCAmelCase					:						Dict	,  __UpperCAmelCase					:						Any=False		)						->    Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  super().construct_mapping(__UpperCAmelCase	,  deep=__UpperCAmelCase		)
         self._check_no_duplicates_on_constructed_node(__UpperCAmelCase		)
         return mapping




def        lowerCAmelCase_			(					__A							)					->    Tuple[Optional[str], str]:







      '''simple docstring'''


      UpperCAmelCase__								=  list(readme_content.splitlines()							)
      if full_content and full_content[0] == "---" and "---" in full_content[1:]:
            UpperCAmelCase__								=  full_content[1:].index("---"							) + 1
            UpperCAmelCase__								=  "\n".join(full_content[1:sep_idx]							)
            return yamlblock, "\n".join(full_content[sep_idx + 1 :]							)

      return None, "\n".join(__A							)





class 					A							(    UpperCAmelCase_					):
   # class attributes
   __UpperCAmelCase			:			Optional[Any]              =      {'train_eval_index'}  # train-eval-index in the YAML metadata


   @classmethod
   def       lowercase_				(cls					:						List[str]	,  __UpperCAmelCase					:						Path		)						->    "DatasetMetadata":
         """simple docstring"""



         with open(__UpperCAmelCase	,  encoding="utf-8"		) as readme_file:
               UpperCAmelCase__		,				UpperCAmelCase__								=  _split_yaml_from_readme(readme_file.read()		)
         if yaml_string is not None:
               return cls.from_yaml_string(__UpperCAmelCase		)
         else:
               return cls()


   def       lowercase_				(self					:						int	,  __UpperCAmelCase					:						Path		)						->    str:
         """simple docstring"""



         if path.exists():
               with open(__UpperCAmelCase	,  encoding="utf-8"		) as readme_file:
                     UpperCAmelCase__								=  readme_file.read()
         else:
               UpperCAmelCase__								=  None
         UpperCAmelCase__								=  self._to_readme(__UpperCAmelCase		)
         with open(__UpperCAmelCase	,  "w"	,  encoding="utf-8"		) as readme_file:
               readme_file.write(__UpperCAmelCase		)


   def       lowercase_				(self					:						str	,  __UpperCAmelCase					:						Optional[str] = None		)						->    str:
         """simple docstring"""



         if readme_content is not None:
               UpperCAmelCase__		,				UpperCAmelCase__								=  _split_yaml_from_readme(__UpperCAmelCase		)
               UpperCAmelCase__								=  "---\n" + self.to_yaml_string() + "---\n" + content
         else:
               UpperCAmelCase__								=  "---\n" + self.to_yaml_string() + "---\n"
         return full_content


   @classmethod
   def       lowercase_				(cls					:						Optional[int]	,  __UpperCAmelCase					:						str		)						->    "DatasetMetadata":
         """simple docstring"""



         UpperCAmelCase__								=  yaml.load(__UpperCAmelCase	,  Loader=_NoDuplicateSafeLoader		) or {}

         # Convert the YAML keys to DatasetMetadata fields
         UpperCAmelCase__								=  {
             (key.replace("-"	,  "_"		) if key.replace("-"	,  "_"		) in cls._FIELDS_WITH_DASHES else key): value
             for key, value in metadata_dict.items()
         }
         return cls(**__UpperCAmelCase		)





   def       lowercase_				(self					:						List[str]		)						->    str:
         """simple docstring"""



         return yaml.safe_dump(
             {
                 (key.replace("_"	,  "-"		) if key in self._FIELDS_WITH_DASHES else key): value
                 for key, value in self.items()
             }	,  sort_keys=__UpperCAmelCase	,  allow_unicode=__UpperCAmelCase	,  encoding="utf-8"	,  ).decode("utf-8"		)




UpperCamelCase__    							= {
    'image-classification': [],
    'translation': [],
    'image-segmentation': [],
    'fill-mask': [],
    'automatic-speech-recognition': [],
    'token-classification': [],
    'sentence-similarity': [],
    'audio-classification': [],
    'question-answering': [],
    'summarization': [],
    'zero-shot-classification': [],
    'table-to-text': [],
    'feature-extraction': [],
    'other': [],
    'multiple-choice': [],
    'text-classification': [],
    'text-to-image': [],
    'text2text-generation': [],
    'zero-shot-image-classification': [],
    'tabular-classification': [],
    'tabular-regression': [],
    'image-to-image': [],
    'tabular-to-text': [],
    'unconditional-image-generation': [],
    'text-retrieval': [],
    'text-to-speech': [],
    'object-detection': [],
    'audio-to-audio': [],
    'text-generation': [],
    'conversational': [],
    'table-question-answering': [],
    'visual-question-answering': [],
    'image-to-text': [],
    'reinforcement-learning': [],
    'voice-activity-detection': [],
    'time-series-forecasting': [],
    'document-question-answering': [],
}


if __name__ == "__main__":
      from argparse import ArgumentParser

      UpperCamelCase__    							= ArgumentParser(usage='Validate the yaml metadata block of a README.md file.')
      ap.add_argument('readme_filepath')
      UpperCamelCase__    							= ap.parse_args()

      UpperCamelCase__    							= Path(args.readme_filepath)
      UpperCamelCase__    							= DatasetMetadata.from_readme(readme_filepath)
      print(dataset_metadata)
      dataset_metadata.to_readme(readme_filepath)



