





from typing import Dict, Iterable, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends


if is_vision_available():
						import PIL

# soft dependency
if is_pytesseract_available():
						import pytesseract

SCREAMING_SNAKE_CASE_		     = logging.get_logger(__name__)



def      __lowercase     (   _SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)				->     Union[str, Any]:


						'''simple docstring'''


						return [
						    int(10_00 * (box[0] / width)			),
						    int(10_00 * (box[1] / height)			),
						    int(10_00 * (box[2] / width)			),
						    int(10_00 * (box[3] / height)			),
						]



def      __lowercase     (   _SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)				->     List[Any]:


						'''simple docstring'''


						SCREAMING_SNAKE_CASE	=		to_pil_image(_SCREAMING_SNAKE_CASE			)
						SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		pil_image.size
						SCREAMING_SNAKE_CASE	=		pytesseract.image_to_data(_SCREAMING_SNAKE_CASE				,		lang=_SCREAMING_SNAKE_CASE				,		output_type="""dict"""				,		config=_SCREAMING_SNAKE_CASE			)
						SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		data["""text"""], data["""left"""], data["""top"""], data["""width"""], data["""height"""]

						# filter empty words and corresponding coordinates
						SCREAMING_SNAKE_CASE	=		[idx for idx, word in enumerate(_SCREAMING_SNAKE_CASE			) if not word.strip()]
						SCREAMING_SNAKE_CASE	=		[word for idx, word in enumerate(_SCREAMING_SNAKE_CASE			) if idx not in irrelevant_indices]
						SCREAMING_SNAKE_CASE	=		[coord for idx, coord in enumerate(_SCREAMING_SNAKE_CASE			) if idx not in irrelevant_indices]
						SCREAMING_SNAKE_CASE	=		[coord for idx, coord in enumerate(_SCREAMING_SNAKE_CASE			) if idx not in irrelevant_indices]
						SCREAMING_SNAKE_CASE	=		[coord for idx, coord in enumerate(_SCREAMING_SNAKE_CASE			) if idx not in irrelevant_indices]
						SCREAMING_SNAKE_CASE	=		[coord for idx, coord in enumerate(_SCREAMING_SNAKE_CASE			) if idx not in irrelevant_indices]

						# turn coordinates into (left, top, left+width, top+height) format
						SCREAMING_SNAKE_CASE	=		[]
						for x, y, w, h in zip(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			):
												SCREAMING_SNAKE_CASE	=		[x, y, x + w, y + h]
												actual_boxes.append(_SCREAMING_SNAKE_CASE			)

						# finally, normalize the bounding boxes
						SCREAMING_SNAKE_CASE	=		[]
						for box in actual_boxes:
												normalized_boxes.append(normalize_box(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)			)

						assert len(_SCREAMING_SNAKE_CASE			) == len(_SCREAMING_SNAKE_CASE			), "Not as many words as there are bounding boxes"

						return words, normalized_boxes



