
import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 		__UpperCAmelCase   (      _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= DanceDiffusionPipeline
							__lowercase   	= UNCONDITIONAL_AUDIO_GENERATION_PARAMS
							__lowercase   	= PipelineTesterMixin.required_optional_params - {
							    """callback""",
							    """latents""",
							    """callback_steps""",
							    """output_type""",
							    """num_images_per_prompt""",
							}
							__lowercase   	= UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
							__lowercase   	= False
							__lowercase   	= False


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								torch.manual_seed(0						)
								_snake_case     		=	UNetaDModel(
								    block_out_channels=(32, 32, 64)					,     extra_in_channels=16					,     sample_size=5_12					,     sample_rate=1_60_00					,     in_channels=2					,     out_channels=2					,     flip_sin_to_cos=lowerCAmelCase_					,     use_timestep_embedding=lowerCAmelCase_					,     time_embedding_type='fourier'					,     mid_block_type='UNetMidBlock1D'					,     down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')					,     up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')					,     )
								_snake_case     		=	IPNDMScheduler()

								_snake_case     		=	{
								    'unet': unet,
								    'scheduler': scheduler,
								}
								return components


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_=0						):





								"""simple docstring"""






								if str(lowerCAmelCase_						).startswith('mps'						):
									_snake_case     		=	torch.manual_seed(lowerCAmelCase_						)
								else:
									_snake_case     		=	torch.Generator(device=lowerCAmelCase_						).manual_seed(lowerCAmelCase_						)
								_snake_case     		=	{
								    'batch_size': 1,
								    'generator': generator,
								    'num_inference_steps': 4,
								}
								return inputs


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	DanceDiffusionPipeline(**lowerCAmelCase_						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	pipe(**lowerCAmelCase_						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, components["unet"].sample_size)
								_snake_case     		=	np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000]						)
								assert np.abs(audio_slice.flatten() - expected_slice						).max() < 1E-2


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_save_load_local()


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3						)


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_save_load_optional_components()


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_attention_slicing_forward_pass()




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								super().test_inference_batch_single_identical(expected_max_diff=3E-3						)


@slow
@require_torch_gpu
class 		__UpperCAmelCase   (      unittest.TestCase		):


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	torch_device

								_snake_case     		=	DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	torch.manual_seed(0						)
								_snake_case     		=	pipe(generator=lowerCAmelCase_					,     num_inference_steps=1_00					,     audio_length_in_s=4.096						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								_snake_case     		=	np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020]						)

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




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	torch_device

								_snake_case     		=	DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'					,     torch_dtype=torch.floataa						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	torch.manual_seed(0						)
								_snake_case     		=	pipe(generator=lowerCAmelCase_					,     num_inference_steps=1_00					,     audio_length_in_s=4.096						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								_snake_case     		=	np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341]						)

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



'''simple docstring'''







import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
					from PIL import Image
else:


					class 		__UpperCAmelCase   :


												@staticmethod
												def 				lowerCamelCase       ( *lowerCAmelCase_					,     **lowerCAmelCase_						):





													"""simple docstring"""






													pass


@is_pipeline_test
@require_vision
@require_torch
class 		__UpperCAmelCase   (      unittest.TestCase		):
							__lowercase   	= MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	pipeline(
								    'zero-shot-object-detection'					,     model='hf-internal-testing/tiny-random-owlvit-object-detection'						)

								_snake_case     		=	[
								    {
								        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								        'candidate_labels': ['cat', 'remote', 'couch'],
								    }
								]
								return object_detector, examples


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	object_detector(examples[0]					,     threshold=0.0						)

								_snake_case     		=	len(lowerCAmelCase_						)
								self.assertGreater(lowerCAmelCase_					,     0						)
								self.assertEqual(
								    lowerCAmelCase_					,     [
								        {
								            'score': ANY(lowerCAmelCase_						),
								            'label': ANY(lowerCAmelCase_						),
								            'box': {'xmin': ANY(lowerCAmelCase_						), 'ymin': ANY(lowerCAmelCase_						), 'xmax': ANY(lowerCAmelCase_						), 'ymax': ANY(lowerCAmelCase_						)},
								        }
								        for i in range(lowerCAmelCase_						)
								    ]					,     )


							@require_tf
							@unittest.skip('Zero Shot Object Detection not implemented in TF'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							@require_torch
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	pipeline(
								    'zero-shot-object-detection'					,     model='hf-internal-testing/tiny-random-owlvit-object-detection'						)

								_snake_case     		=	object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'					,     candidate_labels=['cat', 'remote', 'couch']					,     threshold=0.64					,     )

								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,     )

								_snake_case     		=	object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]					,     threshold=0.64					,     )

								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        [
								            {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,     )


							@require_torch
							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	pipeline('zero-shot-object-detection'						)

								_snake_case     		=	object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,     candidate_labels=['cat', 'remote', 'couch']					,     )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,     )

								_snake_case     		=	object_detector(
								    [
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								    ]					,     )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,     )


							@require_tf
							@unittest.skip('Zero Shot Object Detection not implemented in TF'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							@require_torch
							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	0.2
								_snake_case     		=	pipeline('zero-shot-object-detection'						)

								_snake_case     		=	object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,     candidate_labels=['cat', 'remote', 'couch']					,     threshold=lowerCAmelCase_					,     )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,     )




							@require_torch
							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	2
								_snake_case     		=	pipeline('zero-shot-object-detection'						)

								_snake_case     		=	object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,     candidate_labels=['cat', 'remote', 'couch']					,     top_k=lowerCAmelCase_					,     )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase_					,     decimals=4						)					,     [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,     )

