



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  __magic_name__   (							unittest.TestCase						):






		@require_torch
		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Optional[int]:
								'''simple docstring'''







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






		@unittest.skip("No models are available in TF"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Optional[int]:
								'''simple docstring'''







								pass






		@slow
		@require_torch
		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =     pipeline(
								    task="zero-shot-audio-classification"				,		model="laion/clap-htsat-unfused"				,		)
								# This is an audio of a dog
								UpperCAmelCase         =     load_dataset("ashraq/esc50"      )
								UpperCAmelCase         =     dataset["train"]["audio"][-1]["array"]
								UpperCAmelCase         =     audio_classifier(UpperCamelCase__				,		candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]      )

								self.assertEqual(
								    nested_simplify(UpperCamelCase__      )				,		[
								        {"score": 0.9_99, "label": "Sound of a dog"},
								        {"score": 0.0_01, "label": "Sound of vaccum cleaner"},
								    ]				,		)

								UpperCAmelCase         =     audio_classifier([audio] * 5				,		candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__      )				,		[
								        [
								            {"score": 0.9_99, "label": "Sound of a dog"},
								            {"score": 0.0_01, "label": "Sound of vaccum cleaner"},
								        ],
								    ]
								    * 5				,		)
								UpperCAmelCase         =     audio_classifier(
								    [audio] * 5				,		candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"]				,		batch_size=5      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__      )				,		[
								        [
								            {"score": 0.9_99, "label": "Sound of a dog"},
								            {"score": 0.0_01, "label": "Sound of vaccum cleaner"},
								        ],
								    ]
								    * 5				,		)







		@unittest.skip("No models are available in TF"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					int:
								'''simple docstring'''







								pass








import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
		import torch





@require_torch
@require_optimum
@slow
class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Optional[int]:
								'''simple docstring'''







								UpperCAmelCase         =     "hf-internal-testing/tiny-random-t5"
								UpperCAmelCase         =     AutoTokenizer.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

								UpperCAmelCase         =     tokenizer("This is me"				,		return_tensors="pt"      )

								UpperCAmelCase         =     model.to_bettertransformer()

								self.assertTrue(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

								UpperCAmelCase         =     model.generate(**UpperCamelCase__      )

								UpperCAmelCase         =     model.reverse_bettertransformer()

								self.assertFalse(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

								with tempfile.TemporaryDirectory() as tmpdirname:
														model.save_pretrained(UpperCamelCase__      )

														UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

														self.assertFalse(
														    any("BetterTransformer" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()      )      )

														UpperCAmelCase         =     model_reloaded.generate(**UpperCamelCase__      )
														self.assertTrue(torch.allclose(UpperCamelCase__				,		UpperCamelCase__      )      )







		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Optional[int]:
								'''simple docstring'''







								UpperCAmelCase         =     "hf-internal-testing/tiny-random-t5"
								UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

								UpperCAmelCase         =     model.to_bettertransformer()

								with tempfile.TemporaryDirectory() as tmpdirname:
														with self.assertRaises(UpperCamelCase__      ):
																				model.save_pretrained(UpperCamelCase__      )

														UpperCAmelCase         =     model.reverse_bettertransformer()
														model.save_pretrained(UpperCamelCase__      )



