
'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
      from transformers import TvltImageProcessor

if is_speech_available():
      from transformers import TvltFeatureExtractor

from transformers import TvltProcessor





@require_torch
class  A				(							unittest.TestCase						):





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   Optional[Any]					=						'''ZinengTang/tvlt-base'''
         A       :   List[Any]					=						tempfile.mkdtemp()





     def 				__lowerCAmelCase							(							self						,			**SCREAMING_SNAKE_CASE	) ->				Optional[int]:






         """simple docstring"""






         return TvltImageProcessor.from_pretrained(self.checkpoint						,			**SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self						,			**SCREAMING_SNAKE_CASE	) ->				str:






         """simple docstring"""






         return TvltFeatureExtractor.from_pretrained(self.checkpoint						,			**SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         shutil.rmtree(self.tmpdirname	)





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   Tuple					=						self.get_feature_extractor()

         A       :   Any					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)
         processor.save_pretrained(self.tmpdirname	)
         A       :   List[Any]					=						TvltProcessor.from_pretrained(self.tmpdirname	)

         self.assertIsInstance(processor.feature_extractor						,			SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(processor.image_processor						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[int]:






         """simple docstring"""






         A       :   str					=						self.get_image_processor()
         A       :   Optional[int]					=						self.get_feature_extractor()

         A       :   int					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   Union[str, Any]					=						np.ones([12000]	)

         A       :   List[Any]					=						feature_extractor(SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)
         A       :   Dict					=						processor(audio=SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)

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





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   int					=						self.get_feature_extractor()

         A       :   int					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   int					=						np.ones([3, 224, 224]	)

         A       :   Optional[Any]					=						image_processor(SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)
         A       :   Dict					=						processor(images=SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)

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





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   Union[str, Any]					=						self.get_feature_extractor()

         A       :   List[str]					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   Dict					=						np.ones([12000]	)
         A       :   Tuple					=						np.ones([3, 224, 224]	)

         A       :   Optional[Any]					=						processor(audio=SCREAMING_SNAKE_CASE						,			images=SCREAMING_SNAKE_CASE	)

         self.assertListEqual(list(inputs.keys()	)						,			['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']	)

         # test if it raises when no input is passed
         with pytest.raises(SCREAMING_SNAKE_CASE	):
             processor()





     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         A       :   List[Any]					=						self.get_image_processor()
         A       :   str					=						self.get_feature_extractor()

         A       :   Optional[int]					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

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









"""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


_lowerCAmelCase    :Optional[Any]     =					False
class 							_UpperCAmelCase							( unittest.TestCase      ):





     '''simple docstring'''



     pass



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





     '''simple docstring'''



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





     def  __lowerCAmelCase	(					self		)							->					Dict:
         _UpperCAmelCase							:  Tuple											=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,	torch_dtype=torch.floataa		)
         pipe.to(A		)
         pipe.set_progress_bar_config(disable=A		)

         _UpperCAmelCase							:  List[str]											=    load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''		)

         _UpperCAmelCase							:  Optional[Any]											=    torch.manual_seed(0		)
         _UpperCAmelCase							:  List[Any]											=    pipe.dual_guided(
             prompt='''first prompt'''       ,	image=A       ,	text_to_image_strength=0.75       ,	generator=A       ,	guidance_scale=7.5       ,	num_inference_steps=2       ,	output_type='''numpy'''       ,	).images

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

         _UpperCAmelCase							:  int											=    generator.manual_seed(0		)
         _UpperCAmelCase							:  Union[str, Any]											=    pipe.dual_guided(
             prompt='''first prompt'''       ,	image=A       ,	text_to_image_strength=0.75       ,	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  __lowerCAmelCase	(					self		)							->					List[str]:
         _UpperCAmelCase							:  List[Any]											=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,	torch_dtype=torch.floataa		)
         pipe.to(A		)
         pipe.set_progress_bar_config(disable=A		)

         _UpperCAmelCase							:  int											=    '''cyberpunk 2077'''
         _UpperCAmelCase							:  Optional[int]											=    load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''		)
         _UpperCAmelCase							:  str											=    torch.manual_seed(0		)
         _UpperCAmelCase							:  Optional[Any]											=    pipe.dual_guided(
             prompt=A       ,	image=A       ,	text_to_image_strength=0.75       ,	generator=A       ,	guidance_scale=7.5       ,	num_inference_steps=5_0       ,	output_type='''numpy'''       ,	).images

         _UpperCAmelCase							:  Union[str, Any]											=    image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _UpperCAmelCase							:  List[Any]											=    np.array([0.1_448, 0.1_619, 0.1_741, 0.1_086, 0.1_147, 0.1_128, 0.1_199, 0.1_165, 0.1_001]		)

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

         _UpperCAmelCase							:  Dict											=    '''A painting of a squirrel eating a burger '''
         _UpperCAmelCase							:  Tuple											=    torch.manual_seed(0		)
         _UpperCAmelCase							:  Optional[Any]											=    pipe.text_to_image(
             prompt=A       ,	generator=A       ,	guidance_scale=7.5       ,	num_inference_steps=5_0       ,	output_type='''numpy'''		).images

         _UpperCAmelCase							:  Tuple											=    image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _UpperCAmelCase							:  int											=    np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]		)

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

         _UpperCAmelCase							:  int											=    pipe.image_variation(A       ,	generator=A       ,	output_type='''numpy'''		).images

         _UpperCAmelCase							:  Optional[int]											=    image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _UpperCAmelCase							:  List[str]											=    np.array([0.3_076, 0.3_123, 0.3_284, 0.3_782, 0.3_770, 0.3_894, 0.4_297, 0.4_331, 0.4_456]		)

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


