


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import OwlViTImageProcessor, OwlViTProcessor
@require_vision
class 	UpperCAmelCase_					(					unittest.TestCase    ):

   '''simple docstring'''




   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					str    =				tempfile.mkdtemp()

     # fmt: off
     lowerCamelCase :					Any    =				["", "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
     # fmt: on
     lowerCamelCase :					List[Any]    =				dict(zip(__A						,				range(len(__A ) ) ) )
     lowerCamelCase :					List[Any]    =				["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
     lowerCamelCase :					Optional[Any]    =				{"unk_token": "<unk>"}

     lowerCamelCase :					int    =				os.path.join(self.tmpdirname						,				VOCAB_FILES_NAMES["vocab_file"] )
     lowerCamelCase :					List[Any]    =				os.path.join(self.tmpdirname						,				VOCAB_FILES_NAMES["merges_file"] )
     with open(self.vocab_file						,				"w"						,				encoding="utf-8" ) as fp:
       fp.write(json.dumps(__A ) + "\n" )
     with open(self.merges_file						,				"w"						,				encoding="utf-8" ) as fp:
       fp.write("\n".join(__A ) )

     lowerCamelCase :					str    =				{
         "do_resize": True,
         "size": 20,
         "do_center_crop": True,
         "crop_size": 18,
         "do_normalize": True,
         "image_mean": [0.48145466, 0.4578275, 0.40821073],
         "image_std": [0.26862954, 0.26130258, 0.27577711],
     }
     lowerCamelCase :					str    =				os.path.join(self.tmpdirname						,				__A )
     with open(self.image_processor_file						,				"w"						,				encoding="utf-8" ) as fp:
       json.dump(__A						,				__A )






   def _snake_case    (		self						,				**__A ):






     """simple docstring"""





     return CLIPTokenizer.from_pretrained(self.tmpdirname						,				pad_token="!"						,				**__A )






   def _snake_case    (		self						,				**__A ):






     """simple docstring"""





     return CLIPTokenizerFast.from_pretrained(self.tmpdirname						,				pad_token="!"						,				**__A )






   def _snake_case    (		self						,				**__A ):






     """simple docstring"""





     return OwlViTImageProcessor.from_pretrained(self.tmpdirname						,				**__A )






   def _snake_case    (		self ):






     """simple docstring"""





     shutil.rmtree(self.tmpdirname )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				[np.random.randint(255						,				size=(3, 30, 400)						,				dtype=np.uinta )]

     lowerCamelCase :					Tuple    =				[Image.fromarray(np.moveaxis(__A						,				0						,				-1 ) ) for x in image_inputs]

     return image_inputs






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				self.get_tokenizer()
     lowerCamelCase :					Optional[Any]    =				self.get_rust_tokenizer()
     lowerCamelCase :					Tuple    =				self.get_image_processor()

     lowerCamelCase :					List[Any]    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )
     processor_slow.save_pretrained(self.tmpdirname )
     lowerCamelCase :					Optional[Any]    =				OwlViTProcessor.from_pretrained(self.tmpdirname						,				use_fast=__A )

     lowerCamelCase :					Optional[int]    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )
     processor_fast.save_pretrained(self.tmpdirname )
     lowerCamelCase :					Tuple    =				OwlViTProcessor.from_pretrained(self.tmpdirname )

     self.assertEqual(processor_slow.tokenizer.get_vocab()						,				tokenizer_slow.get_vocab() )
     self.assertEqual(processor_fast.tokenizer.get_vocab()						,				tokenizer_fast.get_vocab() )
     self.assertEqual(tokenizer_slow.get_vocab()						,				tokenizer_fast.get_vocab() )
     self.assertIsInstance(processor_slow.tokenizer						,				__A )
     self.assertIsInstance(processor_fast.tokenizer						,				__A )

     self.assertEqual(processor_slow.image_processor.to_json_string()						,				image_processor.to_json_string() )
     self.assertEqual(processor_fast.image_processor.to_json_string()						,				image_processor.to_json_string() )
     self.assertIsInstance(processor_slow.image_processor						,				__A )
     self.assertIsInstance(processor_fast.image_processor						,				__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				OwlViTProcessor(tokenizer=self.get_tokenizer()						,				image_processor=self.get_image_processor() )
     processor.save_pretrained(self.tmpdirname )

     lowerCamelCase :					int    =				self.get_tokenizer(bos_token="(BOS)"						,				eos_token="(EOS)" )
     lowerCamelCase :					List[str]    =				self.get_image_processor(do_normalize=__A )

     lowerCamelCase :					Optional[int]    =				OwlViTProcessor.from_pretrained(
         self.tmpdirname						,				bos_token="(BOS)"						,				eos_token="(EOS)"						,				do_normalize=__A )

     self.assertEqual(processor.tokenizer.get_vocab()						,				tokenizer_add_kwargs.get_vocab() )
     self.assertIsInstance(processor.tokenizer						,				__A )

     self.assertEqual(processor.image_processor.to_json_string()						,				image_processor_add_kwargs.to_json_string() )
     self.assertIsInstance(processor.image_processor						,				__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				self.get_image_processor()
     lowerCamelCase :					Optional[int]    =				self.get_tokenizer()

     lowerCamelCase :					Dict    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )

     lowerCamelCase :					Tuple    =				self.prepare_image_inputs()

     lowerCamelCase :					int    =				image_processor(__A						,				return_tensors="np" )
     lowerCamelCase :					Union[str, Any]    =				processor(images=__A						,				return_tensors="np" )

     for key in input_image_proc.keys():
       self.assertAlmostEqual(input_image_proc[key].sum()						,				input_processor[key].sum()						,				delta=1e-2 )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.get_image_processor()
     lowerCamelCase :					Dict    =				self.get_tokenizer()

     lowerCamelCase :					Union[str, Any]    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )

     lowerCamelCase :					Tuple    =				"lower newer"

     lowerCamelCase :					Union[str, Any]    =				processor(text=__A						,				return_tensors="np" )

     lowerCamelCase :					List[Any]    =				tokenizer(__A						,				return_tensors="np" )

     for key in encoded_tok.keys():
       self.assertListEqual(encoded_tok[key][0].tolist()						,				encoded_processor[key][0].tolist() )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Any    =				self.get_image_processor()
     lowerCamelCase :					Any    =				self.get_tokenizer()

     lowerCamelCase :					int    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )

     lowerCamelCase :					Optional[Any]    =				"lower newer"
     lowerCamelCase :					Dict    =				self.prepare_image_inputs()

     lowerCamelCase :					Any    =				processor(text=__A						,				images=__A )

     self.assertListEqual(list(inputs.keys() )						,				["input_ids", "attention_mask", "pixel_values"] )

     # test if it raises when no input is passed
     with pytest.raises(__A ):
       processor()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Any    =				"google/owlvit-base-patch32"
     lowerCamelCase :					List[Any]    =				OwlViTProcessor.from_pretrained(__A )

     lowerCamelCase :					Tuple    =				["cat", "nasa badge"]
     lowerCamelCase :					str    =				processor(text=__A )

     lowerCamelCase :					Union[str, Any]    =				16
     self.assertListEqual(list(inputs.keys() )						,				["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape						,				(2, seq_length) )

     # test if it raises when no input is passed
     with pytest.raises(__A ):
       processor()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					str    =				"google/owlvit-base-patch32"
     lowerCamelCase :					Optional[int]    =				OwlViTProcessor.from_pretrained(__A )

     lowerCamelCase :					Dict    =				[["cat", "nasa badge"], ["person"]]
     lowerCamelCase :					int    =				processor(text=__A )

     lowerCamelCase :					Tuple    =				16
     lowerCamelCase :					Any    =				len(__A )
     lowerCamelCase :					Optional[Any]    =				max([len(__A ) for texts in input_texts] )

     self.assertListEqual(list(inputs.keys() )						,				["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape						,				(batch_size * num_max_text_queries, seq_length) )

     # test if it raises when no input is passed
     with pytest.raises(__A ):
       processor()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				"google/owlvit-base-patch32"
     lowerCamelCase :					Tuple    =				OwlViTProcessor.from_pretrained(__A )

     lowerCamelCase :					List[Any]    =				["cat", "nasa badge"]
     lowerCamelCase :					Optional[Any]    =				processor(text=__A )

     lowerCamelCase :					int    =				16
     lowerCamelCase :					List[str]    =				inputs["input_ids"]
     lowerCamelCase :					int    =				[
         [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     ]

     self.assertListEqual(list(inputs.keys() )						,				["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape						,				(2, seq_length) )
     self.assertListEqual(list(input_ids[0] )						,				predicted_ids[0] )
     self.assertListEqual(list(input_ids[1] )						,				predicted_ids[1] )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Any    =				self.get_image_processor()
     lowerCamelCase :					List[str]    =				self.get_tokenizer()

     lowerCamelCase :					str    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )

     lowerCamelCase :					Dict    =				self.prepare_image_inputs()
     lowerCamelCase :					Union[str, Any]    =				self.prepare_image_inputs()

     lowerCamelCase :					Any    =				processor(images=__A						,				query_images=__A )

     self.assertListEqual(list(inputs.keys() )						,				["query_pixel_values", "pixel_values"] )

     # test if it raises when no input is passed
     with pytest.raises(__A ):
       processor()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				self.get_image_processor()
     lowerCamelCase :					Optional[int]    =				self.get_tokenizer()

     lowerCamelCase :					Dict    =				OwlViTProcessor(tokenizer=__A						,				image_processor=__A )

     lowerCamelCase :					Optional[int]    =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     lowerCamelCase :					List[Any]    =				processor.batch_decode(__A )
     lowerCamelCase :					Union[str, Any]    =				tokenizer.batch_decode(__A )

     self.assertListEqual(__A						,				__A )







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 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_=[]	):


  '''simple docstring'''

  lowerCamelCase :					Optional[Any]    =				size[0] - overlap_pixels * 2
  lowerCamelCase :					int    =				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
  lowerCamelCase :					Tuple    =				np.ones((size_y, size_x)      ,						dtype=np.uinta	) * 255
  lowerCamelCase :					List[Any]    =				np.pad(SCREAMING_SNAKE_CASE_      ,						mode="linear_ramp"      ,						pad_width=SCREAMING_SNAKE_CASE_      ,						end_values=0	)

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






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  return max(SCREAMING_SNAKE_CASE_      ,						min(SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	)	)






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''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 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  lowerCamelCase :					Optional[Any]    =				list(SCREAMING_SNAKE_CASE_	)
  rect[0] -= overlap
  rect[1] -= overlap
  rect[2] += overlap
  rect[3] += overlap
  lowerCamelCase :					Any    =				clamp_rect(SCREAMING_SNAKE_CASE_      ,						[0, 0]      ,						[image_size[0], image_size[1]]	)
  return rect






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  lowerCamelCase :					Dict    =				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(SCREAMING_SNAKE_CASE_      ,						(original_slice, 0)	)
  return result






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  lowerCamelCase :					Union[str, Any]    =				(original_image_slice * 4, 0, tile.size[0], tile.size[1])
  lowerCamelCase :					int    =				tile.crop(SCREAMING_SNAKE_CASE_	)
  return tile






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  lowerCamelCase :					int    =				n % d
  return n - divisor
class 	UpperCAmelCase_					(					UpperCamelCase    ):

   '''simple docstring'''




   def __init__(		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A = 350						,				):






     """simple docstring"""





     super().__init__(
         vae=__A						,				text_encoder=__A						,				tokenizer=__A						,				unet=__A						,				low_res_scheduler=__A						,				scheduler=__A						,				max_noise_level=__A						,				)






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				**__A ):






     """simple docstring"""





     torch.manual_seed(0 )
     lowerCamelCase :					Tuple    =				(
         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 ),
     )
     lowerCamelCase :					Union[str, Any]    =				add_overlap_rect(__A						,				__A						,				image.size )
     lowerCamelCase :					List[str]    =				image.crop(__A )
     lowerCamelCase :					Optional[int]    =				((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0]
     lowerCamelCase :					int    =				translated_slice_x - (original_image_slice / 2)
     lowerCamelCase :					Optional[Any]    =				max(0						,				__A )
     lowerCamelCase :					Tuple    =				squeeze_tile(__A						,				__A						,				__A						,				__A )
     lowerCamelCase :					Dict    =				to_input.size
     lowerCamelCase :					Optional[int]    =				to_input.resize((tile_size, tile_size)						,				Image.BICUBIC )
     lowerCamelCase :					Dict    =				super(__A						,				self ).__call__(image=__A						,				**__A ).images[0]
     lowerCamelCase :					Tuple    =				upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4)						,				Image.BICUBIC )
     lowerCamelCase :					Optional[Any]    =				unsqueeze_tile(__A						,				__A )
     lowerCamelCase :					Optional[Any]    =				upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4)						,				Image.BICUBIC )
     lowerCamelCase :					int    =				[]
     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" )
     lowerCamelCase :					int    =				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						,				):






     """simple docstring"""





     lowerCamelCase :					Dict    =				Image.new("RGB"						,				(image.size[0] * 4, image.size[1] * 4) )
     lowerCamelCase :					Union[str, Any]    =				math.ceil(image.size[0] / tile_size )
     lowerCamelCase :					Dict    =				math.ceil(image.size[1] / tile_size )
     lowerCamelCase :					str    =				tcx * tcy
     lowerCamelCase :					int    =				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 		lowercase_(  ):


  '''simple docstring'''

  lowerCamelCase :					Dict    =				"stabilityai/stable-diffusion-x4-upscaler"
  lowerCamelCase :					Union[str, Any]    =				StableDiffusionTiledUpscalePipeline.from_pretrained(SCREAMING_SNAKE_CASE_      ,						revision="fp16"      ,						torch_dtype=torch.floataa	)
  lowerCamelCase :					Optional[Any]    =				pipe.to("cuda"	)
  lowerCamelCase :					List[str]    =				Image.open("../../docs/source/imgs/diffusers_library.jpg"	)

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

  lowerCamelCase :					int    =				pipe(image=SCREAMING_SNAKE_CASE_      ,						prompt="Black font, white background, vector"      ,						noise_level=40      ,						callback=SCREAMING_SNAKE_CASE_	)
  final_image.save("diffusers_library.jpg"	)


if __name__ == "__main__":
   main()



