File size: 40,790 Bytes
7fa96eb
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70841bb
04ef268
 
 
 
0f67d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380a409
0f67d6b
 
ed8a0f3
0f67d6b
f31bcff
0f67d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04ef268
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
900f786
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
900f786
04ef268
 
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
900f786
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
653844c
04ef268
 
 
 
 
 
 
 
 
6cfbb2a
04ef268
 
 
 
 
 
 
a38379b
 
 
 
a8c457b
 
a38379b
 
 
04ef268
 
 
 
a8c457b
04ef268
 
 
70841bb
 
 
04ef268
 
 
 
 
 
a38379b
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
a38379b
04ef268
 
 
 
 
a38379b
 
 
 
 
 
04ef268
 
a38379b
04ef268
a38379b
04ef268
 
 
 
 
 
 
 
70841bb
a38379b
 
04ef268
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9926e79
900f786
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
900f786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5304887
900f786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04ef268
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9926e79
900f786
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5abddf
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5304887
04ef268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5abddf
 
 
 
04ef268
 
f5abddf
 
 
 
 
380a409
f5abddf
 
 
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
import os
import base64
import gradio as gr
from PIL import Image
from src.util import *
from io import BytesIO
from src.pipelines import *
from threading import Thread
from dash import Dash, dcc, html, Input, Output, no_update, callback

app = Dash(__name__)

app.layout = html.Div(
    className="container",
    children=[
        dcc.Graph(
            id="graph", figure=fig, clear_on_unhover=True, style={"height": "90vh"}
        ),
        dcc.Tooltip(id="tooltip"),
        html.Div(id="word-emb-txt", style={"background-color": "white"}),
        html.Div(id="word-emb-vis"),
        html.Div(
            [
                html.Button(id="btn-download-image", hidden=True),
                dcc.Download(id="download-image"),
            ]
        ),
    ],
)


@callback(
    Output("tooltip", "show"),
    Output("tooltip", "bbox"),
    Output("tooltip", "children"),
    Output("tooltip", "direction"),
    Output("word-emb-txt", "children"),
    Output("word-emb-vis", "children"),
    Input("graph", "hoverData"),
)
def display_hover(hoverData):
    if hoverData is None:
        return False, no_update, no_update, no_update, no_update, no_update

    hover_data = hoverData["points"][0]
    bbox = hover_data["bbox"]
    direction = "left"
    index = hover_data["pointNumber"]

    children = [
        html.Img(
            src=images[index],
            style={"width": "250px"},
        ),
        html.P(
            hover_data["text"],
            style={
                "color": "black",
                "font-size": "20px",
                "text-align": "center",
                "background-color": "white",
                "margin": "5px",
            },
        ),
    ]

    emb_children = [
        html.Img(
            src=generate_word_emb_vis(hover_data["text"]),
            style={"width": "100%", "height": "25px"},
        ),
    ]

    return True, bbox, children, direction, hover_data["text"], emb_children


@callback(
    Output("download-image", "data"),
    Input("graph", "clickData"),
)
def download_image(clickData):

    if clickData is None:
        return no_update

    click_data = clickData["points"][0]
    index = click_data["pointNumber"]
    txt = click_data["text"]

    img_encoded = images[index]
    img_decoded = base64.b64decode(img_encoded.split(",")[1])
    img = Image.open(BytesIO(img_decoded))
    img.save(f"{txt}.png")
    return dcc.send_file(f"{txt}.png")


