


from typing import Any, Callable, Dict, List, Optional, Union

import PIL.Image
import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionImgaImgPipeline,
    StableDiffusionInpaintPipelineLegacy,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.configuration_utils import FrozenDict
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import deprecate, logging


__UpperCAmelCase			:			str			      =					logging.get_logger(__name__)  # pylint: disable=invalid-name



class  UpperCAmelCase_						(				_a):
       '''simple docstring'''
       __UpperCamelCase   :  Tuple				=       ["safety_checker", "feature_extractor"]






       def __init__(  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE = True			,       ):




              """simple docstring"""




              super().__init__()
              if hasattr(scheduler.config			,       '''steps_offset'''    ) and scheduler.config.steps_offset != 1:
                     UpperCamelCase     :						Optional[int] 						=		(
                         f"""The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"""
                         f""" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure """
                         '''to update the config accordingly as leaving `steps_offset` might led to incorrect results'''
                         ''' in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,'''
                         ''' it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`'''
                         ''' file'''
                     )
                     deprecate('''steps_offset!=1'''			,       '''1.0.0'''			,       __SCREAMING_SNAKE_CASE			,       standard_warn=__SCREAMING_SNAKE_CASE    )
                     UpperCamelCase     :						Any 						=		dict(scheduler.config    )
                     UpperCamelCase     :						List[Any] 						=		1
                     UpperCamelCase     :						Dict 						=		FrozenDict(__SCREAMING_SNAKE_CASE    )

              self.register_modules(
                  vae=__SCREAMING_SNAKE_CASE			,       text_encoder=__SCREAMING_SNAKE_CASE			,       tokenizer=__SCREAMING_SNAKE_CASE			,       unet=__SCREAMING_SNAKE_CASE			,       scheduler=__SCREAMING_SNAKE_CASE			,       safety_checker=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE			,       )
              self.register_to_config(requires_safety_checker=__SCREAMING_SNAKE_CASE    )






       @property
       def 			_lowercase (  self    ):




              """simple docstring"""




              return {k: getattr(self			,       __SCREAMING_SNAKE_CASE    ) for k in self.config.keys() if not k.startswith('''_'''    )}






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE = "auto"    ):




              """simple docstring"""




              if slice_size == "auto":
                     # half the attention head size is usually a good trade-off between
                     # speed and memory
                     UpperCamelCase     :						Optional[Any] 						=		self.unet.config.attention_head_dim // 2
              self.unet.set_attention_slice(__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              self.enable_attention_slicing(__SCREAMING_SNAKE_CASE    )






       @torch.no_grad()
       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE = 0.8			,       __SCREAMING_SNAKE_CASE = 50			,       __SCREAMING_SNAKE_CASE = 7.5			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       __SCREAMING_SNAKE_CASE = 0.0			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = "pil"			,       __SCREAMING_SNAKE_CASE = True			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       ):




              """simple docstring"""




              return StableDiffusionInpaintPipelineLegacy(**self.components    )(
                  prompt=__SCREAMING_SNAKE_CASE			,       image=__SCREAMING_SNAKE_CASE			,       mask_image=__SCREAMING_SNAKE_CASE			,       strength=__SCREAMING_SNAKE_CASE			,       num_inference_steps=__SCREAMING_SNAKE_CASE			,       guidance_scale=__SCREAMING_SNAKE_CASE			,       negative_prompt=__SCREAMING_SNAKE_CASE			,       num_images_per_prompt=__SCREAMING_SNAKE_CASE			,       eta=__SCREAMING_SNAKE_CASE			,       generator=__SCREAMING_SNAKE_CASE			,       output_type=__SCREAMING_SNAKE_CASE			,       return_dict=__SCREAMING_SNAKE_CASE			,       callback=__SCREAMING_SNAKE_CASE			,       )






       @torch.no_grad()
       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE = 0.8			,       __SCREAMING_SNAKE_CASE = 50			,       __SCREAMING_SNAKE_CASE = 7.5			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       __SCREAMING_SNAKE_CASE = 0.0			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = "pil"			,       __SCREAMING_SNAKE_CASE = True			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       **__SCREAMING_SNAKE_CASE			,       ):




              """simple docstring"""




              return StableDiffusionImgaImgPipeline(**self.components    )(
                  prompt=__SCREAMING_SNAKE_CASE			,       image=__SCREAMING_SNAKE_CASE			,       strength=__SCREAMING_SNAKE_CASE			,       num_inference_steps=__SCREAMING_SNAKE_CASE			,       guidance_scale=__SCREAMING_SNAKE_CASE			,       negative_prompt=__SCREAMING_SNAKE_CASE			,       num_images_per_prompt=__SCREAMING_SNAKE_CASE			,       eta=__SCREAMING_SNAKE_CASE			,       generator=__SCREAMING_SNAKE_CASE			,       output_type=__SCREAMING_SNAKE_CASE			,       return_dict=__SCREAMING_SNAKE_CASE			,       callback=__SCREAMING_SNAKE_CASE			,       callback_steps=__SCREAMING_SNAKE_CASE			,       )






       @torch.no_grad()
       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE = 512			,       __SCREAMING_SNAKE_CASE = 512			,       __SCREAMING_SNAKE_CASE = 50			,       __SCREAMING_SNAKE_CASE = 7.5			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       __SCREAMING_SNAKE_CASE = 0.0			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = "pil"			,       __SCREAMING_SNAKE_CASE = True			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = 1			,       ):




              """simple docstring"""




              return StableDiffusionPipeline(**self.components    )(
                  prompt=__SCREAMING_SNAKE_CASE			,       height=__SCREAMING_SNAKE_CASE			,       width=__SCREAMING_SNAKE_CASE			,       num_inference_steps=__SCREAMING_SNAKE_CASE			,       guidance_scale=__SCREAMING_SNAKE_CASE			,       negative_prompt=__SCREAMING_SNAKE_CASE			,       num_images_per_prompt=__SCREAMING_SNAKE_CASE			,       eta=__SCREAMING_SNAKE_CASE			,       generator=__SCREAMING_SNAKE_CASE			,       latents=__SCREAMING_SNAKE_CASE			,       output_type=__SCREAMING_SNAKE_CASE			,       return_dict=__SCREAMING_SNAKE_CASE			,       callback=__SCREAMING_SNAKE_CASE			,       callback_steps=__SCREAMING_SNAKE_CASE			,       )





