File size: 26,788 Bytes
aaa0b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#!/usr/bin/env python

import datetime
import hashlib
import json
import os
import random
import tempfile
import shortuuid
from apscheduler.schedulers.background import BackgroundScheduler
import shutil

import gradio as gr
import torch
from huggingface_hub import HfApi
from share_btn import community_icon_html, loading_icon_html, share_js

# isort: off
from model import Model
from settings import (
    DEBUG,
    DEFAULT_CUSTOM_TIMESTEPS_1,
    DEFAULT_CUSTOM_TIMESTEPS_2,
    DEFAULT_NUM_IMAGES,
    DEFAULT_NUM_STEPS_3,
    DISABLE_SD_X4_UPSCALER,
    GALLERY_COLUMN_NUM,
    HF_TOKEN,
    MAX_NUM_IMAGES,
    MAX_NUM_STEPS,
    MAX_QUEUE_SIZE,
    MAX_SEED,
    SHOW_ADVANCED_OPTIONS,
    SHOW_CUSTOM_TIMESTEPS_1,
    SHOW_CUSTOM_TIMESTEPS_2,
    SHOW_DEVICE_WARNING,
    SHOW_DUPLICATE_BUTTON,
    SHOW_NUM_IMAGES,
    SHOW_NUM_STEPS_1,
    SHOW_NUM_STEPS_2,
    SHOW_NUM_STEPS_3,
    SHOW_UPSCALE_TO_256_BUTTON,
    UPLOAD_REPO_ID,
    UPLOAD_RESULT_IMAGE,
)
# isort: on

TITLE = '# [DeepFloyd IF](https://github.com/deep-floyd/IF)'
DESCRIPTION = 'The DeepFloyd IF model has been initially released as a non-commercial research-only model. Please make sure you read and abide to the [LICENSE](https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license) before using it.'
DISCLAIMER = 'In this demo, the DeepFloyd team may collect prompts, and user preferences (which of the images the user chose to upscale) for improving future models'
FOOTER = """<div class="footer">
                    <p>Model by <a href="https://huggingface.co/DeepFloyd" style="text-decoration: underline;" target="_blank">DeepFloyd</a> supported by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a>
                    </p>
            </div>
            <div class="acknowledgments">
                    <p><h4>LICENSE</h4>
The model is licensed with a bespoke non-commercial research-only license <a href="https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license" style="text-decoration: underline;" target="_blank">DeepFloyd IF Research License Agreement</a> license. The license forbids you from sharing any content for commercial use, or that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license" style="text-decoration: underline;" target="_blank">read the license</a></p>
                    <p><h4>Biases and content acknowledgment</h4>
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, explicit content and violence. The model was trained on a subset of the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a> and is meant for research purposes. You can read more in the <a href="https://huggingface.co/DeepFloyd/IF-I-IF-v1.0" style="text-decoration: underline;" target="_blank">model card</a></p>
            </div>
        """
if SHOW_DUPLICATE_BUTTON:
    SPACE_ID = os.getenv('SPACE_ID')
    DESCRIPTION += f'\n<p><a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space%20to%20skip%20the%20queue-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>'

if SHOW_DEVICE_WARNING and not torch.cuda.is_available():
    DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'

model = Model()


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed


def get_stage2_index(evt: gr.SelectData) -> int:
    return evt.index


def check_if_stage2_selected(index: int) -> None:
    if index == -1:
        raise gr.Error(
            'You need to select the image you would like to upscale from the Stage 1 results by clicking.'
        )


hf_api = HfApi(token=HF_TOKEN)
if UPLOAD_REPO_ID:
    hf_api.create_repo(repo_id=UPLOAD_REPO_ID,
                       private=True,
                       repo_type='dataset',
                       exist_ok=True)


