




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							:   str						,							_lowerCAmelCase							:   Any						,							_lowerCAmelCase							:   str=[]	):




 """simple docstring"""







 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	) * 255
 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							:   Dict						,							_lowerCAmelCase							:   str						,							_lowerCAmelCase							:   List[Any]	):




 """simple docstring"""







 return max(_lowerCAmelCase						,							min(_lowerCAmelCase						,							_lowerCAmelCase	)	)



def 						lowerCAmelCase		(		_lowerCAmelCase							:   [int]						,							_lowerCAmelCase							:   [int]						,							_lowerCAmelCase							:   [int]	):




 """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							:   [int]						,							_lowerCAmelCase							:   int						,							_lowerCAmelCase							:   [int]	):




 """simple docstring"""







 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							:   Any						,							_lowerCAmelCase							:   Tuple						,							_lowerCAmelCase							:   str						,							_lowerCAmelCase							:   Union[str, Any]	):




 """simple docstring"""







 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							:   str						,							_lowerCAmelCase							:   Optional[int]	):




 """simple docstring"""







 UpperCAmelCase__							 =						(original_image_slice * 4, 0, tile.size[0], tile.size[1])
 UpperCAmelCase__							 =						tile.crop(_lowerCAmelCase	)
 return tile



def 						lowerCAmelCase		(		_lowerCAmelCase							:   List[str]						,							_lowerCAmelCase							:   Tuple	):




 """simple docstring"""







 UpperCAmelCase__							 =						n % d
 return n - divisor
class 			_UpperCamelCase      (				lowerCAmelCase		):
      def __init__(			self   :Union[str, Any] ,      lowerCamelCase   :AutoencoderKL ,      lowerCamelCase   :CLIPTextModel ,      lowerCamelCase   :CLIPTokenizer ,      lowerCamelCase   :UNetaDConditionModel ,      lowerCamelCase   :DDPMScheduler ,      lowerCamelCase   :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] ,      lowerCamelCase   :int = 350 ,      )     ->			Optional[Any]:
       super().__init__(
           vae=lowerCamelCase ,      text_encoder=lowerCamelCase ,      tokenizer=lowerCamelCase ,      unet=lowerCamelCase ,      low_res_scheduler=lowerCamelCase ,      scheduler=lowerCamelCase ,      max_noise_level=lowerCamelCase ,      )
      def 	UpperCAmelCase_						(			self   :Any ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Any ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :int ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Tuple ,      lowerCamelCase   :int ,      **lowerCamelCase   :Optional[int]      )     ->			Union[str, Any]:
       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(lowerCamelCase ,      lowerCamelCase ,      image.size      )
       UpperCAmelCase__							 =						image.crop(lowerCamelCase      )
       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 ,      lowerCamelCase      )
       UpperCAmelCase__							 =						squeeze_tile(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
       UpperCAmelCase__							 =						to_input.size
       UpperCAmelCase__							 =						to_input.resize((tile_size, tile_size) ,      Image.BICUBIC      )
       UpperCAmelCase__							 =						super(lowerCamelCase ,      self      ).__call__(image=lowerCamelCase ,      **lowerCamelCase      ).images[0]
       UpperCAmelCase__							 =						upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4) ,      Image.BICUBIC      )
       UpperCAmelCase__							 =						unsqueeze_tile(lowerCamelCase ,      lowerCamelCase      )
       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=lowerCamelCase      ) ,      mode="L" ,      )
       final_image.paste(
           lowerCamelCase ,      (crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4) ,      lowerCamelCase      )







      @torch.no_grad()
      def __call__(			self   :Optional[int] ,      lowerCamelCase   :Union[str, List[str]] ,      lowerCamelCase   :Union[PIL.Image.Image, List[PIL.Image.Image]] ,      lowerCamelCase   :int = 75 ,      lowerCamelCase   :float = 9.0 ,      lowerCamelCase   :int = 50 ,      lowerCamelCase   :Optional[Union[str, List[str]]] = None ,      lowerCamelCase   :Optional[int] = 1 ,      lowerCamelCase   :float = 0.0 ,      lowerCamelCase   :Optional[torch.Generator] = None ,      lowerCamelCase   :Optional[torch.FloatTensor] = None ,      lowerCamelCase   :Optional[Callable[[int, int, torch.FloatTensor], None]] = None ,      lowerCamelCase   :int = 1 ,      lowerCamelCase   :int = 128 ,      lowerCamelCase   :int = 32 ,      lowerCamelCase   :int = 32 ,      )     ->			int:
       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(lowerCamelCase      ):
        for x in range(lowerCamelCase      ):
         self._process_tile(
             lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      prompt=lowerCamelCase ,      num_inference_steps=lowerCamelCase ,      guidance_scale=lowerCamelCase ,      noise_level=lowerCamelCase ,      negative_prompt=lowerCamelCase ,      num_images_per_prompt=lowerCamelCase ,      eta=lowerCamelCase ,      generator=lowerCamelCase ,      latents=lowerCamelCase ,      )
         current_count += 1
         if callback is not None:
          callback({"progress": current_count / total_tile_count, "image": final_image}      )
       return final_image



def 						lowerCAmelCase		(		):




 """simple docstring"""







 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							:   Any	):
  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()











import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import ASTForAudioClassification, ASTModel
  from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
      AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )


if is_torchaudio_available():
  import torchaudio

  from transformers import ASTFeatureExtractor
class 			_UpperCamelCase      :
      def __init__(			self   :Any ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Optional[int]=13 ,      lowerCamelCase   :Optional[Any]=2 ,      lowerCamelCase   :Any=24 ,      lowerCamelCase   :Union[str, Any]=16 ,      lowerCamelCase   :Any=True ,      lowerCamelCase   :int=True ,      lowerCamelCase   :Optional[Any]=32 ,      lowerCamelCase   :Union[str, Any]=5 ,      lowerCamelCase   :Tuple=4 ,      lowerCamelCase   :Optional[Any]=37 ,      lowerCamelCase   :Optional[Any]="gelu" ,      lowerCamelCase   :int=0.1 ,      lowerCamelCase   :Tuple=0.1 ,      lowerCamelCase   :List[str]=10 ,      lowerCamelCase   :Optional[Any]=0.02 ,      lowerCamelCase   :Optional[int]=None ,      lowerCamelCase   :Optional[Any]=2 ,      lowerCamelCase   :List[Any]=2 ,      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						parent
       UpperCAmelCase__							 =						batch_size
       UpperCAmelCase__							 =						patch_size
       UpperCAmelCase__							 =						max_length
       UpperCAmelCase__							 =						num_mel_bins
       UpperCAmelCase__							 =						is_training
       UpperCAmelCase__							 =						use_labels
       UpperCAmelCase__							 =						hidden_size
       UpperCAmelCase__							 =						num_hidden_layers
       UpperCAmelCase__							 =						num_attention_heads
       UpperCAmelCase__							 =						intermediate_size
       UpperCAmelCase__							 =						hidden_act
       UpperCAmelCase__							 =						hidden_dropout_prob
       UpperCAmelCase__							 =						attention_probs_dropout_prob
       UpperCAmelCase__							 =						type_sequence_label_size
       UpperCAmelCase__							 =						initializer_range
       UpperCAmelCase__							 =						scope
       UpperCAmelCase__							 =						frequency_stride
       UpperCAmelCase__							 =						time_stride

       # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
       UpperCAmelCase__							 =						(self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
       UpperCAmelCase__							 =						(self.max_length - self.patch_size) // self.time_stride + 1
       UpperCAmelCase__							 =						frequency_out_dimension * time_out_dimension
       UpperCAmelCase__							 =						num_patches + 2
      def 	UpperCAmelCase_						(			self   :int      )     ->			List[str]:
       UpperCAmelCase__							 =						floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]      )

       UpperCAmelCase__							 =						None
       if self.use_labels:
        UpperCAmelCase__							 =						ids_tensor([self.batch_size] ,      self.type_sequence_label_size      )

       UpperCAmelCase__							 =						self.get_config()

       return config, input_values, labels
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			Any:
       return ASTConfig(
           patch_size=self.patch_size ,      max_length=self.max_length ,      num_mel_bins=self.num_mel_bins ,      hidden_size=self.hidden_size ,      num_hidden_layers=self.num_hidden_layers ,      num_attention_heads=self.num_attention_heads ,      intermediate_size=self.intermediate_size ,      hidden_act=self.hidden_act ,      hidden_dropout_prob=self.hidden_dropout_prob ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob ,      is_decoder=lowerCamelCase ,      initializer_range=self.initializer_range ,      frequency_stride=self.frequency_stride ,      time_stride=self.time_stride ,      )
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :List[str]      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						ASTModel(config=lowerCamelCase      )
       model.to(lowerCamelCase      )
       model.eval()
       UpperCAmelCase__							 =						model(lowerCamelCase      )
       self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)      )







      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			str:
       UpperCAmelCase__							 =						self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase__
       ) , (
           UpperCAmelCase__
       ) , (
           UpperCAmelCase__
       ) , 
       )							 =						config_and_inputs
       UpperCAmelCase__							 =						{"input_values": input_values}
       return config, inputs_dict



@require_torch
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		(
          (
              ASTModel,
              ASTForAudioClassification,
          )
          if is_torch_available()
          else ()
      )
      UpperCAmelCase_        =		(
          {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
          if is_torch_available()
          else {}
      )
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :str ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :int      )     ->			str:
       if pipeline_test_casse_name == "AudioClassificationPipelineTests":
        return True

       return False
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			int:
       UpperCAmelCase__							 =						ASTModelTester(self      )
       UpperCAmelCase__							 =						ConfigTester(self ,      config_class=lowerCamelCase ,      has_text_modality=lowerCamelCase ,      hidden_size=37      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			str:
       self.config_tester.run_common_tests()
      @unittest.skip(reason="AST does not use inputs_embeds"      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Optional[Any]:
       pass
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Any:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__							 =						model_class(lowerCamelCase      )
        self.assertIsInstance(model.get_input_embeddings() ,      (nn.Module)      )
        UpperCAmelCase__							 =						model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCamelCase ,      nn.Linear      )      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			List[str]:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__							 =						model_class(lowerCamelCase      )
        UpperCAmelCase__							 =						inspect.signature(model.forward      )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        UpperCAmelCase__							 =						[*signature.parameters.keys()]

        UpperCAmelCase__							 =						["input_values"]
        self.assertListEqual(arg_names[:1] ,      lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			Any:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase      )







      @slow
      def 	UpperCAmelCase_						(			self   :int      )     ->			Optional[Any]:
       for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							 =						ASTModel.from_pretrained(lowerCamelCase      )
        self.assertIsNotNone(lowerCamelCase      )



def 						lowerCAmelCase		(		):




 """simple docstring"""







 UpperCAmelCase__							 =						hf_hub_download(
     repo_id="nielsr/audio-spectogram-transformer-checkpoint"						,							filename="sample_audio.flac"						,							repo_type="dataset"	)

 UpperCAmelCase__ , UpperCAmelCase__							 =						torchaudio.load(_lowerCAmelCase	)

 return audio, sampling_rate


@require_torch
@require_torchaudio
class 			_UpperCamelCase      (				unittest.TestCase		):
      @cached_property
      def 	UpperCAmelCase_						(			self   :str      )     ->			Dict:
       return (
           ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"      )
           if is_torchaudio_available()
           else None
       )







      @slow
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[int]:
       UpperCAmelCase__							 =						self.default_feature_extractor
       UpperCAmelCase__							 =						ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						self.default_feature_extractor
       UpperCAmelCase__ , UpperCAmelCase__							 =						prepare_audio()
       UpperCAmelCase__							 =						audio.squeeze().numpy()
       UpperCAmelCase__							 =						feature_extractor(lowerCamelCase ,      sampling_rate=lowerCamelCase ,      return_tensors="pt"      ).to(lowerCamelCase      )

       # forward pass
       with torch.no_grad():
        UpperCAmelCase__							 =						model(**lowerCamelCase      )

       # verify the logits
       UpperCAmelCase__							 =						torch.Size((1, 527)      )
       self.assertEqual(outputs.logits.shape ,      lowerCamelCase      )

       UpperCAmelCase__							 =						torch.tensor([-0.87_60, -7.00_42, -8.66_02]      ).to(lowerCamelCase      )

       self.assertTrue(torch.allclose(outputs.logits[0, :3] ,      lowerCamelCase ,      atol=1e-4      )      )





