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

import yaml





class      lowerCAmelCase						(					yaml.SafeLoader			):



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





     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__=False       ):
            _A=	super().construct_mapping(lowerCAmelCase__				,      deep=lowerCAmelCase__       )
            self._check_no_duplicates_on_constructed_node(lowerCAmelCase__       )
            return mapping


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




       '''simple docstring'''






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

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





class      lowerCAmelCase						(					_a			):
     # class attributes
     _SCREAMING_SNAKE_CASE     :     Any		={"""train_eval_index"""}  # train-eval-index in the YAML metadata



     @classmethod
     def 			a__  (					cls				,      lowerCAmelCase__       ):
            with open(lowerCAmelCase__				,      encoding='utf-8'       ) as readme_file:
                   _A,			_A=	_split_yaml_from_readme(readme_file.read()       )
            if yaml_string is not None:
                   return cls.from_yaml_string(lowerCAmelCase__       )
            else:
                   return cls()



     def 			a__  (					self				,      lowerCAmelCase__       ):
            if path.exists():
                   with open(lowerCAmelCase__				,      encoding='utf-8'       ) as readme_file:
                          _A=	readme_file.read()
            else:
                   _A=	None
            _A=	self._to_readme(lowerCAmelCase__       )
            with open(lowerCAmelCase__				,      'w'				,      encoding='utf-8'       ) as readme_file:
                   readme_file.write(lowerCAmelCase__       )



     def 			a__  (					self				,      lowerCAmelCase__ = None       ):
            if readme_content is not None:
                   _A,			_A=	_split_yaml_from_readme(lowerCAmelCase__       )
                   _A=	'---\n' + self.to_yaml_string() + '---\n' + content
            else:
                   _A=	'---\n' + self.to_yaml_string() + '---\n'
            return full_content



     @classmethod
     def 			a__  (					cls				,      lowerCAmelCase__       ):
            _A=	yaml.load(lowerCAmelCase__				,      Loader=_NoDuplicateSafeLoader       ) or {}

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





     def 			a__  (					self       ):
            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)
import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
      import jax

      from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
      from transformers.models.bert.modeling_flax_bert import FlaxBertModel
      from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel





@require_flax
class      lowerCAmelCase						(					unittest.TestCase			):



     @slow
     def 			a__  (					self       ):
            for model_name in ["bert-base-cased", "bert-large-uncased"]:
                   with self.subTest(lowerCAmelCase__       ):
                          _A=	AutoConfig.from_pretrained(lowerCAmelCase__       )
                          self.assertIsNotNone(lowerCAmelCase__       )
                          self.assertIsInstance(lowerCAmelCase__				,      lowerCAmelCase__       )

                          _A=	FlaxAutoModel.from_pretrained(lowerCAmelCase__       )
                          self.assertIsNotNone(lowerCAmelCase__       )
                          self.assertIsInstance(lowerCAmelCase__				,      lowerCAmelCase__       )



     @slow
     def 			a__  (					self       ):
            for model_name in ["roberta-base", "roberta-large"]:
                   with self.subTest(lowerCAmelCase__       ):
                          _A=	AutoConfig.from_pretrained(lowerCAmelCase__       )
                          self.assertIsNotNone(lowerCAmelCase__       )
                          self.assertIsInstance(lowerCAmelCase__				,      lowerCAmelCase__       )

                          _A=	FlaxAutoModel.from_pretrained(lowerCAmelCase__       )
                          self.assertIsNotNone(lowerCAmelCase__       )
                          self.assertIsInstance(lowerCAmelCase__				,      lowerCAmelCase__       )



     @slow
     def 			a__  (					self       ):
            for model_name in ["bert-base-cased", "bert-large-uncased"]:
                   _A=	AutoTokenizer.from_pretrained(lowerCAmelCase__       )
                   _A=	FlaxBertModel.from_pretrained(lowerCAmelCase__       )
                   _A=	tokenizer('Do you support jax jitted function?'				,      return_tensors=TensorType.JAX       )

                   @jax.jit
                   def eval(**lowerCAmelCase__       ):
                          return model(**lowerCAmelCase__       )

                   eval(**lowerCAmelCase__       ).block_until_ready()



     @slow
     def 			a__  (					self       ):
            for model_name in ["roberta-base", "roberta-large"]:
                   _A=	AutoTokenizer.from_pretrained(lowerCAmelCase__       )
                   _A=	FlaxRobertaModel.from_pretrained(lowerCAmelCase__       )
                   _A=	tokenizer('Do you support jax jitted function?'				,      return_tensors=TensorType.JAX       )

                   @jax.jit
                   def eval(**lowerCAmelCase__       ):
                          return model(**lowerCAmelCase__       )

                   eval(**lowerCAmelCase__       ).block_until_ready()



     def 			a__  (					self       ):
            with self.assertRaisesRegex(
                lowerCAmelCase__				,      'bert-base is not a local folder and is not a valid model identifier'       ):
                   _A=	FlaxAutoModel.from_pretrained('bert-base'       )



     def 			a__  (					self       ):
            with self.assertRaisesRegex(
                lowerCAmelCase__				,      r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'       ):
                   _A=	FlaxAutoModel.from_pretrained(lowerCAmelCase__				,      revision='aaaaaa'       )



     def 			a__  (					self       ):
            with self.assertRaisesRegex(
                lowerCAmelCase__				,      'hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'				,      ):
                   _A=	FlaxAutoModel.from_pretrained('hf-internal-testing/config-no-model'       )





     def 			a__  (					self       ):
            with self.assertRaisesRegex(lowerCAmelCase__				,      'Use `from_pt=True` to load this model'       ):
                   _A=	FlaxAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'       )