



import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin





@skip_mps
class 		UpperCAmelCase						(						lowercase__       ,lowercase__       ,unittest.TestCase				):
   SCREAMING_SNAKE_CASE__							=     IFInpaintingSuperResolutionPipeline
   SCREAMING_SNAKE_CASE__							=     TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''width''', '''height'''}
   SCREAMING_SNAKE_CASE__							=     TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'''original_image'''}				)
   SCREAMING_SNAKE_CASE__							=     PipelineTesterMixin.required_optional_params - {'''latents'''}





   def 	__lowerCAmelCase							(	self			):
         return self._get_superresolution_dummy_components()





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase=0			):



         if str(__lowerCamelCase			).startswith('''mps'''			):
               _lowerCAmelCase 			=  torch.manual_seed(__lowerCamelCase			)
         else:
               _lowerCAmelCase 			=  torch.Generator(device=__lowerCamelCase			).manual_seed(__lowerCamelCase			)

         _lowerCAmelCase 			=  floats_tensor((1, 3, 16, 16)       ,     rng=random.Random(__lowerCamelCase			)			).to(__lowerCamelCase			)
         _lowerCAmelCase 			=  floats_tensor((1, 3, 32, 32)       ,     rng=random.Random(__lowerCamelCase			)			).to(__lowerCamelCase			)
         _lowerCAmelCase 			=  floats_tensor((1, 3, 32, 32)       ,     rng=random.Random(__lowerCamelCase			)			).to(__lowerCamelCase			)

         _lowerCAmelCase 			=  {
             "prompt": "A painting of a squirrel eating a burger",
             "image": image,
             "original_image": original_image,
             "mask_image": mask_image,
             "generator": generator,
             "num_inference_steps": 2,
             "output_type": "numpy",
         }

         return inputs





   @unittest.skipIf(
       torch_device != '''cuda''' or not is_xformers_available()       ,     reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,     )
   def 	__lowerCAmelCase							(	self			):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3			)





   def 	__lowerCAmelCase							(	self			):
         self._test_save_load_optional_components()





   @unittest.skipIf(torch_device != '''cuda'''       ,     reason='''float16 requires CUDA'''			)
   def 	__lowerCAmelCase							(	self			):
         super().test_save_load_floataa(expected_max_diff=1E-1			)





   def 	__lowerCAmelCase							(	self			):
         self._test_attention_slicing_forward_pass(expected_max_diff=1E-2			)





   def 	__lowerCAmelCase							(	self			):
         self._test_save_load_local()





   def 	__lowerCAmelCase							(	self			):
         self._test_inference_batch_single_identical(
             expected_max_diff=1E-2       ,     )










from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding





class 		UpperCAmelCase						(						snake_case_				):
   SCREAMING_SNAKE_CASE__							=     '''ClapFeatureExtractor'''
   SCREAMING_SNAKE_CASE__							=     ('''RobertaTokenizer''', '''RobertaTokenizerFast''')





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





   def __call__(	self       ,     _lowerCAmelCase=None       ,     _lowerCAmelCase=None       ,     _lowerCAmelCase=None       ,     **_lowerCAmelCase			):
         _lowerCAmelCase 			=  kwargs.pop('''sampling_rate'''       ,     _lowerCAmelCase			)

         if text is None and audios is None:
               raise ValueError('''You have to specify either text or audios. Both cannot be none.'''			)

         if text is not None:
               _lowerCAmelCase 			=  self.tokenizer(_lowerCAmelCase       ,     return_tensors=_lowerCAmelCase       ,     **_lowerCAmelCase			)

         if audios is not None:
               _lowerCAmelCase 			=  self.feature_extractor(
                   _lowerCAmelCase       ,     sampling_rate=_lowerCAmelCase       ,     return_tensors=_lowerCAmelCase       ,     **_lowerCAmelCase			)

         if text is not None and audios is not None:
               _lowerCAmelCase 			=  audio_features.input_features
               return encoding
         elif text is not None:
               return encoding
         else:
               return BatchEncoding(data=dict(**_lowerCAmelCase			)       ,     tensor_type=_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     *_lowerCAmelCase       ,     **_lowerCAmelCase			):
         return self.tokenizer.batch_decode(*_lowerCAmelCase       ,     **_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     *_lowerCAmelCase       ,     **_lowerCAmelCase			):
         return self.tokenizer.decode(*_lowerCAmelCase       ,     **_lowerCAmelCase			)





   @property
   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.tokenizer.model_input_names
         _lowerCAmelCase 			=  self.feature_extractor.model_input_names
         return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names			)			)