


from typing import List

from packaging import version
from sklearn.metrics import fa_score

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('3.8'):
						import importlib_metadata
else:
						import importlib.metadata as importlib_metadata


# TODO(Patrick/Anton)
__UpperCamelCase      						=    '\\n'

__UpperCamelCase      						=    '\\n    XTREME-S is a benchmark to evaluate universal cross-lingual speech representations in many languages.\n    XTREME-S covers four task families: speech recognition, classification, speech-to-text translation and retrieval.\n'

__UpperCamelCase      						=    '\nCompute XTREME-S evaluation metric associated to each XTREME-S dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\n    bleu_kwargs: optional dict of keywords to be passed when computing \'bleu\'.\n        Keywords include Dict can be one of \'smooth_method\', \'smooth_value\', \'force\', \'lowercase\',\n        \'tokenize\', \'use_effective_order\'.\n    wer_kwargs: optional dict of keywords to be passed when computing \'wer\' and \'cer\'.\n        Keywords include \'concatenate_texts\'.\nReturns: depending on the XTREME-S task, one or several of:\n    "accuracy": Accuracy - for \'fleurs-lang_id\', \'minds14\'\n    "f1": F1 score - for \'minds14\'\n    "wer": Word error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "cer": Character error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "bleu": BLEU score according to the `sacrebleu` metric - for \'covost2\'\nExamples:\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'mls\')  # \'mls\', \'voxpopuli\', \'fleurs-asr\' or \'babel\'\n    >>> references = ["it is sunny here", "paper and pen are essentials"]\n    >>> predictions = ["it\'s sunny", "paper pen are essential"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'wer\': 0.56, \'cer\': 0.27}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'covost2\')\n    >>> references = ["bonjour paris", "il est necessaire de faire du sport de temps en temp"]\n    >>> predictions = ["bonjour paris", "il est important de faire du sport souvent"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'bleu\': 31.65}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'fleurs-lang_id\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'accuracy\': 0.6}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'minds14\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'f1\': 0.58, \'accuracy\': 0.6}\n'

__UpperCamelCase      						=    ['fleurs-asr', 'mls', 'voxpopuli', 'babel', 'covost2', 'fleurs-lang_id', 'minds14']
__UpperCamelCase      						=    ''

try:
						from jiwer import transforms as tr

						__UpperCamelCase      						=    True
except ImportError:
						__UpperCamelCase      						=    False

if _jiwer_available and version.parse(importlib_metadata.version('jiwer')) < version.parse('2.3.0'):







						class     lowerCamelCase__		(							tr.AbstractTransform ):






									"""simple docstring"""






									def __init__(							self ,  snake_case = " "       ):

												'''simple docstring'''


												UpperCamelCase__   =    sentence_delimiter




									def 			snake_case__			(							self ,  snake_case       ):

												'''simple docstring'''


												return list(snake_case       )







									def 			snake_case__			(							self ,  snake_case       ):

												'''simple docstring'''


												UpperCamelCase__   =    []
												for sent_idx, sentence in enumerate(snake_case       ):
															chars.extend(self.process_string(snake_case       )       )
															if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(snake_case       ) - 1:
																		chars.append(self.sentence_delimiter       )
												return chars



						__UpperCamelCase      						=    tr.Compose(
						    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
						)
elif _jiwer_available:
						__UpperCamelCase      						=    tr.Compose(
						    [
						        tr.RemoveMultipleSpaces(),
						        tr.Strip(),
						        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
						        tr.ReduceToListOfListOfChars(),
						    ]
						)
else:
						__UpperCamelCase      						=    None
def     UpperCamelCase_(			_A				:List[str]			,       _A				:Dict					)->						Tuple:
			return float((preds == labels).mean()					)
def     UpperCamelCase_(			_A				:List[Any]			,       _A				:Tuple					)->						Union[str, Any]:
			return {
			    "f1": float(fa_score(y_true=_A			,       y_pred=_A			,       average="macro"					)					),
			    "accuracy": simple_accuracy(_A			,       _A					),
			}
