




'''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      lowerCAmelCase :


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



            '''simple docstring'''




            __snake_case            =	device
            __snake_case            =	CLIPTokenizerFast.from_pretrained(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	[0.48_145_466, 0.4_578_275, 0.40_821_073]
            __snake_case            =	[0.26_862_954, 0.26_130_258, 0.27_577_711]
            __snake_case            =	torchvision.transforms.Normalize(self.image_mean     ,	self.image_std	)
            __snake_case            =	torchvision.transforms.Resize(224	)
            __snake_case            =	torchvision.transforms.CenterCrop(224	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            __snake_case            =	self.resize(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.center_crop(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.normalize(__SCREAMING_SNAKE_CASE	)
            return images




       def __call__( self     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            __snake_case            =	self.tokenizer(text=__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.preprocess_img(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	{key: value.to(self.device	) for (key, value) in encoding.items()}
            return encoding





class      lowerCAmelCase (			nn.Module):


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



            '''simple docstring'''




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

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


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=5     ,	__SCREAMING_SNAKE_CASE=True	)   ->	Dict:



            '''simple docstring'''




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


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None	)   ->	List[Any]:



            '''simple docstring'''




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


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




            __snake_case            =	self.latent.detach().requires_grad_()
            __snake_case            =	base_latent + transform_vector
            if self.quantize:
                 __snake_case       ,     *__snake_case            =	self.vqgan.quantize(__SCREAMING_SNAKE_CASE	)
            else:
                 __snake_case            =	trans_latent
            return self.vqgan.decode(__SCREAMING_SNAKE_CASE	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None	)   ->	Optional[Any]:



            '''simple docstring'''




            __snake_case            =	self.clip_preprocessor(text=__SCREAMING_SNAKE_CASE     ,	images=__SCREAMING_SNAKE_CASE     ,	return_tensors='''pt'''     ,	padding=__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.clip(**__SCREAMING_SNAKE_CASE	)
            __snake_case            =	clip_outputs.logits_per_image
            if weights is not None:
                 __snake_case            =	similarity_logits * weights
            return similarity_logits.sum()


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            __snake_case            =	self._get_clip_similarity(pos_prompts['''prompts''']     ,	__SCREAMING_SNAKE_CASE     ,	weights=(1 / pos_prompts['''weights'''])	)
            if neg_prompts:
                 __snake_case            =	self._get_clip_similarity(neg_prompts['''prompts''']     ,	__SCREAMING_SNAKE_CASE     ,	weights=neg_prompts['''weights''']	)
            else:
                 __snake_case            =	torch.tensor([1]     ,	device=self.device	)
            __snake_case            =	-torch.log(__SCREAMING_SNAKE_CASE	) + torch.log(__SCREAMING_SNAKE_CASE	)
            return loss


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            __snake_case            =	torch.randn_like(self.latent     ,	requires_grad=__SCREAMING_SNAKE_CASE     ,	device=self.device	)
            __snake_case            =	torch.optim.Adam([vector]     ,	lr=self.lr	)

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


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[Any]:



            '''simple docstring'''




            wandb.init(reinit=__SCREAMING_SNAKE_CASE     ,	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:
                 __snake_case            =	Image.open(__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	image.resize((256, 256)	)
                 wandb.log('''Original Image'''     ,	wandb.Image(__SCREAMING_SNAKE_CASE	)	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




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




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=False     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=None     ,	)   ->	int:



            '''simple docstring'''




            if image_path:
                 __snake_case            =	self._get_latent(__SCREAMING_SNAKE_CASE	)
            else:
                 __snake_case            =	torch.randn(self.latent_dim     ,	device=self.device	)
            if self.log:
                 self._init_logging(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)

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

            __snake_case            =	self.vqgan.decode(self.latent	)[0]
            if show_intermediate:
                 print('''Original Image'''	)
                 show_pil(custom_to_pil(__SCREAMING_SNAKE_CASE	)	)

            __snake_case            =	loop_post_process(__SCREAMING_SNAKE_CASE	)
            for iter, transformed_img in enumerate(self._optimize_CLIP(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)	):
                 if show_intermediate:
                      show_pil(__SCREAMING_SNAKE_CASE	)
                 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(__SCREAMING_SNAKE_CASE	)}	)
            if show_final:
                 show_pil(__SCREAMING_SNAKE_CASE	)
            if save_final:
                 transformed_img.save(os.path.join(self.save_path     ,	F'''iter_{iter:03d}_final.png'''	)	)








"""simple docstring"""
import os

def  lowercase     ():
						with open(os.path.dirname(_lowerCAmelCase						) + """/grid.txt"""						) as f:
												__lowerCAmelCase     =  []  # noqa: E741
												for _ in range(20						):
																		l.append([int(_lowerCAmelCase						) for x in f.readline().split()]						)

												__lowerCAmelCase     =  0

												# right
												for i in range(20						):
																		for j in range(17						):
																								__lowerCAmelCase     =  l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # down
												for i in range(17						):
																		for j in range(20						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # diagonal 1
												for i in range(17						):
																		for j in range(17						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # diagonal 2
												for i in range(17						):
																		for j in range(3 ,    20						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp
												return maximum


if __name__ == "__main__":
			print(solution())
