



import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionModelEditingPipeline,
    UNetaDConditionModel,
)
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 TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


@skip_mps
class 	lowerCamelCase__ (		_a				,					_a				,					_a				,					unittest.TestCase   ):
						_lowerCAmelCase								= StableDiffusionModelEditingPipeline
						_lowerCAmelCase								= TEXT_TO_IMAGE_PARAMS
						_lowerCAmelCase								= TEXT_TO_IMAGE_BATCH_PARAMS
						_lowerCAmelCase								= TEXT_TO_IMAGE_IMAGE_PARAMS
						_lowerCAmelCase								= TEXT_TO_IMAGE_IMAGE_PARAMS


						def   _lowerCamelCase			( self						:  List[Any]			):
													torch.manual_seed(0			)
													a__:       Union[str, Any]											=UNetaDConditionModel(
													    block_out_channels=(3_2, 6_4)      ,       layers_per_block=2      ,       sample_size=3_2      ,       in_channels=4      ,       out_channels=4      ,       down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")      ,       up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")      ,       cross_attention_dim=3_2      ,       )
													a__:       Optional[Any]											=DDIMScheduler()
													torch.manual_seed(0			)
													a__:       Optional[int]											=AutoencoderKL(
													    block_out_channels=[3_2, 6_4]      ,       in_channels=3      ,       out_channels=3      ,       down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]      ,       up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]      ,       latent_channels=4      ,       )
													torch.manual_seed(0			)
													a__:       List[Any]											=CLIPTextConfig(
													    bos_token_id=0      ,       eos_token_id=2      ,       hidden_size=3_2      ,       intermediate_size=3_7      ,       layer_norm_eps=1e-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       pad_token_id=1      ,       vocab_size=1_0_0_0      ,       )
													a__:       Union[str, Any]											=CLIPTextModel(_a			)
													a__:       List[Any]											=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)

													a__:       Any											={
													    "unet": unet,
													    "scheduler": scheduler,
													    "vae": vae,
													    "text_encoder": text_encoder,
													    "tokenizer": tokenizer,
													    "safety_checker": None,
													    "feature_extractor": None,
													}
													return components


						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  List[Any]      ,       _a						:  Tuple=0			):
													a__:       Dict											=torch.manual_seed(_a			)
													a__:       int											={
													    "prompt": "A field of roses",
													    "generator": generator,
													    # Setting height and width to None to prevent OOMs on CPU.
													    "height": None,
													    "width": None,
													    "num_inference_steps": 2,
													    "guidance_scale": 6.0,
													    "output_type": "numpy",
													}
													return inputs


						def   _lowerCamelCase			( self						:  Optional[int]			):
													a__:       Union[str, Any]											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       Optional[int]											=self.get_dummy_components()
													a__:       List[Any]											=StableDiffusionModelEditingPipeline(**_a			)
													a__:       Optional[int]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       Dict											=self.get_dummy_inputs(_a			)
													a__:       Any											=sd_pipe(**_a			).images
													a__:       List[str]											=image[0, -3:, -3:, -1]
													assert image.shape == (1, 6_4, 6_4, 3)

													a__:       Union[str, Any]											=np.array([0.4_7_5_5, 0.5_1_3_2, 0.4_9_7_6, 0.3_9_0_4, 0.3_5_5_4, 0.4_7_6_5, 0.5_1_3_9, 0.5_1_5_8, 0.4_8_8_9]			)

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


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Any											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       Optional[int]											=self.get_dummy_components()
													a__:       Optional[int]											=StableDiffusionModelEditingPipeline(**_a			)
													a__:       str											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       Tuple											=self.get_dummy_inputs(_a			)
													a__:       Any											="french fries"
													a__:       List[Any]											=sd_pipe(**_a      ,       negative_prompt=_a			)
													a__:       Dict											=output.images
													a__:       List[str]											=image[0, -3:, -3:, -1]

													assert image.shape == (1, 6_4, 6_4, 3)

													a__:       Any											=np.array([0.4_9_9_2, 0.5_1_0_1, 0.5_0_0_4, 0.3_9_4_9, 0.3_6_0_4, 0.4_7_3_5, 0.5_2_1_6, 0.5_2_0_4, 0.4_9_1_3]			)

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


						def   _lowerCamelCase			( self						:  int			):
													a__:       int											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       str											=self.get_dummy_components()
													a__:       Optional[Any]											=EulerAncestralDiscreteScheduler(
													    beta_start=0.0_0_0_8_5      ,       beta_end=0.0_1_2      ,       beta_schedule="scaled_linear"			)
													a__:       Optional[int]											=StableDiffusionModelEditingPipeline(**_a			)
													a__:       Optional[Any]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       Tuple											=self.get_dummy_inputs(_a			)
													a__:       Dict											=sd_pipe(**_a			).images
													a__:       Any											=image[0, -3:, -3:, -1]

													assert image.shape == (1, 6_4, 6_4, 3)

													a__:       Optional[Any]											=np.array([0.4_7_4_7, 0.5_3_7_2, 0.4_7_7_9, 0.4_9_8_2, 0.5_5_4_3, 0.4_8_1_6, 0.5_2_3_8, 0.4_9_0_4, 0.5_0_2_7]			)

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


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													a__:       str											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       str											=self.get_dummy_components()
													a__:       str											=PNDMScheduler()
													a__:       Union[str, Any]											=StableDiffusionModelEditingPipeline(**_a			)
													a__:       Optional[int]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       Tuple											=self.get_dummy_inputs(_a			)
													# the pipeline does not expect pndm so test if it raises error.
													with self.assertRaises(_a			):
																				a__:       List[str]											=sd_pipe(**_a			).images


						def   _lowerCamelCase			( self						:  int			):
													super().test_inference_batch_single_identical(expected_max_diff=5e-3			)







						def   _lowerCamelCase			( self						:  Dict			):
													super().test_attention_slicing_forward_pass(expected_max_diff=5e-3			)


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


						def   _lowerCamelCase			( self						:  Tuple			):
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  int=0			):
													a__:       Any											=torch.manual_seed(_a			)
													a__:       List[Any]											={
													    "prompt": "A field of roses",
													    "generator": generator,
													    "num_inference_steps": 3,
													    "guidance_scale": 7.5,
													    "output_type": "numpy",
													}
													return inputs


						def   _lowerCamelCase			( self						:  Optional[Any]			):
													a__:       Optional[Any]											="CompVis/stable-diffusion-v1-4"
													a__:       int											=StableDiffusionModelEditingPipeline.from_pretrained(_a      ,       safety_checker=_a			)
													pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)
													pipe.enable_attention_slicing()

													a__:       Optional[Any]											=self.get_inputs()
													a__:       Union[str, Any]											=pipe(**_a			).images
													a__:       Any											=image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

													a__:       List[Any]											=np.array(
													    [0.6_7_4_9_4_9_6, 0.6_3_8_6_4_5_3, 0.5_1_4_4_3_2_6_7, 0.6_6_0_9_4_9_0_5, 0.6_1_9_2_1_2_1_5, 0.5_4_9_1_3_3_2, 0.5_7_4_4_4_1_7, 0.5_8_0_7_5_1_0_6, 0.5_1_7_4_6_5_8]			)

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

													# make sure image changes after editing
													pipe.edit_model("A pack of roses"      ,       "A pack of blue roses"			)

													a__:       Dict											=pipe(**_a			).images
													a__:       Any											=image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

													assert np.abs(expected_slice - image_slice			).max() > 1e-1







						def   _lowerCamelCase			( self						:  Tuple			):
													torch.cuda.empty_cache()
													torch.cuda.reset_max_memory_allocated()
													torch.cuda.reset_peak_memory_stats()

													a__:       Any											="CompVis/stable-diffusion-v1-4"
													a__:       Any											=DDIMScheduler.from_pretrained(_a      ,       subfolder="scheduler"			)
													a__:       Optional[int]											=StableDiffusionModelEditingPipeline.from_pretrained(
													    _a      ,       scheduler=_a      ,       safety_checker=_a			)
													a__:       int											=pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)
													pipe.enable_attention_slicing(1			)
													pipe.enable_sequential_cpu_offload()

													a__:       Union[str, Any]											=self.get_inputs()
													a__:       Tuple											=pipe(**_a			)

													a__:       int											=torch.cuda.max_memory_allocated()
													# make sure that less than 4.4 GB is allocated
													assert mem_bytes < 4.4 * 1_0**9










