


from scipy.stats import pearsonr

import datasets


_lowerCAmelCase  :		Tuple		     =	'\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n'


_lowerCAmelCase  :		Any		     =	'\nArgs:\n    predictions (`list` of `int`): Predicted class labels, as returned by a model.\n    references (`list` of `int`): Ground truth labels.\n    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n    pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n    p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n    Example 1-A simple example using only predictions and references.\n        >>> pearsonr_metric = datasets.load_metric("pearsonr")\n        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n        >>> print(round(results[\'pearsonr\'], 2))\n        -0.74\n\n    Example 2-The same as Example 1, but that also returns the `p-value`.\n        >>> pearsonr_metric = datasets.load_metric("pearsonr")\n        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n        >>> print(sorted(list(results.keys())))\n        [\'p-value\', \'pearsonr\']\n        >>> print(round(results[\'pearsonr\'], 2))\n        -0.74\n        >>> print(round(results[\'p-value\'], 2))\n        0.15\n'


_lowerCAmelCase  :		Tuple		     =	'\n@article{2020SciPy-NMeth,\nauthor  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n      Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n      Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n      Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n      Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n      Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n      Kern, Robert and Larson, Eric and Carey, C J and\n      Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n      {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n      Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n      Harris, Charles R. and Archibald, Anne M. and\n      Ribeiro, Antonio H. and Pedregosa, Fabian and\n      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n      Computing in Python}},\njournal = {Nature Methods},\nyear    = {2020},\nvolume  = {17},\npages   = {261--272},\nadsurl  = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n'



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,				_KWARGS_DESCRIPTION							)
class  lowerCAmelCase				(				datasets.Metric							):



	def 				lowercase     (					self    ):
		return datasets.MetricInfo(
		    description=_DESCRIPTION ,					citation=_CITATION ,					inputs_description=_KWARGS_DESCRIPTION ,					features=datasets.Features(
		        {
		            'predictions': datasets.Value('float'    ),
		            'references': datasets.Value('float'    ),
		        }    ) ,					reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'] ,					)



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__=False    ):





		if return_pvalue:
			lowerCAmelCase					:    List[Any]      							= pearsonr(UpperCamelCase__ ,					UpperCamelCase__    )
			return {"pearsonr": results[0], "p-value": results[1]}
		else:
			return {"pearsonr": float(pearsonr(UpperCamelCase__ ,					UpperCamelCase__    )[0]    )}




'''simple docstring'''




import inspect
from typing import Callable, List, Optional, Union

import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextModel,
    CLIPTokenizer,
    WhisperForConditionalGeneration,
    WhisperProcessor,
)

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import logging


_lowerCAmelCase  :		Dict		     =	logging.get_logger(__name__)  # pylint: disable=invalid-name



