

'''simple docstring'''







import os
from glob import glob

import imageio
import torch
import torchvision
import wandb
from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan
from loaders import load_vqgan
from PIL import Image
from torch import nn

from transformers import CLIPModel, CLIPTokenizerFast
from utils import get_device, get_timestamp, show_pil


class        A__   :



    def __init__(       self		,					UpperCamelCase__ = "cpu"		,					UpperCamelCase__ = "openai/clip-vit-large-patch14"							)    ->   None:





           '''simple docstring'''
           A_						       =   device
           A_						       =   CLIPTokenizerFast.from_pretrained(UpperCamelCase__							)
           A_						       =   [0.48145466, 0.4578275, 0.40821073]
           A_						       =   [0.26862954, 0.26130258, 0.27577711]
           A_						       =   torchvision.transforms.Normalize(self.image_mean		,					self.image_std							)
           A_						       =   torchvision.transforms.Resize(224							)
           A_						       =   torchvision.transforms.CenterCrop(224							)



    def        snake_case_      (       self		,					UpperCamelCase__							)    ->   int:





           '''simple docstring'''
           A_						       =   self.resize(UpperCamelCase__							)
           A_						       =   self.center_crop(UpperCamelCase__							)
           A_						       =   self.normalize(UpperCamelCase__							)
           return images







    def __call__(       self		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					**UpperCamelCase__							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   self.tokenizer(text=UpperCamelCase__		,					**UpperCamelCase__							)
           A_						       =   self.preprocess_img(UpperCamelCase__							)
           A_						       =   {key: value.to(self.device							) for (key, value) in encoding.items()}
           return encoding


class        A__   (   nn.Module      ):



    def __init__(       self		,					UpperCamelCase__=10		,					UpperCamelCase__=0.01		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=False		,					UpperCamelCase__=True		,					UpperCamelCase__="image"		,					UpperCamelCase__=True		,					UpperCamelCase__=False		,					UpperCamelCase__=False		,					UpperCamelCase__=False		,					)    ->   None:





           '''simple docstring'''
           super().__init__()
           A_						       =   None
           A_						       =   device if device else get_device()
           if vqgan:
                  A_						       =   vqgan
           else:
                  A_						       =   load_vqgan(self.device		,					conf_path=UpperCamelCase__		,					ckpt_path=UpperCamelCase__							)
           self.vqgan.eval()
           if clip:
                  A_						       =   clip
           else:
                  A_						       =   CLIPModel.from_pretrained("""openai/clip-vit-base-patch32"""							)
           self.clip.to(self.device							)
           A_						       =   ProcessorGradientFlow(device=self.device							)

           A_						       =   iterations
           A_						       =   lr
           A_						       =   log
           A_						       =   make_grid
           A_						       =   return_val
           A_						       =   quantize
           A_						       =   self.vqgan.decoder.z_shape



    def        snake_case_      (       self		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=5		,					UpperCamelCase__=True							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   []
           if output_path is None:
                  A_						       =   """./animation.gif"""
           if input_path is None:
                  A_						       =   self.save_path
           A_						       =   sorted(glob(input_path + """/*"""							)							)
           if not len(UpperCamelCase__							):
                  raise ValueError(
                      """No images found in save path, aborting (did you pass save_intermediate=True to the generate"""
                      """ function?)"""							)
           if len(UpperCamelCase__							) == 1:
                  print("""Only one image found in save path, (did you pass save_intermediate=True to the generate function?)"""							)
           A_						       =   total_duration / len(UpperCamelCase__							)
           A_						       =   [frame_duration] * len(UpperCamelCase__							)
           if extend_frames:
                  A_						       =   1.5
                  A_						       =   3
           for file_name in paths:
                  if file_name.endswith(""".png"""							):
                         images.append(imageio.imread(UpperCamelCase__							)							)
           imageio.mimsave(UpperCamelCase__		,					UpperCamelCase__		,					duration=UpperCamelCase__							)
           print(f'''gif saved to {output_path}'''							)



    def        snake_case_      (       self		,					UpperCamelCase__=None		,					UpperCamelCase__=None							)    ->   Optional[int]:





           '''simple docstring'''
           if not (path or img):
                  raise ValueError("""Input either path or tensor"""							)
           if img is not None:
                  raise NotImplementedError
           A_						       =   preprocess(Image.open(UpperCamelCase__							)		,					target_image_size=256							).to(self.device							)
           A_						       =   preprocess_vqgan(UpperCamelCase__							)
           A_       ,      *A_						       =   self.vqgan.encode(UpperCamelCase__							)
           return z



    def        snake_case_      (       self		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   self.latent.detach().requires_grad_()
           A_						       =   base_latent + transform_vector
           if self.quantize:
                  A_       ,      *A_						       =   self.vqgan.quantize(UpperCamelCase__							)
           else:
                  A_						       =   trans_latent
           return self.vqgan.decode(UpperCamelCase__							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__=None							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.clip_preprocessor(text=UpperCamelCase__		,					images=UpperCamelCase__		,					return_tensors="""pt"""		,					padding=UpperCamelCase__							)
           A_						       =   self.clip(**UpperCamelCase__							)
           A_						       =   clip_outputs.logits_per_image
           if weights is not None:
                  A_						       =   similarity_logits * weights
           return similarity_logits.sum()



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Dict:





           '''simple docstring'''
           A_						       =   self._get_clip_similarity(pos_prompts["""prompts"""]		,					UpperCamelCase__		,					weights=(1 / pos_prompts["""weights"""])							)
           if neg_prompts:
                  A_						       =   self._get_clip_similarity(neg_prompts["""prompts"""]		,					UpperCamelCase__		,					weights=neg_prompts["""weights"""]							)
           else:
                  A_						       =   torch.tensor([1]		,					device=self.device							)
           A_						       =   -torch.log(UpperCamelCase__							) + torch.log(UpperCamelCase__							)
           return loss



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[int]:





           '''simple docstring'''
           A_						       =   torch.randn_like(self.latent		,					requires_grad=UpperCamelCase__		,					device=self.device							)
           A_						       =   torch.optim.Adam([vector]		,					lr=self.lr							)

           for i in range(self.iterations							):
                  optim.zero_grad()
                  A_						       =   self._add_vector(UpperCamelCase__							)
                  A_						       =   loop_post_process(UpperCamelCase__							)
                  A_						       =   self._get_CLIP_loss(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)
                  print("""CLIP loss"""		,					UpperCamelCase__							)
                  if self.log:
                         wandb.log({"""CLIP Loss""": clip_loss}							)
                  clip_loss.backward(retain_graph=UpperCamelCase__							)
                  optim.step()
                  if self.return_val == "image":
                         yield custom_to_pil(transformed_img[0]							)
                  else:
                         yield vector



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   str:





           '''simple docstring'''
           wandb.init(reinit=UpperCamelCase__		,					project="""face-editor"""							)
           wandb.config.update({"""Positive Prompts""": positive_prompts}							)
           wandb.config.update({"""Negative Prompts""": negative_prompts}							)
           wandb.config.update({"""lr""": self.lr, """iterations""": self.iterations}							)
           if image_path:
                  A_						       =   Image.open(UpperCamelCase__							)
                  A_						       =   image.resize((256, 256)							)
                  wandb.log("""Original Image"""		,					wandb.Image(UpperCamelCase__							)							)



    def        snake_case_      (       self		,					UpperCamelCase__							)    ->   Optional[int]:





           '''simple docstring'''
           if not prompts:
                  return []
           A_						       =   []
           A_						       =   []
           if isinstance(UpperCamelCase__		,					UpperCamelCase__							):
                  A_						       =   [prompt.strip() for prompt in prompts.split("""|"""							)]
           for prompt in prompts:
                  if isinstance(UpperCamelCase__		,					(tuple, list)							):
                         A_						       =   prompt[0]
                         A_						       =   float(prompt[1]							)
                  elif ":" in prompt:
                         A_       ,      A_						       =   prompt.split(""":"""							)
                         A_						       =   float(UpperCamelCase__							)
                  else:
                         A_						       =   prompt
                         A_						       =   1.0
                  processed_prompts.append(UpperCamelCase__							)
                  weights.append(UpperCamelCase__							)
           return {
               "prompts": processed_prompts,
               "weights": torch.tensor(UpperCamelCase__		,					device=self.device							),
           }







    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					UpperCamelCase__=True		,					UpperCamelCase__=False		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=None		,					)    ->   List[str]:





           '''simple docstring'''
           if image_path:
                  A_						       =   self._get_latent(UpperCamelCase__							)
           else:
                  A_						       =   torch.randn(self.latent_dim		,					device=self.device							)
           if self.log:
                  self._init_logging(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)

           assert pos_prompts, "You must provide at least one positive prompt."
           A_						       =   self.process_prompts(UpperCamelCase__							)
           A_						       =   self.process_prompts(UpperCamelCase__							)
           if save_final and save_path is None:
                  A_						       =   os.path.join("""./outputs/"""		,					"""_""".join(pos_prompts["""prompts"""]							)							)
           if not os.path.exists(UpperCamelCase__							):
                  os.makedirs(UpperCamelCase__							)
           else:
                  A_						       =   save_path + """_""" + get_timestamp()
                  os.makedirs(UpperCamelCase__							)
           A_						       =   save_path

           A_						       =   self.vqgan.decode(self.latent							)[0]
           if show_intermediate:
                  print("""Original Image"""							)
                  show_pil(custom_to_pil(UpperCamelCase__							)							)

           A_						       =   loop_post_process(UpperCamelCase__							)
           for iter, transformed_img in enumerate(self._optimize_CLIP(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)							):
                  if show_intermediate:
                         show_pil(UpperCamelCase__							)
                  if save_intermediate:
                         transformed_img.save(os.path.join(self.save_path		,					f'''iter_{iter:03d}.png'''							)							)
                  if self.log:
                         wandb.log({"""Image""": wandb.Image(UpperCamelCase__							)}							)
           if show_final:
                  show_pil(UpperCamelCase__							)
           if save_final:
                  transformed_img.save(os.path.join(self.save_path		,					f'''iter_{iter:03d}_final.png'''							)							)



'''simple docstring'''







import os
import sys
import unittest


__lowerCamelCase									=     os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__lowerCamelCase									=     os.path.join(git_repo_path, '''src''', '''transformers''')

__lowerCamelCase									=     '''
{0} = None
'''

__lowerCamelCase									=     '''
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
'''


__lowerCamelCase									=     '''
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
'''


class        A__   (   unittest.TestCase      ):



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           A_						       =   find_backend("""    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"""							)
           self.assertIsNone(UpperCamelCase__							)

           A_						       =   find_backend("""    if not is_tokenizers_available():"""							)
           self.assertEqual(UpperCamelCase__		,					"""tokenizers"""							)

           A_						       =   find_backend("""    if not is_tensorflow_text_available():"""							)
           self.assertEqual(UpperCamelCase__		,					"""tensorflow_text"""							)

           A_						       =   find_backend("""    if not (is_sentencepiece_available() and is_tokenizers_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tokenizers"""							)

           A_						       =   find_backend(
               """    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tensorflow_text"""							)

           A_						       =   find_backend(
               """    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tokenizers_and_vision"""							)



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   read_init()
           # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
           self.assertIn("""torch"""		,					UpperCamelCase__							)
           self.assertIn("""tensorflow_text"""		,					UpperCamelCase__							)
           self.assertIn("""sentencepiece_and_tokenizers"""		,					UpperCamelCase__							)

           # Likewise, we can't assert on the exact content of a key
           self.assertIn("""BertModel"""		,					objects["""torch"""]							)
           self.assertIn("""TFBertModel"""		,					objects["""tf"""]							)
           self.assertIn("""FlaxBertModel"""		,					objects["""flax"""]							)
           self.assertIn("""BertModel"""		,					objects["""torch"""]							)
           self.assertIn("""TFBertTokenizer"""		,					objects["""tensorflow_text"""]							)
           self.assertIn("""convert_slow_tokenizer"""		,					objects["""sentencepiece_and_tokenizers"""]							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   create_dummy_object("""CONSTANT"""		,					"""'torch'"""							)
           self.assertEqual(UpperCamelCase__		,					"""\nCONSTANT = None\n"""							)

           A_						       =   create_dummy_object("""function"""		,					"""'torch'"""							)
           self.assertEqual(
               UpperCamelCase__		,					"""\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""							)

           A_						       =   """
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')
"""
           A_						       =   create_dummy_object("""FakeClass"""		,					"""'torch'"""							)
           self.assertEqual(UpperCamelCase__		,					UpperCamelCase__							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   """# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])
"""
           A_						       =   create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}							)
           self.assertEqual(dummy_files["""torch"""]		,					UpperCamelCase__							)
