



from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand
def  UpperCamelCase						(       _A			:				Namespace    )->       Union[str, Any]:





						"""simple docstring"""





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


UpperCAmelCase_							:      int      ="\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      UpperCamelCase    (						_UpperCAmelCase     ):



	@staticmethod
	def 			__A							(       UpperCAmelCase__		):
							A__							=       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=UpperCAmelCase__			,	required=UpperCAmelCase__			,	help="Model's type."		)
							train_parser.add_argument(
							    "--tf_checkpoint"			,	type=UpperCAmelCase__			,	required=UpperCAmelCase__			,	help="TensorFlow checkpoint path or folder."		)
							train_parser.add_argument(
							    "--pytorch_dump_output"			,	type=UpperCAmelCase__			,	required=UpperCAmelCase__			,	help="Path to the PyTorch saved model output."		)
							train_parser.add_argument("--config"			,	type=UpperCAmelCase__			,	default=""			,	help="Configuration file path or folder."		)
							train_parser.add_argument(
							    "--finetuning_task_name"			,	type=UpperCAmelCase__			,	default=UpperCAmelCase__			,	help="Optional fine-tuning task name if the TF model was a finetuned model."			,	)
							train_parser.set_defaults(func=UpperCAmelCase__		)



	def __init__(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	*UpperCAmelCase__			,	):
							A__							=       logging.get_logger("transformers-cli/converting"		)

							self._logger.info(F"""Loading model {model_type}"""		)
							A__							=       model_type
							A__							=       tf_checkpoint
							A__							=       pytorch_dump_output
							A__							=       config
							A__							=       finetuning_task_name



	def 			__A							(       self		):
							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(UpperCAmelCase__		)

													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(UpperCAmelCase__		)

													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(UpperCAmelCase__		)

													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(UpperCAmelCase__		)

													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(UpperCAmelCase__		)

													if "ckpt" in self._tf_checkpoint.lower():
																			A__							=       self._tf_checkpoint
																			A__							=       ""
													else:
																			A__							=       self._tf_checkpoint
																			A__							=       ""
													convert_transfo_xl_checkpoint_to_pytorch(
													    UpperCAmelCase__			,	self._config			,	self._pytorch_dump_output			,	UpperCAmelCase__		)
							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(UpperCAmelCase__		)

													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(UpperCAmelCase__		)

													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]"		)










import datasets

from .evaluate import evaluate


UpperCAmelCase_							:      List[Any]      =  "\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

UpperCAmelCase_							:      Any      =  "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

UpperCAmelCase_							:      Tuple      =  "\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"






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



	def 			__A							(       self		):
							return datasets.MetricInfo(
							    description=_DESCRIPTION			,	citation=_CITATION			,	inputs_description=_KWARGS_DESCRIPTION			,	features=datasets.Features(
							        {
							            "predictions": {"id": datasets.Value("string"		), "prediction_text": datasets.Value("string"		)},
							            "references": {
							                "id": datasets.Value("string"		),
							                "answers": datasets.features.Sequence(
							                    {
							                        "text": datasets.Value("string"		),
							                        "answer_start": datasets.Value("int32"		),
							                    }		),
							            },
							        }		)			,	codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]			,	reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]			,	)



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__		):
							A__							=       {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
							A__							=       [
							    {
							        "paragraphs": [
							            {
							                "qas": [
							                    {
							                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
							                        "id": ref["id"],
							                    }
							                    for ref in references
							                ]
							            }
							        ]
							    }
							]
							A__							=       evaluate(dataset=UpperCAmelCase__			,	predictions=UpperCAmelCase__		)
							return score





