



"""simple docstring"""
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 	lowerCamelCase__       (						self :					Optional[int]       ,		lowerCAmelCase :					Optional[int]		)     ->					List[str]:


     '''simple docstring'''




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

     # Checks parameters that were specified
     self.assertEqual(loaded_config.do_sample       ,		lowerCAmelCase		)
     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       ,		50		)
     self.assertEqual(loaded_config.max_length       ,		20		)
     self.assertEqual(loaded_config.max_time       ,		lowerCAmelCase		)



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=AutoConfig.from_pretrained("""gpt2"""		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=GenerationConfig.from_model_config(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: int     			=GenerationConfig()

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

     # 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 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=GenerationConfig()
     SCREAMING_SNAKE_CASE_: List[Any]     			={
         """max_new_tokens""": 1024,
         """foo""": """bar""",
     }
     SCREAMING_SNAKE_CASE_: List[str]     			=copy.deepcopy(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=generation_config.update(**lowerCAmelCase		)

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

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

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



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=GenerationConfig()
     SCREAMING_SNAKE_CASE_: Any     			="""bar"""

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

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

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=GenerationConfig.from_model_config(lowerCAmelCase		)
     assert not hasattr(lowerCAmelCase       ,		"""foo"""		)  # no new kwargs should be initialized if from config





  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=GenerationConfig()
     self.assertEqual(default_config.temperature       ,		1.0		)
     self.assertEqual(default_config.do_sample       ,		lowerCAmelCase		)
     self.assertEqual(default_config.num_beams       ,		1		)

     SCREAMING_SNAKE_CASE_: Tuple     			=GenerationConfig(
         do_sample=lowerCAmelCase       ,		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       ,		lowerCAmelCase		)
     self.assertEqual(config.num_beams       ,		1		)

     with tempfile.TemporaryDirectory() as tmp_dir:
        config.save_pretrained(lowerCAmelCase		)
        SCREAMING_SNAKE_CASE_: str     			=GenerationConfig.from_pretrained(lowerCAmelCase       ,		temperature=1.0		)

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







@is_staging_test
class 					a	(  unittest.TestCase       ):



  @classmethod
  def 	lowerCamelCase__       (						cls :					int		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=TOKEN
     HfFolder.save_token(lowerCAmelCase		)



  @classmethod
  def 	lowerCamelCase__       (						cls :					Tuple		)     ->					Optional[int]:


     '''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 	lowerCamelCase__       (						self :					int		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[Any]     			=GenerationConfig(
         do_sample=lowerCAmelCase       ,		temperature=0.7       ,		length_penalty=1.0       ,		)
     config.push_to_hub("""test-generation-config"""       ,		use_auth_token=self._token		)

     SCREAMING_SNAKE_CASE_: List[Any]     			=GenerationConfig.from_pretrained(f'''{USER}/test-generation-config'''		)
     for k, v in config.to_dict().items():
        if k != "transformers_version":
           self.assertEqual(lowerCAmelCase       ,		getattr(lowerCAmelCase       ,		lowerCAmelCase		)		)

        # 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(
            lowerCAmelCase       ,		repo_id="""test-generation-config"""       ,		push_to_hub=lowerCAmelCase       ,		use_auth_token=self._token		)

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





  def 	lowerCamelCase__       (						self :					Dict		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=GenerationConfig(
         do_sample=lowerCAmelCase       ,		temperature=0.7       ,		length_penalty=1.0       ,		)
     config.push_to_hub("""valid_org/test-generation-config-org"""       ,		use_auth_token=self._token		)

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

        # 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(
            lowerCAmelCase       ,		repo_id="""valid_org/test-generation-config-org"""       ,		push_to_hub=lowerCAmelCase       ,		use_auth_token=self._token		)

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








"""simple docstring"""
from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml
class 					a	(  yaml.SafeLoader       ):



  def 	lowerCamelCase__       (						self :					int       ,		lowerCAmelCase :					List[str]		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Any     			=[self.constructed_objects[key_node] for key_node, _ in node.value]
     SCREAMING_SNAKE_CASE_: Any     			=[tuple(lowerCAmelCase		) if isinstance(lowerCAmelCase       ,		lowerCAmelCase		) else key for key in keys]
     SCREAMING_SNAKE_CASE_: Dict     			=Counter(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[str]     			=[key for key in counter if counter[key] > 1]
     if duplicate_keys:
        raise TypeError(f'''Got duplicate yaml keys: {duplicate_keys}'''		)





  def 	lowerCamelCase__       (						self :					Tuple       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					Optional[int]=False		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Tuple     			=super().construct_mapping(lowerCAmelCase       ,		deep=lowerCAmelCase		)
     self._check_no_duplicates_on_constructed_node(lowerCAmelCase		)
     return mapping







def      __magic_name__				(							lowercase ):
   SCREAMING_SNAKE_CASE_: Dict     			=list(readme_content.splitlines() )
   if full_content and full_content[0] == "---" and "---" in full_content[1:]:
      SCREAMING_SNAKE_CASE_: Union[str, Any]     			=full_content[1:].index("""---""" ) + 1
      SCREAMING_SNAKE_CASE_: List[str]     			="""\n""".join(full_content[1:sep_idx] )
      return yamlblock, "\n".join(full_content[sep_idx + 1 :] )

   return None, "\n".join(lowercase )
class 					a	(  UpperCAmelCase__       ):
  # class attributes
  UpperCamelCase	:				Tuple     						=					{'train_eval_index'}  # train-eval-index in the YAML metadata



  @classmethod
  def 	lowerCamelCase__       (						cls :					List[Any]       ,		lowerCAmelCase :					Path		)     ->					"DatasetMetadata":


     '''simple docstring'''




     with open(lowerCAmelCase       ,		encoding="""utf-8"""		) as readme_file:
        SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Union[str, Any]     			=_split_yaml_from_readme(readme_file.read()		)
     if yaml_string is not None:
        return cls.from_yaml_string(lowerCAmelCase		)
     else:
        return cls()



  def 	lowerCamelCase__       (						self :					Any       ,		lowerCAmelCase :					Path		)     ->					List[str]:


     '''simple docstring'''




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



  def 	lowerCamelCase__       (						self :					Optional[Any]       ,		lowerCAmelCase :					Optional[str] = None		)     ->					str:


     '''simple docstring'''




     if readme_content is not None:
        SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: List[str]     			=_split_yaml_from_readme(lowerCAmelCase		)
        SCREAMING_SNAKE_CASE_: List[str]     			="""---\n""" + self.to_yaml_string() + """---\n""" + content
     else:
        SCREAMING_SNAKE_CASE_: List[Any]     			="""---\n""" + self.to_yaml_string() + """---\n"""
     return full_content



  @classmethod
  def 	lowerCamelCase__       (						cls :					Optional[int]       ,		lowerCAmelCase :					str		)     ->					"DatasetMetadata":


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=yaml.load(lowerCAmelCase       ,		Loader=_NoDuplicateSafeLoader		) or {}

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





  def 	lowerCamelCase__       (						self :					Dict		)     ->					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=lowerCAmelCase       ,		allow_unicode=lowerCAmelCase       ,		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)



