
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     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			def  lowerCAmelCase__		(    self	:    List[str]  ,		snake_case_	:    Optional[int] ):
							if isinstance(snake_case_  ,		snake_case_ ):
											UpperCamelCase_:							int		=  [label.strip() for label in labels.split(""",""" ) if label.strip()]
							return labels




			def __call__(    self	:    Optional[int]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Tuple ):
							if len(snake_case_ ) == 0 or len(snake_case_ ) == 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(snake_case_ ) )

							if isinstance(snake_case_  ,		snake_case_ ):
											UpperCamelCase_:							Any		=  [sequences]

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

							return sequence_pairs, sequences




@add_end_docstrings(_A					)
class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

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

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

							return inputs

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

							UpperCamelCase_:							Any		=  {}
							if "multi_label" in kwargs:
											UpperCamelCase_:							str		=  kwargs["""multi_label"""]
							return preprocess_params, {}, postprocess_params

			def __call__(    self	:    Optional[int]  ,		snake_case_	:    Union[str, List[str]]  ,		*snake_case_	:    Optional[Any]  ,		**snake_case_	:    int  ,		):
							if len(snake_case_ ) == 0:
											pass
							elif len(snake_case_ ) == 1 and "candidate_labels" not in kwargs:
											UpperCamelCase_:							Optional[int]		=  args[0]
							else:
											raise ValueError(f'''Unable to understand extra arguments {args}''' )

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

			def  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Tuple=None  ,		snake_case_	:    Dict="This example is {}." ):
							UpperCamelCase_,				UpperCamelCase_:							List[str]		=  self._args_parser(snake_case_  ,		snake_case_  ,		snake_case_ )

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

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

			def  lowerCAmelCase__		(    self	:    int  ,		snake_case_	:    int ):
							UpperCamelCase_:							int		=  inputs["""candidate_label"""]
							UpperCamelCase_:							Union[str, Any]		=  inputs["""sequence"""]
							UpperCamelCase_:							Optional[Any]		=  {k: inputs[k] for k in self.tokenizer.model_input_names}
							UpperCamelCase_:							Optional[Any]		=  self.model(**snake_case_ )

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




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

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

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








import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class     _UpperCamelCase			(   _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Tuple    =	CTRLTokenizer
			__UpperCamelCase :  int    =	False
			__UpperCamelCase :  List[str]    =	False

			def  lowerCAmelCase__		(    self	:    List[str] ):
							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							UpperCamelCase_:							int		=  ["""adapt""", """re@@""", """a@@""", """apt""", """c@@""", """t""", """<unk>"""]
							UpperCamelCase_:							int		=  dict(zip(snake_case_  ,		range(len(snake_case_ ) ) ) )
							UpperCamelCase_:							Union[str, Any]		=  ["""#version: 0.2""", """a p""", """ap t</w>""", """r e""", """a d""", """ad apt</w>""", """"""]
							UpperCamelCase_:							Tuple		=  {"""unk_token""": """<unk>"""}

							UpperCamelCase_:							Optional[Any]		=  os.path.join(self.tmpdirname  ,		VOCAB_FILES_NAMES["""vocab_file"""] )
							UpperCamelCase_:							Dict		=  os.path.join(self.tmpdirname  ,		VOCAB_FILES_NAMES["""merges_file"""] )
							with open(self.vocab_file  ,		"""w"""  ,		encoding="""utf-8""" ) as fp:
											fp.write(json.dumps(snake_case_ ) + """\n""" )
							with open(self.merges_file  ,		"""w"""  ,		encoding="""utf-8""" ) as fp:
											fp.write("""\n""".join(snake_case_ ) )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		**snake_case_	:    int ):
							kwargs.update(self.special_tokens_map )
							return CTRLTokenizer.from_pretrained(self.tmpdirname  ,		**snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							Dict		=  """adapt react readapt apt"""
							UpperCamelCase_:							List[str]		=  """adapt react readapt apt"""
							return input_text, output_text




			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							str		=  CTRLTokenizer(self.vocab_file  ,		self.merges_file  ,		**self.special_tokens_map )
							UpperCamelCase_:							List[Any]		=  """adapt react readapt apt"""
							UpperCamelCase_:							Optional[int]		=  """adapt re@@ a@@ c@@ t re@@ adapt apt""".split()
							UpperCamelCase_:							int		=  tokenizer.tokenize(snake_case_ )
							self.assertListEqual(snake_case_  ,		snake_case_ )

							UpperCamelCase_:							List[Any]		=  tokens + [tokenizer.unk_token]

							UpperCamelCase_:							Union[str, Any]		=  [0, 1, 2, 4, 5, 1, 0, 3, 6]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_ )  ,		snake_case_ )