class 	UpperCamelCase__				(   lowerCAmelCase_     ):



					'''simple docstring'''

					__snake_case					:			Optional[Any]				=    ["pixel_values"]






					def __init__(							self		:					Union[str, Any]		,lowerCamelCase__		:					bool = True		,lowerCamelCase__		:					Dict[str, int] = None		,lowerCamelCase__		:					PILImageResampling = PILImageResampling.BILINEAR		,lowerCamelCase__		:					bool = True		,lowerCamelCase__		:					float = 1 / 255		,lowerCamelCase__		:					bool = True		,lowerCamelCase__		:					Union[float, Iterable[float]] = None		,lowerCamelCase__		:					Union[float, Iterable[float]] = None		,lowerCamelCase__		:					bool = True		,lowerCamelCase__		:					Optional[str] = None		,lowerCamelCase__		:					Optional[str] = ""		,**lowerCamelCase__		:					str		,)  ->	None:




											'''simple docstring'''


											super().__init__(**lowerCamelCase__ )
											SCREAMING_SNAKE_CASE	=		size if size is not None else {"""height""": 224, """width""": 224}
											SCREAMING_SNAKE_CASE	=		get_size_dict(lowerCamelCase__ )

											SCREAMING_SNAKE_CASE	=		do_resize
											SCREAMING_SNAKE_CASE	=		size
											SCREAMING_SNAKE_CASE	=		resample
											SCREAMING_SNAKE_CASE	=		do_rescale
											SCREAMING_SNAKE_CASE	=		rescale_value
											SCREAMING_SNAKE_CASE	=		do_normalize
											SCREAMING_SNAKE_CASE	=		image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
											SCREAMING_SNAKE_CASE	=		image_std if image_std is not None else IMAGENET_STANDARD_STD
											SCREAMING_SNAKE_CASE	=		apply_ocr
											SCREAMING_SNAKE_CASE	=		ocr_lang
											SCREAMING_SNAKE_CASE	=		tesseract_config






					def        SCREAMING_SNAKE_CASE__						(							self		:					int		,lowerCamelCase__		:					np.ndarray		,lowerCamelCase__		:					Dict[str, int]		,lowerCamelCase__		:					PILImageResampling = PILImageResampling.BILINEAR		,lowerCamelCase__		:					Optional[Union[str, ChannelDimension]] = None		,**lowerCamelCase__		:					Dict		,)  ->	np.ndarray:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		get_size_dict(lowerCamelCase__ )
											if "height" not in size or "width" not in size:
																	raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" )
											SCREAMING_SNAKE_CASE	=		(size["""height"""], size["""width"""])
											return resize(lowerCamelCase__		,size=lowerCamelCase__		,resample=lowerCamelCase__		,data_format=lowerCamelCase__		,**lowerCamelCase__ )






					def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[Any]		,lowerCamelCase__		:					np.ndarray		,lowerCamelCase__		:					Union[int, float]		,lowerCamelCase__		:					Optional[Union[str, ChannelDimension]] = None		,**lowerCamelCase__		:					List[Any]		,)  ->	np.ndarray:




											'''simple docstring'''


											return rescale(lowerCamelCase__		,scale=lowerCamelCase__		,data_format=lowerCamelCase__		,**lowerCamelCase__ )






					def        SCREAMING_SNAKE_CASE__						(							self		:					List[str]		,lowerCamelCase__		:					np.ndarray		,lowerCamelCase__		:					Union[float, Iterable[float]]		,lowerCamelCase__		:					Union[float, Iterable[float]]		,lowerCamelCase__		:					Optional[Union[str, ChannelDimension]] = None		,**lowerCamelCase__		:					Optional[int]		,)  ->	np.ndarray:




											'''simple docstring'''


											return normalize(lowerCamelCase__		,mean=lowerCamelCase__		,std=lowerCamelCase__		,data_format=lowerCamelCase__		,**lowerCamelCase__ )






					def        SCREAMING_SNAKE_CASE__						(							self		:					List[str]		,lowerCamelCase__		:					ImageInput		,lowerCamelCase__		:					bool = None		,lowerCamelCase__		:					Dict[str, int] = None		,lowerCamelCase__		:					Dict=None		,lowerCamelCase__		:					bool = None		,lowerCamelCase__		:					float = None		,lowerCamelCase__		:					bool = None		,lowerCamelCase__		:					Union[float, Iterable[float]] = None		,lowerCamelCase__		:					Union[float, Iterable[float]] = None		,lowerCamelCase__		:					bool = None		,lowerCamelCase__		:					Optional[str] = None		,lowerCamelCase__		:					Optional[str] = None		,lowerCamelCase__		:					Optional[Union[str, TensorType]] = None		,lowerCamelCase__		:					ChannelDimension = ChannelDimension.FIRST		,**lowerCamelCase__		:					Union[str, Any]		,)  ->	PIL.Image.Image:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		do_resize if do_resize is not None else self.do_resize
											SCREAMING_SNAKE_CASE	=		size if size is not None else self.size
											SCREAMING_SNAKE_CASE	=		get_size_dict(lowerCamelCase__ )
											SCREAMING_SNAKE_CASE	=		resample if resample is not None else self.resample
											SCREAMING_SNAKE_CASE	=		do_rescale if do_rescale is not None else self.do_rescale
											SCREAMING_SNAKE_CASE	=		rescale_factor if rescale_factor is not None else self.rescale_factor
											SCREAMING_SNAKE_CASE	=		do_normalize if do_normalize is not None else self.do_normalize
											SCREAMING_SNAKE_CASE	=		image_mean if image_mean is not None else self.image_mean
											SCREAMING_SNAKE_CASE	=		image_std if image_std is not None else self.image_std
											SCREAMING_SNAKE_CASE	=		apply_ocr if apply_ocr is not None else self.apply_ocr
											SCREAMING_SNAKE_CASE	=		ocr_lang if ocr_lang is not None else self.ocr_lang
											SCREAMING_SNAKE_CASE	=		tesseract_config if tesseract_config is not None else self.tesseract_config

											SCREAMING_SNAKE_CASE	=		make_list_of_images(lowerCamelCase__ )

											if not valid_images(lowerCamelCase__ ):
																	raise ValueError(
																	    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
																	    """torch.Tensor, tf.Tensor or jax.ndarray.""" )

											if do_resize and size is None:
																	raise ValueError("""Size must be specified if do_resize is True.""" )

											if do_rescale and rescale_factor is None:
																	raise ValueError("""Rescale factor must be specified if do_rescale is True.""" )

											if do_normalize and (image_mean is None or image_std is None):
																	raise ValueError("""If do_normalize is True, image_mean and image_std must be specified.""" )

											# All transformations expect numpy arrays.
											SCREAMING_SNAKE_CASE	=		[to_numpy_array(lowerCamelCase__ ) for image in images]

											# Tesseract OCR to get words + normalized bounding boxes
											if apply_ocr:
																	requires_backends(self		,"""pytesseract""" )
																	SCREAMING_SNAKE_CASE	=		[]
																	SCREAMING_SNAKE_CASE	=		[]
																	for image in images:
																							SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE	=		apply_tesseract(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__ )
																							words_batch.append(lowerCamelCase__ )
																							boxes_batch.append(lowerCamelCase__ )

											if do_resize:
																	SCREAMING_SNAKE_CASE	=		[self.resize(image=lowerCamelCase__		,size=lowerCamelCase__		,resample=lowerCamelCase__ ) for image in images]

											if do_rescale:
																	SCREAMING_SNAKE_CASE	=		[self.rescale(image=lowerCamelCase__		,scale=lowerCamelCase__ ) for image in images]

											if do_normalize:
																	SCREAMING_SNAKE_CASE	=		[self.normalize(image=lowerCamelCase__		,mean=lowerCamelCase__		,std=lowerCamelCase__ ) for image in images]

											SCREAMING_SNAKE_CASE	=		[to_channel_dimension_format(lowerCamelCase__		,lowerCamelCase__ ) for image in images]

											SCREAMING_SNAKE_CASE	=		BatchFeature(data={"""pixel_values""": images}		,tensor_type=lowerCamelCase__ )

											if apply_ocr:
																	SCREAMING_SNAKE_CASE	=		words_batch
																	SCREAMING_SNAKE_CASE	=		boxes_batch
											return data









import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
      import onnxruntime as ort

class 	UpperCamelCase__				(   lowerCAmelCase_							,   unittest.TestCase     ):



     '''simple docstring'''

     __snake_case					:			Union[str, Any]				=    "ssube/stable-diffusion-x4-upscaler-onnx"






     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple		,lowerCamelCase__		:					List[str]=0 )  ->	Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		floats_tensor((1, 3, 128, 128)		,rng=random.Random(lowerCamelCase__ ) )
           SCREAMING_SNAKE_CASE	=		torch.manual_seed(lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		{
               """prompt""": """A painting of a squirrel eating a burger""",
               """image""": image,
               """generator""": generator,
               """num_inference_steps""": 3,
               """guidance_scale""": 7.5,
               """output_type""": """numpy""",
           }
           return inputs






     def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[int] )  ->	Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,provider="""CPUExecutionProvider""" )
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		self.get_dummy_inputs()
           SCREAMING_SNAKE_CASE	=		pipe(**lowerCamelCase__ ).images
           SCREAMING_SNAKE_CASE	=		image[0, -3:, -3:, -1].flatten()

           # started as 128, should now be 512
           assert image.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223] )
           assert np.abs(image_slice - expected_slice ).max() < 1e-1






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,provider="""CPUExecutionProvider""" )
           SCREAMING_SNAKE_CASE	=		PNDMScheduler.from_config(pipe.scheduler.config		,skip_prk_steps=lowerCamelCase__ )
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		self.get_dummy_inputs()
           SCREAMING_SNAKE_CASE	=		pipe(**lowerCamelCase__ ).images
           SCREAMING_SNAKE_CASE	=		image[0, -3:, -3:, -1]

           assert image.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.6898892, 0.59240556, 0.52499527, 0.58866215, 0.52258235, 0.52572715, 0.62414473, 0.6174387, 0.6214964] )
           assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1






     def        SCREAMING_SNAKE_CASE__						(							self		:					Dict )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,provider="""CPUExecutionProvider""" )
           SCREAMING_SNAKE_CASE	=		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config )
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		self.get_dummy_inputs()
           SCREAMING_SNAKE_CASE	=		pipe(**lowerCamelCase__ ).images
           SCREAMING_SNAKE_CASE	=		image[0, -3:, -3:, -1]

           assert image.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.7659278, 0.76437664, 0.75579107, 0.7691116, 0.77666986, 0.7727672, 0.7758664, 0.7812226, 0.76942515] )

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






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[Any] )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,provider="""CPUExecutionProvider""" )
           SCREAMING_SNAKE_CASE	=		EulerDiscreteScheduler.from_config(pipe.scheduler.config )
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		self.get_dummy_inputs()
           SCREAMING_SNAKE_CASE	=		pipe(**lowerCamelCase__ ).images
           SCREAMING_SNAKE_CASE	=		image[0, -3:, -3:, -1]

           assert image.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223] )
           assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1






     def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[int] )  ->	Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,provider="""CPUExecutionProvider""" )
           SCREAMING_SNAKE_CASE	=		EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config )
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		self.get_dummy_inputs()
           SCREAMING_SNAKE_CASE	=		pipe(**lowerCamelCase__ ).images
           SCREAMING_SNAKE_CASE	=		image[0, -3:, -3:, -1]

           assert image.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.77424496, 0.773601, 0.7645288, 0.7769598, 0.7772739, 0.7738688, 0.78187233, 0.77879584, 0.767043] )

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

