
"""simple docstring"""






import comet  # From: unbabel-comet
import torch

import datasets


__SCREAMING_SNAKE_CASE							=     datasets.logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     """\
@inproceedings{rei-EtAl:2020:WMT,
   author    = {Rei, Ricardo  and  Stewart, Craig  and  Farinha, Ana C  and  Lavie, Alon},
   title     = {Unbabel's Participation in the WMT20 Metrics Shared Task},
   booktitle      = {Proceedings of the Fifth Conference on Machine Translation},
   month          = {November},
   year           = {2020},
   address        = {Online},
   publisher      = {Association for Computational Linguistics},
   pages     = {909--918},
}
@inproceedings{rei-etal-2020-comet,
   title = \"{COMET}: A Neural Framework for {MT} Evaluation\",
   author = \"Rei, Ricardo  and
      Stewart, Craig  and
      Farinha, Ana C  and
      Lavie, Alon\",
   booktitle = \"Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)\",
   month = nov,
   year = \"2020\",
   address = \"Online\",
   publisher = \"Association for Computational Linguistics\",
   url = \"https://www.aclweb.org/anthology/2020.emnlp-main.213\",
   pages = \"2685--2702\",
}
"""

__SCREAMING_SNAKE_CASE							=     """\
Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA's or MQM).
With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition.

See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information.
"""

__SCREAMING_SNAKE_CASE							=     """
COMET score.

Args:

`sources` (list of str): Source sentences
`predictions` (list of str): candidate translations
`references` (list of str): reference translations
`cuda` (bool): If set to True, runs COMET using GPU
`show_progress` (bool): Shows progress
`model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None.

Returns:
    `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`.
    `scores`: List of scores.

Examples:

    >>> comet_metric = datasets.load_metric('comet')
    >>> # comet_metric = load_metric('comet', 'wmt20-comet-da')  # you can also choose which model to use
    >>> source = [\"Dem Feuer konnte Einhalt geboten werden\", \"Schulen und Kindergärten wurden eröffnet.\"]
    >>> hypothesis = [\"The fire could be stopped\", \"Schools and kindergartens were open\"]
    >>> reference = [\"They were able to control the fire.\", \"Schools and kindergartens opened\"]
    >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source)
    >>> print([round(v, 2) for v in results[\"scores\"]])
    [0.19, 0.92]
"""





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



     def  lowerCamelCase_						(							self :List[Any]   ):







      '''simple docstring'''



      return datasets.MetricInfo(
          description=_DESCRIPTION      ,  citation=_CITATION      ,  homepage='https://unbabel.github.io/COMET/html/index.html'      ,  inputs_description=_KWARGS_DESCRIPTION      ,  features=datasets.Features(
              {
                  'sources': datasets.Value('string'      ,  id='sequence'   ),
                  'predictions': datasets.Value('string'      ,  id='sequence'   ),
                  'references': datasets.Value('string'      ,  id='sequence'   ),
              }   )      ,  codebase_urls=['https://github.com/Unbabel/COMET']      ,  reference_urls=[
              'https://github.com/Unbabel/COMET',
              'https://www.aclweb.org/anthology/2020.emnlp-main.213/',
              'http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6',
          ]      ,  )



     def  lowerCamelCase_						(							self :Any      ,  _lowerCamelCase :Dict   ):







      '''simple docstring'''



      if self.config_name == "default":
       UpperCamelCase_     :					List[Any]   	=comet.load_from_checkpoint(comet.download_model('wmt20-comet-da'   )   )
      else:
       UpperCamelCase_     :					Optional[int]   	=comet.load_from_checkpoint(comet.download_model(self.config_name   )   )




     def  lowerCamelCase_						(							self :List[str]      ,  _lowerCamelCase :Dict      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[int]      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :str=False   ):







      '''simple docstring'''



      if gpus is None:
       UpperCamelCase_     :					List[Any]   	=1 if torch.cuda.is_available() else 0
      UpperCamelCase_     :					Union[str, Any]   	={"src": sources, "mt": predictions, "ref": references}
      UpperCamelCase_     :					Union[str, Any]   	=[dict(zip(snake_case__      ,  snake_case__   )   ) for t in zip(*data.values()   )]
      UpperCamelCase_     :					Optional[int]   	=self.scorer.predict(snake_case__      ,  gpus=snake_case__      ,  progress_bar=snake_case__   )
      return {"mean_score": mean_score, "scores": scores}








