


'''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 	a__       (							a__						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							Tuple:
														if isinstance(lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			[label.strip() for label in labels.split(''','''     ) if label.strip()]
														return labels







							def __call__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							List[str]:
														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_     ):
																					lowerCAmelCase__      =			[sequences]

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

														return sequence_pairs, sequences




@add_end_docstrings(a__						)
class 	a__       (							a__						):





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_=ZeroShotClassificationArgumentHandler()      ,					*lowerCamelCase_      ,					**lowerCamelCase_     ) ->							int:
														lowerCAmelCase__      =			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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														for label, ind in self.model.config.labelaid.items():
																					if label.lower().startswith('''entail'''     ):
																												return ind
														return -1







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_=True      ,					lowerCamelCase_=True      ,					lowerCamelCase_=TruncationStrategy.ONLY_FIRST      ,					**lowerCamelCase_     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			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`'''     )
																					lowerCAmelCase__      =			self.tokenizer.eos_token
														try:
																					lowerCAmelCase__      =			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.

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

														return inputs







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					**lowerCamelCase_     ) ->							int:
														if kwargs.get('''multi_class'''      ,					lowerCamelCase_     ) is not None:
																					lowerCAmelCase__      =			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.'''     )
														lowerCAmelCase__      =			{}
														if "candidate_labels" in kwargs:
																					lowerCAmelCase__      =			self._args_parser._parse_labels(kwargs['''candidate_labels''']     )
														if "hypothesis_template" in kwargs:
																					lowerCAmelCase__      =			kwargs['''hypothesis_template''']

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







							def __call__(	self      ,					lowerCamelCase_      ,					*lowerCamelCase_      ,					**lowerCamelCase_      ,					) ->							Optional[Any]:
														if len(lowerCamelCase_     ) == 0:
																					pass
														elif len(lowerCamelCase_     ) == 1 and "candidate_labels" not in kwargs:
																					lowerCAmelCase__      =			args[0]
														else:
																					raise ValueError(F"""Unable to understand extra arguments {args}"""     )

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_=None      ,					lowerCamelCase_="This example is {}."     ) ->							Any:
														lowerCAmelCase__     ,      lowerCAmelCase__      =			self._args_parser(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )

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

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							Tuple:
														lowerCAmelCase__      =			inputs['''candidate_label''']
														lowerCAmelCase__      =			inputs['''sequence''']
														lowerCAmelCase__      =			{k: inputs[k] for k in self.tokenizer.model_input_names}
														lowerCAmelCase__      =			self.model(**lowerCamelCase_     )

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







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

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

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






def       _snake_case     (  A )		->					bool:
							if not isinstance(A				,					A ):
														raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' )
							if len(A ) == 0:
														raise ValueError('''Input list must be a non empty list''' )
							if len(A ) == 1:
														return True
							lowerCAmelCase__      =			series[1] - series[0]
							for index in range(len(A ) - 1 ):
														if series[index + 1] - series[index] != common_diff:
																					return False
							return True


def       _snake_case     (  A )		->					float:
							if not isinstance(A				,					A ):
														raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' )
							if len(A ) == 0:
														raise ValueError('''Input list must be a non empty list''' )
							lowerCAmelCase__      =			0
							for val in series:
														answer += val
							return answer / len(A )


if __name__ == "__main__":
		import doctest

		doctest.testmod()