with gr.Blocks(css="#step_size_circular {background-color: #666666} #step_size_circular textarea {background-color: #666666}") as demo:
    gr.Markdown("## Stable Diffusion Demo")

    with gr.Tab("Latent Space"):

        with gr.TabItem("Beginner"):
            gr.Markdown("Generate images from text.")

            with gr.Row():
                with gr.Column():
                    prompt_beginner = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )

                    with gr.Row():
                        seed_beginner = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_beginner = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_beginner = gr.Button("Generate Image")

                with gr.Column():
                    images_output_beginner = gr.Image(label="Image")

        @generate_images_button_beginner.click(
            inputs=[prompt_beginner, seed_beginner],
            outputs=[images_output_beginner],
        )
        def generate_images_wrapper(
            prompt, seed, progress=gr.Progress()
        ):
            images, _ = display_poke_images(
                prompt, seed, num_inference_steps=8, poke=False, intermediate=False
            )
            return images

        seed_beginner.change(
            fn=generate_seed_vis, inputs=[seed_beginner], outputs=[seed_vis_beginner]
        )

        with gr.TabItem("Denoising"):
            gr.Markdown("Observe the intermediate images during denoising.")
            gr.HTML(read_html("html/denoising.html"))

            with gr.Row():
                with gr.Column():
                    prompt_denoise = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_inference_steps_denoise = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps",
                    )

                    with gr.Row():
                        seed_denoise = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_denoise = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_denoise = gr.Button("Generate Images")

                with gr.Column():
                    images_output_denoise = gr.Gallery(label="Images", selected_index=0, height=512)
                    gif_denoise = gr.Image(label="GIF")
                    zip_output_denoise = gr.File(label="Download ZIP")

        @generate_images_button_denoise.click(
            inputs=[prompt_denoise, seed_denoise, num_inference_steps_denoise],
            outputs=[images_output_denoise, gif_denoise, zip_output_denoise],
        )
        def generate_images_wrapper(
            prompt, seed, num_inference_steps, progress=gr.Progress()
        ):
            images, _ = display_poke_images(
                prompt, seed, num_inference_steps, poke=False, intermediate=True
            )
            fname = "denoising"
            tab_config = {
                "Tab": "Denoising",
                "Prompt": prompt,
                "Number of Inference Steps": num_inference_steps,
                "Seed": seed,
            }
            export_as_zip(images, fname, tab_config)
            progress(1, desc="Exporting as gif")
            export_as_gif(images, filename="denoising.gif")
            return images, "outputs/denoising.gif", f"outputs/{fname}.zip"

        seed_denoise.change(
            fn=generate_seed_vis, inputs=[seed_denoise], outputs=[seed_vis_denoise]
        )

        with gr.TabItem("Seeds"):
            gr.Markdown(
                "Understand how different starting points in latent space can lead to different images."
            )
            gr.HTML(read_html("html/seeds.html"))

            with gr.Row():
                with gr.Column():
                    prompt_seed = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_images_seed = gr.Slider(
                        minimum=1, maximum=100, step=1, value=5, label="Number of Seeds"
                    )
                    num_inference_steps_seed = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )
                    generate_images_button_seed = gr.Button("Generate Images")

                with gr.Column():
                    images_output_seed = gr.Gallery(label="Images", selected_index=0, height=512)
                    zip_output_seed = gr.File(label="Download ZIP")

        generate_images_button_seed.click(
            fn=display_seed_images,
            inputs=[prompt_seed, num_inference_steps_seed, num_images_seed],
            outputs=[images_output_seed, zip_output_seed],
        )

        with gr.TabItem("Perturbations"):
            gr.Markdown("Explore different perturbations from a point in latent space.")
            gr.HTML(read_html("html/perturbations.html"))

            with gr.Row():
                with gr.Column():
                    prompt_perturb = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_images_perturb = gr.Slider(
                        minimum=0,
                        maximum=100,
                        step=1,
                        value=5,
                        label="Number of Perturbations",
                    )
                    perturbation_size_perturb = gr.Slider(
                        minimum=0,
                        maximum=1,
                        step=0.1,
                        value=0.1,
                        label="Perturbation Size",
                    )
                    num_inference_steps_perturb = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_perturb = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_perturb = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_perturb = gr.Button("Generate Images")

                with gr.Column():
                    images_output_perturb = gr.Gallery(label="Image", selected_index=0, height=512)
                    zip_output_perturb = gr.File(label="Download ZIP")

        generate_images_button_perturb.click(
            fn=display_perturb_images,
            inputs=[
                prompt_perturb,
                seed_perturb,
                num_inference_steps_perturb,
                num_images_perturb,
                perturbation_size_perturb,
            ],
            outputs=[images_output_perturb, zip_output_perturb],
        )
        seed_perturb.change(
            fn=generate_seed_vis, inputs=[seed_perturb], outputs=[seed_vis_perturb]
        )

        with gr.TabItem("Circular"):
            gr.Markdown(
                "Generate a circular path in latent space and observe how the images vary along the path."
            )
            gr.HTML(read_html("html/circular.html"))

            with gr.Row():
                with gr.Column():
                    prompt_circular = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_images_circular = gr.Slider(
                        minimum=1,
                        maximum=100,
                        step=1,
                        value=5,
                        label="Number of Steps around the Circle",
                    )

                    with gr.Row():
                        start_degree_circular = gr.Slider(
                            minimum=0,
                            maximum=360,
                            step=1,
                            value=0,
                            label="Start Angle",
                            info="Enter the value in degrees",
                        )
                        end_degree_circular = gr.Slider(
                            minimum=0,
                            maximum=360,
                            step=1,
                            value=360,
                            label="End Angle",
                            info="Enter the value in degrees",
                        )
                        step_size_circular = gr.Textbox(
                            label="Step Size", value=360 / 5, 
                            elem_id="step_size_circular"

                        )

                    num_inference_steps_circular = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_circular = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_circular = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_circular = gr.Button("Generate Images")

                with gr.Column():
                    images_output_circular = gr.Gallery(label="Image", selected_index=0)
                    gif_circular = gr.Image(label="GIF")
                    zip_output_circular = gr.File(label="Download ZIP")

        num_images_circular.change(
            fn=calculate_step_size,
            inputs=[num_images_circular, start_degree_circular, end_degree_circular],
            outputs=[step_size_circular],
        )
        start_degree_circular.change(
            fn=calculate_step_size,
            inputs=[num_images_circular, start_degree_circular, end_degree_circular],
            outputs=[step_size_circular],
        )
        end_degree_circular.change(
            fn=calculate_step_size,
            inputs=[num_images_circular, start_degree_circular, end_degree_circular],
            outputs=[step_size_circular],
        )
        generate_images_button_circular.click(
            fn=display_circular_images,
            inputs=[
                prompt_circular,
                seed_circular,
                num_inference_steps_circular,
                num_images_circular,
                start_degree_circular,
                end_degree_circular,
            ],
            outputs=[images_output_circular, gif_circular, zip_output_circular],
        )
        seed_circular.change(
            fn=generate_seed_vis, inputs=[seed_circular], outputs=[seed_vis_circular]
        )

        with gr.TabItem("Poke"):
            gr.Markdown("Perturb a region in the image and observe the effect.")
            gr.HTML(read_html("html/poke.html"))

            with gr.Row():
                with gr.Column():
                    prompt_poke = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_inference_steps_poke = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_poke = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_poke = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    pokeX = gr.Slider(
                        label="pokeX",
                        minimum=0,
                        maximum=64,
                        step=1,
                        value=32,
                        info="X coordinate of poke center",
                    )
                    pokeY = gr.Slider(
                        label="pokeY",
                        minimum=0,
                        maximum=64,
                        step=1,
                        value=32,
                        info="Y coordinate of poke center",
                    )
                    pokeHeight = gr.Slider(
                        label="pokeHeight",
                        minimum=0,
                        maximum=64,
                        step=1,
                        value=8,
                        info="Height of the poke",
                    )
                    pokeWidth = gr.Slider(
                        label="pokeWidth",
                        minimum=0,
                        maximum=64,
                        step=1,
                        value=8,
                        info="Width of the poke",
                    )

                    generate_images_button_poke = gr.Button("Generate Images")

                with gr.Column():
                    original_images_output_poke = gr.Image(
                        value=visualize_poke(32, 32, 8, 8)[0], label="Original Image"
                    )
                    poked_images_output_poke = gr.Image(
                        value=visualize_poke(32, 32, 8, 8)[1], label="Poked Image"
                    )
                    zip_output_poke = gr.File(label="Download ZIP")

        pokeX.change(
            visualize_poke,
            inputs=[pokeX, pokeY, pokeHeight, pokeWidth],
            outputs=[original_images_output_poke, poked_images_output_poke],
        )
        pokeY.change(
            visualize_poke,
            inputs=[pokeX, pokeY, pokeHeight, pokeWidth],
            outputs=[original_images_output_poke, poked_images_output_poke],
        )
        pokeHeight.change(
            visualize_poke,
            inputs=[pokeX, pokeY, pokeHeight, pokeWidth],
            outputs=[original_images_output_poke, poked_images_output_poke],
        )
        pokeWidth.change(
            visualize_poke,
            inputs=[pokeX, pokeY, pokeHeight, pokeWidth],
            outputs=[original_images_output_poke, poked_images_output_poke],
        )
        seed_poke.change(
            fn=generate_seed_vis, inputs=[seed_poke], outputs=[seed_vis_poke]
        )

        @generate_images_button_poke.click(
            inputs=[
                prompt_poke,
                seed_poke,
                num_inference_steps_poke,
                pokeX,
                pokeY,
                pokeHeight,
                pokeWidth,
            ],
            outputs=[
                original_images_output_poke,
                poked_images_output_poke,
                zip_output_poke,
            ],
        )
        def generate_images_wrapper(
            prompt,
            seed,
            num_inference_steps,
            pokeX=pokeX,
            pokeY=pokeY,
            pokeHeight=pokeHeight,
            pokeWidth=pokeWidth,
        ):
            _, _ = display_poke_images(
                prompt,
                seed,
                num_inference_steps,
                poke=True,
                pokeX=pokeX,
                pokeY=pokeY,
                pokeHeight=pokeHeight,
                pokeWidth=pokeWidth,
                intermediate=False,
            )
            images, modImages = visualize_poke(pokeX, pokeY, pokeHeight, pokeWidth)
            fname = "poke"
            tab_config = {
                "Tab": "Poke",
                "Prompt": prompt,
                "Number of Inference Steps per Image": num_inference_steps,
                "Seed": seed,
                "PokeX": pokeX,
                "PokeY": pokeY,
                "PokeHeight": pokeHeight,
                "PokeWidth": pokeWidth,
            }
            imgs_list = []
            imgs_list.append((images, "Original Image"))
            imgs_list.append((modImages, "Poked Image"))

            export_as_zip(imgs_list, fname, tab_config)
            return images, modImages, f"outputs/{fname}.zip"

        with gr.TabItem("Guidance"):
            gr.Markdown("Observe the effect of different guidance scales.")
            gr.HTML(read_html("html/guidance.html"))

            with gr.Row():
                with gr.Column():
                    prompt_guidance = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    num_inference_steps_guidance = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )
                    guidance_scale_values = gr.Textbox(
                        lines=1, value="1, 8, 20, 30", label="Guidance Scale Values"
                    )

                    with gr.Row():
                        seed_guidance = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_guidance = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_guidance = gr.Button("Generate Images")

                with gr.Column():
                    images_output_guidance = gr.Gallery(
                        label="Images", selected_index=0,
                        height=512,
                    )
                    zip_output_guidance = gr.File(label="Download ZIP")

        generate_images_button_guidance.click(
            fn=display_guidance_images,
            inputs=[
                prompt_guidance,
                seed_guidance,
                num_inference_steps_guidance,
                guidance_scale_values,
            ],
            outputs=[images_output_guidance, zip_output_guidance],
        )
        seed_guidance.change(
            fn=generate_seed_vis, inputs=[seed_guidance], outputs=[seed_vis_guidance]
        )

        with gr.TabItem("Inpainting"):
            gr.Markdown("Inpaint the image based on the prompt.")
            gr.HTML(read_html("html/inpainting.html"))
    
            with gr.Row():
                with gr.Column():
                    uploaded_img_inpaint = gr.Sketchpad(
                        sources="upload", brush=gr.Brush(colors=["#ffff00"]), type="pil", label="Upload"
                    )
                    prompt_inpaint = gr.Textbox(
                        lines=1, label="Prompt", value="sunglasses"
                    )
                    num_inference_steps_inpaint = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_inpaint = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_inpaint = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    inpaint_button = gr.Button("Inpaint")

                with gr.Column():
                    images_output_inpaint = gr.Image(label="Output")
                    zip_output_inpaint = gr.File(label="Download ZIP")

        inpaint_button.click(
            fn=inpaint,
            inputs=[
                uploaded_img_inpaint,
                num_inference_steps_inpaint,
                seed_inpaint,
                prompt_inpaint,
            ],
            outputs=[images_output_inpaint, zip_output_inpaint],
        )
        seed_inpaint.change(
            fn=generate_seed_vis, inputs=[seed_inpaint], outputs=[seed_vis_inpaint]
        )

    with gr.Tab("CLIP Space"):

        with gr.TabItem("Embeddings"):
            gr.Markdown(
                "Visualize text embedding space in 3D with input texts and output images based on the chosen axis."
            )
            gr.HTML(read_html("html/embeddings.html"))

            with gr.Row():
                output = gr.HTML(
                    f"""
                        <iframe id="html" src="{dash_tunnel}" style="width:100%; height:700px;"></iframe>
                    """
                )
            with gr.Row():
                word2add_rem = gr.Textbox(lines=1, label="Add/Remove word")
                word2change = gr.Textbox(lines=1, label="Change image for word")
                clear_words_button = gr.Button(value="Clear words")

            with gr.Accordion("Custom Semantic Dimensions", open=False):
                with gr.Row():
                    axis_name_1 = gr.Textbox(label="Axis name", value="gender")
                    which_axis_1 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_1"],
                        label="Axis direction",
                    )
                    from_words_1 = gr.Textbox(
                        lines=1,
                        label="Positive",
                        value="prince husband father son uncle",
                    )
                    to_words_1 = gr.Textbox(
                        lines=1,
                        label="Negative",
                        value="princess wife mother daughter aunt",
                    )
                    submit_1 = gr.Button("Submit")

                with gr.Row():
                    axis_name_2 = gr.Textbox(label="Axis name", value="age")
                    which_axis_2 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_2"],
                        label="Axis direction",
                    )
                    from_words_2 = gr.Textbox(
                        lines=1, label="Positive", value="man woman king queen father"
                    )
                    to_words_2 = gr.Textbox(
                        lines=1, label="Negative", value="boy girl prince princess son"
                    )
                    submit_2 = gr.Button("Submit")

                with gr.Row():
                    axis_name_3 = gr.Textbox(label="Axis name", value="residual")
                    which_axis_3 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_3"],
                        label="Axis direction",
                    )
                    from_words_3 = gr.Textbox(lines=1, label="Positive")
                    to_words_3 = gr.Textbox(lines=1, label="Negative")
                    submit_3 = gr.Button("Submit")

                with gr.Row():
                    axis_name_4 = gr.Textbox(label="Axis name", value="number")
                    which_axis_4 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_4"],
                        label="Axis direction",
                    )
                    from_words_4 = gr.Textbox(
                        lines=1,
                        label="Positive",
                        value="boys girls cats puppies computers",
                    )
                    to_words_4 = gr.Textbox(
                        lines=1, label="Negative", value="boy girl cat puppy computer"
                    )
                    submit_4 = gr.Button("Submit")

                with gr.Row():
                    axis_name_5 = gr.Textbox(label="Axis name", value="royalty")
                    which_axis_5 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_5"],
                        label="Axis direction",
                    )
                    from_words_5 = gr.Textbox(
                        lines=1,
                        label="Positive",
                        value="king queen prince princess duchess",
                    )
                    to_words_5 = gr.Textbox(
                        lines=1, label="Negative", value="man woman boy girl woman"
                    )
                    submit_5 = gr.Button("Submit")

                with gr.Row():
                    axis_name_6 = gr.Textbox(label="Axis name")
                    which_axis_6 = gr.Dropdown(
                        choices=["X - Axis", "Y - Axis", "Z - Axis", "---"],
                        value=whichAxisMap["which_axis_6"],
                        label="Axis direction",
                    )
                    from_words_6 = gr.Textbox(lines=1, label="Positive")
                    to_words_6 = gr.Textbox(lines=1, label="Negative")
                    submit_6 = gr.Button("Submit")

        @word2add_rem.submit(inputs=[word2add_rem], outputs=[output, word2add_rem])
        def add_rem_word_and_clear(words):
            return add_rem_word(words), ""

        @word2change.submit(inputs=[word2change], outputs=[output, word2change])
        def change_word_and_clear(word):
            return change_word(word), ""

        clear_words_button.click(fn=clear_words, outputs=[output])

        @submit_1.click(
            inputs=[axis_name_1, which_axis_1, from_words_1, to_words_1],
            outputs=[
                output,
                which_axis_2,
                which_axis_3,
                which_axis_4,
                which_axis_5,
                which_axis_6,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_1"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_2"],
                whichAxisMap["which_axis_3"],
                whichAxisMap["which_axis_4"],
                whichAxisMap["which_axis_5"],
                whichAxisMap["which_axis_6"],
            )

        @submit_2.click(
            inputs=[axis_name_2, which_axis_2, from_words_2, to_words_2],
            outputs=[
                output,
                which_axis_1,
                which_axis_3,
                which_axis_4,
                which_axis_5,
                which_axis_6,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_2"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_1"],
                whichAxisMap["which_axis_3"],
                whichAxisMap["which_axis_4"],
                whichAxisMap["which_axis_5"],
                whichAxisMap["which_axis_6"],
            )

        @submit_3.click(
            inputs=[axis_name_3, which_axis_3, from_words_3, to_words_3],
            outputs=[
                output,
                which_axis_1,
                which_axis_2,
                which_axis_4,
                which_axis_5,
                which_axis_6,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_3"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_1"],
                whichAxisMap["which_axis_2"],
                whichAxisMap["which_axis_4"],
                whichAxisMap["which_axis_5"],
                whichAxisMap["which_axis_6"],
            )

        @submit_4.click(
            inputs=[axis_name_4, which_axis_4, from_words_4, to_words_4],
            outputs=[
                output,
                which_axis_1,
                which_axis_2,
                which_axis_3,
                which_axis_5,
                which_axis_6,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_4"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_1"],
                whichAxisMap["which_axis_2"],
                whichAxisMap["which_axis_3"],
                whichAxisMap["which_axis_5"],
                whichAxisMap["which_axis_6"],
            )

        @submit_5.click(
            inputs=[axis_name_5, which_axis_5, from_words_5, to_words_5],
            outputs=[
                output,
                which_axis_1,
                which_axis_2,
                which_axis_3,
                which_axis_4,
                which_axis_6,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_5"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_1"],
                whichAxisMap["which_axis_2"],
                whichAxisMap["which_axis_3"],
                whichAxisMap["which_axis_4"],
                whichAxisMap["which_axis_6"],
            )

        @submit_6.click(
            inputs=[axis_name_6, which_axis_6, from_words_6, to_words_6],
            outputs=[
                output,
                which_axis_1,
                which_axis_2,
                which_axis_3,
                which_axis_4,
                which_axis_5,
            ],
        )
        def set_axis_wrapper(axis_name, which_axis, from_words, to_words):

            for ax in whichAxisMap:
                if whichAxisMap[ax] == which_axis:
                    whichAxisMap[ax] = "---"

            whichAxisMap["which_axis_6"] = which_axis
            return (
                set_axis(axis_name, which_axis, from_words, to_words),
                whichAxisMap["which_axis_1"],
                whichAxisMap["which_axis_2"],
                whichAxisMap["which_axis_3"],
                whichAxisMap["which_axis_4"],
                whichAxisMap["which_axis_5"],
            )

        with gr.TabItem("Interpolate"):
            gr.Markdown(
                "Interpolate between the first and the second prompt, and observe how the output changes."
            )
            gr.HTML(read_html("html/interpolate.html"))

            with gr.Row():
                with gr.Column():
                    promptA = gr.Textbox(
                        lines=1,
                        label="First Prompt",
                        value="Self-portrait oil painting, a beautiful man with golden hair, 8k",
                    )
                    promptB = gr.Textbox(
                        lines=1,
                        label="Second Prompt",
                        value="Self-portrait oil painting, a beautiful woman with golden hair, 8k",
                    )
                    num_images_interpolate = gr.Slider(
                        minimum=0,
                        maximum=100,
                        step=1,
                        value=5,
                        label="Number of Interpolation Steps",
                    )
                    num_inference_steps_interpolate = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_interpolate = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_interpolate = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_interpolate = gr.Button("Generate Images")

                with gr.Column():
                    images_output_interpolate = gr.Gallery(
                        label="Interpolated Images", selected_index=0,
                        height=512,
                    )
                    gif_interpolate = gr.Image(label="GIF")
                    zip_output_interpolate = gr.File(label="Download ZIP")

        generate_images_button_interpolate.click(
            fn=display_interpolate_images,
            inputs=[
                seed_interpolate,
                promptA,
                promptB,
                num_inference_steps_interpolate,
                num_images_interpolate,
            ],
            outputs=[
                images_output_interpolate,
                gif_interpolate,
                zip_output_interpolate,
            ],
        )
        seed_interpolate.change(
            fn=generate_seed_vis,
            inputs=[seed_interpolate],
            outputs=[seed_vis_interpolate],
        )

        with gr.TabItem("Negative"):
            gr.Markdown("Observe the effect of negative prompts.")
            gr.HTML(read_html("html/negative.html"))

            with gr.Row():
                with gr.Column():
                    prompt_negative = gr.Textbox(
                        lines=1,
                        label="Prompt",
                        value="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
                    )
                    neg_prompt = gr.Textbox(
                        lines=1, label="Negative Prompt", value="Yellow"
                    )
                    num_inference_steps_negative = gr.Slider(
                        minimum=2,
                        maximum=100,
                        step=1,
                        value=8,
                        label="Number of Inference Steps per Image",
                    )

                    with gr.Row():
                        seed_negative = gr.Slider(
                            minimum=0, maximum=100, step=1, value=14, label="Seed"
                        )
                        seed_vis_negative = gr.Plot(
                            value=generate_seed_vis(14), label="Seed"
                        )

                    generate_images_button_negative = gr.Button("Generate Images")

                with gr.Column():
                    images_output_negative = gr.Image(
                        label="Image without Negative Prompt"
                    )
                    images_neg_output_negative = gr.Image(
                        label="Image with Negative Prompt"
                    )
                    zip_output_negative = gr.File(label="Download ZIP")

        seed_negative.change(
            fn=generate_seed_vis, inputs=[seed_negative], outputs=[seed_vis_negative]
        )
        generate_images_button_negative.click(
            fn=display_negative_images,
            inputs=[
                prompt_negative,
                seed_negative,
                num_inference_steps_negative,
                neg_prompt,
            ],
            outputs=[
                images_output_negative,
                images_neg_output_negative,
                zip_output_negative,
            ],
        )

    with gr.Tab("Credits"):
        gr.Markdown("""
                    Author: Adithya Kameswara Rao, Carnegie Mellon University.

                    Advisor: David S. Touretzky, Carnegie Mellon University.

                    This work was funded by a grant from NEOM Company, and by National Science Foundation award IIS-2112633.
                    """)
    

def run_dash():
    app.run(host="127.0.0.1", port="8000")


# def run_gradio():
#     demo.queue()
#     _, _, public_url = demo.launch(share=True)
#     return public_url


if __name__ == "__main__":
    thread = Thread(target=run_dash)
    thread.daemon = True
    thread.start()
    try:
        os.makedirs("outputs", exist_ok=True)
        demo.queue().launch(share=True)
    except KeyboardInterrupt:
        print("Server closed")