


import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


_snake_case             =     logging.getLogger(__name__)
@dataclass(frozen=UpperCamelCase    )
class 	UpperCAmelCase_					:

   '''simple docstring'''




   __A :      str
   __A :      str
   __A :      Optional[str]     		=		None
   __A :      Optional[str]     		=		None
   __A :      Optional[str]     		=		None



@dataclass(frozen=UpperCamelCase    )
class 	UpperCAmelCase_					:

   '''simple docstring'''




   __A :      List[int]
   __A :      Optional[List[int]]     		=		None
   __A :      Optional[List[int]]     		=		None
   __A :      Optional[Union[int, float]]     		=		None
   __A :      Optional[int]     		=		None


if is_torch_available():
   import torch
   from torch.utils.data import Dataset
   class 	UpperCAmelCase_					(					UpperCamelCase    ):

      '''simple docstring'''




      __A :      List[InputFeatures]






      def __init__(		self						,				__A						,				__A						,				__A						,				__A = None						,				__A=False						,				__A = False						,				):






        """simple docstring"""





        lowerCamelCase :					Optional[int]    =				hans_processors[task]()

        lowerCamelCase :					Any    =				os.path.join(
            __A						,				"cached_{}_{}_{}_{}".format(
                "dev" if evaluate else "train"						,				tokenizer.__class__.__name__						,				str(__A )						,				__A						,				)						,				)
        lowerCamelCase :					int    =				processor.get_labels()
        if tokenizer.__class__ in (
            RobertaTokenizer,
            RobertaTokenizerFast,
            XLMRobertaTokenizer,
            BartTokenizer,
            BartTokenizerFast,
        ):
          # HACK(label indices are swapped in RoBERTa pretrained model)
          lowerCamelCase    ,	lowerCamelCase :					str    =				label_list[2], label_list[1]
        lowerCamelCase :					Any    =				label_list

        # Make sure only the first process in distributed training processes the dataset,
        # and the others will use the cache.
        lowerCamelCase :					List[str]    =				cached_features_file + ".lock"
        with FileLock(__A ):
          if os.path.exists(__A ) and not overwrite_cache:
            logger.info(F"""Loading features from cached file {cached_features_file}""" )
            lowerCamelCase :					Optional[Any]    =				torch.load(__A )
          else:
            logger.info(F"""Creating features from dataset file at {data_dir}""" )

            lowerCamelCase :					Union[str, Any]    =				(
                processor.get_dev_examples(__A ) if evaluate else processor.get_train_examples(__A )
            )

            logger.info("Training examples: %s"						,				len(__A ) )
            lowerCamelCase :					Optional[Any]    =				hans_convert_examples_to_features(__A						,				__A						,				__A						,				__A )
            logger.info("Saving features into cached file %s"						,				__A )
            torch.save(self.features						,				__A )






      def __len__(		self ):






        """simple docstring"""





        return len(self.features )






      def __getitem__(		self						,				__A ):






        """simple docstring"""





        return self.features[i]






      def _snake_case    (		self ):






        """simple docstring"""





        return self.label_list