def     UpperCamelCase_(			_A				:int			,       _A				:List[Any]			,       _A				:str="exp"			,       _A				:int=None			,       _A				:Any=False			,       _A				:List[str]=False			,       _A				:Optional[Any]=None			,       _A				:List[Any]=False			,       )->						Optional[int]:
			# xtreme-s can only have one label
			UpperCamelCase__   =    [[label] for label in labels]
			UpperCamelCase__   =    list(_A					)
			try:
						import sacrebleu as scb
			except ImportError:
						raise ValueError(
						    "sacrebleu has to be installed in order to apply the bleu metric for covost2."
						    "You can install it via `pip install sacrebleu`."					)

			if version.parse(scb.__version__					) < version.parse("1.4.12"					):
						raise ImportWarning(
						    "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n"
						    "You can install it with `pip install \"sacrebleu>=1.4.12\"`."					)

			UpperCamelCase__   =    len(labels[0]					)
			if any(len(_A					) != references_per_prediction for refs in labels					):
						raise ValueError("Sacrebleu requires the same number of references for each prediction"					)
			UpperCamelCase__   =    [[refs[i] for refs in labels] for i in range(_A					)]
			UpperCamelCase__   =    scb.corpus_bleu(
			    _A			,       _A			,       smooth_method=_A			,       smooth_value=_A			,       force=_A			,       lowercase=_A			,       use_effective_order=_A			,       **({"tokenize": tokenize} if tokenize else {})			,       )
			return {"bleu": output.score}
