



import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
lowerCamelCase__	=       {'''LayoutLMv2Config''', '''LayoutLMv3Config'''}






@is_pipeline_test
class       SCREAMING_SNAKE_CASE	(							unittest.TestCase    ):
							__lowerCamelCase		:		Tuple			=MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
							__lowerCamelCase		:		Dict			=TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

							if model_mapping is not None:
														__lowerCamelCase		:		List[Any]			={config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
							if tf_model_mapping is not None:
														__lowerCamelCase		:		Union[str, Any]			={
														    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
														}





							def   UpperCamelCase_    (      self						:						Any					, __lowercase						:						List[Any]					, __lowercase						:						Dict					, __lowercase						:						Tuple		):

											'''simple docstring'''
											__a        =						ZeroShotClassificationPipeline(
											    model=__lowercase					, tokenizer=__lowercase					, candidate_labels=["""polics""", """health"""]		)
											return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]





							def   UpperCamelCase_    (      self						:						Optional[Any]					, __lowercase						:						Dict					, __lowercase						:						Any		):

											'''simple docstring'''
											__a        =						classifier("""Who are you voting for in 2020?"""					, candidate_labels="""politics"""		)
											self.assertEqual(__lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		)], """scores""": [ANY(__lowercase		)]}		)

											# No kwarg
											__a        =						classifier("""Who are you voting for in 2020?"""					, ["""politics"""]		)
											self.assertEqual(__lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		)], """scores""": [ANY(__lowercase		)]}		)

											__a        =						classifier("""Who are you voting for in 2020?"""					, candidate_labels=["""politics"""]		)
											self.assertEqual(__lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		)], """scores""": [ANY(__lowercase		)]}		)

											__a        =						classifier("""Who are you voting for in 2020?"""					, candidate_labels="""politics, public health"""		)
											self.assertEqual(
											    __lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		), ANY(__lowercase		)], """scores""": [ANY(__lowercase		), ANY(__lowercase		)]}		)
											self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]		)		)					, 1.0		)

											__a        =						classifier("""Who are you voting for in 2020?"""					, candidate_labels=["""politics""", """public health"""]		)
											self.assertEqual(
											    __lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		), ANY(__lowercase		)], """scores""": [ANY(__lowercase		), ANY(__lowercase		)]}		)
											self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]		)		)					, 1.0		)

											__a        =						classifier(
											    """Who are you voting for in 2020?"""					, candidate_labels="""politics"""					, hypothesis_template="""This text is about {}"""		)
											self.assertEqual(__lowercase					, {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		)], """scores""": [ANY(__lowercase		)]}		)

											# https://github.com/huggingface/transformers/issues/13846
											__a        =						classifier(["""I am happy"""]					, ["""positive""", """negative"""]		)
											self.assertEqual(
											    __lowercase					, [
											        {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		), ANY(__lowercase		)], """scores""": [ANY(__lowercase		), ANY(__lowercase		)]}
											        for i in range(1		)
											    ]					, )
											__a        =						classifier(["""I am happy""", """I am sad"""]					, ["""positive""", """negative"""]		)
											self.assertEqual(
											    __lowercase					, [
											        {"""sequence""": ANY(__lowercase		), """labels""": [ANY(__lowercase		), ANY(__lowercase		)], """scores""": [ANY(__lowercase		), ANY(__lowercase		)]}
											        for i in range(2		)
											    ]					, )

											with self.assertRaises(__lowercase		):
															classifier(""""""					, candidate_labels="""politics"""		)

											with self.assertRaises(__lowercase		):
															classifier(__lowercase					, candidate_labels="""politics"""		)

											with self.assertRaises(__lowercase		):
															classifier("""Who are you voting for in 2020?"""					, candidate_labels=""""""		)

											with self.assertRaises(__lowercase		):
															classifier("""Who are you voting for in 2020?"""					, candidate_labels=__lowercase		)

											with self.assertRaises(__lowercase		):
															classifier(
															    """Who are you voting for in 2020?"""					, candidate_labels="""politics"""					, hypothesis_template="""Not formatting template"""					, )

											with self.assertRaises(__lowercase		):
															classifier(
															    """Who are you voting for in 2020?"""					, candidate_labels="""politics"""					, hypothesis_template=__lowercase					, )

											self.run_entailment_id(__lowercase		)





							def   UpperCamelCase_    (      self						:						Any					, __lowercase						:						str		):

											'''simple docstring'''
											__a        =						zero_shot_classifier.model.config
											__a        =						config.labelaid
											__a        =						zero_shot_classifier.entailment_id

											__a        =						{"""LABEL_0""": 0, """LABEL_1""": 1, """LABEL_2""": 2}
											self.assertEqual(zero_shot_classifier.entailment_id					, -1		)

											__a        =						{"""entailment""": 0, """neutral""": 1, """contradiction""": 2}
											self.assertEqual(zero_shot_classifier.entailment_id					, 0		)

											__a        =						{"""ENTAIL""": 0, """NON-ENTAIL""": 1}
											self.assertEqual(zero_shot_classifier.entailment_id					, 0		)

											__a        =						{"""ENTAIL""": 2, """NEUTRAL""": 1, """CONTR""": 0}
											self.assertEqual(zero_shot_classifier.entailment_id					, 2		)

											__a        =						original_labelaid
											self.assertEqual(__lowercase					, zero_shot_classifier.entailment_id		)





							@require_torch
							def   UpperCamelCase_    (      self						:						Union[str, Any]		):

											'''simple docstring'''
											__a        =						pipeline(
											    """zero-shot-classification"""					, model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					, framework="""pt"""					, )
											# There was a regression in 4.10 for this
											# Adding a test so we don't make the mistake again.
											# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
											zero_shot_classifier(
											    """Who are you voting for in 2020?""" * 100					, candidate_labels=["""politics""", """public health""", """science"""]		)





							@require_torch
							def   UpperCamelCase_    (      self						:						Optional[Any]		):

											'''simple docstring'''
											__a        =						pipeline(
											    """zero-shot-classification"""					, model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					, framework="""pt"""					, )
											__a        =						zero_shot_classifier(
											    """Who are you voting for in 2020?"""					, candidate_labels=["""politics""", """public health""", """science"""]		)

											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": """Who are you voting for in 2020?""",
											        """labels""": ["""science""", """public health""", """politics"""],
											        """scores""": [0.333, 0.333, 0.333],
											    }					, )





							@require_tf
							def   UpperCamelCase_    (      self						:						Optional[int]		):

											'''simple docstring'''
											__a        =						pipeline(
											    """zero-shot-classification"""					, model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					, framework="""tf"""					, )
											__a        =						zero_shot_classifier(
											    """Who are you voting for in 2020?"""					, candidate_labels=["""politics""", """public health""", """science"""]		)

											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": """Who are you voting for in 2020?""",
											        """labels""": ["""science""", """public health""", """politics"""],
											        """scores""": [0.333, 0.333, 0.333],
											    }					, )





							@slow
							@require_torch
							def   UpperCamelCase_    (      self						:						Dict		):

											'''simple docstring'''
											__a        =						pipeline("""zero-shot-classification"""					, model="""roberta-large-mnli"""					, framework="""pt"""		)
											__a        =						zero_shot_classifier(
											    """Who are you voting for in 2020?"""					, candidate_labels=["""politics""", """public health""", """science"""]		)

											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": """Who are you voting for in 2020?""",
											        """labels""": ["""politics""", """public health""", """science"""],
											        """scores""": [0.976, 0.015, 0.009],
											    }					, )
											__a        =						zero_shot_classifier(
											    """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
											    """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
											    """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
											    """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
											    """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
											    """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
											    """ English-to-German translation task, improving over the existing best results, including ensembles by"""
											    """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
											    """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
											    """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
											    """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
											    """ large and limited training data."""					, candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]					, multi_label=__lowercase					, )
											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": (
											            """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
											            """ networks in an encoder-decoder configuration. The best performing models also connect the"""
											            """ encoder and decoder through an attention mechanism. We propose a new simple network"""
											            """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
											            """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
											            """ superior in quality while being more parallelizable and requiring significantly less time to"""
											            """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
											            """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
											            """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
											            """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
											            """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
											            """ other tasks by applying it successfully to English constituency parsing both with large and"""
											            """ limited training data."""
											        ),
											        """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
											        """scores""": [0.817, 0.713, 0.018, 0.018],
											    }					, )







							@slow
							@require_tf
							def   UpperCamelCase_    (      self						:						str		):

											'''simple docstring'''
											__a        =						pipeline("""zero-shot-classification"""					, model="""roberta-large-mnli"""					, framework="""tf"""		)
											__a        =						zero_shot_classifier(
											    """Who are you voting for in 2020?"""					, candidate_labels=["""politics""", """public health""", """science"""]		)

											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": """Who are you voting for in 2020?""",
											        """labels""": ["""politics""", """public health""", """science"""],
											        """scores""": [0.976, 0.015, 0.009],
											    }					, )
											__a        =						zero_shot_classifier(
											    """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
											    """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
											    """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
											    """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
											    """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
											    """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
											    """ English-to-German translation task, improving over the existing best results, including ensembles by"""
											    """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
											    """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
											    """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
											    """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
											    """ large and limited training data."""					, candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]					, multi_label=__lowercase					, )
											self.assertEqual(
											    nested_simplify(__lowercase		)					, {
											        """sequence""": (
											            """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
											            """ networks in an encoder-decoder configuration. The best performing models also connect the"""
											            """ encoder and decoder through an attention mechanism. We propose a new simple network"""
											            """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
											            """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
											            """ superior in quality while being more parallelizable and requiring significantly less time to"""
											            """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
											            """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
											            """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
											            """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
											            """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
											            """ other tasks by applying it successfully to English constituency parsing both with large and"""
											            """ limited training data."""
											        ),
											        """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
											        """scores""": [0.817, 0.713, 0.018, 0.018],
											    }					, )





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


