



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


snake_case_ :	int         =  logging.get_logger(__name__)






class 							lowercase__	(							lowercase   ):





	def 	UpperCamelCase_  ( self					:				List[str] ,lowerCamelCase__					:				str ):







							'''simple docstring'''
							if isinstance(lowerCamelCase__ ,lowerCamelCase__ ):
													_UpperCamelCase					:		Union[str, Any] 				=   [label.strip() for label in labels.split(',' ) if label.strip()]
							return labels





	def __call__( self					:				Optional[Any] ,lowerCamelCase__					:				Optional[Any] ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				Dict ):







							'''simple docstring'''
							if len(lowerCamelCase__ ) == 0 or len(lowerCamelCase__ ) == 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(lowerCamelCase__ ) )

							if isinstance(lowerCamelCase__ ,lowerCamelCase__ ):
													_UpperCamelCase					:		Dict 				=   [sequences]

							_UpperCamelCase					:		str 				=   []
							for sequence in sequences:
													sequence_pairs.extend([[sequence, hypothesis_template.format(lowerCamelCase__ )] for label in labels] )

							return sequence_pairs, sequences






@add_end_docstrings(lowercase   )
class 							lowercase__	(							lowercase   ):





	def __init__( self					:				str ,lowerCamelCase__					:				str=ZeroShotClassificationArgumentHandler() ,*lowerCamelCase__					:				Dict ,**lowerCamelCase__					:				Tuple ):







							'''simple docstring'''
							_UpperCamelCase					:		Optional[Any] 				=   args_parser
							super().__init__(*lowerCamelCase__ ,**lowerCamelCase__ )
							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					:				str ):







							'''simple docstring'''
							for label, ind in self.model.config.labelaid.items():
													if label.lower().startswith('entail' ):
																			return ind
							return -1





	def 	UpperCamelCase_  ( self					:				int ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				List[str]=True ,lowerCamelCase__					:				Union[str, Any]=True ,lowerCamelCase__					:				Optional[int]=TruncationStrategy.ONLY_FIRST ,**lowerCamelCase__					:				Union[str, Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Tuple 				=   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`' )
													_UpperCamelCase					:		List[str] 				=   self.tokenizer.eos_token
							try:
													_UpperCamelCase					:		Optional[Any] 				=   self.tokenizer(
													    lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ,return_tensors=lowerCamelCase__ ,padding=lowerCamelCase__ ,truncation=lowerCamelCase__ ,)
							except Exception as e:
													if "too short" in str(lowerCamelCase__ ):
																			# 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.

																			_UpperCamelCase					:		int 				=   self.tokenizer(
																			    lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ,return_tensors=lowerCamelCase__ ,padding=lowerCamelCase__ ,truncation=TruncationStrategy.DO_NOT_TRUNCATE ,)
													else:
																			raise e

							return inputs





	def 	UpperCamelCase_  ( self					:				Any ,**lowerCamelCase__					:				str ):







							'''simple docstring'''
							if kwargs.get('multi_class' ,lowerCamelCase__ ) is not None:
													_UpperCamelCase					:		Union[str, Any] 				=   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.' )
							_UpperCamelCase					:		Tuple 				=   {}
							if "candidate_labels" in kwargs:
													_UpperCamelCase					:		Optional[int] 				=   self._args_parser._parse_labels(kwargs['candidate_labels'] )
							if "hypothesis_template" in kwargs:
													_UpperCamelCase					:		List[Any] 				=   kwargs['hypothesis_template']

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





	def __call__( self					:				Optional[int] ,lowerCamelCase__					:				Union[str, List[str]] ,*lowerCamelCase__					:				Optional[int] ,**lowerCamelCase__					:				List[str] ,):







							'''simple docstring'''
							if len(lowerCamelCase__ ) == 0:
													pass
							elif len(lowerCamelCase__ ) == 1 and "candidate_labels" not in kwargs:
													_UpperCamelCase					:		List[Any] 				=   args[0]
							else:
													raise ValueError(F'Unable to understand extra arguments {args}' )

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





	def 	UpperCamelCase_  ( self					:				Union[str, Any] ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Optional[Any]=None ,lowerCamelCase__					:				Optional[Any]="This example is {}." ):







							'''simple docstring'''
							_UpperCamelCase				,			_UpperCamelCase					:		Optional[int] 				=   self._args_parser(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ )

							for i, (candidate_label, sequence_pair) in enumerate(zip(lowerCamelCase__ ,lowerCamelCase__ ) ):
													_UpperCamelCase					:		List[str] 				=   self._parse_and_tokenize([sequence_pair] )

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





	def 	UpperCamelCase_  ( self					:				List[Any] ,lowerCamelCase__					:				Union[str, Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Dict 				=   inputs['candidate_label']
							_UpperCamelCase					:		List[Any] 				=   inputs['sequence']
							_UpperCamelCase					:		Tuple 				=   {k: inputs[k] for k in self.tokenizer.model_input_names}
							_UpperCamelCase					:		int 				=   self.model(**lowerCamelCase__ )

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





	def 	UpperCamelCase_  ( self					:				List[Any] ,lowerCamelCase__					:				int ,lowerCamelCase__					:				Optional[int]=False ):







							'''simple docstring'''
							_UpperCamelCase					:		str 				=   [outputs['candidate_label'] for outputs in model_outputs]
							_UpperCamelCase					:		List[str] 				=   [outputs['sequence'] for outputs in model_outputs]
							_UpperCamelCase					:		Optional[Any] 				=   np.concatenate([output['logits'].numpy() for output in model_outputs] )
							_UpperCamelCase					:		str 				=   logits.shape[0]
							_UpperCamelCase					:		Optional[Any] 				=   len(lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   N // n
							_UpperCamelCase					:		Optional[Any] 				=   logits.reshape((num_sequences, n, -1) )

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

							_UpperCamelCase					:		Tuple 				=   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'''






import unicodedata
from dataclasses import dataclass
from typing import Optional, Union

import numpy as np

from transformers.data.data_collator import DataCollatorMixin
from transformers.file_utils import PaddingStrategy
from transformers.tokenization_utils_base import PreTrainedTokenizerBase


def 		_A     (    A__     ,				A__     ,				A__     ,				A__      ):






 """simple docstring"""



 if isinstance(A__     ,				A__      ):
  __lowercase							  = np.full((len(A__      ), sequence_length, 2)     ,				A__      )
 else:
  __lowercase							  = np.full((len(A__      ), sequence_length)     ,				A__      )

 for i, tensor in enumerate(A__      ):
  if padding_side == "right":
   if isinstance(A__     ,				A__      ):
    __lowercase							  = tensor[:sequence_length]
   else:
    __lowercase							  = tensor[:sequence_length]
  else:
   if isinstance(A__     ,				A__      ):
    __lowercase							  = tensor[:sequence_length]
   else:
    __lowercase							  = tensor[:sequence_length]

 return out_tensor.tolist()


def 		_A     (    A__      ):






 """simple docstring"""



 __lowercase							  = ord(A__      )
 if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
  return True
 __lowercase							  = unicodedata.category(A__      )
 if cat.startswith('''P'''      ):
  return True
 return False






@dataclass
class 			lowercase_	(lowerCamelCase__      ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE    :  PreTrainedTokenizerBase
  SCREAMING_SNAKE_CASE    :  Union[bool, str, PaddingStrategy]							=			True
  SCREAMING_SNAKE_CASE    :  Optional[int]							=			None
  SCREAMING_SNAKE_CASE    :  Optional[int]							=			None
  SCREAMING_SNAKE_CASE    :  int							=			-1_0_0
  SCREAMING_SNAKE_CASE    :  str							=			"pt"






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]					,lowercase__   :     List[str]			):
   import torch

   __lowercase							  = '''label''' if '''label''' in features[0].keys() else '''labels'''
   __lowercase							  = [feature[label_name] for feature in features] if label_name in features[0].keys() else None
   __lowercase							  = self.tokenizer.pad(
       lowercase__					,padding=self.padding					,max_length=self.max_length					,pad_to_multiple_of=self.pad_to_multiple_of					,return_tensors='''pt''' if labels is None else None					,)

   if labels is None:
    return batch

   __lowercase							  = torch.tensor(batch['''entity_ids''']			).shape[1]
   __lowercase							  = self.tokenizer.padding_side
   if padding_side == "right":
    __lowercase							  = [
        list(lowercase__			) + [self.label_pad_token_id] * (sequence_length - len(lowercase__			)) for label in labels
    ]
   else:
    __lowercase							  = [
        [self.label_pad_token_id] * (sequence_length - len(lowercase__			)) + list(lowercase__			) for label in labels
    ]

   __lowercase							  = [feature['''ner_tags'''] for feature in features]
   __lowercase							  = padding_tensor(lowercase__					,-1					,lowercase__					,lowercase__			)
   __lowercase							  = [feature['''original_entity_spans'''] for feature in features]
   __lowercase							  = padding_tensor(lowercase__					,(-1, -1)					,lowercase__					,lowercase__			)
   __lowercase							  = {k: torch.tensor(lowercase__					,dtype=torch.intaa			) for k, v in batch.items()}

   return batch

