





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

import yaml

class      A       (	yaml.SafeLoader						):



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int       )						->    Optional[int]:
         '''simple docstring'''

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






      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Optional[Any]=False       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=super().construct_mapping(_lowerCAmelCase		,  deep=_lowerCAmelCase       )
         self._check_no_duplicates_on_constructed_node(_lowerCAmelCase       )
         return mapping




def     a__ (					lowercase__  ):






   '''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(lowercase__  )

class      A       (	__lowercase						):
      # class attributes
      _snake_case							     ={'''train_eval_index'''}  # train-eval-index in the YAML metadata



      @classmethod
      def 	lowerCAmelCase__    (	cls:						Any		,  _lowerCAmelCase:						Path       )						->    "DatasetMetadata":
         '''simple docstring'''

         with open(_lowerCAmelCase		,  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(_lowerCAmelCase       )
         else:
            return cls()



      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						Path       )						->    List[str]:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Optional[str] = None       )						->    str:
         '''simple docstring'''

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



      @classmethod
      def 	lowerCAmelCase__    (	cls:						Optional[Any]		,  _lowerCAmelCase:						str       )						->    "DatasetMetadata":
         '''simple docstring'''

         UpperCAmelCase_											=yaml.load(_lowerCAmelCase		,  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(**_lowerCAmelCase       )






      def 	lowerCAmelCase__    (	self:						int       )						->    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"       )

__lowercase  :   int						      ={
    """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

     __lowercase  :   List[Any]						      =ArgumentParser(usage="""Validate the yaml metadata block of a README.md file.""")
     ap.add_argument("""readme_filepath""")
     __lowercase  :   int						      =ap.parse_args()

     __lowercase  :   Optional[int]						      =Path(args.readme_filepath)
     __lowercase  :   Tuple						      =DatasetMetadata.from_readme(readme_filepath)
     print(dataset_metadata)
     dataset_metadata.to_readme(readme_filepath)











import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


__lowercase  :   Any						      ="""hf-internal-testing/tiny-random-bert"""
__lowercase  :   List[str]						      =os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
__lowercase  :   Dict						      ="""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""

class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  _lowerCAmelCase       )
         # Should have downloaded the file in here
         self.assertTrue(os.path.isdir(_lowerCAmelCase       )       )
         # Cache should contain at least those three subfolders:
         for subfolder in ["blobs", "refs", "snapshots"]:
            self.assertTrue(os.path.isdir(os.path.join(_lowerCAmelCase		,  _lowerCAmelCase       )       )       )
         with open(os.path.join(_lowerCAmelCase		,  "refs"		,  "main"       )       ) as f:
            UpperCAmelCase_											=f.read()
         self.assertEqual(_lowerCAmelCase		,  os.path.join(_lowerCAmelCase		,  "snapshots"		,  _lowerCAmelCase		,  _lowerCAmelCase       )       )
         self.assertTrue(os.path.isfile(_lowerCAmelCase       )       )

         # File is cached at the same place the second time.
         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  _lowerCAmelCase       )
         self.assertEqual(_lowerCAmelCase		,  _lowerCAmelCase       )

         # Using a specific revision to test the full commit hash.
         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  _lowerCAmelCase		,  revision="9b8c223"       )
         self.assertEqual(_lowerCAmelCase		,  os.path.join(_lowerCAmelCase		,  "snapshots"		,  _lowerCAmelCase		,  _lowerCAmelCase       )       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[Any]:
         '''simple docstring'''

         with self.assertRaisesRegex(_lowerCAmelCase		,  "is not a valid model identifier"       ):
            UpperCAmelCase_											=cached_file("tiny-random-bert"		,  _lowerCAmelCase       )

         with self.assertRaisesRegex(_lowerCAmelCase		,  "is not a valid git identifier"       ):
            UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  _lowerCAmelCase		,  revision="aaaa"       )

         with self.assertRaisesRegex(_lowerCAmelCase		,  "does not appear to have a file named"       ):
            UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"       )



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[Any]:
         '''simple docstring'''

         with self.assertRaisesRegex(_lowerCAmelCase		,  "does not appear to have a file named"       ):
            UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"       )

         with open(os.path.join(_lowerCAmelCase		,  "refs"		,  "main"       )       ) as f:
            UpperCAmelCase_											=f.read()
         self.assertTrue(os.path.isfile(os.path.join(_lowerCAmelCase		,  ".no_exist"		,  _lowerCAmelCase		,  "conf"       )       )       )

         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"		,  _raise_exceptions_for_missing_entries=_lowerCAmelCase       )
         self.assertIsNone(_lowerCAmelCase       )

         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"		,  local_files_only=_lowerCAmelCase		,  _raise_exceptions_for_missing_entries=_lowerCAmelCase       )
         self.assertIsNone(_lowerCAmelCase       )

         UpperCAmelCase_											=mock.Mock()
         UpperCAmelCase_											=500
         UpperCAmelCase_											={}
         UpperCAmelCase_											=HTTPError
         UpperCAmelCase_											={}

         # Under the mock environment we get a 500 error when trying to reach the tokenizer.
         with mock.patch("requests.Session.request"		,  return_value=_lowerCAmelCase       ) as mock_head:
            UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"		,  _raise_exceptions_for_connection_errors=_lowerCAmelCase       )
            self.assertIsNone(_lowerCAmelCase       )
            # This check we did call the fake head request
            mock_head.assert_called()



      def 	lowerCAmelCase__    (	self:						Any       )						->    Tuple:
         '''simple docstring'''

         self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"		,  _lowerCAmelCase       )       )
         self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"		,  _lowerCAmelCase       )       )
         self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"		,  _lowerCAmelCase       )       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Dict:
         '''simple docstring'''

         self.assertIsNone(get_file_from_repo("bert-base-cased"		,  "ahah.txt"       )       )

         # The function raises if the repository does not exist.
         with self.assertRaisesRegex(_lowerCAmelCase		,  "is not a valid model identifier"       ):
            get_file_from_repo("bert-base-case"		,  _lowerCAmelCase       )

         # The function raises if the revision does not exist.
         with self.assertRaisesRegex(_lowerCAmelCase		,  "is not a valid git identifier"       ):
            get_file_from_repo("bert-base-cased"		,  _lowerCAmelCase		,  revision="ahaha"       )

         UpperCAmelCase_											=get_file_from_repo("bert-base-cased"		,  _lowerCAmelCase       )
         # The name is the cached name which is not very easy to test, so instead we load the content.
         UpperCAmelCase_											=json.loads(open(_lowerCAmelCase		,  "r"       ).read()       )
         self.assertEqual(config["hidden_size"]		,  768       )






      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Dict:
         '''simple docstring'''

         with tempfile.TemporaryDirectory() as tmp_dir:
            UpperCAmelCase_											=Path(_lowerCAmelCase       ) / "a.txt"
            filename.touch()
            self.assertEqual(get_file_from_repo(_lowerCAmelCase		,  "a.txt"       )		,  str(_lowerCAmelCase       )       )

            self.assertIsNone(get_file_from_repo(_lowerCAmelCase		,  "b.txt"       )       )




