





'''simple docstring'''







import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_lowercase       						=			False
class 			_lowercase						(       unittest.TestCase					):
     pass




@nightly
@require_torch_gpu
class 			_lowercase						(       unittest.TestCase					):







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







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''    ,   torch_dtype=torch.floataa    )
      pipe.to(A__    )
      pipe.set_progress_bar_config(disable=A__    )

      snake_case				=			load_image(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''    )

      snake_case				=			torch.manual_seed(0    )
      snake_case				=			pipe.dual_guided(
          prompt='''first prompt'''    ,   image=A__    ,   text_to_image_strength=0.7_5    ,   generator=A__    ,   guidance_scale=7.5    ,   num_inference_steps=2    ,   output_type='''numpy'''    ,   ).images

      with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(A__    )
       snake_case				=			VersatileDiffusionPipeline.from_pretrained(A__    ,   torch_dtype=torch.floataa    )
      pipe.to(A__    )
      pipe.set_progress_bar_config(disable=A__    )

      snake_case				=			generator.manual_seed(0    )
      snake_case				=			pipe.dual_guided(
          prompt='''first prompt'''    ,   image=A__    ,   text_to_image_strength=0.7_5    ,   generator=A__    ,   guidance_scale=7.5    ,   num_inference_steps=2    ,   output_type='''numpy'''    ,   ).images

      assert np.abs(image - new_image    ).sum() < 1e-5, "Models don't have the same forward pass"







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				=			VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''    ,   torch_dtype=torch.floataa    )
      pipe.to(A__    )
      pipe.set_progress_bar_config(disable=A__    )

      snake_case				=			'''cyberpunk 2077'''
      snake_case				=			load_image(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''    )
      snake_case				=			torch.manual_seed(0    )
      snake_case				=			pipe.dual_guided(
          prompt=A__    ,   image=A__    ,   text_to_image_strength=0.7_5    ,   generator=A__    ,   guidance_scale=7.5    ,   num_inference_steps=50    ,   output_type='''numpy'''    ,   ).images

      snake_case				=			image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      snake_case				=			np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]    )

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

      snake_case				=			'''A painting of a squirrel eating a burger '''
      snake_case				=			torch.manual_seed(0    )
      snake_case				=			pipe.text_to_image(
          prompt=A__    ,   generator=A__    ,   guidance_scale=7.5    ,   num_inference_steps=50    ,   output_type='''numpy'''    ).images

      snake_case				=			image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      snake_case				=			np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]    )

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

      snake_case				=			pipe.image_variation(A__    ,   generator=A__    ,   output_type='''numpy'''    ).images

      snake_case				=			image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      snake_case				=			np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]    )

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








'''simple docstring'''







from ...processing_utils import ProcessorMixin
class 			_lowercase						(       __a					):
     _UpperCAmelCase										=					'''WhisperFeatureExtractor'''
     _UpperCAmelCase										=					'''WhisperTokenizer'''







     def __init__( self    ,   A__    ,   A__    ) ->	Optional[Any]:
      super().__init__(A__    ,   A__    )
      snake_case				=			self.feature_extractor
      snake_case				=			False







     def     UpperCamelCase			( self    ,   A__=None    ,   A__=None    ,   A__=True    ) ->	Union[str, Any]:
      return self.tokenizer.get_decoder_prompt_ids(task=A__    ,   language=A__    ,   no_timestamps=A__    )







     def __call__( self    ,   *A__    ,   **A__    ) ->	Dict:
      # For backward compatibility
      if self._in_target_context_manager:
       return self.current_processor(*A__    ,   **A__    )

      snake_case				=			kwargs.pop('''audio'''    ,   A__    )
      snake_case				=			kwargs.pop('''sampling_rate'''    ,   A__    )
      snake_case				=			kwargs.pop('''text'''    ,   A__    )
      if len(A__    ) > 0:
       snake_case				=			args[0]
       snake_case				=			args[1:]

      if audio is None and text is None:
       raise ValueError('''You need to specify either an `audio` or `text` input to process.'''    )

      if audio is not None:
       snake_case				=			self.feature_extractor(A__    ,   *A__    ,   sampling_rate=A__    ,   **A__    )
      if text is not None:
       snake_case				=			self.tokenizer(A__    ,   **A__    )

      if text is None:
       return inputs

      elif audio is None:
       return encodings
      else:
       snake_case				=			encodings['''input_ids''']
       return inputs







     def     UpperCamelCase			( self    ,   *A__    ,   **A__    ) ->	Optional[Any]:
      return self.tokenizer.batch_decode(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ,   *A__    ,   **A__    ) ->	str:
      return self.tokenizer.decode(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ,   A__    ,   A__="np"    ) ->	Optional[Any]:
      return self.tokenizer.get_prompt_ids(A__    ,   return_tensors=A__    )

