
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       a		(   __UpperCAmelCase					):







							def       UpperCAmelCase__					(						self  :  Any      ,				snake_case__  :  Optional[Any] ):
								"""simple docstring"""







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







							def __call__(						self  :  Optional[Any]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  str      ,				snake_case__  :  List[Any] ):
								"""simple docstring"""







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

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

								return sequence_pairs, sequences

@add_end_docstrings(__UpperCAmelCase					)
class       a		(   __UpperCAmelCase					):







							def __init__(						self  :  Optional[Any]      ,				snake_case__  :  Any=ZeroShotClassificationArgumentHandler()      ,				*snake_case__  :  Tuple      ,				**snake_case__  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase         =							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       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Dict      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  Any=True      ,				snake_case__  :  Optional[int]=True      ,				snake_case__  :  Optional[Any]=TruncationStrategy.ONLY_FIRST      ,				**snake_case__  :  int ):
								"""simple docstring"""







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

										__lowerCAmelCase         =							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       UpperCAmelCase__					(						self  :  str      ,				**snake_case__  :  Dict ):
								"""simple docstring"""







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

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







							def __call__(						self  :  str      ,				snake_case__  :  Union[str, List[str]]      ,				*snake_case__  :  Optional[Any]      ,				**snake_case__  :  Optional[Any]      ,				):
								"""simple docstring"""







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

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







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







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

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

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







							def       UpperCAmelCase__					(						self  :  Tuple      ,				snake_case__  :  Dict ):
								"""simple docstring"""







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

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







							def       UpperCAmelCase__					(						self  :  Union[str, Any]      ,				snake_case__  :  str      ,				snake_case__  :  Tuple=False ):
								"""simple docstring"""







								__lowerCAmelCase         =							[outputs["candidate_label"] for outputs in model_outputs]
								__lowerCAmelCase         =							[outputs["sequence"] for outputs in model_outputs]
								__lowerCAmelCase         =							np.concatenate([output["logits"].numpy() for output in model_outputs] )
								__lowerCAmelCase         =							logits.shape[0]
								__lowerCAmelCase         =							len(snake_case__ )
								__lowerCAmelCase         =							N // n
								__lowerCAmelCase         =							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
									__lowerCAmelCase         =							self.entailment_id
									__lowerCAmelCase         =							-1 if entailment_id == 0 else 0
									__lowerCAmelCase         =							reshaped_outputs[..., [contradiction_id, entailment_id]]
									__lowerCAmelCase         =							np.exp(snake_case__ ) / np.exp(snake_case__ ).sum(-1      ,				keepdims=snake_case__ )
									__lowerCAmelCase         =							scores[..., 1]
								else:
									# softmax the "entailment" logits over all candidate labels
									__lowerCAmelCase         =							reshaped_outputs[..., self.entailment_id]
									__lowerCAmelCase         =							np.exp(snake_case__ ) / np.exp(snake_case__ ).sum(-1      ,				keepdims=snake_case__ )

								__lowerCAmelCase         =							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 pytest

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


UpperCamelCase_   	=	pytest.mark.integration






@pytest.mark.parametrize("path"			,							["paws", "csv"]  )
def 		_UpperCAmelCase   (	UpperCamelCase:     Optional[int]			,							UpperCamelCase:     Tuple  ):






	"""simple docstring"""


	inspect_dataset(UpperCamelCase			,							UpperCamelCase  )
	__lowerCAmelCase         =							path + ".py"
	assert script_name in os.listdir(UpperCamelCase  )
	assert "__pycache__" not in os.listdir(UpperCamelCase  )






@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 		_UpperCAmelCase   (	UpperCamelCase:     List[Any]			,							UpperCamelCase:     Any  ):






	"""simple docstring"""


	inspect_metric(UpperCamelCase			,							UpperCamelCase  )
	__lowerCAmelCase         =							path + ".py"
	assert script_name in os.listdir(UpperCamelCase  )
	assert "__pycache__" not in os.listdir(UpperCamelCase  )






@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 		_UpperCAmelCase   (	UpperCamelCase:     Optional[Any]			,							UpperCamelCase:     int			,							UpperCamelCase:     Tuple  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_config_info(UpperCamelCase			,							config_name=UpperCamelCase  )
	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 		_UpperCAmelCase   (	UpperCamelCase:     Dict			,							UpperCamelCase:     Any			,							UpperCamelCase:     List[str]  ):






	"""simple docstring"""


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






@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 		_UpperCAmelCase   (	UpperCamelCase:     List[str]			,							UpperCamelCase:     Optional[int]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_config_names(UpperCamelCase  )
	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 		_UpperCAmelCase   (	UpperCamelCase:     str			,							UpperCamelCase:     Tuple			,							UpperCamelCase:     List[Any]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_infos(UpperCamelCase  )
	assert list(infos.keys()  ) == expected_configs
	__lowerCAmelCase         =							expected_configs[0]
	assert expected_config in infos
	__lowerCAmelCase         =							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 		_UpperCAmelCase   (	UpperCamelCase:     Optional[int]			,							UpperCamelCase:     int			,							UpperCamelCase:     Optional[Any]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_infos(UpperCamelCase  )
	assert expected_config in infos
	__lowerCAmelCase         =							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 		_UpperCAmelCase   (	UpperCamelCase:     Dict			,							UpperCamelCase:     List[str]			,							UpperCamelCase:     List[Any]  ):






	"""simple docstring"""


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






