
"""simple docstring"""





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 						SCREAMING_SNAKE_CASE_ (						__a     ,				unittest.TestCase ):




		"""simple docstring"""





		__lowercase							:  List[Any]						   =		DanceDiffusionPipeline
		__lowercase							:  Dict						   =		UNCONDITIONAL_AUDIO_GENERATION_PARAMS
		__lowercase							:  Optional[int]						   =		PipelineTesterMixin.required_optional_params - {
		    '''callback''',
		    '''latents''',
		    '''callback_steps''',
		    '''output_type''',
		    '''num_images_per_prompt''',
		}
		__lowercase							:  str						   =		UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
		__lowercase							:  List[Any]						   =		False
		__lowercase							:  str						   =		False




		def 	snake_case_     (     self):
				torch.manual_seed(0)
				__SCREAMING_SNAKE_CASE            = UNetaDModel(
				    block_out_channels=(3_2, 3_2, 6_4)							,  extra_in_channels=1_6							,  sample_size=5_1_2							,  sample_rate=1_6_0_0_0							,  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""")							,  )
				__SCREAMING_SNAKE_CASE            = IPNDMScheduler()

				__SCREAMING_SNAKE_CASE            = {
				    """unet""": unet,
				    """scheduler""": scheduler,
				}
				return components




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__=0):
				if str(lowerCAmelCase__).startswith("""mps"""):
						__SCREAMING_SNAKE_CASE            = torch.manual_seed(lowerCAmelCase__)
				else:
						__SCREAMING_SNAKE_CASE            = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = {
				    """batch_size""": 1,
				    """generator""": generator,
				    """num_inference_steps""": 4,
				}
				return inputs




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = """cpu"""  # ensure determinism for the device-dependent torch.Generator
				__SCREAMING_SNAKE_CASE            = self.get_dummy_components()
				__SCREAMING_SNAKE_CASE            = DanceDiffusionPipeline(**lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = pipe.to(lowerCAmelCase__)
				pipe.set_progress_bar_config(disable=lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = self.get_dummy_inputs(lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = pipe(**lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = output.audios

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

				assert audio.shape == (1, 2, components["unet"].sample_size)
				__SCREAMING_SNAKE_CASE            = np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00])
				assert np.abs(audio_slice.flatten() - expected_slice).max() < 1E-2




		@skip_mps
		def 	snake_case_     (     self):
				return super().test_save_load_local()




		@skip_mps
		def 	snake_case_     (     self):
				return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3)




		@skip_mps
		def 	snake_case_     (     self):
				return super().test_save_load_optional_components()




		@skip_mps
		def 	snake_case_     (     self):
				return super().test_attention_slicing_forward_pass()




		def 	snake_case_     (     self):
				super().test_inference_batch_single_identical(expected_max_diff=3E-3)



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




		"""simple docstring"""





		def 	snake_case_     (     self):
				# clean up the VRAM after each test
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = torch_device

				__SCREAMING_SNAKE_CASE            = DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k""")
				__SCREAMING_SNAKE_CASE            = pipe.to(lowerCAmelCase__)
				pipe.set_progress_bar_config(disable=lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = torch.manual_seed(0)
				__SCREAMING_SNAKE_CASE            = pipe(generator=lowerCAmelCase__							,  num_inference_steps=1_0_0							,  audio_length_in_s=4.0_96)
				__SCREAMING_SNAKE_CASE            = output.audios

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

				assert audio.shape == (1, 2, pipe.unet.sample_size)
				__SCREAMING_SNAKE_CASE            = np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20])

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




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = torch_device

				__SCREAMING_SNAKE_CASE            = DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""							,  torch_dtype=torch.floataa)
				__SCREAMING_SNAKE_CASE            = pipe.to(lowerCAmelCase__)
				pipe.set_progress_bar_config(disable=lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = torch.manual_seed(0)
				__SCREAMING_SNAKE_CASE            = pipe(generator=lowerCAmelCase__							,  num_inference_steps=1_0_0							,  audio_length_in_s=4.0_96)
				__SCREAMING_SNAKE_CASE            = output.audios

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

				assert audio.shape == (1, 2, pipe.unet.sample_size)
				__SCREAMING_SNAKE_CASE            = np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41])

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





import requests





def 				UpperCAmelCase						(    lowercase							,			lowercase							):







				"""simple docstring"""



				__lowercase 			=							{'''Content-Type''': '''application/json'''}
				__lowercase 			=							requests.post(lowercase							,			json={'''text''': message_body}							,			headers=lowercase							)
				if response.status_code != 200:
								__lowercase 			=							(
								    '''Request to slack returned an error '''
								    F"{response.status_code}, the response is:\n{response.text}"
								)
								raise ValueError(lowercase							)


if __name__ == "__main__":
		# Set the slack url to the one provided by Slack when you create the webhook at
		# https://my.slack.com/services/new/incoming-webhook/
		send_slack_message("""<YOUR MESSAGE BODY>""", """<SLACK CHANNEL URL>""")