



import math
from typing import Callable, List, Optional, Union

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

from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
from diffusers.schedulers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase=[]      )   ->					List[str]:


				"""simple docstring"""

				snake_case__	:      Any     		=	size[0] - overlap_pixels * 2
				snake_case__	:      List[str]     		=	size[1] - overlap_pixels * 2
				for letter in ["l", "r"]:
								if letter in remove_borders:
												size_x += overlap_pixels
				for letter in ["t", "b"]:
								if letter in remove_borders:
												size_y += overlap_pixels
				snake_case__	:      List[Any]     		=	np.ones((size_y, size_x)  ,  dtype=np.uinta      ) * 255
				snake_case__	:      List[str]     		=	np.pad(__lowerCAmelCase  ,  mode='''linear_ramp'''  ,  pad_width=__lowerCAmelCase  ,  end_values=0      )

				if "l" in remove_borders:
								snake_case__	:      Optional[int]     		=	mask[:, overlap_pixels : mask.shape[1]]
				if "r" in remove_borders:
								snake_case__	:      Optional[Any]     		=	mask[:, 0 : mask.shape[1] - overlap_pixels]
				if "t" in remove_borders:
								snake_case__	:      List[str]     		=	mask[overlap_pixels : mask.shape[0], :]
				if "b" in remove_borders:
								snake_case__	:      Union[str, Any]     		=	mask[0 : mask.shape[0] - overlap_pixels, :]
				return mask



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					int:


				"""simple docstring"""

				return max(__lowerCAmelCase  ,  min(__lowerCAmelCase  ,  __lowerCAmelCase      )      )



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					Any:


				"""simple docstring"""

				return (
				    clamp(rect[0]  ,  min[0]  ,  max[0]      ),
				    clamp(rect[1]  ,  min[1]  ,  max[1]      ),
				    clamp(rect[2]  ,  min[0]  ,  max[0]      ),
				    clamp(rect[3]  ,  min[1]  ,  max[1]      ),
				)



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					Union[str, Any]:


				"""simple docstring"""

				snake_case__	:      Union[str, Any]     		=	list(__lowerCAmelCase      )
				rect[0] -= overlap
				rect[1] -= overlap
				rect[2] += overlap
				rect[3] += overlap
				snake_case__	:      Tuple     		=	clamp_rect(__lowerCAmelCase  ,  [0, 0]  ,  [image_size[0], image_size[1]]      )
				return rect



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[str]:


				"""simple docstring"""

				snake_case__	:      List[str]     		=	Image.new('''RGB'''  ,  (tile.size[0] + original_slice, tile.size[1])      )
				result.paste(
				    original_image.resize((tile.size[0], tile.size[1])  ,  Image.BICUBIC      ).crop(
				        (slice_x, 0, slice_x + original_slice, tile.size[1])      )  ,  (0, 0)  ,  )
				result.paste(__lowerCAmelCase  ,  (original_slice, 0)      )
				return result



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					str:


				"""simple docstring"""

				snake_case__	:      List[str]     		=	(original_image_slice * 4, 0, tile.size[0], tile.size[1])
				snake_case__	:      str     		=	tile.crop(__lowerCAmelCase      )
				return tile



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[Any]:


				"""simple docstring"""

				snake_case__	:      Tuple     		=	n % d
				return n - divisor