lowerCamelCase							:Tuple				    =					logging.get_logger(__name__)







class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def     _a	(self       ,      lowercase							):
				if isinstance(lowercase       ,      lowercase							):
							A_	:						Tuple	=     [label.strip() for label in labels.split(""","""							) if label.strip()]
				return labels


	def __call__(self       ,      lowercase       ,      lowercase       ,      lowercase							):
				if len(lowercase							) == 0 or len(lowercase							) == 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(lowercase							)							)

				if isinstance(lowercase       ,      lowercase							):
							A_	:						Optional[Any]	=     [sequences]

				A_	:						List[Any]	=     []
				for sequence in sequences:
							sequence_pairs.extend([[sequence, hypothesis_template.format(lowercase							)] for label in labels]							)

				return sequence_pairs, sequences







@add_end_docstrings(__UpperCAmelCase )
class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def __init__(self       ,      lowercase=ZeroShotClassificationArgumentHandler()       ,      *lowercase       ,      **lowercase							):
				A_	:						str	=     args_parser
				super().__init__(*lowercase       ,      **lowercase							)
				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     _a	(self							):
				for label, ind in self.model.config.labelaid.items():
							if label.lower().startswith("""entail"""							):
										return ind
				return -1


	def     _a	(self       ,      lowercase       ,      lowercase=True       ,      lowercase=True       ,      lowercase=TruncationStrategy.ONLY_FIRST       ,      **lowercase							):
				A_	:						Optional[int]	=     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_	:						Any	=     self.tokenizer.eos_token
				try:
							A_	:						Optional[Any]	=     self.tokenizer(
							    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      )
				except Exception as e:
							if "too short" in str(lowercase							):
										# 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_	:						str	=     self.tokenizer(
										    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=TruncationStrategy.DO_NOT_TRUNCATE       ,      )
							else:
										raise e

				return inputs


	def     _a	(self       ,      **lowercase							):
				if kwargs.get("""multi_class"""       ,      lowercase							) is not None:
							A_	:						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."""							)
				A_	:						Tuple	=     {}
				if "candidate_labels" in kwargs:
							A_	:						Optional[int]	=     self._args_parser._parse_labels(kwargs["""candidate_labels"""]							)
				if "hypothesis_template" in kwargs:
							A_	:						List[str]	=     kwargs["""hypothesis_template"""]

				A_	:						Union[str, Any]	=     {}
				if "multi_label" in kwargs:
							A_	:						Optional[int]	=     kwargs["""multi_label"""]
				return preprocess_params, {}, postprocess_params


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

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


	def     _a	(self       ,      lowercase       ,      lowercase=None       ,      lowercase="This example is {}."							):
				A_,    A_	:						Any	=     self._args_parser(lowercase       ,      lowercase       ,      lowercase							)

				for i, (candidate_label, sequence_pair) in enumerate(zip(lowercase       ,      lowercase							)							):
							A_	:						List[Any]	=     self._parse_and_tokenize([sequence_pair]							)

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


	def     _a	(self       ,      lowercase							):
				A_	:						Dict	=     inputs["""candidate_label"""]
				A_	:						Any	=     inputs["""sequence"""]
				A_	:						List[Any]	=     {k: inputs[k] for k in self.tokenizer.model_input_names}
				A_	:						Optional[int]	=     self.model(**lowercase							)

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


	def     _a	(self       ,      lowercase       ,      lowercase=False							):
				A_	:						Tuple	=     [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(lowercase							)
				A_	:						int	=     N // n
				A_	:						int	=     logits.reshape((num_sequences, n, -1)							)

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

				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(),
				}