





'''simple docstring'''




import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer






class UpperCAmelCase							(		__lowerCAmelCase							):



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   tempfile.mkdtemp()
					__UpperCAmelCase					=   5

					# Realm tok
					__UpperCAmelCase					=   [
					    '[UNK]',
					    '[CLS]',
					    '[SEP]',
					    '[PAD]',
					    '[MASK]',
					    'test',
					    'question',
					    'this',
					    'is',
					    'the',
					    'first',
					    'second',
					    'third',
					    'fourth',
					    'fifth',
					    'record',
					    'want',
					    '##want',
					    '##ed',
					    'wa',
					    'un',
					    'runn',
					    '##ing',
					    ',',
					    'low',
					    'lowest',
					]
					__UpperCAmelCase					=   os.path.join(self.tmpdirname     ,			'realm_tokenizer'					)
					os.makedirs(lowerCAmelCase_     ,			exist_ok=lowerCAmelCase_					)
					__UpperCAmelCase					=   os.path.join(lowerCAmelCase_     ,			VOCAB_FILES_NAMES['vocab_file']					)
					with open(self.vocab_file     ,			'w'     ,			encoding='utf-8'					) as vocab_writer:
						vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]					)					)

					__UpperCAmelCase					=   os.path.join(self.tmpdirname     ,			'realm_block_records'					)
					os.makedirs(lowerCAmelCase_     ,			exist_ok=lowerCAmelCase_					)



				def 					__lowerCamelCase						(  self					):
					return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname     ,			'realm_tokenizer'					)					)



				def 					__lowerCamelCase						(  self					):
					shutil.rmtree(self.tmpdirname					)



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   RealmConfig(num_block_records=self.num_block_records					)
					return config



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   Dataset.from_dict(
					    {
					        'id': ['0', '1'],
					        'question': ['foo', 'bar'],
					        'answers': [['Foo', 'Bar'], ['Bar']],
					    }					)
					return dataset



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   np.array(
					    [
					        b'This is the first record',
					        b'This is the second record',
					        b'This is the third record',
					        b'This is the fourth record',
					        b'This is the fifth record',
					        b'This is a longer longer longer record',
					    ]     ,			dtype=lowerCAmelCase_     ,			)
					return block_records



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   RealmRetriever(
					    block_records=self.get_dummy_block_records()     ,			tokenizer=self.get_tokenizer()     ,			)
					return retriever



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   self.get_config()
					__UpperCAmelCase					=   self.get_dummy_retriever()
					__UpperCAmelCase					=   retriever.tokenizer

					__UpperCAmelCase					=   np.array([0, 3]     ,			dtype='long'					)
					__UpperCAmelCase					=   tokenizer(['Test question']					).input_ids
					__UpperCAmelCase					=   tokenizer(
					    ['the fourth']     ,			add_special_tokens=lowerCAmelCase_     ,			return_token_type_ids=lowerCAmelCase_     ,			return_attention_mask=lowerCAmelCase_     ,			).input_ids
					__UpperCAmelCase					=   config.reader_seq_len

					__UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   retriever(
					    lowerCAmelCase_     ,			lowerCAmelCase_     ,			answer_ids=lowerCAmelCase_     ,			max_length=lowerCAmelCase_     ,			return_tensors='np'					)

					self.assertEqual(len(lowerCAmelCase_					)     ,			2					)
					self.assertEqual(len(lowerCAmelCase_					)     ,			2					)
					self.assertEqual(len(lowerCAmelCase_					)     ,			2					)
					self.assertEqual(concat_inputs.input_ids.shape     ,			(2, 10)					)
					self.assertEqual(concat_inputs.attention_mask.shape     ,			(2, 10)					)
					self.assertEqual(concat_inputs.token_type_ids.shape     ,			(2, 10)					)
					self.assertEqual(concat_inputs.special_tokens_mask.shape     ,			(2, 10)					)
					self.assertEqual(
					    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]					)     ,			['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'first', 'record', '[SEP]']     ,			)
					self.assertEqual(
					    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]					)     ,			['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'fourth', 'record', '[SEP]']     ,			)



				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   self.get_config()
					__UpperCAmelCase					=   self.get_dummy_retriever()
					__UpperCAmelCase					=   retriever.tokenizer

					__UpperCAmelCase					=   np.array([0, 3, 5]     ,			dtype='long'					)
					__UpperCAmelCase					=   tokenizer(['Test question']					).input_ids
					__UpperCAmelCase					=   tokenizer(
					    ['the fourth', 'longer longer']     ,			add_special_tokens=lowerCAmelCase_     ,			return_token_type_ids=lowerCAmelCase_     ,			return_attention_mask=lowerCAmelCase_     ,			).input_ids
					__UpperCAmelCase					=   config.reader_seq_len

					__UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   retriever(
					    lowerCAmelCase_     ,			lowerCAmelCase_     ,			answer_ids=lowerCAmelCase_     ,			max_length=lowerCAmelCase_     ,			return_tensors='np'					)

					self.assertEqual([False, True, True]     ,			lowerCAmelCase_					)
					self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]     ,			lowerCAmelCase_					)
					self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]     ,			lowerCAmelCase_					)




				def 					__lowerCamelCase						(  self					):
					__UpperCAmelCase					=   self.get_dummy_retriever()
					retriever.save_pretrained(os.path.join(self.tmpdirname     ,			'realm_block_records'					)					)

					# Test local path
					__UpperCAmelCase					=   retriever.from_pretrained(os.path.join(self.tmpdirname     ,			'realm_block_records'					)					)
					self.assertEqual(retriever.block_records[0]     ,			b'This is the first record'					)

					# Test mocked remote path
					with patch('transformers.models.realm.retrieval_realm.hf_hub_download'					) as mock_hf_hub_download:
						__UpperCAmelCase					=   os.path.join(
						    os.path.join(self.tmpdirname     ,			'realm_block_records'					)     ,			_REALM_BLOCK_RECORDS_FILENAME					)
						__UpperCAmelCase					=   RealmRetriever.from_pretrained('google/realm-cc-news-pretrained-openqa'					)

					self.assertEqual(retriever.block_records[0]     ,			b'This is the first record'					)