class  lowerCAmelCase				(				a							):



	def __init__(					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		super().__init__()

		if safety_checker is None:
			logger.warning(
			    f"You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure"
			    ' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered'
			    ' results in services or applications open to the public. Both the diffusers team and Hugging Face'
			    ' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling'
			    ' it only for use-cases that involve analyzing network behavior or auditing its results. For more'
			    ' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .'    )

		self.register_modules(
		    speech_model=snake_case__ ,					speech_processor=snake_case__ ,					vae=snake_case__ ,					text_encoder=snake_case__ ,					tokenizer=snake_case__ ,					unet=snake_case__ ,					scheduler=snake_case__ ,					feature_extractor=snake_case__ ,					)



	def 				lowercase     (					self ,					snake_case__ = "auto"    ):
		if slice_size == "auto":
			lowerCAmelCase					:    Union[str, Any]      							= self.unet.config.attention_head_dim // 2
		self.unet.set_attention_slice(snake_case__    )



	def 				lowercase     (					self    ):
		self.enable_attention_slicing(snake_case__    )



	@torch.no_grad()
	def __call__(					self ,					snake_case__ ,					snake_case__=1_6000 ,					snake_case__ = 512 ,					snake_case__ = 512 ,					snake_case__ = 50 ,					snake_case__ = 7.5 ,					snake_case__ = None ,					snake_case__ = 1 ,					snake_case__ = 0.0 ,					snake_case__ = None ,					snake_case__ = None ,					snake_case__ = "pil" ,					snake_case__ = True ,					snake_case__ = None ,					snake_case__ = 1 ,					**snake_case__ ,					):
		lowerCAmelCase					:    List[str]      							= self.speech_processor.feature_extractor(
		    snake_case__ ,					return_tensors='pt' ,					sampling_rate=snake_case__    ).input_features.to(self.device    )
		lowerCAmelCase					:    Optional[Any]      							= self.speech_model.generate(snake_case__ ,					max_length=48_0000    )

		lowerCAmelCase					:    str      							= self.speech_processor.tokenizer.batch_decode(snake_case__ ,					skip_special_tokens=snake_case__ ,					normalize=snake_case__    )[
		    0
		]

		if isinstance(snake_case__ ,					snake_case__    ):
			lowerCAmelCase					:    Optional[int]      							= 1
		elif isinstance(snake_case__ ,					snake_case__    ):
			lowerCAmelCase					:    Optional[int]      							= len(snake_case__    )
		else:
			raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(snake_case__    )}"    )

		if height % 8 != 0 or width % 8 != 0:
			raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}."    )

		if (callback_steps is None) or (
		    callback_steps is not None and (not isinstance(snake_case__ ,					snake_case__    ) or callback_steps <= 0)
		):
			raise ValueError(
			    f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
			    f" {type(snake_case__    )}."    )

		# get prompt text embeddings
		lowerCAmelCase					:    str      							= self.tokenizer(
		    snake_case__ ,					padding='max_length' ,					max_length=self.tokenizer.model_max_length ,					return_tensors='pt' ,					)
		lowerCAmelCase					:    Tuple      							= text_inputs.input_ids

		if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
			lowerCAmelCase					:    str      							= self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]    )
			logger.warning(
			    'The following part of your input was truncated because CLIP can only handle sequences up to'
			    f" {self.tokenizer.model_max_length} tokens: {removed_text}"    )
			lowerCAmelCase					:    Union[str, Any]      							= text_input_ids[:, : self.tokenizer.model_max_length]
		lowerCAmelCase					:    Union[str, Any]      							= self.text_encoder(text_input_ids.to(self.device    )    )[0]

		# duplicate text embeddings for each generation per prompt, using mps friendly method
		lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					:    int      							= text_embeddings.shape
		lowerCAmelCase					:    Any      							= text_embeddings.repeat(1 ,					snake_case__ ,					1    )
		lowerCAmelCase					:    Optional[int]      							= text_embeddings.view(bs_embed * num_images_per_prompt ,					snake_case__ ,					-1    )

		# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
		# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
		# corresponds to doing no classifier free guidance.
		lowerCAmelCase					:    List[str]      							= guidance_scale > 1.0
		# get unconditional embeddings for classifier free guidance
		if do_classifier_free_guidance:
			lowerCAmelCase					:    List[str]
			if negative_prompt is None:
				lowerCAmelCase					:    Any      							= [''] * batch_size
			elif type(snake_case__    ) is not type(snake_case__    ):
				raise TypeError(
				    f"`negative_prompt` should be the same type to `prompt`, but got {type(snake_case__    )} !="
				    f" {type(snake_case__    )}."    )
			elif isinstance(snake_case__ ,					snake_case__    ):
				lowerCAmelCase					:    Union[str, Any]      							= [negative_prompt]
			elif batch_size != len(snake_case__    ):
				raise ValueError(
				    f"`negative_prompt`: {negative_prompt} has batch size {len(snake_case__    )}, but `prompt`:"
				    f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
				    ' the batch size of `prompt`.'    )
			else:
				lowerCAmelCase					:    Dict      							= negative_prompt

			lowerCAmelCase					:    Optional[int]      							= text_input_ids.shape[-1]
			lowerCAmelCase					:    int      							= self.tokenizer(
			    snake_case__ ,					padding='max_length' ,					max_length=snake_case__ ,					truncation=snake_case__ ,					return_tensors='pt' ,					)
			lowerCAmelCase					:    Union[str, Any]      							= self.text_encoder(uncond_input.input_ids.to(self.device    )    )[0]

			# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
			lowerCAmelCase					:    List[Any]      							= uncond_embeddings.shape[1]
			lowerCAmelCase					:    List[str]      							= uncond_embeddings.repeat(1 ,					snake_case__ ,					1    )
			lowerCAmelCase					:    Optional[Any]      							= uncond_embeddings.view(batch_size * num_images_per_prompt ,					snake_case__ ,					-1    )

			# For classifier free guidance, we need to do two forward passes.
			# Here we concatenate the unconditional and text embeddings into a single batch
			# to avoid doing two forward passes
			lowerCAmelCase					:    List[str]      							= torch.cat([uncond_embeddings, text_embeddings]    )

		# get the initial random noise unless the user supplied it

		# Unlike in other pipelines, latents need to be generated in the target device
		# for 1-to-1 results reproducibility with the CompVis implementation.
		# However this currently doesn't work in `mps`.
		lowerCAmelCase					:    Union[str, Any]      							= (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
		lowerCAmelCase					:    Dict      							= text_embeddings.dtype
		if latents is None:
			if self.device.type == "mps":
				# randn does not exist on mps
				lowerCAmelCase					:    str      							= torch.randn(snake_case__ ,					generator=snake_case__ ,					device='cpu' ,					dtype=snake_case__    ).to(
				    self.device    )
			else:
				lowerCAmelCase					:    Tuple      							= torch.randn(snake_case__ ,					generator=snake_case__ ,					device=self.device ,					dtype=snake_case__    )
		else:
			if latents.shape != latents_shape:
				raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}"    )
			lowerCAmelCase					:    str      							= latents.to(self.device    )

		# set timesteps
		self.scheduler.set_timesteps(snake_case__    )

		# Some schedulers like PNDM have timesteps as arrays
		# It's more optimized to move all timesteps to correct device beforehand
		lowerCAmelCase					:    Union[str, Any]      							= self.scheduler.timesteps.to(self.device    )

		# scale the initial noise by the standard deviation required by the scheduler
		lowerCAmelCase					:    Any      							= latents * self.scheduler.init_noise_sigma

		# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
		# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
		# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
		# and should be between [0, 1]
		lowerCAmelCase					:    Tuple      							= 'eta' in set(inspect.signature(self.scheduler.step    ).parameters.keys()    )
		lowerCAmelCase					:    Union[str, Any]      							= {}
		if accepts_eta:
			lowerCAmelCase					:    int      							= eta

		for i, t in enumerate(self.progress_bar(snake_case__    )    ):
			# expand the latents if we are doing classifier free guidance
			lowerCAmelCase					:    Dict      							= torch.cat([latents] * 2    ) if do_classifier_free_guidance else latents
			lowerCAmelCase					:    Tuple      							= self.scheduler.scale_model_input(snake_case__ ,					snake_case__    )

			# predict the noise residual
			lowerCAmelCase					:    List[str]      							= self.unet(snake_case__ ,					snake_case__ ,					encoder_hidden_states=snake_case__    ).sample

			# perform guidance
			if do_classifier_free_guidance:
				lowerCAmelCase							,   lowerCAmelCase					:    Dict      							= noise_pred.chunk(2    )
				lowerCAmelCase					:    Tuple      							= noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

			# compute the previous noisy sample x_t -> x_t-1
			lowerCAmelCase					:    int      							= self.scheduler.step(snake_case__ ,					snake_case__ ,					snake_case__ ,					**snake_case__    ).prev_sample

			# call the callback, if provided
			if callback is not None and i % callback_steps == 0:
				callback(snake_case__ ,					snake_case__ ,					snake_case__    )

		lowerCAmelCase					:    List[Any]      							= 1 / 0.1_8_2_1_5 * latents
		lowerCAmelCase					:    Dict      							= self.vae.decode(snake_case__    ).sample

		lowerCAmelCase					:    List[Any]      							= (image / 2 + 0.5).clamp(0 ,					1    )

		# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
		lowerCAmelCase					:    Union[str, Any]      							= image.cpu().permute(0 ,					2 ,					3 ,					1    ).float().numpy()

		if output_type == "pil":
			lowerCAmelCase					:    Dict      							= self.numpy_to_pil(snake_case__    )

		if not return_dict:
			return image

		return StableDiffusionPipelineOutput(images=snake_case__ ,					nsfw_content_detected=snake_case__    )
