






"""simple docstring"""
import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


lowerCamelCase_       			=   logging.get_logger(__name__)







class     UpperCamelCase_ (UpperCAmelCase_  ):
	__magic_name__			   =       ['''input_features''', '''is_longer''']





	def __init__(   self      :				Optional[Any]			,			lowerCAmelCase_      :				int=64			,			lowerCAmelCase_      :				Any=48_000			,			lowerCAmelCase_      :				Optional[int]=480			,			lowerCAmelCase_      :				Any=10			,			lowerCAmelCase_      :				Optional[int]=1_024			,			lowerCAmelCase_      :				str=0.0			,			lowerCAmelCase_      :				List[str]=False			,			lowerCAmelCase_      :				float = 0			,			lowerCAmelCase_      :				float = 14_000			,			lowerCAmelCase_      :				int = None			,			lowerCAmelCase_      :				str = "fusion"			,			lowerCAmelCase_      :				str = "repeatpad"			,			**lowerCAmelCase_      :				List[Any]			,			)		->    Optional[int]:
				super().__init__(
				    feature_size=_lowercase			,			sampling_rate=_lowercase			,			padding_value=_lowercase			,			return_attention_mask=_lowercase			,			**_lowercase			,			)
				UpperCAmelCase_  :				int      =      top_db
				UpperCAmelCase_  :				List[str]      =      truncation
				UpperCAmelCase_  :				Union[str, Any]      =      padding
				UpperCAmelCase_  :				List[str]      =      fft_window_size
				UpperCAmelCase_  :				Optional[Any]      =      (fft_window_size >> 1) + 1
				UpperCAmelCase_  :				Any      =      hop_length
				UpperCAmelCase_  :				Union[str, Any]      =      max_length_s
				UpperCAmelCase_  :				Optional[Any]      =      max_length_s * sampling_rate
				UpperCAmelCase_  :				Union[str, Any]      =      sampling_rate
				UpperCAmelCase_  :				Optional[Any]      =      frequency_min
				UpperCAmelCase_  :				int      =      frequency_max
				UpperCAmelCase_  :				Any      =      mel_filter_bank(
				    num_frequency_bins=self.nb_frequency_bins			,			num_mel_filters=_lowercase			,			min_frequency=_lowercase			,			max_frequency=_lowercase			,			sampling_rate=_lowercase			,			norm=_lowercase			,			mel_scale="htk"			,			)
				UpperCAmelCase_  :				Dict      =      mel_filter_bank(
				    num_frequency_bins=self.nb_frequency_bins			,			num_mel_filters=_lowercase			,			min_frequency=_lowercase			,			max_frequency=_lowercase			,			sampling_rate=_lowercase			,			norm="slaney"			,			mel_scale="slaney"			,			)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    Optional[Any]:
				UpperCAmelCase_  :				List[str]      =      copy.deepcopy(self.__dict__		)
				UpperCAmelCase_  :				Tuple      =      self.__class__.__name__
				if "mel_filters" in output:
							del output["mel_filters"]
				if "mel_filters_slaney" in output:
							del output["mel_filters_slaney"]
				return output





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]			,			lowerCAmelCase_      :				np.array			,			lowerCAmelCase_      :				Optional[np.array] = None		)		->    List[Any]:
				UpperCAmelCase_  :				Optional[Any]      =      spectrogram(
				    _lowercase			,			window_function(self.fft_window_size			,			"hann"		)			,			frame_length=self.fft_window_size			,			hop_length=self.hop_length			,			power=2.0			,			mel_filters=_lowercase			,			log_mel="dB"			,			)
				return log_mel_spectrogram.T





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple			,			lowerCAmelCase_      :				int			,			lowerCAmelCase_      :				List[str]			,			lowerCAmelCase_      :				str		)		->    Any:
				UpperCAmelCase_  :				List[str]      =      np.array_split(list(range(0			,			total_frames - chunk_frames + 1		)		)			,			3		)
				if len(ranges[1]		) == 0:
							# if the audio is too short, we just use the first chunk
							UpperCAmelCase_  :				List[str]      =      [0]
				if len(ranges[2]		) == 0:
							# if the audio is too short, we just use the first chunk
							UpperCAmelCase_  :				Tuple      =      [0]
				# randomly choose index for each part
				UpperCAmelCase_  :				Any      =      np.random.choice(ranges[0]		)
				UpperCAmelCase_  :				List[str]      =      np.random.choice(ranges[1]		)
				UpperCAmelCase_  :				List[Any]      =      np.random.choice(ranges[2]		)

				UpperCAmelCase_  :				Optional[int]      =      mel[idx_front : idx_front + chunk_frames, :]
				UpperCAmelCase_  :				Any      =      mel[idx_middle : idx_middle + chunk_frames, :]
				UpperCAmelCase_  :				List[Any]      =      mel[idx_back : idx_back + chunk_frames, :]

				UpperCAmelCase_  :				str      =      torch.tensor(mel[None, None, :]		)
				UpperCAmelCase_  :				Optional[int]      =      torch.nn.functional.interpolate(
				    _lowercase			,			size=[chunk_frames, 64]			,			mode="bilinear"			,			align_corners=_lowercase		)
				UpperCAmelCase_  :				List[Any]      =      mel_shrink[0][0].numpy()
				UpperCAmelCase_  :				Tuple      =      np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]			,			axis=0		)
				return mel_fusion





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any			,			lowerCAmelCase_      :				np.array			,			lowerCAmelCase_      :				Any			,			lowerCAmelCase_      :				List[str]			,			lowerCAmelCase_      :				List[Any]		)		->    int:
				if waveform.shape[0] > max_length:
							if truncation == "rand_trunc":
										UpperCAmelCase_  :				Tuple      =      True
										# random crop to max_length (for compatibility) -> this should be handled by self.pad
										UpperCAmelCase_  :				Optional[Any]      =      len(_lowercase		) - max_length
										UpperCAmelCase_  :				List[Any]      =      np.random.randint(0			,			overflow + 1		)
										UpperCAmelCase_  :				List[Any]      =      waveform[idx : idx + max_length]
										UpperCAmelCase_  :				List[Any]      =      self._np_extract_fbank_features(_lowercase			,			self.mel_filters_slaney		)[None, :]
							elif truncation == "fusion":
										UpperCAmelCase_  :				Optional[int]      =      self._np_extract_fbank_features(_lowercase			,			self.mel_filters		)
										UpperCAmelCase_  :				List[str]      =      max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
										UpperCAmelCase_  :				Dict      =      mel.shape[0]
										if chunk_frames == total_frames:
													# there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
													# In this case, we just use the whole audio.
													UpperCAmelCase_  :				str      =      np.stack([mel, mel, mel, mel]			,			axis=0		)
													UpperCAmelCase_  :				Optional[Any]      =      False
										else:
													UpperCAmelCase_  :				str      =      self._random_mel_fusion(_lowercase			,			_lowercase			,			_lowercase		)
													UpperCAmelCase_  :				Tuple      =      True
							else:
										raise NotImplementedError(f"""data_truncating {truncation} not implemented"""		)

				else:
							UpperCAmelCase_  :				int      =      False
							# only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
							if waveform.shape[0] < max_length:
										if padding == "repeat":
													UpperCAmelCase_  :				List[str]      =      int(max_length / len(_lowercase		)		)
													UpperCAmelCase_  :				Any      =      np.stack(np.tile(_lowercase			,			n_repeat + 1		)		)[:max_length]
										if padding == "repeatpad":
													UpperCAmelCase_  :				Optional[int]      =      int(max_length / len(_lowercase		)		)
													UpperCAmelCase_  :				int      =      np.stack(np.tile(_lowercase			,			_lowercase		)		)
										UpperCAmelCase_  :				List[str]      =      np.pad(_lowercase			,			(0, max_length - waveform.shape[0])			,			mode="constant"			,			constant_values=0		)

							if truncation == "fusion":
										UpperCAmelCase_  :				List[str]      =      self._np_extract_fbank_features(_lowercase			,			self.mel_filters		)
										UpperCAmelCase_  :				str      =      np.stack([input_mel, input_mel, input_mel, input_mel]			,			axis=0		)
							else:
										UpperCAmelCase_  :				List[Any]      =      self._np_extract_fbank_features(_lowercase			,			self.mel_filters_slaney		)[None, :]

				return input_mel, longer





	def __call__(   self      :				List[str]			,			lowerCAmelCase_      :				Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,			lowerCAmelCase_      :				str = None			,			lowerCAmelCase_      :				Optional[str] = None			,			lowerCAmelCase_      :				Optional[int] = None			,			lowerCAmelCase_      :				Optional[int] = None			,			lowerCAmelCase_      :				Optional[Union[str, TensorType]] = None			,			**lowerCAmelCase_      :				str			,			)		->    Tuple:
				UpperCAmelCase_  :				int      =      truncation if truncation is not None else self.truncation
				UpperCAmelCase_  :				Union[str, Any]      =      padding if padding else self.padding

				if sampling_rate is not None:
							if sampling_rate != self.sampling_rate:
										raise ValueError(
										    f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
										    f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
										    f""" was sampled with {self.sampling_rate} and not {sampling_rate}."""		)
				else:
							logger.warning(
							    "It is strongly recommended to pass the `sampling_rate` argument to this function. "
							    "Failing to do so can result in silent errors that might be hard to debug."		)

				UpperCAmelCase_  :				Any      =      isinstance(_lowercase			,			np.ndarray		) and len(raw_speech.shape		) > 1
				if is_batched_numpy and len(raw_speech.shape		) > 2:
							raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""		)
				UpperCAmelCase_  :				Optional[Any]      =      is_batched_numpy or (
				    isinstance(_lowercase			,			(list, tuple)		) and (isinstance(raw_speech[0]			,			(np.ndarray, tuple, list)		))
				)

				if is_batched:
							UpperCAmelCase_  :				Tuple      =      [np.asarray(_lowercase			,			dtype=np.floataa		) for speech in raw_speech]
				elif not is_batched and not isinstance(_lowercase			,			np.ndarray		):
							UpperCAmelCase_  :				Optional[int]      =      np.asarray(_lowercase			,			dtype=np.floataa		)
				elif isinstance(_lowercase			,			np.ndarray		) and raw_speech.dtype is np.dtype(np.floataa		):
							UpperCAmelCase_  :				Optional[int]      =      raw_speech.astype(np.floataa		)

				# always return batch
				if not is_batched:
							UpperCAmelCase_  :				List[Any]      =      [np.asarray(_lowercase		)]

				# convert to mel spectrogram, truncate and pad if needed.
				UpperCAmelCase_  :				Union[str, Any]      =      [
				    self._get_input_mel(_lowercase			,			max_length if max_length else self.nb_max_samples			,			_lowercase			,			_lowercase		)
				    for waveform in raw_speech
				]

				UpperCAmelCase_  :				Tuple      =      []
				UpperCAmelCase_  :				Any      =      []
				for mel, longer in padded_inputs:
							input_mel.append(_lowercase		)
							is_longer.append(_lowercase		)

				if truncation == "fusion" and sum(_lowercase		) == 0:
							# if no audio is longer than 10s, then randomly select one audio to be longer
							UpperCAmelCase_  :				Dict      =      np.random.randint(0			,			len(_lowercase		)		)
							UpperCAmelCase_  :				List[Any]      =      True

				if isinstance(input_mel[0]			,			_lowercase		):
							UpperCAmelCase_  :				Any      =      [np.asarray(_lowercase			,			dtype=np.floataa		) for feature in input_mel]

				# is_longer is a list of bool
				UpperCAmelCase_  :				Any      =      [[longer] for longer in is_longer]

				UpperCAmelCase_  :				str      =      {"input_features": input_mel, "is_longer": is_longer}
				UpperCAmelCase_  :				Optional[int]      =      BatchFeature(_lowercase		)

				if return_tensors is not None:
							UpperCAmelCase_  :				List[str]      =      input_features.convert_to_tensors(_lowercase		)

				return input_features











