
"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
UpperCamelCase_					:						Optional[Any]				= {'''LayoutLMv2Config''', '''LayoutLMv3Config'''}



@is_pipeline_test
class __lowerCAmelCase				(					unittest.TestCase      ):




			"""simple docstring"""







			snake_case	      =				MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
			snake_case	      =				TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

			if model_mapping is not None:
						snake_case	      =				{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
			if tf_model_mapping is not None:
						snake_case	      =				{
						    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
						}




			def      lowerCamelCase__     (				self						:					Optional[int]       ,   _snake_case						:					Any       ,   _snake_case						:					Any       ,   _snake_case						:					Union[str, Any]	)      -> Optional[int]:







								"""simple docstring"""
								A_				 =						ZeroShotClassificationPipeline(
								    model=_snake_case       ,   tokenizer=_snake_case       ,   candidate_labels=["polics", "health"]	)
								return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]




			def      lowerCamelCase__     (				self						:					Dict       ,   _snake_case						:					List[Any]       ,   _snake_case						:					int	)      -> List[Any]:







								"""simple docstring"""
								A_				 =						classifier("Who are you voting for in 2020?"       ,   candidate_labels="politics"	)
								self.assertEqual(_snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	)], "scores": [ANY(_snake_case	)]}	)

								# No kwarg
								A_				 =						classifier("Who are you voting for in 2020?"       ,   ["politics"]	)
								self.assertEqual(_snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	)], "scores": [ANY(_snake_case	)]}	)

								A_				 =						classifier("Who are you voting for in 2020?"       ,   candidate_labels=["politics"]	)
								self.assertEqual(_snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	)], "scores": [ANY(_snake_case	)]}	)

								A_				 =						classifier("Who are you voting for in 2020?"       ,   candidate_labels="politics, public health"	)
								self.assertEqual(
								    _snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	), ANY(_snake_case	)], "scores": [ANY(_snake_case	), ANY(_snake_case	)]}	)
								self.assertAlmostEqual(sum(nested_simplify(outputs["scores"]	)	)       ,   1.0	)

								A_				 =						classifier("Who are you voting for in 2020?"       ,   candidate_labels=["politics", "public health"]	)
								self.assertEqual(
								    _snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	), ANY(_snake_case	)], "scores": [ANY(_snake_case	), ANY(_snake_case	)]}	)
								self.assertAlmostEqual(sum(nested_simplify(outputs["scores"]	)	)       ,   1.0	)

								A_				 =						classifier(
								    "Who are you voting for in 2020?"       ,   candidate_labels="politics"       ,   hypothesis_template="This text is about {}"	)
								self.assertEqual(_snake_case       ,   {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	)], "scores": [ANY(_snake_case	)]}	)

								# https://github.com/huggingface/transformers/issues/13846
								A_				 =						classifier(["I am happy"]       ,   ["positive", "negative"]	)
								self.assertEqual(
								    _snake_case       ,   [
								        {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	), ANY(_snake_case	)], "scores": [ANY(_snake_case	), ANY(_snake_case	)]}
								        for i in range(1	)
								    ]       ,   )
								A_				 =						classifier(["I am happy", "I am sad"]       ,   ["positive", "negative"]	)
								self.assertEqual(
								    _snake_case       ,   [
								        {"sequence": ANY(_snake_case	), "labels": [ANY(_snake_case	), ANY(_snake_case	)], "scores": [ANY(_snake_case	), ANY(_snake_case	)]}
								        for i in range(2	)
								    ]       ,   )

								with self.assertRaises(_snake_case	):
													classifier(""       ,   candidate_labels="politics"	)

								with self.assertRaises(_snake_case	):
													classifier(_snake_case       ,   candidate_labels="politics"	)

								with self.assertRaises(_snake_case	):
													classifier("Who are you voting for in 2020?"       ,   candidate_labels=""	)

								with self.assertRaises(_snake_case	):
													classifier("Who are you voting for in 2020?"       ,   candidate_labels=_snake_case	)

								with self.assertRaises(_snake_case	):
													classifier(
													    "Who are you voting for in 2020?"       ,   candidate_labels="politics"       ,   hypothesis_template="Not formatting template"       ,   )

								with self.assertRaises(_snake_case	):
													classifier(
													    "Who are you voting for in 2020?"       ,   candidate_labels="politics"       ,   hypothesis_template=_snake_case       ,   )

								self.run_entailment_id(_snake_case	)




			def      lowerCamelCase__     (				self						:					List[Any]       ,   _snake_case						:					Pipeline	)      -> Dict:







								"""simple docstring"""
								A_				 =						zero_shot_classifier.model.config
								A_				 =						config.labelaid
								A_				 =						zero_shot_classifier.entailment_id

								A_				 =						{"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2}
								self.assertEqual(zero_shot_classifier.entailment_id       ,   -1	)

								A_				 =						{"entailment": 0, "neutral": 1, "contradiction": 2}
								self.assertEqual(zero_shot_classifier.entailment_id       ,   0	)

								A_				 =						{"ENTAIL": 0, "NON-ENTAIL": 1}
								self.assertEqual(zero_shot_classifier.entailment_id       ,   0	)

								A_				 =						{"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0}
								self.assertEqual(zero_shot_classifier.entailment_id       ,   2	)

								A_				 =						original_labelaid
								self.assertEqual(_snake_case       ,   zero_shot_classifier.entailment_id	)




			@require_torch
			def      lowerCamelCase__     (				self						:					List[Any]	)      -> Tuple:







								"""simple docstring"""
								A_				 =						pipeline(
								    "zero-shot-classification"       ,   model="sshleifer/tiny-distilbert-base-cased-distilled-squad"       ,   framework="pt"       ,   )
								# There was a regression in 4.10 for this
								# Adding a test so we don't make the mistake again.
								# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
								zero_shot_classifier(
								    "Who are you voting for in 2020?" * 100       ,   candidate_labels=["politics", "public health", "science"]	)




			@require_torch
			def      lowerCamelCase__     (				self						:					Optional[Any]	)      -> str:







								"""simple docstring"""
								A_				 =						pipeline(
								    "zero-shot-classification"       ,   model="sshleifer/tiny-distilbert-base-cased-distilled-squad"       ,   framework="pt"       ,   )
								A_				 =						zero_shot_classifier(
								    "Who are you voting for in 2020?"       ,   candidate_labels=["politics", "public health", "science"]	)

								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["science", "public health", "politics"],
								        "scores": [0.3_3_3, 0.3_3_3, 0.3_3_3],
								    }       ,   )




			@require_tf
			def      lowerCamelCase__     (				self						:					Any	)      -> List[Any]:







								"""simple docstring"""
								A_				 =						pipeline(
								    "zero-shot-classification"       ,   model="sshleifer/tiny-distilbert-base-cased-distilled-squad"       ,   framework="tf"       ,   )
								A_				 =						zero_shot_classifier(
								    "Who are you voting for in 2020?"       ,   candidate_labels=["politics", "public health", "science"]	)

								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["science", "public health", "politics"],
								        "scores": [0.3_3_3, 0.3_3_3, 0.3_3_3],
								    }       ,   )




			@slow
			@require_torch
			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> Optional[int]:







								"""simple docstring"""
								A_				 =						pipeline("zero-shot-classification"       ,   model="roberta-large-mnli"       ,   framework="pt"	)
								A_				 =						zero_shot_classifier(
								    "Who are you voting for in 2020?"       ,   candidate_labels=["politics", "public health", "science"]	)

								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["politics", "public health", "science"],
								        "scores": [0.9_7_6, 0.0_1_5, 0.0_0_9],
								    }       ,   )
								A_				 =						zero_shot_classifier(
								    "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"
								    " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"
								    " through an attention mechanism. We propose a new simple network architecture, the Transformer, based"
								    " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"
								    " machine translation tasks show these models to be superior in quality while being more parallelizable"
								    " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"
								    " English-to-German translation task, improving over the existing best results, including ensembles by"
								    " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"
								    " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"
								    " fraction of the training costs of the best models from the literature. We show that the Transformer"
								    " generalizes well to other tasks by applying it successfully to English constituency parsing both with"
								    " large and limited training data."       ,   candidate_labels=["machine learning", "statistics", "translation", "vision"]       ,   multi_label=_snake_case       ,   )
								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": (
								            "The dominant sequence transduction models are based on complex recurrent or convolutional neural"
								            " networks in an encoder-decoder configuration. The best performing models also connect the"
								            " encoder and decoder through an attention mechanism. We propose a new simple network"
								            " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"
								            " and convolutions entirely. Experiments on two machine translation tasks show these models to be"
								            " superior in quality while being more parallelizable and requiring significantly less time to"
								            " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"
								            " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"
								            " English-to-French translation task, our model establishes a new single-model state-of-the-art"
								            " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"
								            " costs of the best models from the literature. We show that the Transformer generalizes well to"
								            " other tasks by applying it successfully to English constituency parsing both with large and"
								            " limited training data."
								        ),
								        "labels": ["translation", "machine learning", "vision", "statistics"],
								        "scores": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
								    }       ,   )






			@slow
			@require_tf
			def      lowerCamelCase__     (				self						:					Any	)      -> int:







								"""simple docstring"""
								A_				 =						pipeline("zero-shot-classification"       ,   model="roberta-large-mnli"       ,   framework="tf"	)
								A_				 =						zero_shot_classifier(
								    "Who are you voting for in 2020?"       ,   candidate_labels=["politics", "public health", "science"]	)

								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["politics", "public health", "science"],
								        "scores": [0.9_7_6, 0.0_1_5, 0.0_0_9],
								    }       ,   )
								A_				 =						zero_shot_classifier(
								    "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"
								    " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"
								    " through an attention mechanism. We propose a new simple network architecture, the Transformer, based"
								    " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"
								    " machine translation tasks show these models to be superior in quality while being more parallelizable"
								    " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"
								    " English-to-German translation task, improving over the existing best results, including ensembles by"
								    " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"
								    " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"
								    " fraction of the training costs of the best models from the literature. We show that the Transformer"
								    " generalizes well to other tasks by applying it successfully to English constituency parsing both with"
								    " large and limited training data."       ,   candidate_labels=["machine learning", "statistics", "translation", "vision"]       ,   multi_label=_snake_case       ,   )
								self.assertEqual(
								    nested_simplify(_snake_case	)       ,   {
								        "sequence": (
								            "The dominant sequence transduction models are based on complex recurrent or convolutional neural"
								            " networks in an encoder-decoder configuration. The best performing models also connect the"
								            " encoder and decoder through an attention mechanism. We propose a new simple network"
								            " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"
								            " and convolutions entirely. Experiments on two machine translation tasks show these models to be"
								            " superior in quality while being more parallelizable and requiring significantly less time to"
								            " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"
								            " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"
								            " English-to-French translation task, our model establishes a new single-model state-of-the-art"
								            " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"
								            " costs of the best models from the literature. We show that the Transformer generalizes well to"
								            " other tasks by applying it successfully to English constituency parsing both with large and"
								            " limited training data."
								        ),
								        "labels": ["translation", "machine learning", "vision", "statistics"],
								        "scores": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
								    }       ,   )






