
'''simple docstring'''






import argparse

from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta
from transformers.utils import logging


logging.set_verbosity_info()







def 		A_							(						_lowerCAmelCase :     Optional[int]					,						_lowerCAmelCase :     int					,						_lowerCAmelCase :     int				):



      """simple docstring"""




      _lowerCamelCase					: Tuple		    = TaConfig.from_json_file(lowerCAmelCase__				)
      print(F'Building PyTorch model from configuration: {config}'				)
      _lowerCamelCase					: int		    = TaForConditionalGeneration(lowerCAmelCase__				)

      # Load weights from tf checkpoint
      load_tf_weights_in_ta(lowerCAmelCase__					,						lowerCAmelCase__					,						lowerCAmelCase__				)

      # Save pytorch-model
      print(F'Save PyTorch model to {pytorch_dump_path}'				)
      model.save_pretrained(lowerCAmelCase__				)


if __name__ == "__main__":
 UpperCAmelCase_							: Optional[int] =							argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
 )
 parser.add_argument(
     '--config_file',
     default=None,
     type=str,
     required=True,
     help=(
         'The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.'
     ),
 )
 parser.add_argument(
     '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
 )
 UpperCAmelCase_							: Dict =							parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)






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


__UpperCAmelCase						=			logging.get_logger(__name__)



class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			int	, a_    :			List[Any]    )	->	Optional[Any]:



							'''simple docstring'''



							if isinstance(a_	, a_    ):
										a__  :	Any		 =			[label.strip() for label in labels.split(","    ) if label.strip()]
							return labels






				def __call__(		self    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Optional[Any]	, a_    :			List[str]    )	->	Optional[Any]:



							'''simple docstring'''



							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__  :	str		 =			[sequences]

							a__  :	Optional[int]		 =			[]
							for sequence in sequences:
										sequence_pairs.extend([[sequence, hypothesis_template.format(a_    )] for label in labels]    )

							return sequence_pairs, sequences



@add_end_docstrings(_UpperCamelCase  )
class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def __init__(		self    :			str	, a_    :			Optional[Any]=ZeroShotClassificationArgumentHandler()	, *a_    :			Tuple	, **a_    :			str    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[Any]		 =			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    :			Tuple    )	->	str:



							'''simple docstring'''



							for label, ind in self.model.config.labelaid.items():
										if label.lower().startswith("entail"    ):
													return ind
							return -1
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			List[Any]	, a_    :			int=True	, a_    :			Tuple=True	, a_    :			Tuple=TruncationStrategy.ONLY_FIRST	, **a_    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	str		 =			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__  :	List[str]		 =			self.tokenizer.eos_token
							try:
										a__  :	List[str]		 =			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__  :	List[str]		 =			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    :			Tuple	, **a_    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							if kwargs.get("multi_class"	, a_    ) is not None:
										a__  :	str		 =			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__  :	Tuple		 =			{}
							if "candidate_labels" in kwargs:
										a__  :	Any		 =			self._args_parser._parse_labels(kwargs["candidate_labels"]    )
							if "hypothesis_template" in kwargs:
										a__  :	str		 =			kwargs["hypothesis_template"]

							a__  :	Tuple		 =			{}
							if "multi_label" in kwargs:
										a__  :	Dict		 =			kwargs["multi_label"]
							return preprocess_params, {}, postprocess_params
				def __call__(		self    :			str	, a_    :			Union[str, List[str]]	, *a_    :			List[str]	, **a_    :			List[Any]	, )	->	Tuple:



							'''simple docstring'''



							if len(a_    ) == 0:
										pass
							elif len(a_    ) == 1 and "candidate_labels" not in kwargs:
										a__  :	Any		 =			args[0]
							else:
										raise ValueError(F"Unable to understand extra arguments {args}"    )

							return super().__call__(a_	, **a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Tuple	, a_    :			Any=None	, a_    :			Dict="This example is {}."    )	->	Optional[int]:



							'''simple docstring'''



							a__ ,	a__  :	Optional[Any]		 =			self._args_parser(a_	, a_	, a_    )

							for i, (candidate_label, sequence_pair) in enumerate(zip(a_	, a_    )    ):
										a__  :	Union[str, Any]		 =			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    :			Optional[int]	, a_    :			Optional[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			inputs["candidate_label"]
							a__  :	Optional[int]		 =			inputs["sequence"]
							a__  :	Optional[int]		 =			{k: inputs[k] for k in self.tokenizer.model_input_names}
							a__  :	int		 =			self.model(**a_    )

							a__  :	Optional[int]		 =			{
							    "candidate_label": candidate_label,
							    "sequence": sequence,
							    "is_last": inputs["is_last"],
							    **outputs,
							}
							return model_outputs






				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Any	, a_    :			List[str]=False    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			[outputs["candidate_label"] for outputs in model_outputs]
							a__  :	Optional[int]		 =			[outputs["sequence"] for outputs in model_outputs]
							a__  :	Union[str, Any]		 =			np.concatenate([output["logits"].numpy() for output in model_outputs]    )
							a__  :	List[str]		 =			logits.shape[0]
							a__  :	Optional[int]		 =			len(a_    )
							a__  :	List[str]		 =			N // n
							a__  :	int		 =			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__  :	str		 =			self.entailment_id
										a__  :	str		 =			-1 if entailment_id == 0 else 0
										a__  :	str		 =			reshaped_outputs[..., [contradiction_id, entailment_id]]
										a__  :	List[Any]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )
										a__  :	str		 =			scores[..., 1]
							else:
										# softmax the "entailment" logits over all candidate labels
										a__  :	str		 =			reshaped_outputs[..., self.entailment_id]
										a__  :	Optional[int]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )

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