import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
		from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

__UpperCAmelCase						  =     logging.get_logger(__name__)





def 						__lowerCamelCase       (      __magic_name__	:  bytes     , __magic_name__	:  int						):
							a__:       Optional[int]											=F"{sampling_rate}"
							a__:       Union[str, Any]											="1"
							a__:       List[str]											="f32le"
							a__:       Dict											=[
							    "ffmpeg",
							    "-i",
							    "pipe:0",
							    "-ac",
							    ac,
							    "-ar",
							    ar,
							    "-f",
							    format_for_conversion,
							    "-hide_banner",
							    "-loglevel",
							    "quiet",
							    "pipe:1",
							]

							try:
														a__:       List[Any]											=subprocess.Popen(__magic_name__     , stdin=subprocess.PIPE     , stdout=subprocess.PIPE						)
							except FileNotFoundError:
														raise ValueError("ffmpeg was not found but is required to load audio files from filename"						)
							a__:       int											=ffmpeg_process.communicate(__magic_name__						)
							a__:       List[str]											=output_stream[0]

							a__:       str											=np.frombuffer(__magic_name__     , np.floataa						)
							if audio.shape[0] == 0:
														raise ValueError("Malformed soundfile"						)
							return audio







@add_end_docstrings(_a   )
class 	lowerCamelCase__ (		_a   ):


						def __init__( self						:  Union[str, Any]      ,       *_a						:  Dict      ,       **_a						:  Tuple			):
													# Default, might be overriden by the model.config.
													a__:       List[Any]											=5
													super().__init__(*_a      ,       **_a			)

													if self.framework != "pt":
																				raise ValueError(F"The {self.__class__} is only available in PyTorch."			)

													self.check_model_type(_a			)


						def __call__( self						:  Tuple      ,       _a						:  Union[np.ndarray, bytes, str]      ,       **_a						:  Union[str, Any]      ,       ):
													return super().__call__(_a      ,       **_a			)


						def   _lowerCamelCase			( self						:  int      ,       _a						:  Tuple=None      ,       **_a						:  int			):
													# No parameters on this pipeline right now
													a__:       Optional[int]											={}
													if top_k is not None:
																				if top_k > self.model.config.num_labels:
																											a__:       Union[str, Any]											=self.model.config.num_labels
																				a__:       Union[str, Any]											=top_k
													return {}, {}, postprocess_params


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  Union[str, Any]			):
													if isinstance(_a      ,       _a			):
																				if inputs.startswith("http://"			) or inputs.startswith("https://"			):
																											# We need to actually check for a real protocol, otherwise it's impossible to use a local file
																											# like http_huggingface_co.png
																											a__:       List[str]											=requests.get(_a			).content
																				else:
																											with open(_a      ,       "rb"			) as f:
																																		a__:       Dict											=f.read()

													if isinstance(_a      ,       _a			):
																				a__:       Tuple											=ffmpeg_read(_a      ,       self.feature_extractor.sampling_rate			)

													if isinstance(_a      ,       _a			):
																				# Accepting `"array"` which is the key defined in `datasets` for
																				# better integration
																				if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
																											raise ValueError(
																											    "When passing a dictionary to AudioClassificationPipeline, the dict needs to contain a "
																											    "\"raw\" key containing the numpy array representing the audio and a \"sampling_rate\" key, "
																											    "containing the sampling_rate associated with that array"			)

																				a__:       Tuple											=inputs.pop("raw"      ,       _a			)
																				if _inputs is None:
																											# Remove path which will not be used from `datasets`.
																											inputs.pop("path"      ,       _a			)
																											a__:       int											=inputs.pop("array"      ,       _a			)
																				a__:       Optional[Any]											=inputs.pop("sampling_rate"			)
																				a__:       Optional[int]											=_inputs
																				if in_sampling_rate != self.feature_extractor.sampling_rate:
																											import torch

																											if is_torchaudio_available():
																																		from torchaudio import functional as F
																											else:
																																		raise ImportError(
																																		    "torchaudio is required to resample audio samples in AudioClassificationPipeline. "
																																		    "The torchaudio package can be installed through: `pip install torchaudio`."			)

																											a__:       Union[str, Any]											=F.resample(
																											    torch.from_numpy(_a			)      ,       _a      ,       self.feature_extractor.sampling_rate			).numpy()

													if not isinstance(_a      ,       np.ndarray			):
																				raise ValueError("We expect a numpy ndarray as input"			)
													if len(inputs.shape			) != 1:
																				raise ValueError("We expect a single channel audio input for AudioClassificationPipeline"			)

													a__:       Union[str, Any]											=self.feature_extractor(
													    _a      ,       sampling_rate=self.feature_extractor.sampling_rate      ,       return_tensors="pt"			)
													return processed


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  Tuple			):
													a__:       str											=self.model(**_a			)
													return model_outputs







						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Tuple      ,       _a						:  Tuple=5			):
													a__:       Tuple											=model_outputs.logits[0].softmax(-1			)
													a__							,			a__:       Any											=probs.topk(_a			)

													a__:       Union[str, Any]											=scores.tolist()
													a__:       Dict											=ids.tolist()

													a__:       Optional[int]											=[{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_a      ,       _a			)]

													return labels