import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
  pass

if is_speech_available():
  from transformers import EncodecFeatureExtractor, MusicgenProcessor


__UpperCAmelCase			:			Union[str, Any]			      =					random.Random()


def   a						(				SCREAMING_SNAKE_CASE_   :		Optional[Any]				,      SCREAMING_SNAKE_CASE_   :		Optional[Any]=1.0				,      SCREAMING_SNAKE_CASE_   :		Optional[Any]=None				,      SCREAMING_SNAKE_CASE_   :		Union[str, Any]=None							):






       """simple docstring"""





       if rng is None:
              UpperCamelCase     :						str 						=		global_rng

       UpperCamelCase     :						Union[str, Any] 						=		[]
       for batch_idx in range(shape[0]							):
              values.append([]							)
              for _ in range(shape[1]							):
                     values[-1].append(rng.random() * scale							)

       return values



@require_torch
@require_sentencepiece
class  UpperCAmelCase_						(				unittest.TestCase):
       '''simple docstring'''






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		'''facebook/musicgen-small'''
              UpperCamelCase     :						str 						=		tempfile.mkdtemp()






       def 			_lowercase (  self			,       **__SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              return TaTokenizer.from_pretrained(self.checkpoint			,       **__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self			,       **__SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              return EncodecFeatureExtractor.from_pretrained(self.checkpoint			,       **__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              shutil.rmtree(self.tmpdirname    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Union[str, Any] 						=		self.get_tokenizer()
              UpperCamelCase     :						Any 						=		self.get_feature_extractor()

              UpperCamelCase     :						Optional[int] 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              processor.save_pretrained(self.tmpdirname    )
              UpperCamelCase     :						int 						=		MusicgenProcessor.from_pretrained(self.tmpdirname    )

              self.assertEqual(processor.tokenizer.get_vocab()			,       tokenizer.get_vocab()    )
              self.assertIsInstance(processor.tokenizer			,       __SCREAMING_SNAKE_CASE    )

              self.assertEqual(processor.feature_extractor.to_json_string()			,       feature_extractor.to_json_string()    )
              self.assertIsInstance(processor.feature_extractor			,       __SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						str 						=		MusicgenProcessor(tokenizer=self.get_tokenizer()			,       feature_extractor=self.get_feature_extractor()    )
              processor.save_pretrained(self.tmpdirname    )

              UpperCamelCase     :						Any 						=		self.get_tokenizer(bos_token='''(BOS)'''			,       eos_token='''(EOS)'''    )
              UpperCamelCase     :						Union[str, Any] 						=		self.get_feature_extractor(do_normalize=__SCREAMING_SNAKE_CASE			,       padding_value=1.0    )

              UpperCamelCase     :						Union[str, Any] 						=		MusicgenProcessor.from_pretrained(
                  self.tmpdirname			,       bos_token='''(BOS)'''			,       eos_token='''(EOS)'''			,       do_normalize=__SCREAMING_SNAKE_CASE			,       padding_value=1.0    )

              self.assertEqual(processor.tokenizer.get_vocab()			,       tokenizer_add_kwargs.get_vocab()    )
              self.assertIsInstance(processor.tokenizer			,       __SCREAMING_SNAKE_CASE    )

              self.assertEqual(processor.feature_extractor.to_json_string()			,       feature_extractor_add_kwargs.to_json_string()    )
              self.assertIsInstance(processor.feature_extractor			,       __SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		self.get_feature_extractor()
              UpperCamelCase     :						Tuple 						=		self.get_tokenizer()

              UpperCamelCase     :						Any 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						Union[str, Any] 						=		floats_list((3, 1_000)    )

              UpperCamelCase     :						Tuple 						=		feature_extractor(__SCREAMING_SNAKE_CASE			,       return_tensors='''np'''    )
              UpperCamelCase     :						Optional[Any] 						=		processor(__SCREAMING_SNAKE_CASE			,       return_tensors='''np'''    )

              for key in input_feat_extract.keys():
                     self.assertAlmostEqual(input_feat_extract[key].sum()			,       input_processor[key].sum()			,       delta=1e-2    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		self.get_feature_extractor()
              UpperCamelCase     :						List[str] 						=		self.get_tokenizer()

              UpperCamelCase     :						Optional[int] 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						Tuple 						=		'''This is a test string'''

              UpperCamelCase     :						Dict 						=		processor(text=__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						Union[str, Any] 						=		tokenizer(__SCREAMING_SNAKE_CASE    )

              for key in encoded_tok.keys():
                     self.assertListEqual(encoded_tok[key]			,       encoded_processor[key]    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Any 						=		self.get_feature_extractor()
              UpperCamelCase     :						int 						=		self.get_tokenizer()

              UpperCamelCase     :						Optional[int] 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						int 						=		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

              UpperCamelCase     :						int 						=		processor.batch_decode(sequences=__SCREAMING_SNAKE_CASE    )
              UpperCamelCase     :						Union[str, Any] 						=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE    )

              self.assertListEqual(__SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Dict 						=		self.get_feature_extractor()
              UpperCamelCase     :						Union[str, Any] 						=		self.get_tokenizer()

              UpperCamelCase     :						str 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              self.assertListEqual(
                  processor.model_input_names			,       feature_extractor.model_input_names			,       msg='''`processor` and `feature_extractor` model input names do not match'''			,       )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						str 						=		self.get_feature_extractor(padding_side='''left'''    )
              UpperCamelCase     :						Optional[Any] 						=		self.get_tokenizer()

              UpperCamelCase     :						List[Any] 						=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE			,       feature_extractor=__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						List[str] 						=		[floats_list((1, x)    )[0] for x in range(5			,       20			,       5    )]
              UpperCamelCase     :						Tuple 						=		processor(__SCREAMING_SNAKE_CASE    ).padding_mask

              UpperCamelCase     :						List[str] 						=		np.asarray(floats_list((3, 20)    )    )[:, None, :]
              UpperCamelCase     :						Optional[int] 						=		processor.batch_decode(__SCREAMING_SNAKE_CASE			,       padding_mask=__SCREAMING_SNAKE_CASE    )

              self.assertIsInstance(__SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    )

              for audio in decoded_audios:
                     self.assertIsInstance(__SCREAMING_SNAKE_CASE			,       np.ndarray    )

              self.assertTrue(decoded_audios[0].shape == (1, 10)    )
              self.assertTrue(decoded_audios[1].shape == (1, 15)    )
              self.assertTrue(decoded_audios[2].shape == (1, 20)    )