'''simple docstring'''




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=[]    )->     Any:
 __UpperCAmelCase					=   size[0] - overlap_pixels * 2
 __UpperCAmelCase					=   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
 __UpperCAmelCase					=   np.ones((size_y, size_x)       ,    dtype=np.uinta    ) * 2_55
 __UpperCAmelCase					=   np.pad(_lowerCAmelCase       ,    mode='linear_ramp'       ,    pad_width=_lowerCAmelCase       ,    end_values=0    )

 if "l" in remove_borders:
  __UpperCAmelCase					=   mask[:, overlap_pixels : mask.shape[1]]
 if "r" in remove_borders:
  __UpperCAmelCase					=   mask[:, 0 : mask.shape[1] - overlap_pixels]
 if "t" in remove_borders:
  __UpperCAmelCase					=   mask[overlap_pixels : mask.shape[0], :]
 if "b" in remove_borders:
  __UpperCAmelCase					=   mask[0 : mask.shape[0] - overlap_pixels, :]
 return mask







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Any:
 return max(_lowerCAmelCase       ,    min(_lowerCAmelCase       ,    _lowerCAmelCase    )    )







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Dict:
 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    )->     Dict:
 __UpperCAmelCase					=   list(_lowerCAmelCase    )
 rect[0] -= overlap
 rect[1] -= overlap
 rect[2] += overlap
 rect[3] += overlap
 __UpperCAmelCase					=   clamp_rect(_lowerCAmelCase       ,    [0, 0]       ,    [image_size[0], image_size[1]]    )
 return rect







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Tuple:
 __UpperCAmelCase					=   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    )->     List[Any]:
 __UpperCAmelCase					=   (original_image_slice * 4, 0, tile.size[0], tile.size[1])
 __UpperCAmelCase					=   tile.crop(_lowerCAmelCase    )
 return tile







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     Optional[int]:
 __UpperCAmelCase					=   n % d
 return n - divisor