class       a							(			__lowerCamelCase				):



		def __init__(     self     :str   ,__lowercase     :AutoencoderKL   ,__lowercase     :CLIPTextModel   ,__lowercase     :CLIPTokenizer   ,__lowercase     :UNetaDConditionModel   ,__lowercase     :DDPMScheduler   ,__lowercase     :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]   ,__lowercase     :int = 3_5_0   ,):
						super().__init__(
						    vae=__lowercase   ,text_encoder=__lowercase   ,tokenizer=__lowercase   ,unet=__lowercase   ,low_res_scheduler=__lowercase   ,scheduler=__lowercase   ,max_noise_level=__lowercase   ,)



		def 	__lowerCamelCase			(     self     :List[Any]   ,__lowercase     :str   ,__lowercase     :Tuple   ,__lowercase     :Dict   ,__lowercase     :Any   ,__lowercase     :Union[str, Any]   ,__lowercase     :Union[str, Any]   ,__lowercase     :List[Any]   ,**__lowercase     :Optional[int]  ):
						torch.manual_seed(0  )
						snake_case__	:      List[str]     		=	(
						    min(image.size[0] - (tile_size + original_image_slice)   ,x * tile_size  ),
						    min(image.size[1] - (tile_size + original_image_slice)   ,y * tile_size  ),
						    min(image.size[0]   ,(x + 1) * tile_size  ),
						    min(image.size[1]   ,(y + 1) * tile_size  ),
						)
						snake_case__	:      Union[str, Any]     		=	add_overlap_rect(__lowercase   ,__lowercase   ,image.size  )
						snake_case__	:      Dict     		=	image.crop(__lowercase  )
						snake_case__	:      List[str]     		=	((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0]
						snake_case__	:      List[Any]     		=	translated_slice_x - (original_image_slice / 2)
						snake_case__	:      Optional[Any]     		=	max(0   ,__lowercase  )
						snake_case__	:      List[Any]     		=	squeeze_tile(__lowercase   ,__lowercase   ,__lowercase   ,__lowercase  )
						snake_case__	:      Dict     		=	to_input.size
						snake_case__	:      Tuple     		=	to_input.resize((tile_size, tile_size)   ,Image.BICUBIC  )
						snake_case__	:      Union[str, Any]     		=	super(__lowercase   ,self  ).__call__(image=__lowercase   ,**__lowercase  ).images[0]
						snake_case__	:      Tuple     		=	upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4)   ,Image.BICUBIC  )
						snake_case__	:      Union[str, Any]     		=	unsqueeze_tile(__lowercase   ,__lowercase  )
						snake_case__	:      Optional[int]     		=	upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4)   ,Image.BICUBIC  )
						snake_case__	:      List[str]     		=	[]
						if x == 0:
										remove_borders.append('''l'''  )
						elif crop_rect[2] == image.size[0]:
										remove_borders.append('''r'''  )
						if y == 0:
										remove_borders.append('''t'''  )
						elif crop_rect[3] == image.size[1]:
										remove_borders.append('''b'''  )
						snake_case__	:      Dict     		=	Image.fromarray(
						    make_transparency_mask(
						        (upscaled_tile.size[0], upscaled_tile.size[1])   ,tile_border * 4   ,remove_borders=__lowercase  )   ,mode='''L'''   ,)
						final_image.paste(
						    __lowercase   ,(crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4)   ,__lowercase  )






		@torch.no_grad()
		def __call__(     self     :List[str]   ,__lowercase     :Union[str, List[str]]   ,__lowercase     :Union[PIL.Image.Image, List[PIL.Image.Image]]   ,__lowercase     :int = 7_5   ,__lowercase     :float = 9.0   ,__lowercase     :int = 5_0   ,__lowercase     :Optional[Union[str, List[str]]] = None   ,__lowercase     :Optional[int] = 1   ,__lowercase     :float = 0.0   ,__lowercase     :Optional[torch.Generator] = None   ,__lowercase     :Optional[torch.FloatTensor] = None   ,__lowercase     :Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,__lowercase     :int = 1   ,__lowercase     :int = 1_2_8   ,__lowercase     :int = 3_2   ,__lowercase     :int = 3_2   ,):
						snake_case__	:      Optional[Any]     		=	Image.new('''RGB'''   ,(image.size[0] * 4, image.size[1] * 4)  )
						snake_case__	:      Tuple     		=	math.ceil(image.size[0] / tile_size  )
						snake_case__	:      Any     		=	math.ceil(image.size[1] / tile_size  )
						snake_case__	:      Dict     		=	tcx * tcy
						snake_case__	:      Optional[Any]     		=	0
						for y in range(__lowercase  ):
										for x in range(__lowercase  ):
														self._process_tile(
														    __lowercase   ,__lowercase   ,__lowercase   ,__lowercase   ,__lowercase   ,__lowercase   ,__lowercase   ,prompt=__lowercase   ,num_inference_steps=__lowercase   ,guidance_scale=__lowercase   ,noise_level=__lowercase   ,negative_prompt=__lowercase   ,num_images_per_prompt=__lowercase   ,eta=__lowercase   ,generator=__lowercase   ,latents=__lowercase   ,)
														current_count += 1
														if callback is not None:
																		callback({'''progress''': current_count / total_tile_count, '''image''': final_image}  )
						return final_image



def 			_lowerCAmelCase       (					)   ->					Optional[Any]:


				"""simple docstring"""

				snake_case__	:      str     		=	'''stabilityai/stable-diffusion-x4-upscaler'''
				snake_case__	:      str     		=	StableDiffusionTiledUpscalePipeline.from_pretrained(__lowerCAmelCase  ,  revision='''fp16'''  ,  torch_dtype=torch.floataa      )
				snake_case__	:      Tuple     		=	pipe.to('''cuda'''      )
				snake_case__	:      Union[str, Any]     		=	Image.open('''../../docs/source/imgs/diffusers_library.jpg'''      )

				def callback(__lowerCAmelCase      ):
								print(f"""progress: {obj['progress']:.4f}"""      )
								obj["image"].save('''diffusers_library_progress.jpg'''      )

				snake_case__	:      Tuple     		=	pipe(image=__lowerCAmelCase  ,  prompt='''Black font, white background, vector'''  ,  noise_level=40  ,  callback=__lowerCAmelCase      )
				final_image.save('''diffusers_library.jpg'''      )


if __name__ == "__main__":
				main()






import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


A__								=						'''http://www.mocksite.com/file1.txt'''
A__								=						'''"text": ["foo", "foo"]'''
A__								=						'''6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'''


class       a							:
		__lowerCAmelCase	:    Optional[int]											=					2_00
		__lowerCAmelCase	:    List[str]											=					{"""Content-Length""": """100"""}
		__lowerCAmelCase	:    Dict											=					{}



		def 	__lowerCamelCase			(     self     :Dict   ,**__lowercase     :List[Any]  ):
						return [bytes(__lowercase   ,'''utf-8'''  )]



def 			_lowerCAmelCase       (					*__lowerCAmelCase  ,  **__lowerCAmelCase      )   ->					Optional[Any]:


				"""simple docstring"""

				return MockResponse()



