





'''simple docstring'''


from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand


def 						__a(SCREAMING_SNAKE_CASE_	:       Namespace     ):



    '''simple docstring'''





    return ConvertCommand(
        args.model_type		,							args.tf_checkpoint		,							args.pytorch_dump_output		,							args.config		,							args.finetuning_task_name     )


_SCREAMING_SNAKE_CASE									=				"\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n"





class  lowerCAmelCase_		(		__magic_name__		):




 @staticmethod
 def 				_snake_case (						_lowerCAmelCase		)     ->				str:
     _lowerCAmelCase						      =   parser.add_parser(
         "convert"      ,  help="CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."      ,  )
     train_parser.add_argument("--model_type"      ,  type=_lowerCAmelCase      ,  required=_lowerCAmelCase      ,  help="Model's type."		)
     train_parser.add_argument(
         "--tf_checkpoint"      ,  type=_lowerCAmelCase      ,  required=_lowerCAmelCase      ,  help="TensorFlow checkpoint path or folder."		)
     train_parser.add_argument(
         "--pytorch_dump_output"      ,  type=_lowerCAmelCase      ,  required=_lowerCAmelCase      ,  help="Path to the PyTorch saved model output."		)
     train_parser.add_argument("--config"      ,  type=_lowerCAmelCase      ,  default=""      ,  help="Configuration file path or folder."		)
     train_parser.add_argument(
         "--finetuning_task_name"      ,  type=_lowerCAmelCase      ,  default=_lowerCAmelCase      ,  help="Optional fine-tuning task name if the TF model was a finetuned model."      ,  )
     train_parser.set_defaults(func=_lowerCAmelCase		)




 def __init__(						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  *_lowerCAmelCase      ,  )     ->				Dict:
     _lowerCAmelCase						      =   logging.get_logger("transformers-cli/converting"		)

     self._logger.info(f'''Loading model {model_type}'''		)
     _lowerCAmelCase						      =   model_type
     _lowerCAmelCase						      =   tf_checkpoint
     _lowerCAmelCase						      =   pytorch_dump_output
     _lowerCAmelCase						      =   config
     _lowerCAmelCase						      =   finetuning_task_name





 def 				_snake_case (						self		)     ->				str:
     if self._model_type == "albert":
         try:
             from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
                 convert_tf_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_tf_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "bert":
         try:
             from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
                 convert_tf_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_tf_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "funnel":
         try:
             from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
                 convert_tf_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_tf_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "t5":
         try:
             from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_tf_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "gpt":
         from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
             convert_openai_checkpoint_to_pytorch,
         )

         convert_openai_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "transfo_xl":
         try:
             from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
                 convert_transfo_xl_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         if "ckpt" in self._tf_checkpoint.lower():
             _lowerCAmelCase						      =   self._tf_checkpoint
             _lowerCAmelCase						      =   ""
         else:
             _lowerCAmelCase						      =   self._tf_checkpoint
             _lowerCAmelCase						      =   ""
         convert_transfo_xl_checkpoint_to_pytorch(
             _lowerCAmelCase      ,  self._config      ,  self._pytorch_dump_output      ,  _lowerCAmelCase		)
     elif self._model_type == "gpt2":
         try:
             from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
                 convert_gpta_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     elif self._model_type == "xlnet":
         try:
             from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
                 convert_xlnet_checkpoint_to_pytorch,
             )
         except ImportError:
             raise ImportError(_lowerCAmelCase		)

         convert_xlnet_checkpoint_to_pytorch(
             self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output      ,  self._finetuning_task_name		)
     elif self._model_type == "xlm":
         from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
             convert_xlm_checkpoint_to_pytorch,
         )

         convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._pytorch_dump_output		)
     elif self._model_type == "lxmert":
         from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
             convert_lxmert_checkpoint_to_pytorch,
         )

         convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._pytorch_dump_output		)
     elif self._model_type == "rembert":
         from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
             convert_rembert_tf_checkpoint_to_pytorch,
         )

         convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint      ,  self._config      ,  self._pytorch_dump_output		)
     else:
         raise ValueError(
             "--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]"		)









'''simple docstring'''


import re
import string

import numpy as np

import datasets


_SCREAMING_SNAKE_CASE									=				"\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n"

_SCREAMING_SNAKE_CASE									=				"\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results[\"exact_match\"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]\n    >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results[\"exact_match\"], 1))\n    33.3\n\n"

_SCREAMING_SNAKE_CASE									=				"\n"

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




 def 				_snake_case (						self		)     ->				List[str]:
     return datasets.MetricInfo(
         description=_DESCRIPTION      ,  citation=_CITATION      ,  inputs_description=_KWARGS_DESCRIPTION      ,  features=datasets.Features(
             {
                 "predictions": datasets.Value("string"      ,  id="sequence"		),
                 "references": datasets.Value("string"      ,  id="sequence"		),
             }		)      ,  reference_urls=[]      ,  )





 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase=None      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=False      ,  )     ->				str:
     if regexes_to_ignore is not None:
         for s in regexes_to_ignore:
             _lowerCAmelCase						      =   np.array([re.sub(_lowerCAmelCase      ,  ""      ,  _lowerCAmelCase		) for x in predictions]		)
             _lowerCAmelCase						      =   np.array([re.sub(_lowerCAmelCase      ,  ""      ,  _lowerCAmelCase		) for x in references]		)
     else:
         _lowerCAmelCase						      =   np.asarray(_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.asarray(_lowerCAmelCase		)

     if ignore_case:
         _lowerCAmelCase						      =   np.char.lower(_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.lower(_lowerCAmelCase		)

     if ignore_punctuation:
         _lowerCAmelCase						      =   string.punctuation.maketrans(""      ,  ""      ,  string.punctuation		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)

     if ignore_numbers:
         _lowerCAmelCase						      =   string.digits.maketrans(""      ,  ""      ,  string.digits		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)

     _lowerCAmelCase						      =   predictions == references

     return {"exact_match": np.mean(_lowerCAmelCase		) * 100}


