



'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()





class 						_SCREAMING_SNAKE_CASE      ( unittest.TestCase						):






			"""simple docstring"""







			@property
			def 						_lowerCamelCase       (   self    ):







				"""simple docstring"""



				torch.manual_seed(0    )
				__lowerCamelCase		       =		UNetaDModel(
				    block_out_channels=(32, 64)       , layers_per_block=2       , sample_size=32       , in_channels=3       , out_channels=3       , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')       , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')       , )
				return model


			def 						_lowerCamelCase       (   self    ):







				"""simple docstring"""



				__lowerCamelCase		       =		self.dummy_uncond_unet
				__lowerCamelCase		       =		KarrasVeScheduler()

				__lowerCamelCase		       =		KarrasVePipeline(unet=_snake_case       , scheduler=_snake_case    )
				pipe.to(_snake_case    )
				pipe.set_progress_bar_config(disable=_snake_case    )

				__lowerCamelCase		       =		torch.manual_seed(0    )
				__lowerCamelCase		       =		pipe(num_inference_steps=2       , generator=_snake_case       , output_type='''numpy'''    ).images

				__lowerCamelCase		       =		torch.manual_seed(0    )
				__lowerCamelCase		       =		pipe(num_inference_steps=2       , generator=_snake_case       , output_type='''numpy'''       , return_dict=_snake_case    )[0]

				__lowerCamelCase		       =		image[0, -3:, -3:, -1]
				__lowerCamelCase		       =		image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 32, 32, 3)
				__lowerCamelCase		       =		np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]    )

				assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1E-2





@slow
@require_torch
class 						_SCREAMING_SNAKE_CASE      ( unittest.TestCase						):






			"""simple docstring"""







			def 						_lowerCamelCase       (   self    ):







				"""simple docstring"""



				__lowerCamelCase		       =		'''google/ncsnpp-celebahq-256'''
				__lowerCamelCase		       =		UNetaDModel.from_pretrained(_snake_case    )
				__lowerCamelCase		       =		KarrasVeScheduler()

				__lowerCamelCase		       =		KarrasVePipeline(unet=_snake_case       , scheduler=_snake_case    )
				pipe.to(_snake_case    )
				pipe.set_progress_bar_config(disable=_snake_case    )

				__lowerCamelCase		       =		torch.manual_seed(0    )
				__lowerCamelCase		       =		pipe(num_inference_steps=20       , generator=_snake_case       , output_type='''numpy'''    ).images

				__lowerCamelCase		       =		image[0, -3:, -3:, -1]
				assert image.shape == (1, 2_56, 2_56, 3)
				__lowerCamelCase		       =		np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6]    )

				assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2









import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class  _UpperCAmelCase   (			unittest.TestCase      ):

				"""simple docstring"""




				lowercase__											=			MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				lowercase__											=			TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




				def 							lowercase__  (	self		:       Any,    lowerCamelCase		:       Optional[Any],    lowerCamelCase		:       Any,    lowerCamelCase		:       Tuple    ):

								'''simple docstring'''
								lowercase__  					=     AudioClassificationPipeline(model=lowerCamelCase,    feature_extractor=lowerCamelCase    )

								# test with a raw waveform
								lowercase__  					=     np.zeros((34_000,)    )
								lowercase__  					=     np.zeros((14_000,)    )
								return audio_classifier, [audioa, audio]




				def 							lowercase__  (	self		:       str,    lowerCamelCase		:       Dict,    lowerCamelCase		:       Union[str, Any]    ):

								'''simple docstring'''
								lowercase__					,		lowercase__  					=     examples
								lowercase__  					=     audio_classifier(lowerCamelCase    )
								# by default a model is initialized with num_labels=2
								self.assertEqual(
								    lowerCamelCase,    [
								        {'''score''': ANY(lowerCamelCase    ), '''label''': ANY(lowerCamelCase    )},
								        {'''score''': ANY(lowerCamelCase    ), '''label''': ANY(lowerCamelCase    )},
								    ],    )
								lowercase__  					=     audio_classifier(lowerCamelCase,    top_k=1    )
								self.assertEqual(
								    lowerCamelCase,    [
								        {'''score''': ANY(lowerCamelCase    ), '''label''': ANY(lowerCamelCase    )},
								    ],    )

								self.run_torchaudio(lowerCamelCase    )




				@require_torchaudio
				def 							lowercase__  (	self		:       Optional[int],    lowerCamelCase		:       List[Any]    ):

								'''simple docstring'''
								import datasets

								# test with a local file
								lowercase__  					=     datasets.load_dataset('''hf-internal-testing/librispeech_asr_dummy''',    '''clean''',    split='''validation'''    )
								lowercase__  					=     dataset[0]['''audio''']['''array''']
								lowercase__  					=     audio_classifier(lowerCamelCase    )
								self.assertEqual(
								    lowerCamelCase,    [
								        {'''score''': ANY(lowerCamelCase    ), '''label''': ANY(lowerCamelCase    )},
								        {'''score''': ANY(lowerCamelCase    ), '''label''': ANY(lowerCamelCase    )},
								    ],    )




				@require_torch
				def 							lowercase__  (	self		:       int    ):

								'''simple docstring'''
								lowercase__  					=     '''anton-l/wav2vec2-random-tiny-classifier'''

								lowercase__  					=     pipeline('''audio-classification''',    model=lowerCamelCase    )

								lowercase__  					=     np.ones((8_000,)    )
								lowercase__  					=     audio_classifier(lowerCamelCase,    top_k=4    )

								lowercase__  					=     [
								    {'''score''': 0.0842, '''label''': '''no'''},
								    {'''score''': 0.0838, '''label''': '''up'''},
								    {'''score''': 0.0837, '''label''': '''go'''},
								    {'''score''': 0.0834, '''label''': '''right'''},
								]
								lowercase__  					=     [
								    {'''score''': 0.0845, '''label''': '''stop'''},
								    {'''score''': 0.0844, '''label''': '''on'''},
								    {'''score''': 0.0841, '''label''': '''right'''},
								    {'''score''': 0.0834, '''label''': '''left'''},
								]
								self.assertIn(nested_simplify(lowerCamelCase,    decimals=4    ),    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )

								lowercase__  					=     {'''array''': np.ones((8_000,)    ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
								lowercase__  					=     audio_classifier(lowerCamelCase,    top_k=4    )
								self.assertIn(nested_simplify(lowerCamelCase,    decimals=4    ),    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )




				@require_torch
				@slow
				def 							lowercase__  (	self		:       List[str]    ):

								'''simple docstring'''
								import datasets

								lowercase__  					=     '''superb/wav2vec2-base-superb-ks'''

								lowercase__  					=     pipeline('''audio-classification''',    model=lowerCamelCase    )
								lowercase__  					=     datasets.load_dataset('''anton-l/superb_dummy''',    '''ks''',    split='''test'''    )

								lowercase__  					=     np.array(dataset[3]['''speech'''],    dtype=np.floataa    )
								lowercase__  					=     audio_classifier(lowerCamelCase,    top_k=4    )
								self.assertEqual(
								    nested_simplify(lowerCamelCase,    decimals=3    ),    [
								        {'''score''': 0.981, '''label''': '''go'''},
								        {'''score''': 0.007, '''label''': '''up'''},
								        {'''score''': 0.006, '''label''': '''_unknown_'''},
								        {'''score''': 0.001, '''label''': '''down'''},
								    ],    )




				@require_tf
				@unittest.skip('''Audio classification is not implemented for TF'''    )
				def 							lowercase__  (	self		:       Optional[int]    ):

								'''simple docstring'''
								pass




