


"""simple docstring"""







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)
__lowercase          = """\
"""

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

__lowercase          = """
Compute XTREME-S evaluation metric associated to each XTREME-S dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
    bleu_kwargs: optional dict of keywords to be passed when computing 'bleu'.
        Keywords include Dict can be one of 'smooth_method', 'smooth_value', 'force', 'lowercase',
        'tokenize', 'use_effective_order'.
    wer_kwargs: optional dict of keywords to be passed when computing 'wer' and 'cer'.
        Keywords include 'concatenate_texts'.
Returns: depending on the XTREME-S task, one or several of:
    \"accuracy\": Accuracy - for 'fleurs-lang_id', 'minds14'
    \"f1\": F1 score - for 'minds14'
    \"wer\": Word error rate - for 'mls', 'fleurs-asr', 'voxpopuli', 'babel'
    \"cer\": Character error rate - for 'mls', 'fleurs-asr', 'voxpopuli', 'babel'
    \"bleu\": BLEU score according to the `sacrebleu` metric - for 'covost2'
Examples:

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'mls')  # 'mls', 'voxpopuli', 'fleurs-asr' or 'babel'
    >>> references = [\"it is sunny here\", \"paper and pen are essentials\"]
    >>> predictions = [\"it's sunny\", \"paper pen are essential\"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'wer': 0.56, 'cer': 0.27}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'covost2')
    >>> references = [\"bonjour paris\", \"il est necessaire de faire du sport de temps en temp\"]
    >>> predictions = [\"bonjour paris\", \"il est important de faire du sport souvent\"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'bleu': 31.65}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'fleurs-lang_id')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'accuracy': 0.6}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'minds14')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'f1': 0.58, 'accuracy': 0.6}
"""

__lowercase          = ["""fleurs-asr""", """mls""", """voxpopuli""", """babel""", """covost2""", """fleurs-lang_id""", """minds14"""]
__lowercase          = """"""

try:
 from jiwer import transforms as tr

 __lowercase          = True
except ImportError:
 __lowercase          = False

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






 class 		_A    (    tr.AbstractTransform    ):







     """simple docstring"""




     def __init__(   self							:       List[str]							,					__UpperCAmelCase							:       str = " "):
       a		:						Tuple 				=     sentence_delimiter






     def       __snake_case				(   self							:       Optional[int]							,					__UpperCAmelCase							:       str):
       return list(__UpperCAmelCase)






     def       __snake_case				(   self							:       Optional[int]							,					__UpperCAmelCase							:       List[str]):
       a		:						str 				=     []
       for sent_idx, sentence in enumerate(__UpperCAmelCase):
         chars.extend(self.process_string(__UpperCAmelCase))
         if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(__UpperCAmelCase) - 1:
           chars.append(self.sentence_delimiter)
       return chars






 __lowercase          = tr.Compose(
     [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
 )
elif _jiwer_available:
 __lowercase          = tr.Compose(
     [
         tr.RemoveMultipleSpaces(),
         tr.Strip(),
         tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
         tr.ReduceToListOfListOfChars(),
     ]
 )
else:
 __lowercase          = None


def 	lowercase     (							A_    ,  A_			)-> List[str]:
  '''simple docstring'''







  return float((preds == labels).mean()			)


def 	lowercase     (							A_    ,  A_			)-> Tuple:
  '''simple docstring'''







  return {
      "f1": float(fa_score(y_true=A_    ,  y_pred=A_    ,  average="macro"			)			),
      "accuracy": simple_accuracy(A_    ,  A_			),
  }


def 	lowercase     (							A_    ,  A_    ,  A_="exp"    ,  A_=None    ,  A_=False    ,  A_=False    ,  A_=None    ,  A_=False    ,  )-> str:
  '''simple docstring'''







  a		:						Optional[Any] 				=     [[label] for label in labels]
  a		:						List[str] 				=     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\"`."			)

  a		:						Any 				=     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"			)
  a		:						Dict 				=     [[refs[i] for refs in labels] for i in range(A_			)]
  a		:						List[Any] 				=     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 	lowercase     (							A_    ,  A_    ,  A_    ,  A_			)-> str:
  '''simple docstring'''







  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:
    a		:						Union[str, Any] 				=     compute_measures(A_    ,  A_			)["wer"]

    a		:						Tuple 				=     compute_measures(A_    ,  A_    ,  truth_transform=A_    ,  hypothesis_transform=A_			)["wer"]
    return {"wer": wer, "cer": cer}
  else:

    def compute_score(A_    ,  A_    ,  A_="wer"			):
      a		:						List[Any] 				=     0
      a		:						List[str] 				=     0
      for prediction, reference in zip(A_    ,  A_			):
        if score_type == "wer":
          a		:						str 				=     compute_measures(A_    ,  A_			)
        elif score_type == "cer":
          a		:						Any 				=     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 		_A    (    datasets.Metric    ):







    """simple docstring"""




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

      a		:						List[Any] 				=     "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(__UpperCAmelCase), "references": datasets.Value(__UpperCAmelCase)})							,					codebase_urls=[]							,					reference_urls=[]							,					format="numpy"							,					)






    def       __snake_case				(   self							:       Optional[int]							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       str=None							,					__UpperCAmelCase							:       Any=None):
      a		:						Union[str, Any] 				=     bleu_kwargs if bleu_kwargs is not None else {}
      a		:						int 				=     wer_kwargs if wer_kwargs is not None else {}

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










