






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


__magic_name__:     Dict     		=	logging.get_logger(__name__)





class        snake_case__					(							__snake_case      ):
	def 			__magic_name__				(				self			,   lowerCAmelCase__ )							->      int:

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







	def __call__(				self			,   lowerCAmelCase__			,   lowerCAmelCase__			,   lowerCAmelCase__ )							->      Any:

							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_ ):
													__magic_name__			:   Any   						=					[sequences]

							__magic_name__			:   List[str]   						=					[]
							for sequence in sequences:
													sequence_pairs.extend([[sequence, hypothesis_template.format(a_ )] for label in labels] )

							return sequence_pairs, sequences





@add_end_docstrings(__snake_case      )
class        snake_case__					(							__snake_case      ):
	def __init__(				self			,   lowerCAmelCase__=ZeroShotClassificationArgumentHandler()			,   *lowerCAmelCase__			,   **lowerCAmelCase__ )							->      Dict:
							__magic_name__			:   str   						=					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 			__magic_name__				(				self )							->      Any:

							for label, ind in self.model.config.labelaid.items():
													if label.lower().startswith("""entail""" ):
																			return ind
							return -1
	def 			__magic_name__				(				self			,   lowerCAmelCase__			,   lowerCAmelCase__=True			,   lowerCAmelCase__=True			,   lowerCAmelCase__=TruncationStrategy.ONLY_FIRST			,   **lowerCAmelCase__ )							->      Union[str, Any]:
							__magic_name__			:   Optional[Any]   						=					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`""" )
													__magic_name__			:   str   						=					self.tokenizer.eos_token
							try:
													__magic_name__			:   Union[str, Any]   						=					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.

																			__magic_name__			:   Tuple   						=					self.tokenizer(
																			    a_			,   add_special_tokens=a_			,   return_tensors=a_			,   padding=a_			,   truncation=TruncationStrategy.DO_NOT_TRUNCATE			,   )
													else:
																			raise e

							return inputs
	def 			__magic_name__				(				self			,   **lowerCAmelCase__ )							->      Union[str, Any]:

							if kwargs.get("""multi_class"""			,   a_ ) is not None:
													__magic_name__			:   List[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.""" )
							__magic_name__			:   List[str]   						=					{}
							if "candidate_labels" in kwargs:
													__magic_name__			:   Dict   						=					self._args_parser._parse_labels(kwargs["""candidate_labels"""] )
							if "hypothesis_template" in kwargs:
													__magic_name__			:   int   						=					kwargs['''hypothesis_template''']

							__magic_name__			:   str   						=					{}
							if "multi_label" in kwargs:
													__magic_name__			:   Optional[Any]   						=					kwargs['''multi_label''']
							return preprocess_params, {}, postprocess_params
	def __call__(				self			,   lowerCAmelCase__			,   *lowerCAmelCase__			,   **lowerCAmelCase__			,   )							->      Union[str, Any]:

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

							return super().__call__(a_			,   **a_ )
	def 			__magic_name__				(				self			,   lowerCAmelCase__			,   lowerCAmelCase__=None			,   lowerCAmelCase__="This example is {}." )							->      Union[str, Any]:
							__magic_name__			:   int   						=					self._args_parser(a_			,   a_			,   a_ )

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

													yield {
													    "candidate_label": candidate_label,
													    "sequence": sequences[0],
													    "is_last": i == len(a_ ) - 1,
													    **model_input,
													}
	def 			__magic_name__				(				self			,   lowerCAmelCase__ )							->      Optional[Any]:
							__magic_name__			:   List[Any]   						=					inputs['''candidate_label''']
							__magic_name__			:   List[Any]   						=					inputs['''sequence''']
							__magic_name__			:   str   						=					{k: inputs[k] for k in self.tokenizer.model_input_names}
							__magic_name__			:   Union[str, Any]   						=					self.model(**a_ )

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







	def 			__magic_name__				(				self			,   lowerCAmelCase__			,   lowerCAmelCase__=False )							->      List[str]:
							__magic_name__			:   Any   						=					[outputs['''candidate_label'''] for outputs in model_outputs]
							__magic_name__			:   List[str]   						=					[outputs['''sequence'''] for outputs in model_outputs]
							__magic_name__			:   List[str]   						=					np.concatenate([output["""logits"""].numpy() for output in model_outputs] )
							__magic_name__			:   Union[str, Any]   						=					logits.shape[0]
							__magic_name__			:   Tuple   						=					len(a_ )
							__magic_name__			:   int   						=					N // n
							__magic_name__			:   List[str]   						=					logits.reshape((num_sequences, n, -1) )

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

							__magic_name__			:   Union[str, Any]   						=					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"""
from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
			from transformers import (
			    TFAutoModel,
			    TFAutoModelForCausalLM,
			    TFAutoModelForMaskedLM,
			    TFAutoModelForPreTraining,
			    TFAutoModelForQuestionAnswering,
			    TFAutoModelForSeqaSeqLM,
			    TFAutoModelForSequenceClassification,
			    TFAutoModelForTableQuestionAnswering,
			    TFAutoModelForTokenClassification,
			    TFAutoModelWithLMHead,
			    TFBertForMaskedLM,
			    TFBertForPreTraining,
			    TFBertForQuestionAnswering,
			    TFBertForSequenceClassification,
			    TFBertModel,
			    TFFunnelBaseModel,
			    TFFunnelModel,
			    TFGPTaLMHeadModel,
			    TFRobertaForMaskedLM,
			    TFTaForConditionalGeneration,
			    TFTapasForQuestionAnswering,
			)
			from transformers.models.auto.modeling_tf_auto import (
			    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
			    TF_MODEL_FOR_MASKED_LM_MAPPING,
			    TF_MODEL_FOR_PRETRAINING_MAPPING,
			    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
			    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
			    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
			    TF_MODEL_MAPPING,
			)
			from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST



class 			_UpperCAmelCase (							__snake_case				):






		'''simple docstring'''

		lowerCamelCase__				='new-model'


if is_tf_available():



			class 			_UpperCAmelCase (							__snake_case				):






					'''simple docstring'''

					lowerCamelCase__				=NewModelConfig



@require_tf
class 			_UpperCAmelCase (							unittest.TestCase				):






		'''simple docstring'''

		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				'''bert-base-cased'''
			__snake_case	:						Dict						=				AutoConfig.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						int						=				TFAutoModel.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				'''bert-base-cased'''
			__snake_case	:						Optional[Any]						=				AutoConfig.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						List[Any]						=				TFAutoModelForPreTraining.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						int						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Dict						=				TFAutoModelForCausalLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						int						=				TFAutoModelForCausalLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						Tuple						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Tuple						=				TFAutoModelWithLMHead.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						List[str]						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Optional[Any]						=				TFAutoModelForMaskedLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						int						=				TFAutoModelForMaskedLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						int						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						List[Any]						=				TFAutoModelForSeqaSeqLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						Union[str, Any]						=				TFAutoModelForSeqaSeqLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in ["bert-base-uncased"]:
				__snake_case	:						Any						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Dict						=				TFAutoModelForSequenceClassification.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in ["bert-base-uncased"]:
				__snake_case	:						Optional[int]						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Optional[Any]						=				TFAutoModelForQuestionAnswering.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		@require_tensorflow_probability
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
				__snake_case	:						Dict						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						List[str]						=				TFAutoModelForTableQuestionAnswering.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						Dict						=				TFAutoModelForTableQuestionAnswering.from_pretrained(
				    a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Any						=				TFAutoModelWithLMHead.from_pretrained(a_					)
			self.assertIsInstance(a_				,       a_					)
			self.assertEqual(model.num_parameters()				,       1_44_10					)
			self.assertEqual(model.num_parameters(only_trainable=a_					)				,       1_44_10					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Union[str, Any]						=				TFAutoModelWithLMHead.from_pretrained(a_					)
			self.assertIsInstance(a_				,       a_					)
			self.assertEqual(model.num_parameters()				,       1_44_10					)
			self.assertEqual(model.num_parameters(only_trainable=a_					)				,       1_44_10					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						Optional[Any]						=				copy.deepcopy(model.config					)
			__snake_case	:						int						=				['''FunnelBaseModel''']
			__snake_case	:						Any						=				TFAutoModel.from_config(a_					)
			self.assertIsInstance(a_				,       a_					)

			with tempfile.TemporaryDirectory() as tmp_dir:
				model.save_pretrained(a_					)
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained(a_					)
				self.assertIsInstance(a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			try:
				AutoConfig.register('''new-model'''				,       a_					)

				__snake_case	:						List[str]						=				[
				    TFAutoModel,
				    TFAutoModelForCausalLM,
				    TFAutoModelForMaskedLM,
				    TFAutoModelForPreTraining,
				    TFAutoModelForQuestionAnswering,
				    TFAutoModelForSequenceClassification,
				    TFAutoModelForTokenClassification,
				]

				for auto_class in auto_classes:
					with self.subTest(auto_class.__name__					):
						# Wrong config class will raise an error
						with self.assertRaises(a_					):
							auto_class.register(a_				,       a_					)
						auto_class.register(a_				,       a_					)
						# Trying to register something existing in the Transformers library will raise an error
						with self.assertRaises(a_					):
							auto_class.register(a_				,       a_					)

						# Now that the config is registered, it can be used as any other config with the auto-API
						__snake_case	:						Union[str, Any]						=				BertModelTester(self					).get_config()
						__snake_case	:						str						=				NewModelConfig(**tiny_config.to_dict()					)
						__snake_case	:						Optional[int]						=				auto_class.from_config(a_					)
						self.assertIsInstance(a_				,       a_					)

						with tempfile.TemporaryDirectory() as tmp_dir:
							model.save_pretrained(a_					)
							__snake_case	:						Optional[int]						=				auto_class.from_pretrained(a_					)
							self.assertIsInstance(a_				,       a_					)

			finally:
				if "new-model" in CONFIG_MAPPING._extra_content:
					del CONFIG_MAPPING._extra_content["new-model"]
				for mapping in (
				    TF_MODEL_MAPPING,
				    TF_MODEL_FOR_PRETRAINING_MAPPING,
				    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
				    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
				    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
				    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
				    TF_MODEL_FOR_MASKED_LM_MAPPING,
				):
					if NewModelConfig in mapping._extra_content:
						del mapping._extra_content[NewModelConfig]



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       '''bert-base is not a local folder and is not a valid model identifier'''					):
				__snake_case	:						Any						=				TFAutoModel.from_pretrained('''bert-base'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained(a_				,       revision='''aaaaaa'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       '''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'''				,       ):
				__snake_case	:						Any						=				TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(a_				,       '''Use `from_pt=True` to load this model'''					):
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''					)
			with RequestCounter() as counter:
				__snake_case	:						List[Any]						=				TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''					)
				self.assertEqual(counter.get_request_count				,       0					)
				self.assertEqual(counter.head_request_count				,       1					)
				self.assertEqual(counter.other_request_count				,       0					)

			# With a sharded checkpoint
			__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''					)
			with RequestCounter() as counter:
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''					)
				self.assertEqual(counter.get_request_count				,       0					)
				self.assertEqual(counter.head_request_count				,       1					)
				self.assertEqual(counter.other_request_count				,       0					)