if is_tf_available():
   import tensorflow as tf
   class 	UpperCAmelCase_					:

      '''simple docstring'''




      __A :      List[InputFeatures]






      def __init__(		self						,				__A						,				__A						,				__A						,				__A = 128						,				__A=False						,				__A = False						,				):






        """simple docstring"""





        lowerCamelCase :					List[str]    =				hans_processors[task]()
        lowerCamelCase :					List[str]    =				processor.get_labels()
        if tokenizer.__class__ in (
            RobertaTokenizer,
            RobertaTokenizerFast,
            XLMRobertaTokenizer,
            BartTokenizer,
            BartTokenizerFast,
        ):
          # HACK(label indices are swapped in RoBERTa pretrained model)
          lowerCamelCase    ,	lowerCamelCase :					List[Any]    =				label_list[2], label_list[1]
        lowerCamelCase :					Dict    =				label_list

        lowerCamelCase :					Any    =				processor.get_dev_examples(__A ) if evaluate else processor.get_train_examples(__A )
        lowerCamelCase :					Tuple    =				hans_convert_examples_to_features(__A						,				__A						,				__A						,				__A )

        def gen():
          for ex_index, ex in tqdm.tqdm(enumerate(self.features )						,				desc="convert examples to features" ):
            if ex_index % 1_0000 == 0:
              logger.info("Writing example %d of %d" % (ex_index, len(__A )) )

            yield (
                {
                    "example_id": 0,
                    "input_ids": ex.input_ids,
                    "attention_mask": ex.attention_mask,
                    "token_type_ids": ex.token_type_ids,
                },
                ex.label,
            )

        lowerCamelCase :					int    =				tf.data.Dataset.from_generator(
            __A						,				(
                {
                    "example_id": tf.intaa,
                    "input_ids": tf.intaa,
                    "attention_mask": tf.intaa,
                    "token_type_ids": tf.intaa,
                },
                tf.intaa,
            )						,				(
                {
                    "example_id": tf.TensorShape([] ),
                    "input_ids": tf.TensorShape([None, None] ),
                    "attention_mask": tf.TensorShape([None, None] ),
                    "token_type_ids": tf.TensorShape([None, None] ),
                },
                tf.TensorShape([] ),
            )						,				)






      def _snake_case    (		self ):






        """simple docstring"""





        return self.dataset






      def __len__(		self ):






        """simple docstring"""





        return len(self.features )






      def __getitem__(		self						,				__A ):






        """simple docstring"""





        return self.features[i]






      def _snake_case    (		self ):






        """simple docstring"""





        return self.label_list
class 	UpperCAmelCase_					(					UpperCamelCase    ):

   '''simple docstring'''




   def _snake_case    (		self						,				__A ):






     """simple docstring"""





     return self._create_examples(self._read_tsv(os.path.join(__A						,				"heuristics_train_set.txt" ) )						,				"train" )






   def _snake_case    (		self						,				__A ):






     """simple docstring"""





     return self._create_examples(self._read_tsv(os.path.join(__A						,				"heuristics_evaluation_set.txt" ) )						,				"dev" )






   def _snake_case    (		self ):






     """simple docstring"""





     return ["contradiction", "entailment", "neutral"]






   def _snake_case    (		self						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					int    =				[]
     for i, line in enumerate(__A ):
       if i == 0:
         continue
       lowerCamelCase :					Optional[Any]    =				"%s-%s" % (set_type, line[0])
       lowerCamelCase :					int    =				line[5]
       lowerCamelCase :					Dict    =				line[6]
       lowerCamelCase :					int    =				line[7][2:] if line[7].startswith("ex" ) else line[7]
       lowerCamelCase :					str    =				line[0]
       examples.append(InputExample(guid=__A						,				text_a=__A						,				text_b=__A						,				label=__A						,				pairID=__A ) )
     return examples






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


  '''simple docstring'''

  lowerCamelCase :					Dict    =				{label: i for i, label in enumerate(SCREAMING_SNAKE_CASE_	)}

  lowerCamelCase :					Optional[int]    =				[]
  for ex_index, example in tqdm.tqdm(enumerate(SCREAMING_SNAKE_CASE_	)      ,						desc="convert examples to features"	):
    if ex_index % 10000 == 0:
      logger.info("Writing example %d" % (ex_index)	)

    lowerCamelCase :					Optional[Any]    =				tokenizer(
        example.text_a      ,						example.text_b      ,						add_special_tokens=SCREAMING_SNAKE_CASE_      ,						max_length=SCREAMING_SNAKE_CASE_      ,						padding="max_length"      ,						truncation=SCREAMING_SNAKE_CASE_      ,						return_overflowing_tokens=SCREAMING_SNAKE_CASE_      ,						)

    lowerCamelCase :					str    =				label_map[example.label] if example.label in label_map else 0

    lowerCamelCase :					Tuple    =				int(example.pairID	)

    features.append(InputFeatures(**SCREAMING_SNAKE_CASE_      ,						label=SCREAMING_SNAKE_CASE_      ,						pairID=SCREAMING_SNAKE_CASE_	)	)

  for i, example in enumerate(examples[:5]	):
    logger.info("*** Example ***"	)
    logger.info(f"""guid: {example}"""	)
    logger.info(f"""features: {features[i]}"""	)

  return features


_snake_case             =     {
    '''hans''': 3,
}







_snake_case             =     {
    '''hans''': HansProcessor,
}







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



