



import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
   import torch

if is_soundfile_availble():
   import soundfile as sf

if is_vision_available():
   from PIL import Image






def  UpperCAmelCase__    (    lowercase__=""   ) ->      Tuple:
      __lowercase					=				tempfile.mkdtemp()
      return os.path.join(_lowerCamelCase						,       str(uuid.uuida()   ) + suffix   )
@require_soundfile
@require_torch
class 			_lowerCAmelCase   ( unittest.TestCase			):






   """simple docstring"""





   def 		snake_case__			(				self	:       Tuple   )						->					Union[str, Any]:
         """simple docstring"""







         __lowercase					=				torch.rand(12			,     dtype=torch.floataa   ) - 0.5
         __lowercase					=				AgentAudio(lowercase   )
         __lowercase					=				str(agent_type.to_string()   )

         # Ensure that the tensor and the agent_type's tensor are the same
         self.assertTrue(torch.allclose(lowercase			,     agent_type.to_raw()			,     atol=1E-4   )   )

         del agent_type

         # Ensure the path remains even after the object deletion
         self.assertTrue(os.path.exists(lowercase   )   )

         # Ensure that the file contains the same value as the original tensor
         __lowercase					=				sf.read(lowercase   )
         self.assertTrue(torch.allclose(lowercase			,     torch.tensor(lowercase   )			,     atol=1E-4   )   )







   def 		snake_case__			(				self	:       List[str]   )						->					str:
         """simple docstring"""







         __lowercase					=				torch.rand(12			,     dtype=torch.floataa   ) - 0.5
         __lowercase					=				get_new_path(suffix=""".wav"""   )
         sf.write(lowercase			,     lowercase			,     16_000   )

         __lowercase					=				AgentAudio(lowercase   )

         self.assertTrue(torch.allclose(lowercase			,     agent_type.to_raw()			,     atol=1E-4   )   )
         self.assertEqual(agent_type.to_string()			,     lowercase   )





@require_vision
@require_torch
class 			_lowerCAmelCase   ( unittest.TestCase			):






   """simple docstring"""





   def 		snake_case__			(				self	:       Any   )						->					Tuple:
         """simple docstring"""







         __lowercase					=				torch.randint(0			,     256			,     (64, 64, 3)   )
         __lowercase					=				AgentImage(lowercase   )
         __lowercase					=				str(agent_type.to_string()   )

         # Ensure that the tensor and the agent_type's tensor are the same
         self.assertTrue(torch.allclose(lowercase			,     agent_type._tensor			,     atol=1E-4   )   )

         self.assertIsInstance(agent_type.to_raw()			,     Image.Image   )

         # Ensure the path remains even after the object deletion
         del agent_type
         self.assertTrue(os.path.exists(lowercase   )   )





   def 		snake_case__			(				self	:       Tuple   )						->					Union[str, Any]:
         """simple docstring"""







         __lowercase					=				Path(get_tests_dir("""fixtures/tests_samples/COCO"""   )   ) / "000000039769.png"
         __lowercase					=				Image.open(lowercase   )
         __lowercase					=				AgentImage(lowercase   )

         self.assertTrue(path.samefile(agent_type.to_string()   )   )
         self.assertTrue(image == agent_type.to_raw()   )

         # Ensure the path remains even after the object deletion
         del agent_type
         self.assertTrue(os.path.exists(lowercase   )   )







   def 		snake_case__			(				self	:       Union[str, Any]   )						->					Union[str, Any]:
         """simple docstring"""







         __lowercase					=				Path(get_tests_dir("""fixtures/tests_samples/COCO"""   )   ) / "000000039769.png"
         __lowercase					=				Image.open(lowercase   )
         __lowercase					=				AgentImage(lowercase   )

         self.assertFalse(path.samefile(agent_type.to_string()   )   )
         self.assertTrue(image == agent_type.to_raw()   )

         # Ensure the path remains even after the object deletion
         del agent_type
         self.assertTrue(os.path.exists(lowercase   )   )





class 			_lowerCAmelCase   ( unittest.TestCase			):






   """simple docstring"""





   def 		snake_case__			(				self	:       List[str]   )						->					int:
         """simple docstring"""







         __lowercase					=				"Hey!"
         __lowercase					=				AgentText(lowercase   )

         self.assertEqual(lowercase			,     agent_type.to_string()   )
         self.assertEqual(lowercase			,     agent_type.to_raw()   )
         self.assertEqual(lowercase			,     lowercase   )










import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
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_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_lowerCAmelCase   ( _UpperCAmelCase    ,					_UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  int      =			IFImgaImgSuperResolutionPipeline
			lowercase__      :  Optional[int]      =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
			lowercase__      :  Optional[int]      =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""}			)
			lowercase__      :  Tuple      =			PipelineTesterMixin.required_optional_params - {"""latents"""}





			def 		snake_case__			(				self	:       Tuple   )						->					Any:
									"""simple docstring"""







									return self._get_superresolution_dummy_components()





			def 		snake_case__			(				self	:       List[str]			,     lowercase	:       Optional[int]			,     lowercase	:       Optional[Any]=0   )						->					Union[str, Any]:
									"""simple docstring"""







									if str(lowercase   ).startswith("""mps"""   ):
															__lowercase					=				torch.manual_seed(lowercase   )
									else:
															__lowercase					=				torch.Generator(device=lowercase   ).manual_seed(lowercase   )

									__lowercase					=				floats_tensor((1, 3, 32, 32)			,     rng=random.Random(lowercase   )   ).to(lowercase   )
									__lowercase					=				floats_tensor((1, 3, 16, 16)			,     rng=random.Random(lowercase   )   ).to(lowercase   )

									__lowercase					=				{
									    """prompt""": """A painting of a squirrel eating a burger""",
									    """image""": image,
									    """original_image""": original_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 		snake_case__			(				self	:       Optional[Any]   )						->					Tuple:
									"""simple docstring"""







									self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )





			def 		snake_case__			(				self	:       List[Any]   )						->					Union[str, Any]:
									"""simple docstring"""







									self._test_save_load_optional_components()





			@unittest.skipIf(torch_device != """cuda"""			,     reason="""float16 requires CUDA"""   )
			def 		snake_case__			(				self	:       List[Any]   )						->					Dict:
									"""simple docstring"""







									super().test_save_load_floataa(expected_max_diff=1E-1   )





			def 		snake_case__			(				self	:       Dict   )						->					int:
									"""simple docstring"""







									self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )





			def 		snake_case__			(				self	:       Optional[Any]   )						->					int:
									"""simple docstring"""







									self._test_save_load_local()







			def 		snake_case__			(				self	:       Optional[int]   )						->					Optional[int]:
									"""simple docstring"""







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





