'''simple docstring'''
import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
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


enable_full_determinism()






class 			__lowercase		(						unittest.TestCase						):







     """simple docstring"""






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







     @property
     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[Any]		    =  1
         __UpperCamelCase	:	List[str]		    =  3
         __UpperCamelCase	:	Dict		    =  (3_2, 3_2)

         __UpperCamelCase	:	Optional[Any]		    =  floats_tensor((batch_size, num_channels) + sizes     ,			rng=random.Random(0    )    ).to(_lowerCamelCase    )
         return image







     @property
     def lowerCAmelCase     (		self    ):
         torch.manual_seed(0    )
         __UpperCamelCase	:	Any		    =  UNetaDConditionModel(
             block_out_channels=(3_2, 3_2, 6_4)     ,			layers_per_block=2     ,			sample_size=3_2     ,			in_channels=7     ,			out_channels=4     ,			down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D', 'CrossAttnDownBlock2D')     ,			up_block_types=('CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'UpBlock2D')     ,			cross_attention_dim=3_2     ,			attention_head_dim=8     ,			use_linear_projection=_lowerCamelCase     ,			only_cross_attention=(True, True, False)     ,			num_class_embeds=1_0_0     ,			)
         return model







     @property
     def lowerCAmelCase     (		self    ):
         torch.manual_seed(0    )
         __UpperCamelCase	:	List[str]		    =  AutoencoderKL(
             block_out_channels=[3_2, 3_2, 6_4]     ,			in_channels=3     ,			out_channels=3     ,			down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D']     ,			up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D']     ,			latent_channels=4     ,			)
         return model







     @property
     def lowerCAmelCase     (		self    ):
         torch.manual_seed(0    )
         __UpperCamelCase	:	Optional[Any]		    =  CLIPTextConfig(
             bos_token_id=0     ,			eos_token_id=2     ,			hidden_size=3_2     ,			intermediate_size=3_7     ,			layer_norm_eps=1E-0_5     ,			num_attention_heads=4     ,			num_hidden_layers=5     ,			pad_token_id=1     ,			vocab_size=1_0_0_0     ,			hidden_act='gelu'     ,			projection_dim=5_1_2     ,			)
         return CLIPTextModel(_lowerCamelCase    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[Any]		    =  'cpu'  # ensure determinism for the device-dependent torch.Generator
         __UpperCamelCase	:	str		    =  self.dummy_cond_unet_upscale
         __UpperCamelCase	:	Union[str, Any]		    =  DDPMScheduler()
         __UpperCamelCase	:	str		    =  DDIMScheduler(prediction_type='v_prediction'    )
         __UpperCamelCase	:	Tuple		    =  self.dummy_vae
         __UpperCamelCase	:	Tuple		    =  self.dummy_text_encoder
         __UpperCamelCase	:	Tuple		    =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

         __UpperCamelCase	:	Dict		    =  self.dummy_image.cpu().permute(0     ,			2     ,			3     ,			1    )[0]
         __UpperCamelCase	:	Any		    =  Image.fromarray(np.uinta(_lowerCamelCase    )    ).convert('RGB'    ).resize((6_4, 6_4)    )

         # make sure here that pndm scheduler skips prk
         __UpperCamelCase	:	List[str]		    =  StableDiffusionUpscalePipeline(
             unet=_lowerCamelCase     ,			low_res_scheduler=_lowerCamelCase     ,			scheduler=_lowerCamelCase     ,			vae=_lowerCamelCase     ,			text_encoder=_lowerCamelCase     ,			tokenizer=_lowerCamelCase     ,			max_noise_level=3_5_0     ,			)
         __UpperCamelCase	:	List[Any]		    =  sd_pipe.to(_lowerCamelCase    )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase    )

         __UpperCamelCase	:	List[str]		    =  'A painting of a squirrel eating a burger'
         __UpperCamelCase	:	int		    =  torch.Generator(device=_lowerCamelCase    ).manual_seed(0    )
         __UpperCamelCase	:	str		    =  sd_pipe(
             [prompt]     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			guidance_scale=6.0     ,			noise_level=2_0     ,			num_inference_steps=2     ,			output_type='np'     ,			)

         __UpperCamelCase	:	Any		    =  output.images

         __UpperCamelCase	:	Union[str, Any]		    =  torch.Generator(device=_lowerCamelCase    ).manual_seed(0    )
         __UpperCamelCase	:	Optional[Any]		    =  sd_pipe(
             [prompt]     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			guidance_scale=6.0     ,			noise_level=2_0     ,			num_inference_steps=2     ,			output_type='np'     ,			return_dict=_lowerCamelCase     ,			)[0]

         __UpperCamelCase	:	List[str]		    =  image[0, -3:, -3:, -1]
         __UpperCamelCase	:	Dict		    =  image_from_tuple[0, -3:, -3:, -1]

         __UpperCamelCase	:	str		    =  low_res_image.size[0] * 4
         assert image.shape == (1, expected_height_width, expected_height_width, 3)
         __UpperCamelCase	:	Tuple		    =  np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1]    )

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







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Tuple		    =  'cpu'  # ensure determinism for the device-dependent torch.Generator
         __UpperCamelCase	:	Tuple		    =  self.dummy_cond_unet_upscale
         __UpperCamelCase	:	Optional[Any]		    =  DDPMScheduler()
         __UpperCamelCase	:	Dict		    =  DDIMScheduler(prediction_type='v_prediction'    )
         __UpperCamelCase	:	int		    =  self.dummy_vae
         __UpperCamelCase	:	List[Any]		    =  self.dummy_text_encoder
         __UpperCamelCase	:	Optional[int]		    =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

         __UpperCamelCase	:	List[Any]		    =  self.dummy_image.cpu().permute(0     ,			2     ,			3     ,			1    )[0]
         __UpperCamelCase	:	Union[str, Any]		    =  Image.fromarray(np.uinta(_lowerCamelCase    )    ).convert('RGB'    ).resize((6_4, 6_4)    )

         # make sure here that pndm scheduler skips prk
         __UpperCamelCase	:	Tuple		    =  StableDiffusionUpscalePipeline(
             unet=_lowerCamelCase     ,			low_res_scheduler=_lowerCamelCase     ,			scheduler=_lowerCamelCase     ,			vae=_lowerCamelCase     ,			text_encoder=_lowerCamelCase     ,			tokenizer=_lowerCamelCase     ,			max_noise_level=3_5_0     ,			)
         __UpperCamelCase	:	List[Any]		    =  sd_pipe.to(_lowerCamelCase    )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase    )

         __UpperCamelCase	:	Optional[Any]		    =  'A painting of a squirrel eating a burger'
         __UpperCamelCase	:	Optional[Any]		    =  sd_pipe(
             2 * [prompt]     ,			image=2 * [low_res_image]     ,			guidance_scale=6.0     ,			noise_level=2_0     ,			num_inference_steps=2     ,			output_type='np'     ,			)
         __UpperCamelCase	:	Optional[int]		    =  output.images
         assert image.shape[0] == 2

         __UpperCamelCase	:	Optional[Any]		    =  torch.Generator(device=_lowerCamelCase    ).manual_seed(0    )
         __UpperCamelCase	:	List[Any]		    =  sd_pipe(
             [prompt]     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			num_images_per_prompt=2     ,			guidance_scale=6.0     ,			noise_level=2_0     ,			num_inference_steps=2     ,			output_type='np'     ,			)
         __UpperCamelCase	:	List[str]		    =  output.images
         assert image.shape[0] == 2







     @unittest.skipIf(torch_device != 'cuda'     ,			'This test requires a GPU'    )
     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	int		    =  self.dummy_cond_unet_upscale
         __UpperCamelCase	:	Tuple		    =  DDPMScheduler()
         __UpperCamelCase	:	List[Any]		    =  DDIMScheduler(prediction_type='v_prediction'    )
         __UpperCamelCase	:	str		    =  self.dummy_vae
         __UpperCamelCase	:	Union[str, Any]		    =  self.dummy_text_encoder
         __UpperCamelCase	:	str		    =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

         __UpperCamelCase	:	Union[str, Any]		    =  self.dummy_image.cpu().permute(0     ,			2     ,			3     ,			1    )[0]
         __UpperCamelCase	:	Tuple		    =  Image.fromarray(np.uinta(_lowerCamelCase    )    ).convert('RGB'    ).resize((6_4, 6_4)    )

         # put models in fp16, except vae as it overflows in fp16
         __UpperCamelCase	:	Optional[int]		    =  unet.half()
         __UpperCamelCase	:	int		    =  text_encoder.half()

         # make sure here that pndm scheduler skips prk
         __UpperCamelCase	:	Optional[Any]		    =  StableDiffusionUpscalePipeline(
             unet=_lowerCamelCase     ,			low_res_scheduler=_lowerCamelCase     ,			scheduler=_lowerCamelCase     ,			vae=_lowerCamelCase     ,			text_encoder=_lowerCamelCase     ,			tokenizer=_lowerCamelCase     ,			max_noise_level=3_5_0     ,			)
         __UpperCamelCase	:	Optional[int]		    =  sd_pipe.to(_lowerCamelCase    )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase    )

         __UpperCamelCase	:	Tuple		    =  'A painting of a squirrel eating a burger'
         __UpperCamelCase	:	Tuple		    =  torch.manual_seed(0    )
         __UpperCamelCase	:	Tuple		    =  sd_pipe(
             [prompt]     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			num_inference_steps=2     ,			output_type='np'     ,			).images

         __UpperCamelCase	:	Optional[Any]		    =  low_res_image.size[0] * 4
         assert image.shape == (1, expected_height_width, expected_height_width, 3)






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







     """simple docstring"""






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







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[str]		    =  load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
             '/sd2-upscale/low_res_cat.png'    )
         __UpperCamelCase	:	str		    =  load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
             '/upsampled_cat.npy'    )

         __UpperCamelCase	:	Union[str, Any]		    =  'stabilityai/stable-diffusion-x4-upscaler'
         __UpperCamelCase	:	Tuple		    =  StableDiffusionUpscalePipeline.from_pretrained(_lowerCamelCase    )
         pipe.to(_lowerCamelCase    )
         pipe.set_progress_bar_config(disable=_lowerCamelCase    )
         pipe.enable_attention_slicing()

         __UpperCamelCase	:	Optional[Any]		    =  'a cat sitting on a park bench'

         __UpperCamelCase	:	Union[str, Any]		    =  torch.manual_seed(0    )
         __UpperCamelCase	:	Tuple		    =  pipe(
             prompt=_lowerCamelCase     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			output_type='np'     ,			)
         __UpperCamelCase	:	List[str]		    =  output.images[0]

         assert image.shape == (5_1_2, 5_1_2, 3)
         assert np.abs(expected_image - image    ).max() < 1E-3







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Dict		    =  load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
             '/sd2-upscale/low_res_cat.png'    )
         __UpperCamelCase	:	Optional[int]		    =  load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
             '/upsampled_cat_fp16.npy'    )

         __UpperCamelCase	:	str		    =  'stabilityai/stable-diffusion-x4-upscaler'
         __UpperCamelCase	:	Optional[Any]		    =  StableDiffusionUpscalePipeline.from_pretrained(
             _lowerCamelCase     ,			torch_dtype=torch.floataa     ,			)
         pipe.to(_lowerCamelCase    )
         pipe.set_progress_bar_config(disable=_lowerCamelCase    )
         pipe.enable_attention_slicing()

         __UpperCamelCase	:	Tuple		    =  'a cat sitting on a park bench'

         __UpperCamelCase	:	Any		    =  torch.manual_seed(0    )
         __UpperCamelCase	:	Union[str, Any]		    =  pipe(
             prompt=_lowerCamelCase     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			output_type='np'     ,			)
         __UpperCamelCase	:	Tuple		    =  output.images[0]

         assert image.shape == (5_1_2, 5_1_2, 3)
         assert np.abs(expected_image - image    ).max() < 5E-1







     def lowerCAmelCase     (		self    ):
         torch.cuda.empty_cache()
         torch.cuda.reset_max_memory_allocated()
         torch.cuda.reset_peak_memory_stats()

         __UpperCamelCase	:	Union[str, Any]		    =  load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
             '/sd2-upscale/low_res_cat.png'    )

         __UpperCamelCase	:	Optional[Any]		    =  'stabilityai/stable-diffusion-x4-upscaler'
         __UpperCamelCase	:	Optional[int]		    =  StableDiffusionUpscalePipeline.from_pretrained(
             _lowerCamelCase     ,			torch_dtype=torch.floataa     ,			)
         pipe.to(_lowerCamelCase    )
         pipe.set_progress_bar_config(disable=_lowerCamelCase    )
         pipe.enable_attention_slicing(1    )
         pipe.enable_sequential_cpu_offload()

         __UpperCamelCase	:	str		    =  'a cat sitting on a park bench'

         __UpperCamelCase	:	List[str]		    =  torch.manual_seed(0    )
         __UpperCamelCase	:	List[Any]		    =  pipe(
             prompt=_lowerCamelCase     ,			image=_lowerCamelCase     ,			generator=_lowerCamelCase     ,			num_inference_steps=5     ,			output_type='np'     ,			)

         __UpperCamelCase	:	str		    =  torch.cuda.max_memory_allocated()
         # make sure that less than 2.9 GB is allocated
         assert mem_bytes < 2.9 * 1_0**9




