

import gc
import tempfile
import unittest

import numpy as np
import torch

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


UpperCAmelCase_       :				Tuple		   =      False






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




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




	def 					lowerCAmelCase_					(    self	:    Optional[Any]       ):
		__A   : List[str]								= VersatileDiffusionDualGuidedPipeline.from_pretrained("""shi-labs/versatile-diffusion"""       )
		# remove text_unet
		pipe.remove_unused_weights()
		pipe.to(__A       )
		pipe.set_progress_bar_config(disable=__A       )

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

		__A   : int								= torch.manual_seed(0       )
		__A   : int								= pipe(
		    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       )
			__A   : Union[str, Any]								= VersatileDiffusionDualGuidedPipeline.from_pretrained(__A       )

		pipe.to(__A       )
		pipe.set_progress_bar_config(disable=__A       )

		__A   : Dict								= generator.manual_seed(0       )
		__A   : List[Any]								= pipe(
		    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 					lowerCAmelCase_					(    self	:    List[Any]       ):
		__A   : Any								= VersatileDiffusionDualGuidedPipeline.from_pretrained("""shi-labs/versatile-diffusion"""       )
		pipe.remove_unused_weights()
		pipe.to(__A       )
		pipe.set_progress_bar_config(disable=__A       )

		__A   : List[Any]								= """cyberpunk 2077"""
		__A   : Optional[int]								= load_image(
		    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"""       )
		__A   : Optional[Any]								= torch.manual_seed(0       )
		__A   : Dict								= pipe(
		    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

		__A   : Union[str, Any]								= image[0, 253:256, 253:256, -1]

		assert image.shape == (1, 512, 512, 3)
		__A   : int								= np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]       )

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








import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
			from PIL import Image






class     lowerCamelCase_			(  unittest.TestCase	,			_lowercase  ):




	def 					lowerCAmelCase_					(    self	:    Dict       ):
		__A   : str								= load_tool("""image-captioning"""       )
		self.tool.setup()
		__A   : Dict								= load_tool("""image-captioning"""				,			remote=__A       )




	def 					lowerCAmelCase_					(    self	:    Optional[int]       ):
		__A   : List[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
		__A   : Optional[Any]								= self.tool(__A       )
		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )




	def 					lowerCAmelCase_					(    self	:    Dict       ):
		__A   : Any								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
		__A   : List[str]								= self.remote_tool(__A       )
		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )




	def 					lowerCAmelCase_					(    self	:    Union[str, Any]       ):
		__A   : Optional[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
		__A   : str								= self.tool(image=__A       )
		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )




	def 					lowerCAmelCase_					(    self	:    str       ):
		__A   : Dict								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
		__A   : List[Any]								= self.remote_tool(image=__A       )
		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )





