





import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


snake_case_     :       List[str]						=  False
try:
					snake_case_     :       int						=  _is_package_available("google.colab")
except ModuleNotFoundError:
					pass


@input.register
class 					__a      :

							def __init__(							self							:     str       ,     __magic_name__							:     str = None       ,     __magic_name__							:     list = []   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  0
													UpperCAmelCase_			:      Union[str, Any]     						=  choices
													UpperCAmelCase_			:      Union[str, Any]     						=  prompt
													if sys.platform == "win32":
																			UpperCAmelCase_			:      Union[str, Any]     						=  '''*'''
													else:
																			UpperCAmelCase_			:      Optional[int]     						=  '''➔ '''

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     str = ""   )					->       Optional[int]:







													"""simple docstring"""
													if sys.platform != "win32":
																			writeColor(self.choices[index]       ,     32       ,     UpperCamelCase_   )
													else:
																			forceWrite(self.choices[index]       ,     UpperCamelCase_   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     int   )					->       Optional[int]:







													"""simple docstring"""
													if index == self.position:
																			forceWrite(F""" {self.arrow_char} """   )
																			self.write_choice(UpperCamelCase_   )
													else:
																			forceWrite(F"""    {self.choices[index]}"""   )
													reset_cursor()

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Direction       ,     __magic_name__							:     int = 1   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.position
													if direction == Direction.DOWN:
																			if self.position + 1 >= len(self.choices   ):
																									return
																			self.position += num_spaces
													else:
																			if self.position - 1 < 0:
																									return
																			self.position -= num_spaces
													clear_line()
													self.print_choice(UpperCamelCase_   )
													move_cursor(UpperCamelCase_       ,     direction.name   )
													self.print_choice(self.position   )

							@input.mark(KEYMAP['''up''']   )
							def     UpperCAmelCase__	(							self							:     Dict   )					->       List[str]:







													"""simple docstring"""
													self.move_direction(Direction.UP   )

							@input.mark(KEYMAP['''down''']   )
							def     UpperCAmelCase__	(							self							:     str   )					->       Optional[Any]:







													"""simple docstring"""
													self.move_direction(Direction.DOWN   )

							@input.mark(KEYMAP['''newline''']   )
							def     UpperCAmelCase__	(							self							:     int   )					->       Any:







													"""simple docstring"""
													move_cursor(len(self.choices   ) - self.position       ,     '''DOWN'''   )
													return self.position

							@input.mark(KEYMAP['''interrupt''']   )
							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       Dict:







													"""simple docstring"""
													move_cursor(len(self.choices   ) - self.position       ,     '''DOWN'''   )
													raise KeyboardInterrupt

							@input.mark_multiple(*[KEYMAP[str(UpperCamelCase_   )] for number in range(10   )]   )
							def     UpperCAmelCase__	(							self							:     str   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  int(chr(self.current_selection   )   )
													UpperCAmelCase_			:      Optional[int]     						=  index - self.position
													if index == self.position:
																			return
													if index < len(self.choices   ):
																			if self.position > index:
																									self.move_direction(Direction.UP       ,     -movement   )
																			elif self.position < index:
																									self.move_direction(Direction.DOWN       ,     UpperCamelCase_   )
																			else:
																									return
													else:
																			return



							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     int = 0   )					->       Any:







													"""simple docstring"""
													if self.prompt:
																			linebreak()
																			forceWrite(self.prompt       ,     '''\n'''   )
																			if in_colab:
																									forceWrite('''Please input a choice index (starting from 0), and press enter'''       ,     '''\n'''   )
																			else:
																									forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter'''       ,     '''\n'''   )
													UpperCAmelCase_			:      str     						=  default_choice
													for i in range(len(self.choices   )   ):
																			self.print_choice(UpperCamelCase_   )
																			forceWrite('''\n'''   )
													move_cursor(len(self.choices   ) - self.position       ,     '''UP'''   )
													with cursor.hide():
																			while True:
																									if in_colab:
																															try:
																																					UpperCAmelCase_			:      Optional[int]     						=  int(builtins.input()   )
																															except ValueError:
																																					UpperCAmelCase_			:      List[str]     						=  default_choice
																									else:
																															UpperCAmelCase_			:      int     						=  self.handle_input()
																									if choice is not None:
																															reset_cursor()
																															for _ in range(len(self.choices   ) + 1   ):
																																					move_cursor(1       ,     '''UP'''   )
																																					clear_line()
																															self.write_choice(UpperCamelCase_       ,     '''\n'''   )
																															return choice












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


snake_case_     :       List[str]						=  False


class 					__a      (unittest.TestCase     ):
							pass





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

							def     UpperCAmelCase__	(							self							:     int   )					->       str:







													"""simple docstring"""
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													UpperCAmelCase_			:      Optional[int]     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Union[str, Any]     						=  pipe.dual_guided(
													    prompt='''first prompt'''       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=2       ,     output_type='''numpy'''       ,     ).images

													with tempfile.TemporaryDirectory() as tmpdirname:
																			pipe.save_pretrained(__magic_name__   )
																			UpperCAmelCase_			:      Optional[int]     						=  VersatileDiffusionPipeline.from_pretrained(__magic_name__       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Any     						=  generator.manual_seed(0   )
													UpperCAmelCase_			:      Dict     						=  pipe.dual_guided(
													    prompt='''first prompt'''       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     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							:     str   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  '''cyberpunk 2077'''
													UpperCAmelCase_			:      Union[str, Any]     						=  load_image(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''   )
													UpperCAmelCase_			:      Tuple     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[Any]     						=  pipe.dual_guided(
													    prompt=__magic_name__       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=50       ,     output_type='''numpy'''       ,     ).images

													UpperCAmelCase_			:      List[str]     						=  image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Union[str, Any]     						=  np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]   )

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

													UpperCAmelCase_			:      Tuple     						=  '''A painting of a squirrel eating a burger '''
													UpperCAmelCase_			:      Optional[int]     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      List[Any]     						=  pipe.text_to_image(
													    prompt=__magic_name__       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=50       ,     output_type='''numpy'''   ).images

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

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Any     						=  np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]   )

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

													UpperCAmelCase_			:      Tuple     						=  pipe.image_variation(__magic_name__       ,     generator=__magic_name__       ,     output_type='''numpy'''   ).images

													UpperCAmelCase_			:      Optional[Any]     						=  image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      List[str]     						=  np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]   )

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





