


"""simple docstring"""



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             =   False
try:
  __snake_case             =   _is_package_available("""google.colab""")
except ModuleNotFoundError:
  pass





@input.register
class _lowerCAmelCase    :







   def __init__(     self						,     UpperCamelCase__ = None						,     UpperCamelCase__ = []						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   0
       snake_case   :				List[Any]				 =   choices
       snake_case   :				Union[str, Any]				 =   prompt
       if sys.platform == "win32":
           snake_case   :				Any				 =   '''*'''
       else:
           snake_case   :				Optional[Any]				 =   '''➔ '''







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = ""						)     ->		Optional[Any]:

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       if index == self.position:
           forceWrite(F' {self.arrow_char} '						)
           self.write_choice(_lowerCamelCase						)
       else:
           forceWrite(F'    {self.choices[index]}'						)
       reset_cursor()







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = 1						)     ->		List[str]:

       '''simple docstring'''




       snake_case   :				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(_lowerCamelCase						)
       move_cursor(_lowerCamelCase						,     direction.name						)
       self.print_choice(self.position						)







   @input.mark(KEYMAP["up"]						)
   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       self.move_direction(Direction.UP						)







   @input.mark(KEYMAP["down"]						)
   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       self.move_direction(Direction.DOWN						)







   @input.mark(KEYMAP["newline"]						)
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




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







   @input.mark(KEYMAP["interrupt"]						)
   def 			lowerCamelCase		(     self						)     ->		Optional[Any]:

       '''simple docstring'''




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







   @input.mark_multiple(*[KEYMAP[str(_lowerCamelCase						)] for number in range(10						)]						)
   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   int(chr(self.current_selection						)						)
       snake_case   :				Union[str, Any]				 =   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						,     _lowerCamelCase						)
           else:
               return
       else:
           return







   def 			lowerCamelCase		(     self						,     UpperCamelCase__ = 0						)     ->		Dict:

       '''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"						)
       snake_case   :				int				 =   default_choice
       for i in range(len(self.choices						)						):
           self.print_choice(_lowerCamelCase						)
           forceWrite("\n"						)
       move_cursor(len(self.choices						) - self.position						,     "UP"						)
       with cursor.hide():
           while True:
               if in_colab:
                   try:
                       snake_case   :				Optional[Any]				 =   int(builtins.input()						)
                   except ValueError:
                       snake_case   :				List[Any]				 =   default_choice
               else:
                   snake_case   :				Dict				 =   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(_lowerCamelCase						,     "\n"						)
                   return choice









"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class _lowerCAmelCase    (				unittest.TestCase     ):







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				int				 =   UNetaDModel(
           block_out_channels=(32, 64)						,     layers_per_block=2						,     sample_size=32						,     in_channels=3						,     out_channels=3						,     down_block_types=("DownBlock2D", "AttnDownBlock2D")						,     up_block_types=("AttnUpBlock2D", "UpBlock2D")						,     )
       return model







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   self.dummy_uncond_unet
       snake_case   :				Tuple				 =   KarrasVeScheduler()

       snake_case   :				int				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Optional[Any]				 =   torch.manual_seed(0						)
       snake_case   :				List[Any]				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Dict				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						,     return_dict=UpperCamelCase__						)[0]

       snake_case   :				Tuple				 =   image[0, -3:, -3:, -1]
       snake_case   :				Tuple				 =   image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case   :				int				 =   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]						)

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







@slow
@require_torch
class _lowerCAmelCase    (				unittest.TestCase     ):







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   "google/ncsnpp-celebahq-256"
       snake_case   :				List[str]				 =   UNetaDModel.from_pretrained(UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   KarrasVeScheduler()

       snake_case   :				Optional[int]				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Union[str, Any]				 =   pipe(num_inference_steps=20						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				int				 =   image[0, -3:, -3:, -1]
       assert image.shape == (1, 256, 256, 3)
       snake_case   :				Any				 =   np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]						)

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





