





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


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





class 					A_					(   _a						):







       """simple docstring"""





       def __init__(     self						:						Any    ,__A						:						WhisperForConditionalGeneration    ,__A						:						WhisperProcessor    ,__A						:						AutoencoderKL    ,__A						:						CLIPTextModel    ,__A						:						CLIPTokenizer    ,__A						:						UNetaDConditionModel    ,__A						:						Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]    ,__A						:						StableDiffusionSafetyChecker    ,__A						:						CLIPImageProcessor    ,)    ->       str:
         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=A_    ,speech_processor=A_    ,vae=A_    ,text_encoder=A_    ,tokenizer=A_    ,unet=A_    ,scheduler=A_    ,feature_extractor=A_    ,)







       def  __UpperCAmelCase			(     self						:						int    ,__A						:						Optional[Union[str, int]] = "auto"	)    ->       List[str]:




         if slice_size == "auto":
           _lowercase						    =			self.unet.config.attention_head_dim // 2
         self.unet.set_attention_slice(A_	)







       def  __UpperCAmelCase			(     self						:						Union[str, Any]	)    ->       Optional[Any]:
         self.enable_attention_slicing(A_	)







       @torch.no_grad()
       def __call__(     self						:						List[str]    ,__A						:						List[Any]    ,__A						:						int=1_6000    ,__A						:						int = 512    ,__A						:						int = 512    ,__A						:						int = 50    ,__A						:						float = 7.5    ,__A						:						Optional[Union[str, List[str]]] = None    ,__A						:						Optional[int] = 1    ,__A						:						float = 0.0    ,__A						:						Optional[torch.Generator] = None    ,__A						:						Optional[torch.FloatTensor] = None    ,__A						:						Optional[str] = "pil"    ,__A						:						bool = True    ,__A						:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None    ,__A						:						int = 1    ,**__A						:						str    ,)    ->       Union[str, Any]:
         _lowercase						    =			self.speech_processor.feature_extractor(
             A_    ,return_tensors='pt'    ,sampling_rate=A_	).input_features.to(self.device	)
         _lowercase						    =			self.speech_model.generate(A_    ,max_length=48_0000	)

         _lowercase						    =			self.speech_processor.tokenizer.batch_decode(A_    ,skip_special_tokens=A_    ,normalize=A_	)[
             0
         ]

         if isinstance(A_    ,A_	):
           _lowercase						    =			1
         elif isinstance(A_    ,A_	):
           _lowercase						    =			len(A_	)
         else:
           raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(A_	)}"""	)

         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(A_    ,A_	) or callback_steps <= 0)
         ):
           raise ValueError(
               F"""`callback_steps` has to be a positive integer but is {callback_steps} of type"""
               F""" {type(A_	)}."""	)

         # get prompt text embeddings
         _lowercase						    =			self.tokenizer(
             A_    ,padding='max_length'    ,max_length=self.tokenizer.model_max_length    ,return_tensors='pt'    ,)
         _lowercase						    =			text_inputs.input_ids

         if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
           _lowercase						    =			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}"""	)
           _lowercase						    =			text_input_ids[:, : self.tokenizer.model_max_length]
         _lowercase						    =			self.text_encoder(text_input_ids.to(self.device	)	)[0]

         # duplicate text embeddings for each generation per prompt, using mps friendly method
         _lowercase    ,		_lowercase    ,		_lowercase						    =			text_embeddings.shape
         _lowercase						    =			text_embeddings.repeat(1    ,A_    ,1	)
         _lowercase						    =			text_embeddings.view(bs_embed * num_images_per_prompt    ,A_    ,-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.
         _lowercase						    =			guidance_scale > 1.0
         # get unconditional embeddings for classifier free guidance
         if do_classifier_free_guidance:
           _lowercase						    =			42
           if negative_prompt is None:
             _lowercase						    =			[''] * batch_size
           elif type(A_	) is not type(A_	):
             raise TypeError(
                 F"""`negative_prompt` should be the same type to `prompt`, but got {type(A_	)} !="""
                 F""" {type(A_	)}."""	)
           elif isinstance(A_    ,A_	):
             _lowercase						    =			[negative_prompt]
           elif batch_size != len(A_	):
             raise ValueError(
                 F"""`negative_prompt`: {negative_prompt} has batch size {len(A_	)}, but `prompt`:"""
                 F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
                 ' the batch size of `prompt`.'	)
           else:
             _lowercase						    =			negative_prompt

           _lowercase						    =			text_input_ids.shape[-1]
           _lowercase						    =			self.tokenizer(
               A_    ,padding='max_length'    ,max_length=A_    ,truncation=A_    ,return_tensors='pt'    ,)
           _lowercase						    =			self.text_encoder(uncond_input.input_ids.to(self.device	)	)[0]

           # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
           _lowercase						    =			uncond_embeddings.shape[1]
           _lowercase						    =			uncond_embeddings.repeat(1    ,A_    ,1	)
           _lowercase						    =			uncond_embeddings.view(batch_size * num_images_per_prompt    ,A_    ,-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
           _lowercase						    =			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`.
         _lowercase						    =			(batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
         _lowercase						    =			text_embeddings.dtype
         if latents is None:
           if self.device.type == "mps":
             # randn does not exist on mps
             _lowercase						    =			torch.randn(A_    ,generator=A_    ,device='cpu'    ,dtype=A_	).to(
                 self.device	)
           else:
             _lowercase						    =			torch.randn(A_    ,generator=A_    ,device=self.device    ,dtype=A_	)
         else:
           if latents.shape != latents_shape:
             raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}"""	)
           _lowercase						    =			latents.to(self.device	)

         # set timesteps
         self.scheduler.set_timesteps(A_	)

         # Some schedulers like PNDM have timesteps as arrays
         # It's more optimized to move all timesteps to correct device beforehand
         _lowercase						    =			self.scheduler.timesteps.to(self.device	)

         # scale the initial noise by the standard deviation required by the scheduler
         _lowercase						    =			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]
         _lowercase						    =			'eta' in set(inspect.signature(self.scheduler.step	).parameters.keys()	)
         _lowercase						    =			{}
         if accepts_eta:
           _lowercase						    =			eta

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

           # predict the noise residual
           _lowercase						    =			self.unet(A_    ,A_    ,encoder_hidden_states=A_	).sample

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

           # compute the previous noisy sample x_t -> x_t-1
           _lowercase						    =			self.scheduler.step(A_    ,A_    ,A_    ,**A_	).prev_sample

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

         _lowercase						    =			1 / 0.18215 * latents
         _lowercase						    =			self.vae.decode(A_	).sample

         _lowercase						    =			(image / 2 + 0.5).clamp(0    ,1	)

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

         if output_type == "pil":
           _lowercase						    =			self.numpy_to_pil(A_	)

         if not return_dict:
           return image

         return StableDiffusionPipelineOutput(images=A_    ,nsfw_content_detected=A_	)



import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__snake_case   							=						get_tests_dir('''fixtures''')



class 	__lowerCamelCase			(unittest.TestCase						):







       def 		snake_case_	(			self: int					):






            '''simple docstring'''




            __UpperCamelCase =			mock.Mock()
            __UpperCamelCase =			500
            __UpperCamelCase =			{}
            __UpperCamelCase =			HTTPError
            __UpperCamelCase =			{}

            # Download this model to make sure it's in the cache.
            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'					)
            # Under the mock environment we get a 500 error when trying to reach the model.
            with mock.patch('requests.Session.request',return_value=A_					) as mock_head:
                 __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'					)
                 # This check we did call the fake head request
                 mock_head.assert_called()







       def 		snake_case_	(			self: Dict					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(
                'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'					)



@is_staging_test
class 	__lowerCamelCase			(unittest.TestCase						):







       @classmethod
       def 		snake_case_	(			cls: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			TOKEN
            HfFolder.save_token(A_					)







       @classmethod
       def 		snake_case_	(			cls: Tuple					):






            '''simple docstring'''




            try:
                 delete_repo(token=cls._token,repo_id='test-feature-extractor'					)
            except HTTPError:
                 pass

            try:
                 delete_repo(token=cls._token,repo_id='valid_org/test-feature-extractor-org'					)
            except HTTPError:
                 pass

            try:
                 delete_repo(token=cls._token,repo_id='test-dynamic-feature-extractor'					)
            except HTTPError:
                 pass







       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(A_					)
            feature_extractor.push_to_hub('test-feature-extractor',use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)

            # Reset repo
            delete_repo(token=self._token,repo_id='test-feature-extractor'					)

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     A_,repo_id='test-feature-extractor',push_to_hub=A_,use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)







       def 		snake_case_	(			self: List[str]					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(A_					)
            feature_extractor.push_to_hub('valid_org/test-feature-extractor',use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)

            # Reset repo
            delete_repo(token=self._token,repo_id='valid_org/test-feature-extractor'					)

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     A_,repo_id='valid_org/test-feature-extractor-org',push_to_hub=A_,use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)







       def 		snake_case_	(			self: int					):






            '''simple docstring'''




            CustomFeatureExtractor.register_for_auto_class()
            __UpperCamelCase =			CustomFeatureExtractor.from_pretrained(A_					)

            feature_extractor.push_to_hub('test-dynamic-feature-extractor',use_auth_token=self._token					)

            # This has added the proper auto_map field to the config
            self.assertDictEqual(
                feature_extractor.auto_map,{'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor'},)

            __UpperCamelCase =			AutoFeatureExtractor.from_pretrained(
                F'''{USER}/test-dynamic-feature-extractor''',trust_remote_code=A_					)
            # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
            self.assertEqual(new_feature_extractor.__class__.__name__,'CustomFeatureExtractor'					)


