



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 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			def 		snake_case__			(				self	:       Union[str, Any]			,     lowercase	:       int   )						->					str:
									"""simple docstring"""







									if isinstance(lowercase			,     lowercase   ):
															__lowercase					=				[label.strip() for label in labels.split(""","""   ) if label.strip()]
									return labels







			def __call__(				self	:       List[Any]			,     lowercase	:       str			,     lowercase	:       Any			,     lowercase	:       Optional[int]   )						->					Union[str, Any]:
									"""simple docstring"""







									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   ):
															__lowercase					=				[sequences]

									__lowercase					=				[]
									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			):






			"""simple docstring"""





			def __init__(				self	:       List[Any]			,     lowercase	:       Tuple=ZeroShotClassificationArgumentHandler()			,     *lowercase	:       List[str]			,     **lowercase	:       Any   )						->					Optional[int]:
									"""simple docstring"""







									__lowercase					=				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 		snake_case__			(				self	:       List[str]   )						->					List[Any]:
									"""simple docstring"""







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





			def 		snake_case__			(				self	:       int			,     lowercase	:       Optional[Any]			,     lowercase	:       Any=True			,     lowercase	:       Tuple=True			,     lowercase	:       Tuple=TruncationStrategy.ONLY_FIRST			,     **lowercase	:       str   )						->					str:
									"""simple docstring"""







									__lowercase					=				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`"""   )
															__lowercase					=				self.tokenizer.eos_token
									try:
															__lowercase					=				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.

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

									return inputs





			def 		snake_case__			(				self	:       Tuple			,     **lowercase	:       Dict   )						->					Dict:
									"""simple docstring"""







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

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





			def __call__(				self	:       Dict			,     lowercase	:       Union[str, List[str]]			,     *lowercase	:       List[Any]			,     **lowercase	:       Dict			,     )						->					Union[str, Any]:
									"""simple docstring"""







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

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





			def 		snake_case__			(				self	:       List[str]			,     lowercase	:       int			,     lowercase	:       Tuple=None			,     lowercase	:       Tuple="This example is {}."   )						->					Union[str, Any]:
									"""simple docstring"""







									__lowercase	,			__lowercase					=				self._args_parser(lowercase			,     lowercase			,     lowercase   )

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

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





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       Optional[int]   )						->					Optional[Any]:
									"""simple docstring"""







									__lowercase					=				inputs["""candidate_label"""]
									__lowercase					=				inputs["""sequence"""]
									__lowercase					=				{k: inputs[k] for k in self.tokenizer.model_input_names}
									__lowercase					=				self.model(**lowercase   )

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







			def 		snake_case__			(				self	:       List[str]			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[str]=False   )						->					Tuple:
									"""simple docstring"""







									__lowercase					=				[outputs["""candidate_label"""] for outputs in model_outputs]
									__lowercase					=				[outputs["""sequence"""] for outputs in model_outputs]
									__lowercase					=				np.concatenate([output["""logits"""].numpy() for output in model_outputs]   )
									__lowercase					=				logits.shape[0]
									__lowercase					=				len(lowercase   )
									__lowercase					=				N // n
									__lowercase					=				logits.reshape((num_sequences, n, -1)   )

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

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










import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class 			_lowerCAmelCase   ( _UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  Tuple      =			LxmertTokenizer
			lowercase__      :  List[str]      =			LxmertTokenizerFast
			lowercase__      :  Optional[Any]      =			True
			lowercase__      :  List[Any]      =			True





			def 		snake_case__			(				self	:       Tuple   )						->					Tuple:
									"""simple docstring"""







									super().setUp()

									__lowercase					=				[
									    """[UNK]""",
									    """[CLS]""",
									    """[SEP]""",
									    """want""",
									    """##want""",
									    """##ed""",
									    """wa""",
									    """un""",
									    """runn""",
									    """##ing""",
									    """,""",
									    """low""",
									    """lowest""",
									]
									__lowercase					=				os.path.join(self.tmpdirname			,     VOCAB_FILES_NAMES["""vocab_file"""]   )
									with open(self.vocab_file			,     """w"""			,     encoding="""utf-8"""   ) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]   )   )





			def 		snake_case__			(				self	:       Optional[int]			,     lowercase	:       int   )						->					List[Any]:
									"""simple docstring"""







									__lowercase					=				"""UNwant\u00E9d,running"""
									__lowercase					=				"""unwanted, running"""
									return input_text, output_text





			def 		snake_case__			(				self	:       str   )						->					Any:
									"""simple docstring"""







									__lowercase					=				self.tokenizer_class(self.vocab_file   )

									__lowercase					=				tokenizer.tokenize("""UNwant\u00E9d,running"""   )
									self.assertListEqual(lowercase			,     ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]   )
									self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase   )			,     [7, 4, 5, 10, 8, 9]   )







			def 		snake_case__			(				self	:       Union[str, Any]   )						->					int:
									"""simple docstring"""







									if not self.test_rust_tokenizer:
															return

									__lowercase					=				self.get_tokenizer()
									__lowercase					=				self.get_rust_tokenizer()

									__lowercase					=				"""I was born in 92000, and this is falsé."""

									__lowercase					=				tokenizer.tokenize(lowercase   )
									__lowercase					=				rust_tokenizer.tokenize(lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )

									__lowercase					=				tokenizer.encode(lowercase			,     add_special_tokens=lowercase   )
									__lowercase					=				rust_tokenizer.encode(lowercase			,     add_special_tokens=lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )

									__lowercase					=				self.get_rust_tokenizer()
									__lowercase					=				tokenizer.encode(lowercase   )
									__lowercase					=				rust_tokenizer.encode(lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )





