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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import torch
import random
import numpy as np
import gradio as gr
from glob import glob
from datetime import datetime

from diffusers import StableDiffusionPipeline,AutoencoderKL
from diffusers import DDIMScheduler, LCMScheduler, EulerDiscreteScheduler

import torch.nn.functional as F
from PIL import Image,ImageDraw
from utils.pipeline import ZePoPipeline
from utils.attn_control import AttentionStyle
from torchvision.utils import save_image
import utils.ptp_utils as ptp_utils

import torchvision.transforms as transforms

try:
    import xformers
    is_xformers = True
except ImportError:
    is_xformers = False

css = """
.toolbutton {
    margin-buttom: 0em 0em 0em 0em;
    max-width: 2.5em;
    min-width: 2.5em !important;
    height: 2.5em;
}
"""
# import sys
# sys.setrecursionlimit(100000)


class GlobalText:
    def __init__(self):
        
        # config dirs
        self.basedir                = os.getcwd()
        self.stable_diffusion_dir   = os.path.join(self.basedir, "models", "StableDiffusion")
        self.personalized_model_dir = './models/Stable-diffusion'
        self.lora_model_dir         = './models/Lora'
        self.savedir            = os.path.join(self.basedir, "samples", datetime.now().strftime("Gradio-%Y-%m-%dT%H-%M-%S"))
        self.savedir_sample         = os.path.join(self.savedir, "sample")

        # self.savedir_mask         = os.path.join(self.savedir, "mask")

        self.stable_diffusion_list   = ["SimianLuo/LCM_Dreamshaper_v7"
                                        ]
        self.personalized_model_list = []
        self.lora_model_list = []

        self.tokenizer             = None
        self.text_encoder          = None
        self.vae                   = None
        self.unet                  = None
        self.pipeline              = None
        self.torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
        self.lora_model_state_dict = {}
        self.device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

    def init_source_image_path(self, source_path):
        self.source_paths = sorted(glob(os.path.join(source_path, '*')))
        self.max_source_index = len(self.source_paths) // 12
        return self.source_paths[0:12]
    def init_style_image_path(self, style_path):
        self.style_paths = sorted(glob(os.path.join(style_path, '*')))
        self.max_style_index = len(self.style_paths) // 12
        return self.style_paths[0:12]
    def init_results_image_path(self):
        results_paths = [os.path.join(self.savedir_sample, file) for file in os.listdir(self.savedir_sample)]
        self.results_paths = sorted(results_paths, key=os.path.getctime, reverse=True)
        self.max_results_index = len(self.results_paths) // 12
        return self.results_paths[0:12]
    
    def load_base_pipeline(self, model_path):
        
        time_start = datetime.now()

        self.scheduler = 'LCM'
        scheduler = LCMScheduler.from_pretrained(model_path, subfolder="scheduler")
        self.pipeline = ZePoPipeline.from_pretrained(model_path,scheduler=scheduler,torch_dtype=torch.float16,).to('cuda')
        if is_xformers:
            self.pipeline.enable_xformers_memory_efficient_attention()
        time_end = datetime.now()
        print(f'Load {model_path} successful in {time_end-time_start}')
        return gr.Dropdown()
    
    def refresh_stable_diffusion(self,model_path):
        
        self.load_base_pipeline(model_path)
        
        return self.stable_diffusion_list[0]

    def update_base_model(self, base_model_dropdown):
        if self.pipeline is None:
            gr.Info(f"Please select a pretrained model path.")
            return None
        else:
            base_model = self.personalized_model_list[base_model_dropdown]
            mid_model = StableDiffusionPipeline.from_single_file(base_model)
            self.pipeline.vae = mid_model.vae
            self.pipeline.unet = mid_model.unet
            self.pipeline.text_encoder = mid_model.text_encoder
            self.pipeline.to(self.device)
            self.personal_model_loaded = base_model_dropdown.split('.')[0]
            print(f'load {base_model_dropdown} model success!')
            return gr.Dropdown()

    
    def generate(self, source, style, 
                       num_steps, co_feat_step,strength,
                       start_ac_layer, end_ac_layer,
                       sty_guidance,cfg_scale, mix_q_scale,
                       Scheduler, save_intermediate, seed, de_bug,
                       target_prompt, negative_prompt_textbox,
                       width_slider,height_slider,
                       tome_sx, tome_sy, tome_ratio,tome,
                       ):


        os.makedirs(self.savedir, exist_ok=True)
        os.makedirs(self.savedir_sample, exist_ok=True)
        
        if self.pipeline == None:
            self.refresh_stable_diffusion(self.stable_diffusion_list[-1])
        model = self.pipeline

        if Scheduler == 'DDIM':
            model.scheduler = DDIMScheduler.from_config(model.scheduler.config)
            print(f"Successful adoption of DDIM scheduler")
        if Scheduler == 'LCM':
            model.scheduler = LCMScheduler.from_config(model.scheduler.config)
            print(f"Successful adoption of LCM scheduler")
        if Scheduler == 'EulerDiscrete':
            model.scheduler = EulerDiscreteScheduler.from_config(model.scheduler.config)

        if seed != '-1' and seed != "": torch.manual_seed(int(seed))
        else: torch.seed()
        
        seed = torch.initial_seed()
        print(f"Seed: {seed}")

        self.sample_count = len(os.listdir(self.savedir_sample))
        

        prompts = [target_prompt] * 3
        source = source.resize((width_slider, height_slider))
        style = style.resize((width_slider, height_slider))


        with torch.no_grad():

            controller = AttentionStyle(num_steps, 
                                         start_ac_layer,
                                         end_ac_layer,
                                         style_guidance=sty_guidance,
                                         mix_q_scale=mix_q_scale,
                                         de_bug=de_bug,
                                         )
                                                        
            ptp_utils.register_attention_control(model, controller, 
                                                 tome, 
                                                 sx=tome_sx,
                                                 sy=tome_sy,
                                                 ratio=tome_ratio,
                                                 de_bug=de_bug,)

            time_begin = datetime.now()
            generate_image = model(prompt=prompts,
                                negative_prompt=negative_prompt_textbox,
                                image=source,
                                style=style,
                                num_inference_steps=num_steps,
                                eta=0.0,
                                guidance_scale=cfg_scale,
                                strength=strength,
                                save_intermediate=save_intermediate,
                                fix_step_index=co_feat_step,
                                de_bug = de_bug,
                                callback = None
                   ).images
            time_end = datetime.now()
            print('generate one image with time {}'.format(time_end-time_begin))

            save_file_name = f"{self.sample_count}_step{num_steps}_sl{start_ac_layer}_el{end_ac_layer}_ST{strength}_CF{co_feat_step}_STG{sty_guidance}_MQ{mix_q_scale}_CFG{cfg_scale}_seed{seed}.jpg"

            
            save_file_path = os.path.join(self.savedir, save_file_name)
                    
            save_image(torch.tensor(generate_image).permute(0, 3, 1, 2), save_file_path, nrow=3, padding=0)
            save_image(torch.tensor(generate_image[2:]).permute(0, 3, 1, 2), os.path.join(self.savedir_sample, save_file_name), nrow=3, padding=0)
            self.init_results_image_path()
        return [
            generate_image[0],
            generate_image[1],
            generate_image[2],
            self.init_results_image_path()
            ]
    