def     UpperCamelCase_(			_A				:List[str]			,       _A				:Any			,       _A				:List[str]			,       _A				:Any					)->						List[str]:
			try:
						from jiwer import compute_measures
			except ImportError:
						raise ValueError(
						    F'''jiwer has to be installed in order to apply the wer metric for {config_name}.'''
						    "You can install it via `pip install jiwer`."					)

			if concatenate_texts:
						UpperCamelCase__   =    compute_measures(_A			,       _A					)["wer"]

						UpperCamelCase__   =    compute_measures(_A			,       _A			,       truth_transform=_A			,       hypothesis_transform=_A					)["wer"]
						return {"wer": wer, "cer": cer}
			else:

						def compute_score(_A				:Dict			,       _A				:Tuple			,       _A				:Any="wer"					):
									UpperCamelCase__   =    0
									UpperCamelCase__   =    0
									for prediction, reference in zip(_A			,       _A					):
												if score_type == "wer":
															UpperCamelCase__   =    compute_measures(_A			,       _A					)
												elif score_type == "cer":
															UpperCamelCase__   =    compute_measures(
															    _A			,       _A			,       truth_transform=_A			,       hypothesis_transform=_A					)
												incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
												total += measures["substitutions"] + measures["deletions"] + measures["hits"]
									return incorrect / total

						return {"wer": compute_score(_A			,       _A			,       "wer"					), "cer": compute_score(_A			,       _A			,       "cer"					)}







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   ,		_KWARGS_DESCRIPTION )
class     lowerCamelCase__		(							datasets.Metric ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						if self.config_name not in _CONFIG_NAMES:
									raise KeyError(F'''You should supply a configuration name selected in {_CONFIG_NAMES}'''       )

						UpperCamelCase__   =    "int64" if self.config_name in ["fleurs-lang_id", "minds14"] else "string"

						return datasets.MetricInfo(
						    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(
						        {"predictions": datasets.Value(snake_case       ), "references": datasets.Value(snake_case       )}       ) ,  codebase_urls=[] ,  reference_urls=[] ,  format="numpy" ,  )







			def 			snake_case__			(							self ,  snake_case ,  snake_case ,  snake_case=None ,  snake_case=None       ):

						'''simple docstring'''


						UpperCamelCase__   =    bleu_kwargs if bleu_kwargs is not None else {}
						UpperCamelCase__   =    wer_kwargs if wer_kwargs is not None else {}

						if self.config_name == "fleurs-lang_id":
									return {"accuracy": simple_accuracy(snake_case ,  snake_case       )}
						elif self.config_name == "minds14":
									return fa_and_simple_accuracy(snake_case ,  snake_case       )
						elif self.config_name == "covost2":
									UpperCamelCase__   =    bleu_kwargs.pop("smooth_method" ,  "exp"       )
									UpperCamelCase__   =    bleu_kwargs.pop("smooth_value" ,  snake_case       )
									UpperCamelCase__   =    bleu_kwargs.pop("force" ,  snake_case       )
									UpperCamelCase__   =    bleu_kwargs.pop("lowercase" ,  snake_case       )
									UpperCamelCase__   =    bleu_kwargs.pop("tokenize" ,  snake_case       )
									UpperCamelCase__   =    bleu_kwargs.pop("use_effective_order" ,  snake_case       )
									return bleu(
									    preds=snake_case ,  labels=snake_case ,  smooth_method=snake_case ,  smooth_value=snake_case ,  force=snake_case ,  lowercase=snake_case ,  tokenize=snake_case ,  use_effective_order=snake_case ,  )
						elif self.config_name in ["fleurs-asr", "mls", "voxpopuli", "babel"]:
									UpperCamelCase__   =    wer_kwargs.pop("concatenate_texts" ,  snake_case       )
									return wer_and_cer(snake_case ,  snake_case ,  snake_case ,  self.config_name       )
						else:
									raise KeyError(F'''You should supply a configuration name selected in {_CONFIG_NAMES}'''       )






import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			@staticmethod
			def 			snake_case__			(							snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    parser.add_parser("login" ,  help="Log in using the same credentials as on huggingface.co"       )
						login_parser.set_defaults(func=lambda snake_case       : LoginCommand(snake_case       )       )
						UpperCamelCase__   =    parser.add_parser("whoami" ,  help="Find out which huggingface.co account you are logged in as."       )
						whoami_parser.set_defaults(func=lambda snake_case       : WhoamiCommand(snake_case       )       )
						UpperCamelCase__   =    parser.add_parser("logout" ,  help="Log out"       )
						logout_parser.set_defaults(func=lambda snake_case       : LogoutCommand(snake_case       )       )

						# new system: git-based repo system
						UpperCamelCase__   =    parser.add_parser(
						    "repo" ,  help="Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos." ,  )
						UpperCamelCase__   =    repo_parser.add_subparsers(
						    help="Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"       )
						UpperCamelCase__   =    repo_subparsers.add_parser(
						    "create" ,  help="Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"       )
						repo_create_parser.add_argument(
						    "name" ,  type=snake_case ,  help="Name for your model's repo. Will be namespaced under your username to build the model id." ,  )
						repo_create_parser.add_argument("--organization" ,  type=snake_case ,  help="Optional: organization namespace."       )
						repo_create_parser.add_argument("-y" ,  "--yes" ,  action="store_true" ,  help="Optional: answer Yes to the prompt"       )
						repo_create_parser.set_defaults(func=lambda snake_case       : RepoCreateCommand(snake_case       )       )







class     lowerCamelCase__		:






			"""simple docstring"""






			_UpperCamelCase						:   int					=						'\u001b[1m'
			_UpperCamelCase						:   List[Any]					=						'\u001b[31m'
			_UpperCamelCase						:   Tuple					=						'\u001b[90m'
			_UpperCamelCase						:   Tuple					=						'\u001b[0m'




			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


						return F'''{cls._bold}{s}{cls._reset}'''




			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


						return F'''{cls._bold}{cls._red}{s}{cls._reset}'''







			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


						return F'''{cls._gray}{s}{cls._reset}'''



def     UpperCamelCase_(			_A				:List[List[Union[str, int]]]			,       _A				:List[str]					)->						str:
			UpperCamelCase__   =    [max(len(str(_A					)					) for x in col					) for col in zip(*_A			,       _A					)]
			UpperCamelCase__   =    ("{{:{}}} " * len(_A					)).format(*_A					)
			UpperCamelCase__   =    []
			lines.append(row_format.format(*_A					)					)
			lines.append(row_format.format(*["-" * w for w in col_widths]					)					)
			for row in rows:
						lines.append(row_format.format(*_A					)					)
			return "\n".join(_A					)







class     lowerCamelCase__		:






			"""simple docstring"""






			def __init__(							self ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    args







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						print(
						    ANSI.red(
						        "ERROR! `huggingface-cli login` uses an outdated login mechanism "
						        "that is not compatible with the Hugging Face Hub backend anymore. "
						        "Please use `huggingface-cli login instead."       )       )







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						print(
						    ANSI.red(
						        "WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use "
						        "`huggingface-cli whoami` instead."       )       )
						UpperCamelCase__   =    HfFolder.get_token()
						if token is None:
									print("Not logged in"       )
									exit()
						try:
									UpperCamelCase__, UpperCamelCase__   =    whoami(snake_case       )
									print(snake_case       )
									if orgs:
												print(ANSI.bold("orgs: "       ) ,  ",".join(snake_case       )       )
						except HTTPError as e:
									print(snake_case       )
									print(ANSI.red(e.response.text       )       )
									exit(1       )







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						print(
						    ANSI.red(
						        "ERROR! `transformers-cli logout` uses an outdated logout mechanism "
						        "that is not compatible with the Hugging Face Hub backend anymore. "
						        "Please use `huggingface-cli logout instead."       )       )







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


						print(
						    ANSI.red(
						        "WARNING! Managing repositories through transformers-cli is deprecated. "
						        "Please use `huggingface-cli` instead."       )       )
						UpperCamelCase__   =    HfFolder.get_token()
						if token is None:
									print("Not logged in"       )
									exit(1       )
						try:
									UpperCamelCase__   =    subprocess.check_output(["git", "--version"]       ).decode("utf-8"       )
									print(ANSI.gray(stdout.strip()       )       )
						except FileNotFoundError:
									print("Looks like you do not have git installed, please install."       )

						try:
									UpperCamelCase__   =    subprocess.check_output(["git-lfs", "--version"]       ).decode("utf-8"       )
									print(ANSI.gray(stdout.strip()       )       )
						except FileNotFoundError:
									print(
									    ANSI.red(
									        "Looks like you do not have git-lfs installed, please install."
									        " You can install from https://git-lfs.github.com/."
									        " Then run `git lfs install` (you only have to do this once)."       )       )
						print(""       )

						UpperCamelCase__, UpperCamelCase__   =    whoami(snake_case       )
						UpperCamelCase__   =    self.args.organization if self.args.organization is not None else user
						UpperCamelCase__   =    F'''{namespace}/{self.args.name}'''
						print(F'''You are about to create {ANSI.bold(snake_case       )}'''       )

						if not self.args.yes:
									UpperCamelCase__   =    input("Proceed? [Y/n] "       ).lower()
									if not (choice == "" or choice == "y" or choice == "yes"):
												print("Abort"       )
												exit()
						try:
									UpperCamelCase__   =    create_repo(snake_case ,  name=self.args.name ,  organization=self.args.organization       )
						except HTTPError as e:
									print(snake_case       )
									print(ANSI.red(e.response.text       )       )
									exit(1       )
						print("\nYour repo now lives at:"       )
						print(F'''  {ANSI.bold(snake_case       )}'''       )
						print("\nYou can clone it locally with the command below, and commit/push as usual."       )
						print(F'''\n  git clone {url}'''       )
						print(""       )