'''simple docstring'''
import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
  import bsa
  from bsa import BeautifulSoup


a=  logging.get_logger(__name__)






class 			__lowercase		(						_lowerCamelCase						):







     """simple docstring"""






     def __init__(		self     ,			**_lowerCamelCase    ):
         requires_backends(self     ,			['bs4']    )
         super().__init__(**_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	str		    =  []
         __UpperCamelCase	:	Dict		    =  []
         __UpperCamelCase	:	Union[str, Any]		    =  element if element.name else element.parent
         for parent in child.parents:  # type: bs4.element.Tag
             __UpperCamelCase	:	str		    =  parent.find_all(child.name     ,			recursive=_lowerCamelCase    )
             xpath_tags.append(child.name    )
             xpath_subscripts.append(
                 0 if 1 == len(_lowerCamelCase    ) else next(i for i, s in enumerate(_lowerCamelCase     ,			1    ) if s is child    )    )
             __UpperCamelCase	:	Union[str, Any]		    =  parent
         xpath_tags.reverse()
         xpath_subscripts.reverse()
         return xpath_tags, xpath_subscripts







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	List[str]		    =  BeautifulSoup(_lowerCamelCase     ,			'html.parser'    )

         __UpperCamelCase	:	int		    =  []
         __UpperCamelCase	:	List[Any]		    =  []
         __UpperCamelCase	:	int		    =  []

         for element in html_code.descendants:
             if type(_lowerCamelCase    ) == bsa.element.NavigableString:
                 if type(element.parent    ) != bsa.element.Tag:
                     continue

                 __UpperCamelCase	:	Tuple		    =  html.unescape(_lowerCamelCase    ).strip()
                 if not text_in_this_tag:
                     continue

                 all_doc_strings.append(_lowerCamelCase    )

                 __UpperCamelCase    ,					__UpperCamelCase	:	Union[str, Any]		    =  self.xpath_soup(_lowerCamelCase    )
                 stringaxtag_seq.append(_lowerCamelCase    )
                 stringaxsubs_seq.append(_lowerCamelCase    )

         if len(_lowerCamelCase    ) != len(_lowerCamelCase    ):
             raise ValueError('Number of doc strings and xtags does not correspond'    )
         if len(_lowerCamelCase    ) != len(_lowerCamelCase    ):
             raise ValueError('Number of doc strings and xsubs does not correspond'    )

         return all_doc_strings, stringaxtag_seq, stringaxsubs_seq







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	Dict		    =  ''
         for tagname, subs in zip(_lowerCamelCase     ,			_lowerCamelCase    ):
             xpath += f"""/{tagname}"""
             if subs != 0:
                 xpath += f"""[{subs}]"""
         return xpath







     def __call__(		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	List[Any]		    =  False

         # Check that strings has a valid type
         if isinstance(_lowerCamelCase     ,			_lowerCamelCase    ):
             __UpperCamelCase	:	List[str]		    =  True
         elif isinstance(_lowerCamelCase     ,			(list, tuple)    ):
             if len(_lowerCamelCase    ) == 0 or isinstance(html_strings[0]     ,			_lowerCamelCase    ):
                 __UpperCamelCase	:	Optional[int]		    =  True

         if not valid_strings:
             raise ValueError(
                 'HTML strings must of type `str`, `List[str]` (batch of examples), '
                 f"""but is of type {type(_lowerCamelCase    )}."""    )

         __UpperCamelCase	:	Dict		    =  bool(isinstance(_lowerCamelCase     ,			(list, tuple)    ) and (isinstance(html_strings[0]     ,			_lowerCamelCase    ))    )

         if not is_batched:
             __UpperCamelCase	:	Tuple		    =  [html_strings]

         # Get nodes + xpaths
         __UpperCamelCase	:	Tuple		    =  []
         __UpperCamelCase	:	Tuple		    =  []
         for html_string in html_strings:
             __UpperCamelCase    ,					__UpperCamelCase    ,					__UpperCamelCase	:	str		    =  self.get_three_from_single(_lowerCamelCase    )
             nodes.append(_lowerCamelCase    )
             __UpperCamelCase	:	Union[str, Any]		    =  []
             for node, tag_list, sub_list in zip(_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
                 __UpperCamelCase	:	Optional[Any]		    =  self.construct_xpath(_lowerCamelCase     ,			_lowerCamelCase    )
                 xpath_strings.append(_lowerCamelCase    )
             xpaths.append(_lowerCamelCase    )

         # return as Dict
         __UpperCamelCase	:	Optional[int]		    =  {'nodes': nodes, 'xpaths': xpaths}
         __UpperCamelCase	:	Union[str, Any]		    =  BatchFeature(data=_lowerCamelCase     ,			tensor_type=_lowerCamelCase    )

         return encoded_inputs