"""simple docstring"""





import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
		import torch

if is_datasets_available():
		from datasets import load_dataset

UpperCamelCase_					:						Union[str, Any]				= random.Random()
def 							A_     (__a				,   __a=1.0				,   __a=None				,   __a=None				):

					'''simple docstring'''





					if rng is None:
										A_				 =						global_rng

					A_				 =						[]
					for batch_idx in range(shape[0]				):
										values.append([]				)
										for _ in range(shape[1]				):
															values[-1].append(rng.random() * scale				)

					return values



class __lowerCAmelCase				(					unittest.TestCase      ):




			"""simple docstring"""







			def __init__(				self						:					Optional[Any]       ,   _snake_case						:					str       ,   _snake_case						:					Union[str, Any]=7       ,   _snake_case						:					Any=400       ,   _snake_case						:					Tuple=2_000       ,   _snake_case						:					str=2_048       ,   _snake_case						:					List[Any]=128       ,   _snake_case						:					List[Any]=1       ,   _snake_case						:					Union[str, Any]=512       ,   _snake_case						:					List[str]=30       ,   _snake_case						:					List[str]=44_100       ,   )      -> Optional[int]:







								"""simple docstring"""
								A_				 =						parent
								A_				 =						batch_size
								A_				 =						min_seq_length
								A_				 =						max_seq_length
								A_				 =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								A_				 =						spectrogram_length
								A_				 =						feature_size
								A_				 =						num_audio_channels
								A_				 =						hop_length
								A_				 =						chunk_length
								A_				 =						sampling_rate




			def      lowerCamelCase__     (				self						:					Any	)      -> Optional[int]:







								"""simple docstring"""
								return {
								    "spectrogram_length": self.spectrogram_length,
								    "feature_size": self.feature_size,
								    "num_audio_channels": self.num_audio_channels,
								    "hop_length": self.hop_length,
								    "chunk_length": self.chunk_length,
								    "sampling_rate": self.sampling_rate,
								}






			def      lowerCamelCase__     (				self						:					Dict       ,   _snake_case						:					str=False       ,   _snake_case						:					Optional[Any]=False	)      -> List[str]:







								"""simple docstring"""
								def _flatten(_snake_case						:					List[Any]	):
													return list(itertools.chain(*_snake_case	)	)

								if equal_length:
													A_				 =						[floats_list((self.max_seq_length, self.feature_size)	) for _ in range(self.batch_size	)]
								else:
													# make sure that inputs increase in size
													A_				 =						[
													    floats_list((x, self.feature_size)	)
													    for x in range(self.min_seq_length       ,   self.max_seq_length       ,   self.seq_length_diff	)
													]
								if numpify:
													A_				 =						[np.asarray(_snake_case	) for x in speech_inputs]
								return speech_inputs





