

'''simple docstring'''






import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaImgaImgPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()






class     lowercase_			(lowerCamelCase__       , unittest.TestCase   ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE					:						Any           =    KandinskyVaaImgaImgPipeline
  SCREAMING_SNAKE_CASE					:						int           =    ['image_embeds', 'negative_image_embeds', 'image']
  SCREAMING_SNAKE_CASE					:						List[str]           =    [
      'image_embeds',
      'negative_image_embeds',
      'image',
  ]
  SCREAMING_SNAKE_CASE					:						List[str]           =    [
      'generator',
      'height',
      'width',
      'strength',
      'guidance_scale',
      'num_inference_steps',
      'return_dict',
      'guidance_scale',
      'num_images_per_prompt',
      'output_type',
      'return_dict',
  ]
  SCREAMING_SNAKE_CASE					:						Tuple           =    False






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   return 3_2






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  Dict    ):
   return 3_2






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   return self.time_input_dim






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  List[Any]    ):
   return self.time_input_dim * 4






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   return 1_0_0






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   torch.manual_seed(0    )

   __lowercase													=				{
       '''in_channels''': 4,
       # Out channels is double in channels because predicts mean and variance
       '''out_channels''': 8,
       '''addition_embed_type''': '''image''',
       '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''),
       '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''),
       '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''',
       '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2),
       '''layers_per_block''': 1,
       '''encoder_hid_dim''': self.text_embedder_hidden_size,
       '''encoder_hid_dim_type''': '''image_proj''',
       '''cross_attention_dim''': self.cross_attention_dim,
       '''attention_head_dim''': 4,
       '''resnet_time_scale_shift''': '''scale_shift''',
       '''class_embed_type''': None,
   }

   __lowercase													=				UNetaDConditionModel(**lowercase__    )
   return model






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   return {
       "block_out_channels": [3_2, 6_4],
       "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
       "in_channels": 3,
       "latent_channels": 4,
       "layers_per_block": 1,
       "norm_num_groups": 8,
       "norm_type": "spatial",
       "num_vq_embeddings": 1_2,
       "out_channels": 3,
       "up_block_types": [
           "AttnUpDecoderBlock2D",
           "UpDecoderBlock2D",
       ],
       "vq_embed_dim": 4,
   }






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   torch.manual_seed(0    )
   __lowercase													=				VQModel(**self.dummy_movq_kwargs    )
   return model






  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   __lowercase													=				self.dummy_unet
   __lowercase													=				self.dummy_movq

   __lowercase													=				{
       '''num_train_timesteps''': 1_0_0_0,
       '''beta_schedule''': '''linear''',
       '''beta_start''': 0.0_0_0_8_5,
       '''beta_end''': 0.0_1_2,
       '''clip_sample''': False,
       '''set_alpha_to_one''': False,
       '''steps_offset''': 0,
       '''prediction_type''': '''epsilon''',
       '''thresholding''': False,
   }

   __lowercase													=				DDIMScheduler(**lowercase__    )

   __lowercase													=				{
       '''unet''': unet,
       '''scheduler''': scheduler,
       '''movq''': movq,
   }

   return components






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]      ,lowercase__     :  Optional[Any]      ,lowercase__     :  Tuple=0    ):
   __lowercase													=				floats_tensor((1, self.text_embedder_hidden_size)      ,rng=random.Random(lowercase__    )    ).to(lowercase__    )
   __lowercase													=				floats_tensor((1, self.text_embedder_hidden_size)      ,rng=random.Random(seed + 1    )    ).to(
       lowercase__    )
   # create init_image
   __lowercase													=				floats_tensor((1, 3, 6_4, 6_4)      ,rng=random.Random(lowercase__    )    ).to(lowercase__    )
   __lowercase													=				image.cpu().permute(0      ,2      ,3      ,1    )[0]
   __lowercase													=				Image.fromarray(np.uinta(lowercase__    )    ).convert('''RGB'''    ).resize((2_5_6, 2_5_6)    )

   if str(lowercase__    ).startswith('''mps'''    ):
    __lowercase													=				torch.manual_seed(lowercase__    )
   else:
    __lowercase													=				torch.Generator(device=lowercase__    ).manual_seed(lowercase__    )
   __lowercase													=				{
       '''image''': init_image,
       '''image_embeds''': image_embeds,
       '''negative_image_embeds''': negative_image_embeds,
       '''generator''': generator,
       '''height''': 6_4,
       '''width''': 6_4,
       '''num_inference_steps''': 1_0,
       '''guidance_scale''': 7.0,
       '''strength''': 0.2,
       '''output_type''': '''np''',
   }
   return inputs






  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   __lowercase													=				'''cpu'''

   __lowercase													=				self.get_dummy_components()

   __lowercase													=				self.pipeline_class(**lowercase__    )
   __lowercase													=				pipe.to(lowercase__    )

   pipe.set_progress_bar_config(disable=lowercase__    )

   __lowercase													=				pipe(**self.get_dummy_inputs(lowercase__    )    )
   __lowercase													=				output.images

   __lowercase													=				pipe(
       **self.get_dummy_inputs(lowercase__    )      ,return_dict=lowercase__      ,)[0]

   __lowercase													=				image[0, -3:, -3:, -1]
   __lowercase													=				image_from_tuple[0, -3:, -3:, -1]

   assert image.shape == (1, 6_4, 6_4, 3)

   __lowercase													=				np.array(
       [0.6_1_9_9_7_7_8, 0.6_3_9_8_4_4_0_6, 0.4_6_1_4_5_7_8_5, 0.6_2_9_4_4_9_8_4, 0.5_6_2_2_2_1_5, 0.4_7_3_0_6_1_3_2, 0.4_7_4_4_1_4_5_6, 0.4_6_0_7_6_0_6, 0.4_8_7_1_9_2_6_3]    )
   assert (
       np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
   ), F" expected_slice {expected_slice}, but got {image_slice.flatten()}"
   assert (
       np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1e-2
   ), F" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"