"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowercase          = logging.get_logger(__name__)

__lowercase          = {
    """squeezebert/squeezebert-uncased""": (
        """https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/config.json"""
    ),
    """squeezebert/squeezebert-mnli""": """https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/config.json""",
    """squeezebert/squeezebert-mnli-headless""": (
        """https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/config.json"""
    ),
}






class 		_A    (    _a    ):







    """simple docstring"""




    UpperCAmelCase			:				Tuple      =			SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
    UpperCAmelCase			:				Any      =			"""squeezebert"""






    def __init__(   self							:       List[str]							,					__UpperCAmelCase							:       Tuple=30522							,					__UpperCAmelCase							:       Tuple=768							,					__UpperCAmelCase							:       int=12							,					__UpperCAmelCase							:       str=12							,					__UpperCAmelCase							:       List[Any]=3072							,					__UpperCAmelCase							:       Dict="gelu"							,					__UpperCAmelCase							:       List[Any]=0.1							,					__UpperCAmelCase							:       Optional[int]=0.1							,					__UpperCAmelCase							:       Optional[Any]=512							,					__UpperCAmelCase							:       int=2							,					__UpperCAmelCase							:       Dict=0.02							,					__UpperCAmelCase							:       Optional[int]=1e-12							,					__UpperCAmelCase							:       Optional[Any]=0							,					__UpperCAmelCase							:       Optional[Any]=768							,					__UpperCAmelCase							:       Tuple=4							,					__UpperCAmelCase							:       Dict=4							,					__UpperCAmelCase							:       str=4							,					__UpperCAmelCase							:       List[Any]=1							,					__UpperCAmelCase							:       Optional[int]=4							,					__UpperCAmelCase							:       Optional[int]=4							,					**__UpperCAmelCase							:       Tuple							,					):
      super().__init__(pad_token_id=__UpperCAmelCase							,					**__UpperCAmelCase)

      a		:						List[Any] 				=     vocab_size
      a		:						List[Any] 				=     hidden_size
      a		:						Dict 				=     num_hidden_layers
      a		:						int 				=     num_attention_heads
      a		:						Dict 				=     hidden_act
      a		:						Tuple 				=     intermediate_size
      a		:						int 				=     hidden_dropout_prob
      a		:						List[Any] 				=     attention_probs_dropout_prob
      a		:						Union[str, Any] 				=     max_position_embeddings
      a		:						Union[str, Any] 				=     type_vocab_size
      a		:						Optional[Any] 				=     initializer_range
      a		:						Any 				=     layer_norm_eps
      a		:						Any 				=     embedding_size
      a		:						Tuple 				=     q_groups
      a		:						List[str] 				=     k_groups
      a		:						str 				=     v_groups
      a		:						Optional[Any] 				=     post_attention_groups
      a		:						List[str] 				=     intermediate_groups
      a		:						Optional[int] 				=     output_groups






class 		_A    (    _a    ):







    """simple docstring"""




    @property
    def       __snake_case				(   self							:       Optional[Any]):
      if self.task == "multiple-choice":
        a		:						Optional[int] 				=     {0: "batch", 1: "choice", 2: "sequence"}
      else:
        a		:						Tuple 				=     {0: "batch", 1: "sequence"}
      return OrderedDict(
          [
              ("input_ids", dynamic_axis),
              ("attention_mask", dynamic_axis),
              ("token_type_ids", dynamic_axis),
          ])






