





"""simple docstring"""



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    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		int							=	pipeline(
         task='''zero-shot-audio-classification'''   ,		model='''hf-internal-testing/tiny-clap-htsat-unfused'''    )
     __A       :		int							=	load_dataset('''ashraq/esc50'''    )
     __A       :		Dict							=	dataset["train"]["audio"][-1]["array"]
     __A       :		Optional[int]							=	audio_classifier(snake_case__   ,		candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']    )
     self.assertEqual(
         nested_simplify(snake_case__    )   ,		[{'''score''': 0.5_0_1, '''label''': '''Sound of a dog'''}, {'''score''': 0.4_9_9, '''label''': '''Sound of vaccum cleaner'''}]   ,		)

 @unittest.skip('''No models are available in TF'''    )
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     pass

 @slow
 @require_torch
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Union[str, Any]							=	pipeline(
         task='''zero-shot-audio-classification'''   ,		model='''laion/clap-htsat-unfused'''   ,		)
     # This is an audio of a dog
     __A       :		str							=	load_dataset('''ashraq/esc50'''    )
     __A       :		int							=	dataset["train"]["audio"][-1]["array"]
     __A       :		List[str]							=	audio_classifier(snake_case__   ,		candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']    )

     self.assertEqual(
         nested_simplify(snake_case__    )   ,		[
             {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
             {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
         ]   ,		)

     __A       :		Any							=	audio_classifier([audio] * 5   ,		candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']    )
     self.assertEqual(
         nested_simplify(snake_case__    )   ,		[
             [
                 {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
                 {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
             ],
         ]
         * 5   ,		)
     __A       :		Tuple							=	audio_classifier(
         [audio] * 5   ,		candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']   ,		batch_size=5    )
     self.assertEqual(
         nested_simplify(snake_case__    )   ,		[
             [
                 {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
                 {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
             ],
         ]
         * 5   ,		)







 @unittest.skip('''No models are available in TF'''    )
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     pass












"""simple docstring"""



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 			lowerCAmelCase__			(    unittest.TestCase   ):
   @slow
   def       __a							(		self      :	List[str]							):


        '''simple docstring'''
        UpperCAmelCase__						:    Union[str, Any]							=							FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small"							)
        UpperCAmelCase__						:    str							=							AutoTokenizer.from_pretrained("google/mt5-small"							)

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

        UpperCAmelCase__						:    Any							=							shift_tokens_right(snake_case__							,			model.config.pad_token_id							,			model.config.decoder_start_token_id							)

        UpperCAmelCase__						:    Any							=							model(snake_case__							,			decoder_input_ids=snake_case__							).logits
        UpperCAmelCase__						:    Any							=							optax.softmax_cross_entropy(snake_case__							,			onehot(snake_case__							,			logits.shape[-1]							)							).mean()

        UpperCAmelCase__						:    List[str]							=							-(labels.shape[-1] * loss.item())

        UpperCAmelCase__						:    Dict							=							-84.9127
        self.assertTrue(abs(mtf_score - EXPECTED_SCORE							) < 1e-4							)