def get_param_file_hash_name(param_filepath: str) -> str:
    if not UPLOAD_REPO_ID:
        return ''
    with open(param_filepath, 'rb') as f:
        md5 = hashlib.md5(f.read()).hexdigest()
    utcnow = datetime.datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-%f')
    return f'{utcnow}-{md5}'


def upload_stage1_result(stage1_param_path: str, stage1_result_path: str,
                         save_name: str) -> None:
    if not UPLOAD_REPO_ID:
        return
    try:
        folder_params = "tmp/results/stage1_params"
        folder_results = "tmp/results/stage1_results"
        
        path_params = f"{folder_params}/{save_name}.json"
        path_results = f"{folder_results}/{save_name}.pth"
        
        os.makedirs(folder_params, exist_ok=True)
        os.makedirs(folder_results, exist_ok=True)
        
        shutil.copy(stage1_param_path, path_params)
        shutil.copy(stage1_result_path, path_results)

    except Exception as e:
        print(e)


def upload_stage2_info(stage1_param_file_hash_name: str,
                       stage2_output_path: str,
                       selected_index_for_upscale: int, seed_2: int,
                       guidance_scale_2: float, custom_timesteps_2: str,
                       num_inference_steps_2: int) -> None:
    if not UPLOAD_REPO_ID:
        return
    if not stage1_param_file_hash_name:
        raise ValueError

    stage2_params = {
        'stage1_param_file_hash_name': stage1_param_file_hash_name,
        'selected_index_for_upscale': selected_index_for_upscale,
        'seed_2': seed_2,
        'guidance_scale_2': guidance_scale_2,
        'custom_timesteps_2': custom_timesteps_2,
        'num_inference_steps_2': num_inference_steps_2,
    }
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as param_file:
        param_file.write(json.dumps(stage2_params))
    stage2_param_file_hash_name = get_param_file_hash_name(param_file.name)
    save_name = f'{stage1_param_file_hash_name}_{stage2_param_file_hash_name}'
    
    try:
        folder_params = "tmp/results/stage2_params"

        os.makedirs(folder_params, exist_ok=True)
        path_params = f"{folder_params}/{save_name}.json"
        shutil.copy(param_file.name, path_params)
        
        if UPLOAD_RESULT_IMAGE:
            folder_results = "tmp/results/stage2_results"
            os.makedirs(folder_results, exist_ok=True)
            path_results = f"{folder_results}/{save_name}.png"
            shutil.copy(stage2_output_path, path_results)

    except Exception as e:
        print(e)


def upload_stage2_3_info(stage1_param_file_hash_name: str,
                         stage2_3_output_path: str,
                         selected_index_for_upscale: int, seed_2: int,
                         guidance_scale_2: float, custom_timesteps_2: str,
                         num_inference_steps_2: int, prompt: str,
                         negative_prompt: str, seed_3: int,
                         guidance_scale_3: float,
                         num_inference_steps_3: int) -> None:
    if not UPLOAD_REPO_ID:
        return
    if not stage1_param_file_hash_name:
        raise ValueError

    stage2_3_params = {
        'stage1_param_file_hash_name': stage1_param_file_hash_name,
        'selected_index_for_upscale': selected_index_for_upscale,
        'seed_2': seed_2,
        'guidance_scale_2': guidance_scale_2,
        'custom_timesteps_2': custom_timesteps_2,
        'num_inference_steps_2': num_inference_steps_2,
        'prompt': prompt,
        'negative_prompt': negative_prompt,
        'seed_3': seed_3,
        'guidance_scale_3': guidance_scale_3,
        'num_inference_steps_3': num_inference_steps_3,
    }
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as param_file:
        param_file.write(json.dumps(stage2_3_params))
    stage2_3_param_file_hash_name = get_param_file_hash_name(param_file.name)
    save_name = f'{stage1_param_file_hash_name}_{stage2_3_param_file_hash_name}'

    try:
        folder_params = "tmp/results/stage2_3_params"
        os.makedirs(folder_params, exist_ok=True)
        path_params = f"{folder_params}/{save_name}.json"
        shutil.copy(param_file.name, path_params)

        if UPLOAD_RESULT_IMAGE:
            folder_results = "tmp/results/stage2_3_results"
            os.makedirs(folder_results, exist_ok=True)
            path_results = f"{folder_results}/{save_name}.png"
            shutil.copy(stage2_3_output_path, path_results)
    except Exception as e:
        print(e)