@pytest.mark.parametrize('''urls_type'''  ,  [str, list, dict]      )
def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[str]:


				"""simple docstring"""

				import requests

				monkeypatch.setattr(__lowerCAmelCase  ,  '''request'''  ,  __lowerCAmelCase      )

				snake_case__	:      Union[str, Any]     		=	URL
				if issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      Optional[Any]     		=	url
				elif issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      int     		=	[url]
				elif issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      int     		=	{'''train''': url}
				snake_case__	:      Dict     		=	'''dummy'''
				snake_case__	:      Any     		=	'''downloads'''
				snake_case__	:      int     		=	tmp_path
				snake_case__	:      Any     		=	DownloadConfig(
				    cache_dir=os.path.join(__lowerCAmelCase  ,  __lowerCAmelCase      )  ,  use_etag=__lowerCAmelCase  ,  )
				snake_case__	:      Tuple     		=	DownloadManager(dataset_name=__lowerCAmelCase  ,  download_config=__lowerCAmelCase      )
				snake_case__	:      List[Any]     		=	dl_manager.download(__lowerCAmelCase      )
				snake_case__	:      Union[str, Any]     		=	urls
				for downloaded_paths in [downloaded_paths]:
								if isinstance(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												snake_case__	:      Optional[int]     		=	[downloaded_paths]
												snake_case__	:      Dict     		=	[urls]
								elif isinstance(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												assert "train" in downloaded_paths.keys()
												snake_case__	:      str     		=	downloaded_paths.values()
												snake_case__	:      List[str]     		=	urls.values()
								assert downloaded_paths
								for downloaded_path, input_url in zip(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												assert downloaded_path == dl_manager.downloaded_paths[input_url]
												snake_case__	:      List[Any]     		=	Path(__lowerCAmelCase      )
												snake_case__	:      Any     		=	downloaded_path.parts
												assert parts[-1] == HASH
												assert parts[-2] == cache_subdir
												assert downloaded_path.exists()
												snake_case__	:      List[str]     		=	downloaded_path.read_text()
												assert content == CONTENT
												snake_case__	:      List[str]     		=	downloaded_path.with_suffix('''.json'''      )
												assert metadata_downloaded_path.exists()
												snake_case__	:      str     		=	json.loads(metadata_downloaded_path.read_text()      )
												assert metadata_content == {"url": URL, "etag": None}



@pytest.mark.parametrize('''paths_type'''  ,  [str, list, dict]      )
def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					Any:


				"""simple docstring"""

				snake_case__	:      Any     		=	str(__lowerCAmelCase      )
				if issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      Tuple     		=	filename
				elif issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      Dict     		=	[filename]
				elif issubclass(__lowerCAmelCase  ,  __lowerCAmelCase      ):
								snake_case__	:      Dict     		=	{'''train''': filename}
				snake_case__	:      Any     		=	'''dummy'''
				snake_case__	:      Any     		=	xz_file.parent
				snake_case__	:      List[str]     		=	'''extracted'''
				snake_case__	:      Dict     		=	DownloadConfig(
				    cache_dir=__lowerCAmelCase  ,  use_etag=__lowerCAmelCase  ,  )
				snake_case__	:      Dict     		=	DownloadManager(dataset_name=__lowerCAmelCase  ,  download_config=__lowerCAmelCase      )
				snake_case__	:      str     		=	dl_manager.extract(__lowerCAmelCase      )
				snake_case__	:      int     		=	paths
				for extracted_paths in [extracted_paths]:
								if isinstance(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												snake_case__	:      Dict     		=	[extracted_paths]
												snake_case__	:      Optional[Any]     		=	[paths]
								elif isinstance(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												assert "train" in extracted_paths.keys()
												snake_case__	:      int     		=	extracted_paths.values()
												snake_case__	:      int     		=	paths.values()
								assert extracted_paths
								for extracted_path, input_path in zip(__lowerCAmelCase  ,  __lowerCAmelCase      ):
												assert extracted_path == dl_manager.extracted_paths[input_path]
												snake_case__	:      Optional[int]     		=	Path(__lowerCAmelCase      )
												snake_case__	:      int     		=	extracted_path.parts
												assert parts[-1] == hash_url_to_filename(__lowerCAmelCase  ,  etag=__lowerCAmelCase      )
												assert parts[-2] == extracted_subdir
												assert extracted_path.exists()
												snake_case__	:      List[Any]     		=	extracted_path.read_text()
												snake_case__	:      List[str]     		=	text_file.read_text()
												assert extracted_file_content == expected_file_content



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[Any]:


				"""simple docstring"""

				assert path.endswith('''.jsonl'''      )
				for num_items, line in enumerate(__lowerCAmelCase  ,  start=1      ):
								snake_case__	:      Any     		=	json.loads(line.decode('''utf-8'''      )      )
								assert item.keys() == {"col_1", "col_2", "col_3"}
				assert num_items == 4



@pytest.mark.parametrize('''archive_jsonl'''  ,  ['''tar_jsonl_path''', '''zip_jsonl_path''']      )
def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					Union[str, Any]:


				"""simple docstring"""

				snake_case__	:      Any     		=	request.getfixturevalue(__lowerCAmelCase      )
				snake_case__	:      Union[str, Any]     		=	DownloadManager()
				for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase      )  ,  start=1      ):
								_test_jsonl(__lowerCAmelCase  ,  __lowerCAmelCase      )
				assert num_jsonl == 2



@pytest.mark.parametrize('''archive_nested_jsonl'''  ,  ['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path''']      )
def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[str]:


				"""simple docstring"""

				snake_case__	:      Union[str, Any]     		=	request.getfixturevalue(__lowerCAmelCase      )
				snake_case__	:      Optional[int]     		=	DownloadManager()
				for num_tar, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase      )  ,  start=1      ):
								for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(__lowerCAmelCase      )  ,  start=1      ):
												_test_jsonl(__lowerCAmelCase  ,  __lowerCAmelCase      )
				assert num_tar == 1
				assert num_jsonl == 2





def 			_lowerCAmelCase       (					__lowerCAmelCase      )   ->					str:


				"""simple docstring"""

				snake_case__	:      Any     		=	DownloadManager()
				for num_file, file in enumerate(dl_manager.iter_files(__lowerCAmelCase      )  ,  start=1      ):
								assert os.path.basename(__lowerCAmelCase      ) == ("test.txt" if num_file == 1 else "train.txt")
				assert num_file == 2

