



import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow

@is_pipeline_test
@require_torch
class 				snake_case__(unittest.TestCase      ):

					"""simple docstring"""






					@require_torch
					def 			snake_case					(							self :     int					):
									lowercase__					:   Tuple													=					pipeline(
									    task="zero-shot-audio-classification"				,       model="hf-internal-testing/tiny-clap-htsat-unfused"					)
									lowercase__					:   Optional[int]													=					load_dataset("ashraq/esc50"					)
									lowercase__					:   List[str]													=					dataset["train"]["audio"][-1]["array"]
									lowercase__					:   List[str]													=					audio_classifier(__SCREAMING_SNAKE_CASE				,       candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]					)
									self.assertEqual(
									    nested_simplify(__SCREAMING_SNAKE_CASE					)				,       [{"score": 0.501, "label": "Sound of a dog"}, {"score": 0.499, "label": "Sound of vaccum cleaner"}]				,       )







					@unittest.skip("No models are available in TF"					)
					def 			snake_case					(							self :     Optional[Any]					):
									pass







					@slow
					@require_torch
					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Union[str, Any]													=					pipeline(
									    task="zero-shot-audio-classification"				,       model="laion/clap-htsat-unfused"				,       )
									# This is an audio of a dog
									lowercase__					:   Union[str, Any]													=					load_dataset("ashraq/esc50"					)
									lowercase__					:   Dict													=					dataset["train"]["audio"][-1]["array"]
									lowercase__					:   Any													=					audio_classifier(__SCREAMING_SNAKE_CASE				,       candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]					)

									self.assertEqual(
									    nested_simplify(__SCREAMING_SNAKE_CASE					)				,       [
									        {"score": 0.999, "label": "Sound of a dog"},
									        {"score": 0.001, "label": "Sound of vaccum cleaner"},
									    ]				,       )

									lowercase__					:   List[Any]													=					audio_classifier([audio] * 5				,       candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]					)
									self.assertEqual(
									    nested_simplify(__SCREAMING_SNAKE_CASE					)				,       [
									        [
									            {"score": 0.999, "label": "Sound of a dog"},
									            {"score": 0.001, "label": "Sound of vaccum cleaner"},
									        ],
									    ]
									    * 5				,       )
									lowercase__					:   Dict													=					audio_classifier(
									    [audio] * 5				,       candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]				,       batch_size=5					)
									self.assertEqual(
									    nested_simplify(__SCREAMING_SNAKE_CASE					)				,       [
									        [
									            {"score": 0.999, "label": "Sound of a dog"},
									            {"score": 0.001, "label": "Sound of vaccum cleaner"},
									        ],
									    ]
									    * 5				,       )







					@unittest.skip("No models are available in TF"					)
					def 			snake_case					(							self :     Optional[Any]					):
									pass









import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
				import optax
				from flax.training.common_utils import onehot

				from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
				from transformers.models.ta.modeling_flax_ta import shift_tokens_right

@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class 				snake_case__(unittest.TestCase      ):

					"""simple docstring"""






					@slow
					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Dict													=					FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small"					)
									lowercase__					:   Dict													=					AutoTokenizer.from_pretrained("google/mt5-small"					)

									lowercase__					:   Optional[Any]													=					tokenizer("Hello there"				,       return_tensors="np"					).input_ids
									lowercase__					:   Optional[Any]													=					tokenizer("Hi I am"				,       return_tensors="np"					).input_ids

									lowercase__					:   int													=					shift_tokens_right(SCREAMING_SNAKE_CASE				,       model.config.pad_token_id				,       model.config.decoder_start_token_id					)

									lowercase__					:   Tuple													=					model(SCREAMING_SNAKE_CASE				,       decoder_input_ids=SCREAMING_SNAKE_CASE					).logits
									lowercase__					:   Dict													=					optax.softmax_cross_entropy(SCREAMING_SNAKE_CASE				,       onehot(SCREAMING_SNAKE_CASE				,       logits.shape[-1]					)					).mean()

									lowercase__					:   Union[str, Any]													=					-(labels.shape[-1] * loss.item())

									lowercase__					:   Union[str, Any]													=					-84.9_127
									self.assertTrue(abs(mtf_score - EXPECTED_SCORE					) < 1E-4					)




