


"""simple docstring"""




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


lowercase_			     =  logging.get_logger(__name__)
class 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE       ):


		'''simple docstring'''



		def 			__UpperCAmelCase       (  self							, _a      ):
				if isinstance(_a							, _a      ):
						__a           =			[label.strip() for label in labels.split(''','''      ) if label.strip()]
				return labels





		def __call__(  self							, _a							, _a							, _a      ):
				if len(_a      ) == 0 or len(_a      ) == 0:
						raise ValueError('''You must include at least one label and at least one sequence.'''      )
				if hypothesis_template.format(labels[0]      ) == hypothesis_template:
						raise ValueError(
						    (
						        '''The provided hypothesis_template "{}" was not able to be formatted with the target labels. '''
						        '''Make sure the passed template includes formatting syntax such as {{}} where the label should go.'''
						    ).format(_a      )      )

				if isinstance(_a							, _a      ):
						__a           =			[sequences]

				__a           =			[]
				for sequence in sequences:
						sequence_pairs.extend([[sequence, hypothesis_template.format(_a      )] for label in labels]      )

				return sequence_pairs, sequences







@add_end_docstrings(__SCREAMING_SNAKE_CASE       )
class 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE       ):


		'''simple docstring'''



		def __init__(  self							, _a=ZeroShotClassificationArgumentHandler()							, *_a							, **_a      ):
				__a           =			args_parser
				super().__init__(*_a							, **_a      )
				if self.entailment_id == -1:
						logger.warning(
						    '''Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '''
						    '''-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.'''      )

		@property
		def 			__UpperCAmelCase       (  self      ):
				for label, ind in self.model.config.labelaid.items():
						if label.lower().startswith('''entail'''      ):
								return ind
				return -1

		def 			__UpperCAmelCase       (  self							, _a							, _a=True							, _a=True							, _a=TruncationStrategy.ONLY_FIRST							, **_a      ):
				__a           =			self.framework
				if self.tokenizer.pad_token is None:
						# Override for tokenizers not supporting padding
						logger.error(
						    '''Tokenizer was not supporting padding necessary for zero-shot, attempting to use '''
						    ''' `pad_token=eos_token`'''      )
						__a           =			self.tokenizer.eos_token
				try:
						__a           =			self.tokenizer(
						    _a							, add_special_tokens=_a							, return_tensors=_a							, padding=_a							, truncation=_a							, )
				except Exception as e:
						if "too short" in str(_a      ):
								# tokenizers might yell that we want to truncate
								# to a value that is not even reached by the input.
								# In that case we don't want to truncate.
								# It seems there's not a really better way to catch that
								# exception.

								__a           =			self.tokenizer(
								    _a							, add_special_tokens=_a							, return_tensors=_a							, padding=_a							, truncation=TruncationStrategy.DO_NOT_TRUNCATE							, )
						else:
								raise e

				return inputs

		def 			__UpperCAmelCase       (  self							, **_a      ):
				if kwargs.get('''multi_class'''							, _a      ) is not None:
						__a           =			kwargs['''multi_class''']
						logger.warning(
						    '''The `multi_class` argument has been deprecated and renamed to `multi_label`. '''
						    '''`multi_class` will be removed in a future version of Transformers.'''      )
				__a           =			{}
				if "candidate_labels" in kwargs:
						__a           =			self._args_parser._parse_labels(kwargs['''candidate_labels''']      )
				if "hypothesis_template" in kwargs:
						__a           =			kwargs['''hypothesis_template''']

				__a           =			{}
				if "multi_label" in kwargs:
						__a           =			kwargs['''multi_label''']
				return preprocess_params, {}, postprocess_params

		def __call__(  self							, _a							, *_a							, **_a							, ):
				if len(_a      ) == 0:
						pass
				elif len(_a      ) == 1 and "candidate_labels" not in kwargs:
						__a           =			args[0]
				else:
						raise ValueError(f'''Unable to understand extra arguments {args}'''      )

				return super().__call__(_a							, **_a      )

		def 			__UpperCAmelCase       (  self							, _a							, _a=None							, _a="This example is {}."      ):
				__a      ,     __a           =			self._args_parser(_a							, _a							, _a      )

				for i, (candidate_label, sequence_pair) in enumerate(zip(_a							, _a      )      ):
						__a           =			self._parse_and_tokenize([sequence_pair]      )

						yield {
						    "candidate_label": candidate_label,
						    "sequence": sequences[0],
						    "is_last": i == len(_a      ) - 1,
						    **model_input,
						}

		def 			__UpperCAmelCase       (  self							, _a      ):
				__a           =			inputs['''candidate_label''']
				__a           =			inputs['''sequence''']
				__a           =			{k: inputs[k] for k in self.tokenizer.model_input_names}
				__a           =			self.model(**_a      )

				__a           =			{
				    '''candidate_label''': candidate_label,
				    '''sequence''': sequence,
				    '''is_last''': inputs['''is_last'''],
				    **outputs,
				}
				return model_outputs





		def 			__UpperCAmelCase       (  self							, _a							, _a=False      ):
				__a           =			[outputs['''candidate_label'''] for outputs in model_outputs]
				__a           =			[outputs['''sequence'''] for outputs in model_outputs]
				__a           =			np.concatenate([output['''logits'''].numpy() for output in model_outputs]      )
				__a           =			logits.shape[0]
				__a           =			len(_a      )
				__a           =			N // n
				__a           =			logits.reshape((num_sequences, n, -1)      )

				if multi_label or len(_a      ) == 1:
						# softmax over the entailment vs. contradiction dim for each label independently
						__a           =			self.entailment_id
						__a           =			-1 if entailment_id == 0 else 0
						__a           =			reshaped_outputs[..., [contradiction_id, entailment_id]]
						__a           =			np.exp(_a      ) / np.exp(_a      ).sum(-1							, keepdims=_a      )
						__a           =			scores[..., 1]
				else:
						# softmax the "entailment" logits over all candidate labels
						__a           =			reshaped_outputs[..., self.entailment_id]
						__a           =			np.exp(_a      ) / np.exp(_a      ).sum(-1							, keepdims=_a      )

				__a           =			list(reversed(scores[0].argsort()      )      )
				return {
				    "sequence": sequences[0],
				    "labels": [candidate_labels[i] for i in top_inds],
				    "scores": scores[0, top_inds].tolist(),
				}










"""simple docstring"""




from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand

def        lowercase							(		lowerCAmelCase__ :     Namespace			) ->      Tuple:
		return ConvertCommand(
		    args.model_type    ,					args.tf_checkpoint    ,					args.pytorch_dump_output    ,					args.config    ,					args.finetuning_task_name			)


lowercase_			     =  "\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					(   __SCREAMING_SNAKE_CASE       ):


		'''simple docstring'''



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

		def __init__(  self							, _a							, _a							, _a							, _a							, _a							, *_a							, ):
				__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 			__UpperCAmelCase       (  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(_a      )

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

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

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

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

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

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

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