def update_upscale_button(selected_index: int) -> tuple[dict, dict]:
    if selected_index == -1:
        return gr.update(interactive=False), gr.update(interactive=False)
    else:
        return gr.update(interactive=True), gr.update(interactive=True)


def _update_result_view(show_gallery: bool) -> tuple[dict, dict]:
    return gr.update(visible=show_gallery), gr.update(visible=not show_gallery)


def show_gallery_view() -> tuple[dict, dict]:
    return _update_result_view(True)


def show_upscaled_view() -> tuple[dict, dict]:
    return _update_result_view(False)

def upload_files():
    """Zips files and uploads to dataset. Local data is deleted
    """
    if os.path.exists("tmp/results") and os.path.isdir("tmp/results"):
        try:
            random_folder = random.randint(0,1000)
            shutil.make_archive("tmp/results", 'zip', "tmp/results")
            hf_api.upload_file(
                path_or_fileobj="tmp/results.zip",
                path_in_repo=f"{random_folder}/results_{shortuuid.uuid()}.zip",
                repo_id=UPLOAD_REPO_ID,
                repo_type="dataset",
            )
            shutil.rmtree("tmp/results")
        except Exception as e:
            print(e)

examples = [
    'high quality dslr photo, a photo product of a lemon inspired by natural and organic materials, wooden accents, intricately decorated with glowing vines of led lights, inspired by baroque luxury',
    'paper quilling, extremely detailed, paper quilling of a nordic mountain landscape, 8k rendering',
    'letters made of candy on a plate that says "diet"',
    'a photo of a violet baseball cap with yellow text: "deep floyd". 50mm lens, photo realism, cine lens. violet baseball cap says "deep floyd". reflections, render. yellow stitch text "deep floyd"',
    'ultra close-up color photo portrait of rainbow owl with deer horns in the woods',
    'a cloth embroidered with the text "laion" and an embroidered cute baby lion face',
    'product image of a crochet Cthulhu the great old one emerging from a spacetime wormhole made of wool',
    'a little green budgie parrot driving small red toy car in new york street, photo',
    'origami dancer in white paper, 3d render, ultra-detailed, on white background, studio shot.',
    'glowing mushrooms in a natural environment with smoke in the frame',
    'a subway train\'s digital sign saying "open source", vsco preset, 35mm photo, film grain, in a dim subway station',
    'a bowl full of few adorable golden doodle puppies, the doodles dusted in powdered sugar and look delicious, bokeh, cannon. professional macro photo, super detailed. cute sweet golden doodle confectionery, baking puppies in powdered sugar in the bowl',
    'a face of a woman made completely out of foliage, twigs, leaves and flowers, side view'
]