global_text = GlobalText()


def ui():
    with gr.Blocks(css=css) as demo:
        gr.Markdown(
            """
            # [ZePo: Zero-Shot Portrait Stylization with Faster Sampling](https://arxiv.org/abs/2408.05492)
            Jin Liu, Huaibo Huang, Jie Cao, Ran He<br>
            [Arxiv](https://arxiv.org/abs/2408.05492) | [Github](https://github.com/liujin112/ZePo)
            """
        )
        with gr.Column(variant="panel"):
            gr.Markdown(
                """
                ### 1. Select a pretrained model.
                """
            )
            with gr.Row():
                stable_diffusion_dropdown = gr.Dropdown(
                    label="Pretrained Model Path",
                    choices=global_text.stable_diffusion_list,
                    interactive=True,
                    allow_custom_value=True
                )
                stable_diffusion_dropdown.change(fn=global_text.load_base_pipeline, inputs=[stable_diffusion_dropdown], outputs=[stable_diffusion_dropdown])
                
                stable_diffusion_refresh_button = gr.Button(value="\U0001F503", elem_classes="toolbutton")
                def update_stable_diffusion(stable_diffusion_dropdown):
                    global_text.refresh_stable_diffusion(stable_diffusion_dropdown)
                    
                stable_diffusion_refresh_button.click(fn=update_stable_diffusion, inputs=[stable_diffusion_dropdown], outputs=[stable_diffusion_dropdown])


        with gr.Column(variant="panel"):
            gr.Markdown(
                """
                ### 2. Configs for ZePo.
                """
            )
            with gr.Tab("Configs"):
                
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
                            source_image = gr.Image(label="Source Image",  elem_id="img2maskimg", sources="upload",  type="pil",image_mode="RGB", height=256)
                            style_image = gr.Image(label="Style Image", elem_id="img2maskimg", sources="upload", type="pil", image_mode="RGB", height=256)
                        
                        generate_image = gr.Image(label="Image with PortraitDiff", type="pil", interactive=True, image_mode="RGB", height=512)


                        with gr.Row():
                            recons_content = gr.Image(label="reconstructed content", type="pil", image_mode="RGB", height=256)
                            recons_style = gr.Image(label="reconstructed style", type="pil", image_mode="RGB", height=256)
                        prompt_textbox = gr.Textbox(label="Prompt", value='head', lines=1)
                        negative_prompt_textbox = gr.Textbox(label="Negative prompt", lines=1)
                    with gr.Row(equal_height=False):
                        with gr.Column():
                            with gr.Tab("Resolution"):
                                width_slider     = gr.Slider(label="Width", value=512, minimum=256, maximum=1024, step=64)
                                height_slider    = gr.Slider(label="Height", value=512, minimum=256, maximum=1024, step=64)
                                Scheduler = gr.Dropdown(
                                            ["DDIM", "LCM", "EulerDiscrete"],
                                            value="LCM",
                                            label="Scheduler", info="Select a Scheduler")


                            with gr.Tab("Content Gallery"):
                                
                                with gr.Row():
                                    source_path = gr.Textbox(value='./data/content', label="Source Path")
                                    refresh_source_list_button = gr.Button(value="\U0001F503", elem_classes="toolbutton")
                                source_gallery_index = gr.Slider(label="Index", value=0, minimum=0, maximum=50, step=1)   
                                num_gallery_images = 12   
                                source_image_gallery = gr.Gallery(value=[], columns=4, label="Source Image List")
                                refresh_source_list_button.click(fn=global_text.init_source_image_path, inputs=[source_path], outputs=[source_image_gallery]) 

                                def update_source_list(index):
                                    if int(index) < 0:
                                        index = 0
                                    if int(index) > global_text.max_source_index:
                                        index = global_text.max_source_index
                                    return global_text.source_paths[int(index)*num_gallery_images:(int(index)+1)*num_gallery_images]
                                
                                source_gallery_index.change(fn=update_source_list, inputs=[source_gallery_index], outputs=[source_image_gallery])

                            with gr.Tab("Style Gallery"):
                                
                                with gr.Row():
                                    style_path = gr.Textbox(value='./data/style', label="style Path")
                                    refresh_style_list_button = gr.Button(value="\U0001F503", elem_classes="toolbutton")
                                style_gallery_index = gr.Slider(label="Index", value=0, minimum=0, maximum=50, step=1)   
                                num_gallery_images = 12   
                                style_image_gallery = gr.Gallery(value=[], columns=4, label="style Image List")
                                refresh_style_list_button.click(fn=global_text.init_style_image_path, inputs=[style_path], outputs=[style_image_gallery]) 
                                

                                def update_style_list(index):
                                    if int(index) < 0:
                                        index = 0
                                    if int(index) > global_text.max_style_index:
                                        index = global_text.max_style_index
                                    return global_text.style_paths[int(index)*num_gallery_images:(int(index)+1)*num_gallery_images]
                                
                                style_gallery_index.change(fn=update_style_list, inputs=[style_gallery_index], outputs=[style_image_gallery])
                            
                            with gr.Tab("Results Gallery"):
                                with gr.Row():
                                    refresh_results_list_button = gr.Button(value="\U0001F503", elem_classes="toolbutton")
                                    results_gallery_index = gr.Slider(label="Index", value=0, minimum=0, maximum=50, step=1)   
                                num_gallery_images = 12   
                                results_image_gallery = gr.Gallery(value=[], columns=4, label="style Image List")
                                refresh_results_list_button.click(fn=global_text.init_results_image_path, inputs=[], outputs=[results_image_gallery]) 
                                

                                def update_results_list(index):
                                    if int(index) < 0:
                                        index = 0
                                    if int(index) > global_text.max_results_index:
                                        index = global_text.max_results_index
                                    return global_text.results_paths[int(index)*num_gallery_images:(int(index)+1)*num_gallery_images]
                                
                                results_gallery_index.change(fn=update_results_list, inputs=[results_gallery_index], outputs=[style_image_gallery])
                            
                            

                            with gr.Row():    
                                generate_button = gr.Button(value="Generate", variant='primary')

                            with gr.Tab('Base Configs'):
                                num_steps = gr.Slider(label="Total Steps", value=4, minimum=0, maximum=25, step=1)
                                strength = gr.Slider(label="Noisy Ratio", value=0.5, minimum=0, maximum=1, step=0.01,info="How much noise applied to souce image, 50% for better balance.")
                                co_feat_step = gr.Slider(label="Consistency Feature Extract Step", value=99, minimum=0, maximum=999, step=1)
                                

                                with gr.Row():    
                                    start_ac_layer = gr.Slider(label="Start Layer of AC",
                                                            minimum=0,
                                                            maximum=16,
                                                            value=8,
                                                            step=1)
                                    end_ac_layer = gr.Slider(label="End Layer of AC",
                                                            minimum=0,
                                                            maximum=16,
                                                            value=16,
                                                            step=1)
                                    
                                with gr.Row():     
                                    Style_Guidance = gr.Slider(label="Style Guidance Scale",
                                                    minimum=-1,
                                                    maximum=3,
                                                    value=1.2,
                                                    step=0.01,
                                                    )
                                    mix_q_scale = gr.Slider(label='Query Mix Ratio',
                                                            minimum=0,
                                                            maximum=2,
                                                            step=0.05,
                                                            value=1.0,
                                                            )
                                cfg_scale_slider = gr.Slider(label="CFG Scale", value=2.5, minimum=0, maximum=20, info="Classifier-free guidance scale.")
                                
                                with gr.Row():     
                                    save_intermediate = gr.Checkbox(label="save_intermediate", value=False)
                                    de_bug = gr.Checkbox(value=False,label='DeBug')
                            with gr.Tab('ToMe'):
                                with gr.Row():
                                    tome = gr.Checkbox(label="Token Merge", value=True)
                                    
                                    tome_ratio = gr.Slider(label='ratio: ',
                                                            minimum=0,
                                                            maximum=1,
                                                            step=0.1,
                                                            value=0.5)
                                with gr.Row():
                                    tome_sx = gr.Slider(label='sx:',
                                                            minimum=0,
                                                            maximum=64,
                                                            step=2,
                                                            value=2)
                                    tome_sy = gr.Slider(label='sy:',
                                                            minimum=0,
                                                            maximum=64,
                                                            step=2,
                                                            value=2)
                            
                                
                            with gr.Row():
                                seed_textbox = gr.Textbox(label="Seed", value=-1)
                                seed_button  = gr.Button(value="\U0001F3B2", elem_classes="toolbutton")
                                seed_button.click(fn=lambda: random.randint(1, 1e16), inputs=[], outputs=[seed_textbox])
        inputs = [
            source_image, style_image,
            num_steps,co_feat_step,strength,
            start_ac_layer, end_ac_layer,
            Style_Guidance,cfg_scale_slider,mix_q_scale,
            Scheduler, save_intermediate, seed_textbox, de_bug,  
            prompt_textbox, negative_prompt_textbox,
            width_slider,height_slider,
            tome_sx, tome_sy, tome_ratio, tome,
        ]

        generate_button.click(
            fn=global_text.generate,
            inputs=inputs,
            outputs=[recons_style,recons_content,generate_image,results_image_gallery]
        )

        ex = gr.Examples(
        [
          ["./data/content/27032.jpg","./data/style/27.jpg",4,0.8,0.5,8427921159605868845],         
          ["./data/content/29812.jpg","./data/style/47.jpg",4,0.5,0.65,8119359809263726691],
        ],
        [source_image, style_image, num_steps,strength, mix_q_scale, seed_textbox],
        [
            "Example 1",
        ],)


    return demo

if __name__ == "__main__":
    demo = ui()
    demo.launch(server_name='172.18.32.44',show_error=True)