@require_torch
@require_torchaudio
class __lowerCAmelCase				(					_lowercase     ,				unittest.TestCase      ):




			"""simple docstring"""







			snake_case	      =				TvltFeatureExtractor




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> List[Any]:







								"""simple docstring"""
								A_				 =						TvltFeatureExtractionTester(self	)




			def      lowerCamelCase__     (				self						:					Tuple	)      -> Union[str, Any]:







								"""simple docstring"""
								A_				 =						self.feature_extraction_class(**self.feat_extract_dict	)
								self.assertTrue(hasattr(_snake_case       ,   "spectrogram_length"	)	)
								self.assertTrue(hasattr(_snake_case       ,   "feature_size"	)	)
								self.assertTrue(hasattr(_snake_case       ,   "num_audio_channels"	)	)
								self.assertTrue(hasattr(_snake_case       ,   "hop_length"	)	)
								self.assertTrue(hasattr(_snake_case       ,   "chunk_length"	)	)
								self.assertTrue(hasattr(_snake_case       ,   "sampling_rate"	)	)




			def      lowerCamelCase__     (				self						:					Tuple	)      -> Union[str, Any]:







								"""simple docstring"""
								A_				 =						self.feature_extraction_class(**self.feat_extract_dict	)

								with tempfile.TemporaryDirectory() as tmpdirname:
													A_				 =						feat_extract_first.save_pretrained(_snake_case	)[0]
													check_json_file_has_correct_format(_snake_case	)
													A_				 =						self.feature_extraction_class.from_pretrained(_snake_case	)

								A_				 =						feat_extract_first.to_dict()
								A_				 =						feat_extract_second.to_dict()
								A_				 =						dict_first.pop("mel_filters"	)
								A_				 =						dict_second.pop("mel_filters"	)
								self.assertTrue(np.allclose(_snake_case       ,   _snake_case	)	)
								self.assertEqual(_snake_case       ,   _snake_case	)




			def      lowerCamelCase__     (				self						:					Optional[int]	)      -> Tuple:







								"""simple docstring"""
								A_				 =						self.feature_extraction_class(**self.feat_extract_dict	)

								with tempfile.TemporaryDirectory() as tmpdirname:
													A_				 =						os.path.join(_snake_case       ,   "feat_extract.json"	)
													feat_extract_first.to_json_file(_snake_case	)
													A_				 =						self.feature_extraction_class.from_json_file(_snake_case	)

								A_				 =						feat_extract_first.to_dict()
								A_				 =						feat_extract_second.to_dict()
								A_				 =						dict_first.pop("mel_filters"	)
								A_				 =						dict_second.pop("mel_filters"	)
								self.assertTrue(np.allclose(_snake_case       ,   _snake_case	)	)
								self.assertEqual(_snake_case       ,   _snake_case	)




			def      lowerCamelCase__     (				self						:					int	)      -> List[Any]:







								"""simple docstring"""
								# Initialize feature_extractor
								A_				 =						self.feature_extraction_class(**self.feat_extract_dict	)

								# create three inputs of length 800, 1000, and 1200
								A_				 =						[floats_list((1, x)	)[0] for x in range(800       ,   1_400       ,   200	)]
								A_				 =						[np.asarray(_snake_case	) for speech_input in speech_inputs]

								# Test not batched input
								A_				 =						feature_extractor(np_speech_inputs[0]       ,   return_tensors="np"       ,   sampling_rate=44_100	).audio_values

								self.assertTrue(encoded_audios.ndim == 4	)
								self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size	)
								self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length	)
								self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels	)

								# Test batched
								A_				 =						feature_extractor(_snake_case       ,   return_tensors="np"       ,   sampling_rate=44_100	).audio_values

								self.assertTrue(encoded_audios.ndim == 4	)
								self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size	)
								self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length	)
								self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels	)

								# Test audio masking
								A_				 =						feature_extractor(
								    _snake_case       ,   return_tensors="np"       ,   sampling_rate=44_100       ,   mask_audio=_snake_case	).audio_values

								self.assertTrue(encoded_audios.ndim == 4	)
								self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size	)
								self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length	)
								self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels	)

								# Test 2-D numpy arrays are batched.
								A_				 =						[floats_list((1, x)	)[0] for x in (800, 800, 800)]
								A_				 =						np.asarray(_snake_case	)
								A_				 =						feature_extractor(_snake_case       ,   return_tensors="np"       ,   sampling_rate=44_100	).audio_values
								self.assertTrue(encoded_audios.ndim == 4	)
								self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size	)
								self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length	)
								self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels	)




			def      lowerCamelCase__     (				self						:					Dict       ,   _snake_case						:					int	)      -> List[str]:







								"""simple docstring"""
								A_				 =						load_dataset("hf-internal-testing/librispeech_asr_dummy"       ,   "clean"       ,   split="validation"	)
								# automatic decoding with librispeech
								A_				 =						ds.sort("id"	).select(range(_snake_case	)	)[:num_samples]["audio"]

								return [x["array"] for x in speech_samples]






			def      lowerCamelCase__     (				self						:					List[str]	)      -> int:







								"""simple docstring"""
								A_				 =						self._load_datasamples(1	)
								A_				 =						TvltFeatureExtractor()
								A_				 =						feature_extractor(_snake_case       ,   return_tensors="pt"	).audio_values

								self.assertEquals(audio_values.shape       ,   (1, 1, 192, 128)	)

								A_				 =						torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]]	)
								self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]       ,   _snake_case       ,   atol=1e-4	)	)