@slow
@require_torch_gpu
class     lowercase_			(unittest.TestCase   ):




  """simple docstring"""




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






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   __lowercase													=				load_numpy(
       '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
       '''/kandinskyv22/kandinskyv22_img2img_frog.npy'''    )

   __lowercase													=				load_image(
       '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png'''    )
   __lowercase													=				'''A red cartoon frog, 4k'''

   __lowercase													=				KandinskyVaaPriorPipeline.from_pretrained(
       '''kandinsky-community/kandinsky-2-2-prior'''      ,torch_dtype=torch.floataa    )
   pipe_prior.to(lowercase__    )

   __lowercase													=				KandinskyVaaImgaImgPipeline.from_pretrained(
       '''kandinsky-community/kandinsky-2-2-decoder'''      ,torch_dtype=torch.floataa    )
   __lowercase													=				pipeline.to(lowercase__    )

   pipeline.set_progress_bar_config(disable=lowercase__    )

   __lowercase													=				torch.Generator(device='''cpu'''    ).manual_seed(0    )
   __lowercase						,			__lowercase													=				pipe_prior(
       lowercase__      ,generator=lowercase__      ,num_inference_steps=5      ,negative_prompt=''''''      ,).to_tuple()

   __lowercase													=				pipeline(
       image=lowercase__      ,image_embeds=lowercase__      ,negative_image_embeds=lowercase__      ,generator=lowercase__      ,num_inference_steps=1_0_0      ,height=7_6_8      ,width=7_6_8      ,strength=0.2      ,output_type='''np'''      ,)

   __lowercase													=				output.images[0]

   assert image.shape == (7_6_8, 7_6_8, 3)

   assert_mean_pixel_difference(lowercase__      ,lowercase__    )




'''simple docstring'''






import re


def 	_A  (					A__   ):






 """simple docstring"""



 __lowercase													=				re.compile(
     R'''^(?:0|94|\+94|0{2}94)''' R'''7(0|1|2|4|5|6|7|8)''' R'''(-| |)''' R'''\d{7}$'''   )

 return bool(re.search(A__  ,       A__   )   )


if __name__ == "__main__":
 lowerCAmelCase__      		=			'''0094702343221'''

 print(is_sri_lankan_phone_number(phone))