with gr.Blocks(css='style.css') as demo:
    gr.Markdown(TITLE)
    gr.Markdown(DESCRIPTION)
    with gr.Box():
        with gr.Row(elem_id='prompt-container').style(equal_height=True):
            with gr.Column():
                prompt = gr.Text(
                    label='Prompt',
                    show_label=False,
                    max_lines=1,
                    placeholder='Enter your prompt',
                    elem_id='prompt-text-input',
                ).style(container=False)
                negative_prompt = gr.Text(
                    label='Negative prompt',
                    show_label=False,
                    max_lines=1,
                    placeholder='Enter a negative prompt',
                    elem_id='negative-prompt-text-input',
                ).style(container=False)
            generate_button = gr.Button('Generate').style(full_width=False)

        with gr.Column() as gallery_view:
            gallery = gr.Gallery(label='Stage 1 results',
                                 show_label=False,
                                 elem_id='gallery').style(
                                     columns=GALLERY_COLUMN_NUM,
                                     object_fit='contain')
            gr.Markdown('Pick your favorite generation to upscale.')
            with gr.Row():
                upscale_to_256_button = gr.Button(
                    'Upscale to 256px',
                    visible=SHOW_UPSCALE_TO_256_BUTTON
                    or DISABLE_SD_X4_UPSCALER,
                    interactive=False)
                upscale_button = gr.Button('Upscale',
                                           interactive=False,
                                           visible=not DISABLE_SD_X4_UPSCALER)
        with gr.Column(visible=False) as upscale_view:
            result = gr.Image(label='Result',
                              show_label=False,
                              type='filepath',
                              interactive=False,
                              elem_id='upscaled-image').style(height=640)
            back_to_selection_button = gr.Button('Back to selection')
            with gr.Group(elem_id="share-btn-container"):
                community_icon = gr.HTML(community_icon_html)
                loading_icon = gr.HTML(loading_icon_html)
                share_button = gr.Button(
                    "Share to community", elem_id="share-btn")
                share_button.click(None, [], [], _js=share_js)
        with gr.Accordion('Advanced options',
                          open=False,
                          visible=SHOW_ADVANCED_OPTIONS):
            with gr.Tabs():
                with gr.Tab(label='Generation'):
                    seed_1 = gr.Slider(label='Seed',
                                       minimum=0,
                                       maximum=MAX_SEED,
                                       step=1,
                                       value=0)
                    randomize_seed_1 = gr.Checkbox(label='Randomize seed',
                                                   value=True)
                    guidance_scale_1 = gr.Slider(label='Guidance scale',
                                                 minimum=1,
                                                 maximum=20,
                                                 step=0.1,
                                                 value=7.0)
                    custom_timesteps_1 = gr.Dropdown(
                        label='Custom timesteps 1',
                        choices=[
                            'none',
                            'fast27',
                            'smart27',
                            'smart50',
                            'smart100',
                            'smart185',
                        ],
                        value=DEFAULT_CUSTOM_TIMESTEPS_1,
                        visible=SHOW_CUSTOM_TIMESTEPS_1)
                    num_inference_steps_1 = gr.Slider(
                        label='Number of inference steps',
                        minimum=1,
                        maximum=MAX_NUM_STEPS,
                        step=1,
                        value=100,
                        visible=SHOW_NUM_STEPS_1)
                    num_images = gr.Slider(label='Number of images',
                                           minimum=1,
                                           maximum=MAX_NUM_IMAGES,
                                           step=1,
                                           value=DEFAULT_NUM_IMAGES,
                                           visible=SHOW_NUM_IMAGES)
                with gr.Tab(label='Super-resolution 1'):
                    seed_2 = gr.Slider(label='Seed',
                                       minimum=0,
                                       maximum=MAX_SEED,
                                       step=1,
                                       value=0)
                    randomize_seed_2 = gr.Checkbox(label='Randomize seed',
                                                   value=True)
                    guidance_scale_2 = gr.Slider(label='Guidance scale',
                                                 minimum=1,
                                                 maximum=20,
                                                 step=0.1,
                                                 value=4.0)
                    custom_timesteps_2 = gr.Dropdown(
                        label='Custom timesteps 2',
                        choices=[
                            'none',
                            'fast27',
                            'smart27',
                            'smart50',
                            'smart100',
                            'smart185',
                        ],
                        value=DEFAULT_CUSTOM_TIMESTEPS_2,
                        visible=SHOW_CUSTOM_TIMESTEPS_2)
                    num_inference_steps_2 = gr.Slider(
                        label='Number of inference steps',
                        minimum=1,
                        maximum=MAX_NUM_STEPS,
                        step=1,
                        value=50,
                        visible=SHOW_NUM_STEPS_2)
                with gr.Tab(label='Super-resolution 2'):
                    seed_3 = gr.Slider(label='Seed',
                                       minimum=0,
                                       maximum=MAX_SEED,
                                       step=1,
                                       value=0)
                    randomize_seed_3 = gr.Checkbox(label='Randomize seed',
                                                   value=True)
                    guidance_scale_3 = gr.Slider(label='Guidance scale',
                                                 minimum=1,
                                                 maximum=20,
                                                 step=0.1,
                                                 value=9.0)
                    num_inference_steps_3 = gr.Slider(
                        label='Number of inference steps',
                        minimum=1,
                        maximum=MAX_NUM_STEPS,
                        step=1,
                        value=DEFAULT_NUM_STEPS_3,
                        visible=SHOW_NUM_STEPS_3)

    gr.Examples(examples=examples, inputs=prompt, examples_per_page=4)

    with gr.Box(visible=DEBUG):
        with gr.Row():
            with gr.Accordion(label='Hidden params'):
                stage1_param_path = gr.Text(label='Stage 1 param path')
                stage1_result_path = gr.Text(label='Stage 1 result path')
                stage1_param_file_hash_name = gr.Text(
                    label='Stage 1 param file hash name')
                selected_index_for_stage2 = gr.Number(
                    label='Selected index for Stage 2', value=-1, precision=0)
    gr.Markdown(DISCLAIMER)
    gr.HTML(FOOTER)
    stage1_inputs = [
        prompt,
        negative_prompt,
        seed_1,
        num_images,
        guidance_scale_1,
        custom_timesteps_1,
        num_inference_steps_1,
    ]
    stage1_outputs = [
        gallery,
        stage1_param_path,
        stage1_result_path,
    ]

    prompt.submit(
        fn=randomize_seed_fn,
        inputs=[seed_1, randomize_seed_1],
        outputs=seed_1,
        queue=False,
    ).then(
        fn=lambda: -1,
        outputs=selected_index_for_stage2,
        queue=False,
    ).then(
        fn=show_gallery_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    ).then(
        fn=update_upscale_button,
        inputs=selected_index_for_stage2,
        outputs=[
            upscale_button,
            upscale_to_256_button,
        ],
        queue=False,
    ).then(
        fn=model.run_stage1,
        inputs=stage1_inputs,
        outputs=stage1_outputs,
    ).success(
        fn=get_param_file_hash_name,
        inputs=stage1_param_path,
        outputs=stage1_param_file_hash_name,
        queue=False,
    ).then(
        fn=upload_stage1_result,
        inputs=[
            stage1_param_path,
            stage1_result_path,
            stage1_param_file_hash_name,
        ],
        queue=False,
    )

    negative_prompt.submit(
        fn=randomize_seed_fn,
        inputs=[seed_1, randomize_seed_1],
        outputs=seed_1,
        queue=False,
    ).then(
        fn=lambda: -1,
        outputs=selected_index_for_stage2,
        queue=False,
    ).then(
        fn=show_gallery_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    ).then(
        fn=update_upscale_button,
        inputs=selected_index_for_stage2,
        outputs=[
            upscale_button,
            upscale_to_256_button,
        ],
        queue=False,
    ).then(
        fn=model.run_stage1,
        inputs=stage1_inputs,
        outputs=stage1_outputs,
    ).success(
        fn=get_param_file_hash_name,
        inputs=stage1_param_path,
        outputs=stage1_param_file_hash_name,
        queue=False,
    ).then(
        fn=upload_stage1_result,
        inputs=[
            stage1_param_path,
            stage1_result_path,
            stage1_param_file_hash_name,
        ],
        queue=False,
    )

    generate_button.click(
        fn=randomize_seed_fn,
        inputs=[seed_1, randomize_seed_1],
        outputs=seed_1,
        queue=False,
    ).then(
        fn=lambda: -1,
        outputs=selected_index_for_stage2,
        queue=False,
    ).then(
        fn=show_gallery_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    ).then(
        fn=update_upscale_button,
        inputs=selected_index_for_stage2,
        outputs=[
            upscale_button,
            upscale_to_256_button,
        ],
        queue=False,
    ).then(
        fn=model.run_stage1,
        inputs=stage1_inputs,
        outputs=stage1_outputs,
        api_name='generate64',
    ).success(
        fn=get_param_file_hash_name,
        inputs=stage1_param_path,
        outputs=stage1_param_file_hash_name,
        queue=False,
    ).then(
        fn=upload_stage1_result,
        inputs=[
            stage1_param_path,
            stage1_result_path,
            stage1_param_file_hash_name,
        ],
        queue=False,
    )

    gallery.select(
        fn=get_stage2_index,
        outputs=selected_index_for_stage2,
        queue=False,
    )

    selected_index_for_stage2.change(
        fn=update_upscale_button,
        inputs=selected_index_for_stage2,
        outputs=[
            upscale_button,
            upscale_to_256_button,
        ],
        queue=False,
    )

    stage2_inputs = [
        stage1_result_path,
        selected_index_for_stage2,
        seed_2,
        guidance_scale_2,
        custom_timesteps_2,
        num_inference_steps_2,
    ]

    upscale_to_256_button.click(
        fn=check_if_stage2_selected,
        inputs=selected_index_for_stage2,
        queue=False,
    ).then(
        fn=randomize_seed_fn,
        inputs=[seed_2, randomize_seed_2],
        outputs=seed_2,
        queue=False,
    ).then(
        fn=show_upscaled_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    ).then(
        fn=model.run_stage2,
        inputs=stage2_inputs,
        outputs=result,
        api_name='upscale256',
    ).success(
        fn=upload_stage2_info,
        inputs=[
            stage1_param_file_hash_name,
            result,
            selected_index_for_stage2,
            seed_2,
            guidance_scale_2,
            custom_timesteps_2,
            num_inference_steps_2,
        ],
        queue=False,
    )

    stage2_3_inputs = [
        stage1_result_path,
        selected_index_for_stage2,
        seed_2,
        guidance_scale_2,
        custom_timesteps_2,
        num_inference_steps_2,
        prompt,
        negative_prompt,
        seed_3,
        guidance_scale_3,
        num_inference_steps_3,
    ]

    upscale_button.click(
        fn=check_if_stage2_selected,
        inputs=selected_index_for_stage2,
        queue=False,
    ).then(
        fn=randomize_seed_fn,
        inputs=[seed_2, randomize_seed_2],
        outputs=seed_2,
        queue=False,
    ).then(
        fn=randomize_seed_fn,
        inputs=[seed_3, randomize_seed_3],
        outputs=seed_3,
        queue=False,
    ).then(
        fn=show_upscaled_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    ).then(
        fn=model.run_stage2_3,
        inputs=stage2_3_inputs,
        outputs=result,
        api_name='upscale1024',
    ).success(
        fn=upload_stage2_3_info,
        inputs=[
            stage1_param_file_hash_name,
            result,
            selected_index_for_stage2,
            seed_2,
            guidance_scale_2,
            custom_timesteps_2,
            num_inference_steps_2,
            prompt,
            negative_prompt,
            seed_3,
            guidance_scale_3,
            num_inference_steps_3,
        ],
        queue=False,
    )

    back_to_selection_button.click(
        fn=show_gallery_view,
        outputs=[
            gallery_view,
            upscale_view,
        ],
        queue=False,
    )
    
    if UPLOAD_REPO_ID:
        scheduler = BackgroundScheduler()
        scheduler.add_job(func=upload_files, trigger="interval", seconds=60*20)
        scheduler.start()

demo.queue(api_open=False, max_size=MAX_QUEUE_SIZE).launch(debug=DEBUG)