"""simple docstring"""
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference







class     UpperCamelCase_ (__A       ,		unittest.TestCase  ):
	__magic_name__			   =       ShapEPipeline
	__magic_name__			   =       ['''prompt''']
	__magic_name__			   =       ['''prompt''']
	__magic_name__			   =       [
	    '''num_images_per_prompt''',
	    '''num_inference_steps''',
	    '''generator''',
	    '''latents''',
	    '''guidance_scale''',
	    '''frame_size''',
	    '''output_type''',
	    '''return_dict''',
	]
	__magic_name__			   =       False





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    Dict:
				return 32





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]		)		->    Tuple:
				return 32





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    Optional[int]:
				return self.time_input_dim * 4





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    List[Any]:
				return 8





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Dict		)		->    int:
				UpperCAmelCase_  :				str      =      CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)
				return tokenizer





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    List[str]:
				torch.manual_seed(0		)
				UpperCAmelCase_  :				Dict      =      CLIPTextConfig(
				    bos_token_id=0			,			eos_token_id=2			,			hidden_size=self.text_embedder_hidden_size			,			projection_dim=self.text_embedder_hidden_size			,			intermediate_size=37			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			pad_token_id=1			,			vocab_size=1_000			,			)
				return CLIPTextModelWithProjection(lowerCAmelCase_		)





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Dict		)		->    Optional[Any]:
				torch.manual_seed(0		)

				UpperCAmelCase_  :				str      =      {
				    "num_attention_heads": 2,
				    "attention_head_dim": 16,
				    "embedding_dim": self.time_input_dim,
				    "num_embeddings": 32,
				    "embedding_proj_dim": self.text_embedder_hidden_size,
				    "time_embed_dim": self.time_embed_dim,
				    "num_layers": 1,
				    "clip_embed_dim": self.time_input_dim * 2,
				    "additional_embeddings": 0,
				    "time_embed_act_fn": "gelu",
				    "norm_in_type": "layer",
				    "encoder_hid_proj_type": None,
				    "added_emb_type": None,
				}

				UpperCAmelCase_  :				List[Any]      =      PriorTransformer(**lowerCAmelCase_		)
				return model





	@property
	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    int:
				torch.manual_seed(0		)

				UpperCAmelCase_  :				Any      =      {
				    "param_shapes": (
				        (self.renderer_dim, 93),
				        (self.renderer_dim, 8),
				        (self.renderer_dim, 8),
				        (self.renderer_dim, 8),
				    ),
				    "d_latent": self.time_input_dim,
				    "d_hidden": self.renderer_dim,
				    "n_output": 12,
				    "background": (
				        0.1,
				        0.1,
				        0.1,
				    ),
				}
				UpperCAmelCase_  :				Tuple      =      ShapERenderer(**lowerCAmelCase_		)
				return model





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]		)		->    Tuple:
				UpperCAmelCase_  :				str      =      self.dummy_prior
				UpperCAmelCase_  :				Dict      =      self.dummy_text_encoder
				UpperCAmelCase_  :				Any      =      self.dummy_tokenizer
				UpperCAmelCase_  :				int      =      self.dummy_renderer

				UpperCAmelCase_  :				Optional[int]      =      HeunDiscreteScheduler(
				    beta_schedule="exp"			,			num_train_timesteps=1_024			,			prediction_type="sample"			,			use_karras_sigmas=lowerCAmelCase_			,			clip_sample=lowerCAmelCase_			,			clip_sample_range=1.0			,			)
				UpperCAmelCase_  :				str      =      {
				    "prior": prior,
				    "text_encoder": text_encoder,
				    "tokenizer": tokenizer,
				    "renderer": renderer,
				    "scheduler": scheduler,
				}

				return components





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any			,			lowerCAmelCase_      :				int			,			lowerCAmelCase_      :				Any=0		)		->    Union[str, Any]:
				if str(lowerCAmelCase_		).startswith("mps"		):
							UpperCAmelCase_  :				str      =      torch.manual_seed(lowerCAmelCase_		)
				else:
							UpperCAmelCase_  :				Union[str, Any]      =      torch.Generator(device=lowerCAmelCase_		).manual_seed(lowerCAmelCase_		)
				UpperCAmelCase_  :				List[Any]      =      {
				    "prompt": "horse",
				    "generator": generator,
				    "num_inference_steps": 1,
				    "frame_size": 32,
				    "output_type": "np",
				}
				return inputs





	def   _SCREAMING_SNAKE_CASE   (   self      :				str		)		->    List[Any]:
				UpperCAmelCase_  :				Any      =      "cpu"

				UpperCAmelCase_  :				Union[str, Any]      =      self.get_dummy_components()

				UpperCAmelCase_  :				Tuple      =      self.pipeline_class(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Union[str, Any]      =      pipe.to(lowerCAmelCase_		)

				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				int      =      pipe(**self.get_dummy_inputs(lowerCAmelCase_		)		)
				UpperCAmelCase_  :				Dict      =      output.images[0]
				UpperCAmelCase_  :				Any      =      image[0, -3:, -3:, -1]

				assert image.shape == (20, 32, 32, 3)

				UpperCAmelCase_  :				Optional[int]      =      np.array(
				    [
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				        0.0_0_0_3_9_2_1_6,
				    ]		)

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





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]		)		->    Optional[Any]:
				# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
				self._test_inference_batch_consistent(batch_sizes=[1, 2]		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				int		)		->    List[str]:
				UpperCAmelCase_  :				Optional[Any]      =      torch_device == "cpu"
				UpperCAmelCase_  :				Dict      =      True

				self._test_inference_batch_single_identical(
				    batch_size=2			,			test_max_difference=lowerCAmelCase_			,			relax_max_difference=lowerCAmelCase_			,			)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any		)		->    List[str]:
				UpperCAmelCase_  :				int      =      self.get_dummy_components()
				UpperCAmelCase_  :				Tuple      =      self.pipeline_class(**lowerCAmelCase_		)
				UpperCAmelCase_  :				List[str]      =      pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				List[str]      =      1
				UpperCAmelCase_  :				List[Any]      =      2

				UpperCAmelCase_  :				int      =      self.get_dummy_inputs(lowerCAmelCase_		)

				for key in inputs.keys():
							if key in self.batch_params:
										UpperCAmelCase_  :				int      =      batch_size * [inputs[key]]

				UpperCAmelCase_  :				int      =      pipe(**lowerCAmelCase_			,			num_images_per_prompt=lowerCAmelCase_		)[0]

				assert images.shape[0] == batch_size * num_images_per_prompt







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





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    Any:
				# clean up the VRAM after each test
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()





	def   _SCREAMING_SNAKE_CASE   (   self      :				str		)		->    Union[str, Any]:
				UpperCAmelCase_  :				Any      =      load_numpy(
				    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
				    "/shap_e/test_shap_e_np_out.npy"		)
				UpperCAmelCase_  :				str      =      ShapEPipeline.from_pretrained("openai/shap-e"		)
				UpperCAmelCase_  :				Union[str, Any]      =      pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				Union[str, Any]      =      torch.Generator(device=lowerCAmelCase_		).manual_seed(0		)

				UpperCAmelCase_  :				Optional[int]      =      pipe(
				    "a shark"			,			generator=lowerCAmelCase_			,			guidance_scale=1_5.0			,			num_inference_steps=64			,			frame_size=64			,			output_type="np"			,			).images[0]

				assert images.shape == (20, 64, 64, 3)

				assert_mean_pixel_difference(lowerCAmelCase_			,			lowerCAmelCase_		)