@nightly
@require_onnxruntime
@require_torch_gpu
class 	UpperCamelCase__				(   unittest.TestCase     ):



     '''simple docstring'''

     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	Optional[int]:




           '''simple docstring'''


           return (
               "CUDAExecutionProvider",
               {
                   "gpu_mem_limit": "15000000000",  # 15GB
                   "arena_extend_strategy": "kSameAsRequested",
               },
           )






     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					Union[str, Any] )  ->	List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		ort.SessionOptions()
           SCREAMING_SNAKE_CASE	=		False
           return options






     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/img2img/sketch-mountains-input.jpg""" )
           SCREAMING_SNAKE_CASE	=		init_image.resize((128, 128) )
           # using the PNDM scheduler by default
           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(
               """ssube/stable-diffusion-x4-upscaler-onnx"""		,provider=self.gpu_provider		,sess_options=self.gpu_options		,)
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		"""A fantasy landscape, trending on artstation"""

           SCREAMING_SNAKE_CASE	=		torch.manual_seed(0 )
           SCREAMING_SNAKE_CASE	=		pipe(
               prompt=lowerCamelCase__		,image=lowerCamelCase__		,guidance_scale=7.5		,num_inference_steps=10		,generator=lowerCamelCase__		,output_type="""np"""		,)
           SCREAMING_SNAKE_CASE	=		output.images
           SCREAMING_SNAKE_CASE	=		images[0, 255:258, 383:386, -1]

           assert images.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972] )
           # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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






     def        SCREAMING_SNAKE_CASE__						(							self		:					Any )  ->	Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/img2img/sketch-mountains-input.jpg""" )
           SCREAMING_SNAKE_CASE	=		init_image.resize((128, 128) )
           SCREAMING_SNAKE_CASE	=		LMSDiscreteScheduler.from_pretrained(
               """ssube/stable-diffusion-x4-upscaler-onnx"""		,subfolder="""scheduler""" )
           SCREAMING_SNAKE_CASE	=		OnnxStableDiffusionUpscalePipeline.from_pretrained(
               """ssube/stable-diffusion-x4-upscaler-onnx"""		,scheduler=lowerCamelCase__		,provider=self.gpu_provider		,sess_options=self.gpu_options		,)
           pipe.set_progress_bar_config(disable=lowerCamelCase__ )

           SCREAMING_SNAKE_CASE	=		"""A fantasy landscape, trending on artstation"""

           SCREAMING_SNAKE_CASE	=		torch.manual_seed(0 )
           SCREAMING_SNAKE_CASE	=		pipe(
               prompt=lowerCamelCase__		,image=lowerCamelCase__		,guidance_scale=7.5		,num_inference_steps=20		,generator=lowerCamelCase__		,output_type="""np"""		,)
           SCREAMING_SNAKE_CASE	=		output.images
           SCREAMING_SNAKE_CASE	=		images[0, 255:258, 383:386, -1]

           assert images.shape == (1, 512, 512, 3)
           SCREAMING_SNAKE_CASE	=		np.array(
               [0.50173753, 0.50223356, 0.502039, 0.50233036, 0.5023725, 0.5022601, 0.5018758, 0.50234085, 0.50241566] )
           # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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