class UpperCAmelCase							(		UpperCAmelCase_							):



    def __init__(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A = 350     ,			):
     super().__init__(
         vae=__A     ,			text_encoder=__A     ,			tokenizer=__A     ,			unet=__A     ,			low_res_scheduler=__A     ,			scheduler=__A     ,			max_noise_level=__A     ,			)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			**__A					):
     torch.manual_seed(0					)
     __UpperCAmelCase					=   (
         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					),
     )
     __UpperCAmelCase					=   add_overlap_rect(__A     ,			__A     ,			image.size					)
     __UpperCAmelCase					=   image.crop(__A					)
     __UpperCAmelCase					=   ((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0]
     __UpperCAmelCase					=   translated_slice_x - (original_image_slice / 2)
     __UpperCAmelCase					=   max(0     ,			__A					)
     __UpperCAmelCase					=   squeeze_tile(__A     ,			__A     ,			__A     ,			__A					)
     __UpperCAmelCase					=   to_input.size
     __UpperCAmelCase					=   to_input.resize((tile_size, tile_size)     ,			Image.BICUBIC					)
     __UpperCAmelCase					=   super(__A     ,			self					).__call__(image=__A     ,			**__A					).images[0]
     __UpperCAmelCase					=   upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4)     ,			Image.BICUBIC					)
     __UpperCAmelCase					=   unsqueeze_tile(__A     ,			__A					)
     __UpperCAmelCase					=   upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4)     ,			Image.BICUBIC					)
     __UpperCAmelCase					=   []
     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'					)
     __UpperCAmelCase					=   Image.fromarray(
         make_transparency_mask(
             (upscaled_tile.size[0], upscaled_tile.size[1])     ,			tile_border * 4     ,			remove_borders=__A					)     ,			mode='L'     ,			)
     final_image.paste(
         __A     ,			(crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4)     ,			__A					)




    @torch.no_grad()
    def __call__(  self     ,			__A     ,			__A     ,			__A = 75     ,			__A = 9.0     ,			__A = 50     ,			__A = None     ,			__A = 1     ,			__A = 0.0     ,			__A = None     ,			__A = None     ,			__A = None     ,			__A = 1     ,			__A = 128     ,			__A = 32     ,			__A = 32     ,			):
     __UpperCAmelCase					=   Image.new('RGB'     ,			(image.size[0] * 4, image.size[1] * 4)					)
     __UpperCAmelCase					=   math.ceil(image.size[0] / tile_size					)
     __UpperCAmelCase					=   math.ceil(image.size[1] / tile_size					)
     __UpperCAmelCase					=   tcx * tcy
     __UpperCAmelCase					=   0
     for y in range(__A					):
      for x in range(__A					):
       self._process_tile(
           __A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			prompt=__A     ,			num_inference_steps=__A     ,			guidance_scale=__A     ,			noise_level=__A     ,			negative_prompt=__A     ,			num_images_per_prompt=__A     ,			eta=__A     ,			generator=__A     ,			latents=__A     ,			)
       current_count += 1
       if callback is not None:
        callback({'progress': current_count / total_tile_count, 'image': final_image}					)
     return final_image







def _lowerCAmelCase     (      )->     str:
 # Run a demo
 __UpperCAmelCase					=   'stabilityai/stable-diffusion-x4-upscaler'
 __UpperCAmelCase					=   StableDiffusionTiledUpscalePipeline.from_pretrained(_lowerCAmelCase       ,    revision='fp16'       ,    torch_dtype=torch.floataa    )
 __UpperCAmelCase					=   pipe.to('cuda'    )
 __UpperCAmelCase					=   Image.open('../../docs/source/imgs/diffusers_library.jpg'    )

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

 __UpperCAmelCase					=   pipe(image=_lowerCAmelCase       ,    prompt='Black font, white background, vector'       ,    noise_level=40       ,    callback=_lowerCAmelCase    )
 final_image.save('diffusers_library.jpg'    )


if __name__ == "__main__":
    main()





