



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


SCREAMING_SNAKE_CASE__       =      False


class      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					pass







@nightly
@require_torch_gpu
class      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()



					def     _a							(     self					:       Union[str, Any]					):






								"""simple docstring"""


								A__       =    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'				,					torch_dtype=torch.floataa					)
								pipe.to(__lowerCamelCase					)
								pipe.set_progress_bar_config(disable=__lowerCamelCase					)

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

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

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

								A__       =    generator.manual_seed(0					)
								A__       =    pipe.dual_guided(
								    prompt='first prompt'				,					image=__lowerCamelCase				,					text_to_image_strength=0.75				,					generator=__lowerCamelCase				,					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     _a							(     self					:       Dict					):






								"""simple docstring"""


								A__       =    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'				,					torch_dtype=torch.floataa					)
								pipe.to(__lowerCamelCase					)
								pipe.set_progress_bar_config(disable=__lowerCamelCase					)

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

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

								assert image.shape == (1, 5_12, 5_12, 3)
								A__       =    np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]					)

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

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

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

								assert image.shape == (1, 5_12, 5_12, 3)
								A__       =    np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]					)

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

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

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

								assert image.shape == (1, 5_12, 5_12, 3)
								A__       =    np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]					)

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









from typing import Dict

from .base import GenericTensor, Pipeline


class      __lowerCAmelCase     (		UpperCAmelCase_      ):
					"""simple docstring"""






					def     _a							(     self					:       Any				,					_snake_case					:       str=None				,					_snake_case					:       Dict=None				,					_snake_case					:       Any=None				,					**_snake_case					:       str					):






								"""simple docstring"""


								if tokenize_kwargs is None:
											A__       =    {}

								if truncation is not None:
											if "truncation" in tokenize_kwargs:
														raise ValueError(
														    'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)'					)
											A__       =    truncation

								A__       =    tokenize_kwargs

								A__       =    {}
								if return_tensors is not None:
											A__       =    return_tensors

								return preprocess_params, {}, postprocess_params



					def     _a							(     self					:       Any				,					_snake_case					:       Dict				,					**_snake_case					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    self.framework
								A__       =    self.tokenizer(_snake_case				,					return_tensors=_snake_case				,					**_snake_case					)
								return model_inputs



					def     _a							(     self					:       List[Any]				,					_snake_case					:       Dict					):






								"""simple docstring"""


								A__       =    self.model(**_snake_case					)
								return model_outputs



					def     _a							(     self					:       Optional[Any]				,					_snake_case					:       List[Any]				,					_snake_case					:       str=False					):






								"""simple docstring"""


								if return_tensors:
											return model_outputs[0]
								if self.framework == "pt":
											return model_outputs[0].tolist()
								elif self.framework == "tf":
											return model_outputs[0].numpy().tolist()



					def __call__(     self					:       Dict				,					*_snake_case					:       int				,					**_snake_case					:       List[str]					):






								"""simple docstring"""


								return super().__call__(*_snake_case				,					**_snake_case					)




