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

import yaml


class 						SCREAMING_SNAKE_CASE__    (     yaml.SafeLoader     ):




							def 	__snake_case(						self   :					Any	,							_UpperCamelCase   :					List[Any]      )					->				Union[str, Any]:



												'''simple docstring'''

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





							def 	__snake_case(						self   :					Union[str, Any]	,							_UpperCamelCase   :					Optional[int]	,							_UpperCamelCase   :					Optional[int]=False      )					->				Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       super().construct_mapping(_UpperCamelCase	,							deep=_UpperCamelCase      )
												self._check_no_duplicates_on_constructed_node(_UpperCamelCase      )
												return mapping





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

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


class 						SCREAMING_SNAKE_CASE__    (     a     ):
							# class attributes
							lowercase__						:       Any								=							{"""train_eval_index"""}  # train-eval-index in the YAML metadata




							@classmethod
							def 	__snake_case(						cls   :					int	,							_UpperCamelCase   :					Path      )					->				"DatasetMetadata":



												'''simple docstring'''

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




							def 	__snake_case(						self   :					Optional[int]	,							_UpperCamelCase   :					Path      )					->				List[Any]:



												'''simple docstring'''

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




							def 	__snake_case(						self   :					str	,							_UpperCamelCase   :					Optional[str] = None      )					->				str:



												'''simple docstring'''

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




							@classmethod
							def 	__snake_case(						cls   :					List[Any]	,							_UpperCamelCase   :					str      )					->				"DatasetMetadata":



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       yaml.load(_UpperCamelCase	,							Loader=_NoDuplicateSafeLoader      ) or {}

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





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



_lowerCamelCase					:   Tuple							=		{
    '''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

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

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





import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


_lowerCamelCase					:   str							=		threading.Lock()
_lowerCamelCase					:   Optional[logging.Handler]							=		None

_lowerCamelCase					:   Any							=		{
    '''debug''': logging.DEBUG,
    '''info''': logging.INFO,
    '''warning''': logging.WARNING,
    '''error''': logging.ERROR,
    '''critical''': logging.CRITICAL,
}

_lowerCamelCase					:   Union[str, Any]							=		logging.WARNING

_lowerCamelCase					:   List[Any]							=		True





def 	__lowerCamelCase							():
					SCREAMING_SNAKE_CASE    			=       os.getenv("TRANSFORMERS_VERBOSITY"      , UpperCAmelCase__  )
					if env_level_str:
										if env_level_str in log_levels:
															return log_levels[env_level_str]
										else:
															logging.getLogger().warning(
															    F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, "
															    F"has to be one of: { ', '.join(log_levels.keys()  ) }"  )
					return _default_log_level





def 	__lowerCamelCase							():
					return __name__.split("."  )[0]





def 	__lowerCamelCase							():
					return logging.getLogger(_get_library_name()  )





def 	__lowerCamelCase							():
					global _default_handler

					with _lock:
										if _default_handler:
															# This library has already configured the library root logger.
															return
										SCREAMING_SNAKE_CASE    			=       logging.StreamHandler()  # Set sys.stderr as stream.
										SCREAMING_SNAKE_CASE    			=       sys.stderr.flush

										# Apply our default configuration to the library root logger.
										SCREAMING_SNAKE_CASE    			=       _get_library_root_logger()
										library_root_logger.addHandler(_default_handler  )
										library_root_logger.setLevel(_get_default_logging_level()  )
										SCREAMING_SNAKE_CASE    			=       False





def 	__lowerCamelCase							():
					global _default_handler

					with _lock:
										if not _default_handler:
															return

										SCREAMING_SNAKE_CASE    			=       _get_library_root_logger()
										library_root_logger.removeHandler(_default_handler  )
										library_root_logger.setLevel(logging.NOTSET  )
										SCREAMING_SNAKE_CASE    			=       None





def 	__lowerCamelCase							():
					return log_levels





def 	__lowerCamelCase							(UpperCAmelCase__     :							Optional[str] = None  ):

					if name is None:
										SCREAMING_SNAKE_CASE    			=       _get_library_name()

					_configure_library_root_logger()
					return logging.getLogger(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					_configure_library_root_logger()
					return _get_library_root_logger().getEffectiveLevel()





def 	__lowerCamelCase							(UpperCAmelCase__     :							int  ):
					_configure_library_root_logger()
					_get_library_root_logger().setLevel(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					return set_verbosity(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					return set_verbosity(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					return set_verbosity(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					return set_verbosity(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					_configure_library_root_logger()

					assert _default_handler is not None
					_get_library_root_logger().removeHandler(_default_handler  )





def 	__lowerCamelCase							():
					_configure_library_root_logger()

					assert _default_handler is not None
					_get_library_root_logger().addHandler(_default_handler  )





def 	__lowerCamelCase							(UpperCAmelCase__     :							logging.Handler  ):
					_configure_library_root_logger()

					assert handler is not None
					_get_library_root_logger().addHandler(UpperCAmelCase__  )





def 	__lowerCamelCase							(UpperCAmelCase__     :							logging.Handler  ):
					_configure_library_root_logger()

					assert handler is not None and handler not in _get_library_root_logger().handlers
					_get_library_root_logger().removeHandler(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					_configure_library_root_logger()
					SCREAMING_SNAKE_CASE    			=       False





def 	__lowerCamelCase							():
					_configure_library_root_logger()
					SCREAMING_SNAKE_CASE    			=       True





def 	__lowerCamelCase							():
					SCREAMING_SNAKE_CASE    			=       _get_library_root_logger().handlers

					for handler in handlers:
										SCREAMING_SNAKE_CASE    			=       logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"  )
										handler.setFormatter(UpperCAmelCase__  )





def 	__lowerCamelCase							():
					SCREAMING_SNAKE_CASE    			=       _get_library_root_logger().handlers

					for handler in handlers:
										handler.setFormatter(UpperCAmelCase__  )





def 	__lowerCamelCase							(self     :							str      , *UpperCAmelCase__     :							Union[str, Any]      , **UpperCAmelCase__     :							List[str]  ):
					SCREAMING_SNAKE_CASE    			=       os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS"      , UpperCAmelCase__  )
					if no_advisory_warnings:
										return
					self.warning(*UpperCAmelCase__      , **UpperCAmelCase__  )


_lowerCamelCase					:   str							=		warning_advice





@functools.lru_cache(UpperCAmelCase__  )
def 	__lowerCamelCase							(self     :							List[str]      , *UpperCAmelCase__     :							Tuple      , **UpperCAmelCase__     :							int  ):
					self.warning(*UpperCAmelCase__      , **UpperCAmelCase__  )


_lowerCamelCase					:   Dict							=		warning_once


class 						lowercase    :




							def __init__(						self   :					List[Any]	,							*_UpperCamelCase   :					Union[str, Any]	,							**_UpperCamelCase   :					str      )					->				List[Any]:  # pylint: disable=unused-argument



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       args[0] if args else None




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



												'''simple docstring'''

												return iter(self._iterator      )




							def __getattr__(						self   :					List[str]	,							_UpperCamelCase   :					Any      )					->				List[Any]:



												'''simple docstring'''

												def empty_fn(*_UpperCamelCase   :					List[str]	,							**_UpperCamelCase   :					Union[str, Any]      ):  # pylint: disable=unused-argument
																	return

												return empty_fn




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



												'''simple docstring'''

												return self





							def __exit__(						self   :					Optional[Any]	,							_UpperCamelCase   :					Tuple	,							_UpperCamelCase   :					str	,							_UpperCamelCase   :					List[str]      )					->				Union[str, Any]:



												'''simple docstring'''

												return



class 						lowercase    :




							def __call__(						self   :					Union[str, Any]	,							*_UpperCamelCase   :					Optional[Any]	,							**_UpperCamelCase   :					Tuple      )					->				Tuple:



												'''simple docstring'''

												if _tqdm_active:
																	return tqdm_lib.tqdm(*_UpperCamelCase	,							**_UpperCamelCase      )
												else:
																	return EmptyTqdm(*_UpperCamelCase	,							**_UpperCamelCase      )




							def 	__snake_case(						self   :					Dict	,							*_UpperCamelCase   :					Dict	,							**_UpperCamelCase   :					Union[str, Any]      )					->				Union[str, Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       None
												if _tqdm_active:
																	return tqdm_lib.tqdm.set_lock(*_UpperCamelCase	,							**_UpperCamelCase      )





							def 	__snake_case(						self   :					Optional[int]      )					->				Union[str, Any]:



												'''simple docstring'''

												if _tqdm_active:
																	return tqdm_lib.tqdm.get_lock()



_lowerCamelCase					:   Union[str, Any]							=		_tqdm_cls()





def 	__lowerCamelCase							():
					global _tqdm_active
					return bool(_tqdm_active  )





def 	__lowerCamelCase							():
					global _tqdm_active
					SCREAMING_SNAKE_CASE    			=       True
					hf_hub_utils.enable_progress_bars()





def 	__lowerCamelCase							():
					global _tqdm_active
					SCREAMING_SNAKE_CASE    			=       False
					hf_hub_utils.disable_progress_bars()




