




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


a													=			False




class       _A				(		unittest.TestCase     ):
		pass




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






		def 				UpperCAmelCase (    self			):
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion"""						,		torch_dtype=torch.floataa			)
								pipe.to(_SCREAMING_SNAKE_CASE			)
								pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE			)

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

								_UpperCAmelCase      =			torch.manual_seed(0			)
								_UpperCAmelCase      =			pipe.dual_guided(
								    prompt="""first prompt"""						,		image=_SCREAMING_SNAKE_CASE						,		text_to_image_strength=0.75						,		generator=_SCREAMING_SNAKE_CASE						,		guidance_scale=7.5						,		num_inference_steps=2						,		output_type="""numpy"""						,		).images

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

								_UpperCAmelCase      =			generator.manual_seed(0			)
								_UpperCAmelCase      =			pipe.dual_guided(
								    prompt="""first prompt"""						,		image=_SCREAMING_SNAKE_CASE						,		text_to_image_strength=0.75						,		generator=_SCREAMING_SNAKE_CASE						,		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 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion"""						,		torch_dtype=torch.floataa			)
								pipe.to(_SCREAMING_SNAKE_CASE			)
								pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE			)

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

								_UpperCAmelCase      =			image[0, 253:256, 253:256, -1]

								assert image.shape == (1, 512, 512, 3)
								_UpperCAmelCase      =			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

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

								_UpperCAmelCase      =			image[0, 253:256, 253:256, -1]

								assert image.shape == (1, 512, 512, 3)
								_UpperCAmelCase      =			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

								_UpperCAmelCase      =			pipe.image_variation(_SCREAMING_SNAKE_CASE						,		generator=_SCREAMING_SNAKE_CASE						,		output_type="""numpy"""			).images

								_UpperCAmelCase      =			image[0, 253:256, 253:256, -1]

								assert image.shape == (1, 512, 512, 3)
								_UpperCAmelCase      =			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





import os
import string
import sys


a													=			1 << 8

a													=			{
    "tab": ord("\t"),
    "newline": ord("\r"),
    "esc": 27,
    "up": 65 + ARROW_KEY_FLAG,
    "down": 66 + ARROW_KEY_FLAG,
    "right": 67 + ARROW_KEY_FLAG,
    "left": 68 + ARROW_KEY_FLAG,
    "mod_int": 91,
    "undefined": sys.maxsize,
    "interrupt": 3,
    "insert": 50,
    "delete": 51,
    "pg_up": 53,
    "pg_down": 54,
}

a													=			KEYMAP["up"]
a													=			KEYMAP["left"]

if sys.platform == "win32":
				a													=			[]
				a													=			{
				    b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG,
				    b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG,
				    b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG,
				    b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG,
				    b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG,
				    b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG,
				    b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG,
				    b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG,
				}

for i in range(10):
				a													=			ord(str(i))




def        _SCREAMING_SNAKE_CASE      (		)						->				Optional[int]:
						if os.name == "nt":
												import msvcrt

												_UpperCAmelCase      =			"""mbcs"""
												# Flush the keyboard buffer
												while msvcrt.kbhit():
																		msvcrt.getch()
												if len(snake_case       ) == 0:
																		# Read the keystroke
																		_UpperCAmelCase      =			msvcrt.getch()

																		# If it is a prefix char, get second part
																		if ch in (b"\x00", b"\xe0"):
																								_UpperCAmelCase      =			ch + msvcrt.getch()
																								# Translate actual Win chars to bullet char types
																								try:
																														_UpperCAmelCase      =			chr(WIN_KEYMAP[cha]       )
																														WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""]       )       )
																														WIN_CH_BUFFER.append(snake_case       )
																														if ord(snake_case       ) in (
																														    KEYMAP["insert"] - 1 << 9,
																														    KEYMAP["delete"] - 1 << 9,
																														    KEYMAP["pg_up"] - 1 << 9,
																														    KEYMAP["pg_down"] - 1 << 9,
																														):
																																				WIN_CH_BUFFER.append(chr(1_2_6       )       )
																														_UpperCAmelCase      =			chr(KEYMAP["""esc"""]       )
																								except KeyError:
																														_UpperCAmelCase      =			cha[1]
																		else:
																								_UpperCAmelCase      =			ch.decode(snake_case       )
												else:
																		_UpperCAmelCase      =			WIN_CH_BUFFER.pop(0       )
						elif os.name == "posix":
												import termios
												import tty

												_UpperCAmelCase      =			sys.stdin.fileno()
												_UpperCAmelCase      =			termios.tcgetattr(snake_case       )
												try:
																		tty.setraw(snake_case       )
																		_UpperCAmelCase      =			sys.stdin.read(1       )
												finally:
																		termios.tcsetattr(snake_case			,     termios.TCSADRAIN			,     snake_case       )
						return ch




def        _SCREAMING_SNAKE_CASE      (		)						->				Dict:
						_UpperCAmelCase      =			get_raw_chars()
						if ord(snake_case       ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
												return char

						elif ord(snake_case       ) == KEYMAP["esc"]:
												_UpperCAmelCase      =			get_raw_chars()
												if ord(snake_case       ) == KEYMAP["mod_int"]:
																		_UpperCAmelCase      =			get_raw_chars()
																		if ord(snake_case       ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(snake_case       ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																								return chr(ord(snake_case       ) + ARROW_KEY_FLAG       )
																		else:
																								return KEYMAP["undefined"]
												else:
																		return get_raw_chars()

						else:
												if char in string.printable:
																		return char
												else:
																		return KEYMAP["undefined"]