"""simple docstring"""



import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


_lowerCAmelCase	:	str				=      logging.get_logger(__name__)


_lowerCAmelCase	:	int				=      {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

_lowerCAmelCase	:	List[Any]				=      {
    """vocab_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""",
    },
}

_lowerCAmelCase	:	List[str]				=      {
    """allenai/led-base-16384""": 16_384,
}




class 			lowerCAmelCase__			(    __magic_name__   ):
   SCREAMING_SNAKE_CASE_    =VOCAB_FILES_NAMES
   SCREAMING_SNAKE_CASE_    =PRETRAINED_VOCAB_FILES_MAP
   SCREAMING_SNAKE_CASE_    =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   SCREAMING_SNAKE_CASE_    =LEDTokenizer
   SCREAMING_SNAKE_CASE_    =['''input_ids''', '''attention_mask''']
   def __init__(		self      :	Optional[Any]							,			snake_case__      :	str=None							,			snake_case__      :	List[Any]=None							,			snake_case__      :	Dict=None							,			snake_case__      :	List[str]="replace"							,			snake_case__      :	Optional[int]="<s>"							,			snake_case__      :	List[str]="</s>"							,			snake_case__      :	Union[str, Any]="</s>"							,			snake_case__      :	Dict="<s>"							,			snake_case__      :	Tuple="<unk>"							,			snake_case__      :	Any="<pad>"							,			snake_case__      :	Dict="<mask>"							,			snake_case__      :	int=False							,			snake_case__      :	Optional[int]=True							,			**snake_case__      :	List[Any]							,			):


        '''simple docstring'''
        super().__init__(
            snake_case__							,			snake_case__							,			tokenizer_file=snake_case__							,			errors=snake_case__							,			bos_token=snake_case__							,			eos_token=snake_case__							,			sep_token=snake_case__							,			cls_token=snake_case__							,			unk_token=snake_case__							,			pad_token=snake_case__							,			mask_token=snake_case__							,			add_prefix_space=snake_case__							,			trim_offsets=snake_case__							,			**snake_case__							,			)

        UpperCAmelCase__						:    str							=							json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()							)
        if pre_tok_state.get("add_prefix_space"							,			snake_case__							) != add_prefix_space:
             UpperCAmelCase__						:    Dict							=							getattr(snake_case__							,			pre_tok_state.pop("type"							)							)
             UpperCAmelCase__						:    str							=							add_prefix_space
             UpperCAmelCase__						:    Any							=							pre_tok_class(**snake_case__							)

        UpperCAmelCase__						:    Dict							=							add_prefix_space

        # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
        UpperCAmelCase__						:    List[str]							=							"post_processor"
        UpperCAmelCase__						:    List[Any]							=							getattr(self.backend_tokenizer							,			snake_case__							,			snake_case__							)
        if tokenizer_component_instance:
             UpperCAmelCase__						:    int							=							json.loads(tokenizer_component_instance.__getstate__()							)

             # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
             if "sep" in state:
                  UpperCAmelCase__						:    Optional[Any]							=							tuple(state["sep"]							)
             if "cls" in state:
                  UpperCAmelCase__						:    Any							=							tuple(state["cls"]							)

             UpperCAmelCase__						:    Any							=							False

             if state.get("add_prefix_space"							,			snake_case__							) != add_prefix_space:
                  UpperCAmelCase__						:    Union[str, Any]							=							add_prefix_space
                  UpperCAmelCase__						:    List[Any]							=							True

             if state.get("trim_offsets"							,			snake_case__							) != trim_offsets:
                  UpperCAmelCase__						:    Optional[int]							=							trim_offsets
                  UpperCAmelCase__						:    List[Any]							=							True

             if changes_to_apply:
                  UpperCAmelCase__						:    List[str]							=							getattr(snake_case__							,			state.pop("type"							)							)
                  UpperCAmelCase__						:    Optional[int]							=							component_class(**snake_case__							)
                  setattr(self.backend_tokenizer							,			snake_case__							,			snake_case__							)
   @property
   # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
   def       __a							(		self      :	Any							):


        '''simple docstring'''
        if self._mask_token is None:
             if self.verbose:
                  logger.error("Using mask_token, but it is not set yet."							)
             return None
        return str(self._mask_token							)
   @mask_token.setter
   def       __a							(		self      :	Any							,			snake_case__      :	Dict							):


        '''simple docstring'''
        UpperCAmelCase__						:    str							=							AddedToken(snake_case__							,			lstrip=snake_case__							,			rstrip=snake_case__							) if isinstance(snake_case__							,			snake_case__							) else value
        UpperCAmelCase__						:    Dict							=							value
   def       __a							(		self      :	str							,			*snake_case__      :	Any							,			**snake_case__      :	Optional[int]							):


        '''simple docstring'''
        UpperCAmelCase__						:    Any							=							kwargs.get("is_split_into_words"							,			snake_case__							)

        if is_split_into_words and not self.add_prefix_space:
             raise ValueError(
                 f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                 "to use it with pretokenized inputs."							)

        return super()._batch_encode_plus(*snake_case__							,			**snake_case__							)
   def       __a							(		self      :	List[str]							,			*snake_case__      :	Union[str, Any]							,			**snake_case__      :	List[Any]							):


        '''simple docstring'''
        UpperCAmelCase__						:    Optional[Any]							=							kwargs.get("is_split_into_words"							,			snake_case__							)

        if is_split_into_words and not self.add_prefix_space:
             raise ValueError(
                 f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                 "to use it with pretokenized inputs."							)

        return super()._encode_plus(*snake_case__							,			**snake_case__							)
   def       __a							(		self      :	Tuple							,			snake_case__      :	str							,			snake_case__      :	Optional[str] = None							):


        '''simple docstring'''
        UpperCAmelCase__						:    Union[str, Any]							=							self._tokenizer.model.save(snake_case__							,			name=snake_case__							)
        return tuple(snake_case__							)
   def       __a							(		self      :	str							,			snake_case__      :	List[Any]							,			snake_case__      :	str=None							):


        '''simple docstring'''
        UpperCAmelCase__						:    Optional[Any]							=							[self.bos_token_id] + token_ids_a + [self.eos_token_id]
        if token_ids_a is None:
             return output

        return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]
   def       __a							(		self      :	str							,			snake_case__      :	List[int]							,			snake_case__      :	Optional[List[int]] = None							):


        '''simple docstring'''
        UpperCAmelCase__						:    Optional[int]							=							[self.sep_token_id]
        UpperCAmelCase__						:    Optional[Any]							=							[self.cls_token_id]

        if token_ids_a is None:
             return len(cls + token_ids_a + sep							) * [0]
        return len(cls + token_ids_a + sep + sep + token_ids_a + sep							) * [0]




   def       __a							(		self      :	Any							,			snake_case__      :	Union[Dict[str, EncodedInput], BatchEncoding]							,			snake_case__      :	Optional[int] = None							,			snake_case__      :	PaddingStrategy = PaddingStrategy.DO_NOT_PAD							,			snake_case__      :	Optional[int] = None							,			snake_case__      :	Optional[bool] = None							,			):


        '''simple docstring'''
        UpperCAmelCase__						:    str							=							super()._pad(
            encoded_inputs=snake_case__							,			max_length=snake_case__							,			padding_strategy=snake_case__							,			pad_to_multiple_of=snake_case__							,			return_attention_mask=snake_case__							,			)

        # Load from model defaults
        if return_attention_mask is None:
             UpperCAmelCase__						:    Optional[int]							=							"attention_mask" in self.model_input_names

        if return_attention_mask and "global_attention_mask" in encoded_inputs:
             UpperCAmelCase__						:    List[Any]							=							encoded_inputs[self.model_input_names[0]]
             # `global_attention_mask` need to have the same length as other (sequential) inputs.
             UpperCAmelCase__						:    Any							=							len(encoded_inputs["global_attention_mask"]							) != len(snake_case__							)

             if needs_to_be_padded:
                  UpperCAmelCase__						:    List[str]							=							len(snake_case__							) - len(encoded_inputs["global_attention_mask"]							)

                  if self.padding_side == "right":
                       # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
                       UpperCAmelCase__						:    Dict							=							(
                           encoded_inputs["global_attention_mask"] + [-1] * difference
                       )
                  elif self.padding_side == "left":
                       UpperCAmelCase__						:    Dict							=							[-1] * difference + encoded_inputs[
                           "global_attention_mask"
                       ]
                  else:
                       raise ValueError("Invalid padding strategy:" + str(self.padding_side							)							)

        return encoded_inputs


