






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


a__  	=  logging.get_logger(__name__)


class  _lowerCAmelCase      (		lowercase_   ):



				"""simple docstring"""



				def        __magic_name__				(   self     :     Tuple      ,		UpperCamelCase__     :     Union[str, Any]):






											'''simple docstring'''







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



				def __call__(   self     :     Optional[Any]      ,		UpperCamelCase__     :     List[str]      ,		UpperCamelCase__     :     Tuple      ,		UpperCamelCase__     :     List[str]):






											'''simple docstring'''







											if len(UpperCamelCase__) == 0 or len(UpperCamelCase__) == 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(UpperCamelCase__))

											if isinstance(UpperCamelCase__      ,		UpperCamelCase__):
																		snake_case__							=							[sequences]

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

											return sequence_pairs, sequences


@add_end_docstrings(lowercase_   )
class  _lowerCAmelCase      (		lowercase_   ):



				"""simple docstring"""



				def __init__(   self     :     str      ,		UpperCamelCase__     :     List[Any]=ZeroShotClassificationArgumentHandler()      ,		*UpperCamelCase__     :     str      ,		**UpperCamelCase__     :     Optional[Any]):






											'''simple docstring'''







											snake_case__							=							args_parser
											super().__init__(*UpperCamelCase__      ,		**UpperCamelCase__)
											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     :     Dict):






											'''simple docstring'''







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



				def        __magic_name__				(   self     :     List[Any]      ,		UpperCamelCase__     :     Optional[Any]      ,		UpperCamelCase__     :     str=True      ,		UpperCamelCase__     :     Optional[Any]=True      ,		UpperCamelCase__     :     Union[str, Any]=TruncationStrategy.ONLY_FIRST      ,		**UpperCamelCase__     :     Any):






											'''simple docstring'''







											snake_case__							=							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`""")
																		snake_case__							=							self.tokenizer.eos_token
											try:
																		snake_case__							=							self.tokenizer(
																		    UpperCamelCase__      ,		add_special_tokens=UpperCamelCase__      ,		return_tensors=UpperCamelCase__      ,		padding=UpperCamelCase__      ,		truncation=UpperCamelCase__      ,		)
											except Exception as e:
																		if "too short" in str(UpperCamelCase__):
																									# 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.

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

											return inputs



				def        __magic_name__				(   self     :     Dict      ,		**UpperCamelCase__     :     int):






											'''simple docstring'''







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

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



				def __call__(   self     :     Any      ,		UpperCamelCase__     :     str      ,		*UpperCamelCase__     :     List[Any]      ,		**UpperCamelCase__     :     List[Any]      ,		):






											'''simple docstring'''







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

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



				def        __magic_name__				(   self     :     Tuple      ,		UpperCamelCase__     :     str      ,		UpperCamelCase__     :     Optional[Any]=None      ,		UpperCamelCase__     :     int="This example is {}."):






											'''simple docstring'''







											snake_case__							=							self._args_parser(UpperCamelCase__      ,		UpperCamelCase__      ,		UpperCamelCase__)

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

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



				def        __magic_name__				(   self     :     Dict      ,		UpperCamelCase__     :     Any):






											'''simple docstring'''







											snake_case__							=							inputs['candidate_label']
											snake_case__							=							inputs['sequence']
											snake_case__							=							{k: inputs[k] for k in self.tokenizer.model_input_names}
											snake_case__							=							self.model(**UpperCamelCase__)

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



				def        __magic_name__				(   self     :     Union[str, Any]      ,		UpperCamelCase__     :     str      ,		UpperCamelCase__     :     Union[str, Any]=False):






											'''simple docstring'''







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

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

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






import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


lowercase				=pytest.mark.integration





@pytest.mark.parametrize('path'			,       ['paws', 'csv']				)
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]				):







   '''simple docstring'''




   inspect_dataset(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[str]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.filterwarnings('ignore:inspect_metric is deprecated:FutureWarning'				)
@pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning'				)
@pytest.mark.parametrize('path'			,       ['accuracy']				)
def lowerCamelCase__     (		__lowerCamelCase			:				Dict			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   inspect_metric(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[Any]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, config_name, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Any			,       __lowerCamelCase			:				Optional[int]			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)
   assert info.config_name == config_name
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, expected'			,       [
        ('squad', 'plain_text'),
        ('acronym_identification', 'default'),
        ('lhoestq/squad', 'plain_text'),
        ('lhoestq/test', 'default'),
        ('lhoestq/demo1', 'lhoestq--demo1'),
        ('dalle-mini/wit', 'dalle-mini--wit'),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Dict				       =get_dataset_config_names(__lowerCamelCase				)
   assert expected in config_names





@pytest.mark.parametrize(
    'path, expected_configs, expected_splits_in_first_config'			,       [
        ('squad', ['plain_text'], ['train', 'validation']),
        ('dalle-mini/wit', ['dalle-mini--wit'], ['train']),
        ('paws', ['labeled_final', 'labeled_swap', 'unlabeled_final'], ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				List[Any]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =get_dataset_infos(__lowerCamelCase				)
   assert list(infos.keys()				) == expected_configs
   _UpperCAmelCase  :  Optional[int]				       =expected_configs[0]
   assert expected_config in infos
   _UpperCAmelCase  :  Optional[int]				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits_in_first_config





@pytest.mark.parametrize(
    'path, expected_config, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				int				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_infos(__lowerCamelCase				)
   assert expected_config in infos
   _UpperCAmelCase  :  Dict				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Union[str, Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				Any				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_split_names(__lowerCamelCase			,       config_name=__lowerCamelCase				)

