File size: 68,857 Bytes
2a00960
 
 
 
 
 
ec9288d
2a00960
 
ec9288d
2a00960
ec9288d
2a00960
2cde909
ec9288d
 
 
 
2a00960
947238d
2a00960
 
 
 
 
 
161b0b1
2a00960
 
 
 
ec9288d
2a00960
6cad0b7
 
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
2a00960
ec9288d
 
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
 
 
ec9288d
2a00960
 
 
 
ec9288d
2a00960
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
 
161b0b1
ec9288d
2a00960
ec9288d
2a00960
 
ec9288d
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
121fe52
2a00960
ec9288d
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
ec9288d
2a00960
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
ec9288d
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
 
 
ec9288d
 
2a00960
 
 
 
ec9288d
 
2a00960
 
 
ec9288d
 
2a00960
ec9288d
 
b198a0a
ec9288d
 
2a00960
 
 
 
 
 
ec9288d
 
 
2a00960
 
 
ec9288d
 
 
2a00960
 
 
 
ec9288d
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd9f83d
ec9288d
dd9f83d
 
 
3bddcc3
 
99958f3
3bddcc3
8ac9ea0
a99c1e3
 
dd9f83d
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
 
ec9288d
2a00960
 
 
 
 
ec9288d
 
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
929f992
2a00960
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
 
 
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b198a0a
ec9288d
 
 
 
 
 
 
2a00960
 
 
 
ec9288d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1cf5de
ec9288d
 
 
 
 
2a00960
 
 
 
 
 
 
 
ec9288d
 
2a00960
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
 
 
 
121fe52
2a00960
 
 
 
 
ec9288d
 
 
 
 
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
a7ce971
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
 
 
 
2a00960
 
ec9288d
2a00960
 
ec9288d
 
2a00960
 
 
 
 
 
ec9288d
 
121fe52
2a00960
 
 
 
 
 
 
 
 
 
2003a89
 
 
 
121fe52
2a00960
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
3c9306c
 
2a00960
 
 
 
ec9288d
 
2a00960
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
7bed5b5
 
 
 
 
2a00960
c7b412c
2a00960
34fed27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42dbbca
34fed27
d5ab71c
34fed27
2a00960
 
 
 
 
ec9288d
 
2a00960
 
 
 
 
 
ec9288d
 
2a00960
 
 
 
 
 
ec9288d
2a00960
 
 
 
 
 
 
ec9288d
 
 
 
2a00960
 
ec9288d
 
 
 
 
 
 
2a00960
 
 
 
 
 
 
 
ec9288d
 
2a00960
 
 
ec9288d
2a00960
 
 
 
 
 
 
ec9288d
 
2a00960
 
 
ec9288d
2a00960
 
 
 
 
 
 
ec9288d
2a00960
 
ec9288d
 
 
 
2a00960
 
 
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec9288d
2a00960
ec9288d
2a00960
 
 
 
ec9288d
2a00960
 
 
 
ec9288d
2a00960
 
6cad0b7
2a00960
ec9288d
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
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.
import base64
import copy
import glob
import io
import os, csv, sys
import random
import re
import shlex
import string
import subprocess
import threading
import spaces

subprocess.run(shlex.split('pip install flash-attn --no-build-isolation'),
               env=os.environ | {'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"})

import cv2
import gradio as gr
import numpy as np
import torch
import transformers
from PIL import Image
from transformers import AutoModel, AutoTokenizer

from ace_inference import ACEInference
from scepter.modules.utils.config import Config
from scepter.modules.utils.directory import get_md5
from scepter.modules.utils.file_system import FS
from scepter.studio.utils.env import init_env
from importlib.metadata import version

from example import get_examples
from utils import load_image

csv.field_size_limit(sys.maxsize)

refresh_sty = '\U0001f504'  # 🔄
clear_sty = '\U0001f5d1'  # 🗑️
upload_sty = '\U0001f5bc'  # 🖼️
sync_sty = '\U0001f4be'  # 💾
chat_sty = '\U0001F4AC'  # 💬
video_sty = '\U0001f3a5'  # 🎥

lock = threading.Lock()


class ChatBotUI(object):
    def __init__(self,
                 cfg_general_file,
                 is_debug=False,
                 language='en',
                 root_work_dir='./'):
        try:
            from diffusers import CogVideoXImageToVideoPipeline
            from diffusers.utils import export_to_video
        except Exception as e:
            print(f"Import diffusers failed, please install or upgrade diffusers. Error information: {e}")

        cfg = Config(cfg_file=cfg_general_file)
        if cfg.have("FILE_SYSTEM"):
            for file_sys in cfg.FILE_SYSTEM:
                fs_prefix = FS.init_fs_client(file_sys)
        else:
            fs_prefix = FS.init_fs_client(cfg)
        cfg.WORK_DIR = os.path.join(root_work_dir, cfg.WORK_DIR)
        if not FS.exists(cfg.WORK_DIR):
            FS.make_dir(cfg.WORK_DIR)
        cfg = init_env(cfg)
        self.cache_dir = cfg.WORK_DIR
        self.chatbot_examples = get_examples(self.cache_dir) if not cfg.get('SKIP_EXAMPLES', False) else []
        self.model_cfg_dir = cfg.MODEL.EDIT_MODEL.MODEL_CFG_DIR
        self.model_yamls = glob.glob(os.path.join(self.model_cfg_dir,
                                                  '*.yaml'))
        self.model_choices = dict()
        self.default_model_name = ''
        for i in self.model_yamls:
            model_cfg = Config(load=True, cfg_file=i)
            model_name = model_cfg.NAME
            if model_cfg.IS_DEFAULT: self.default_model_name = model_name
            self.model_choices[model_name] = model_cfg
        print('Models: ', self.model_choices.keys())

        #FS.get_from("ms://AI-ModelScope/FLUX.1-dev@flux1-dev.safetensors")
        #FS.get_from("ms://AI-ModelScope/FLUX.1-dev@ae.safetensors")
        #FS.get_dir_to_local_dir("ms://AI-ModelScope/FLUX.1-dev@text_encoder_2/")
        #FS.get_dir_to_local_dir("ms://AI-ModelScope/FLUX.1-dev@tokenizer_2/")
        #FS.get_dir_to_local_dir("ms://AI-ModelScope/FLUX.1-dev@text_encoder/")
        #FS.get_dir_to_local_dir("ms://AI-ModelScope/FLUX.1-dev@tokenizer/")
        
        assert len(self.model_choices) > 0
        if self.default_model_name == "": self.default_model_name = list(self.model_choices.keys())[0]
        self.model_name = self.default_model_name
        self.pipe = ACEInference()
        self.pipe.init_from_cfg(self.model_choices[self.default_model_name])
        self.max_msgs = 20
        self.enable_i2v = cfg.get('ENABLE_I2V', False)
        self.gradio_version = version('gradio')

        if self.enable_i2v:
            self.i2v_model_dir = cfg.MODEL.I2V.MODEL_DIR
            self.i2v_model_name = cfg.MODEL.I2V.MODEL_NAME
            if self.i2v_model_name == 'CogVideoX-5b-I2V':
                with FS.get_dir_to_local_dir(self.i2v_model_dir) as local_dir:
                    self.i2v_pipe = CogVideoXImageToVideoPipeline.from_pretrained(
                        local_dir, torch_dtype=torch.bfloat16).cuda()
            else:
                raise NotImplementedError

            with FS.get_dir_to_local_dir(
                    cfg.MODEL.CAPTIONER.MODEL_DIR) as local_dir:
                self.captioner = AutoModel.from_pretrained(
                    local_dir,
                    torch_dtype=torch.bfloat16,
                    low_cpu_mem_usage=True,
                    use_flash_attn=True,
                    trust_remote_code=True).eval().cuda()
                self.llm_tokenizer = AutoTokenizer.from_pretrained(
                    local_dir, trust_remote_code=True, use_fast=False)
                self.llm_generation_config = dict(max_new_tokens=1024,
                                                  do_sample=True)
                self.llm_prompt = cfg.LLM.PROMPT
                self.llm_max_num = 2

            with FS.get_dir_to_local_dir(
                    cfg.MODEL.ENHANCER.MODEL_DIR) as local_dir:
                self.enhancer = transformers.pipeline(
                    'text-generation',
                    model=local_dir,
                    model_kwargs={'torch_dtype': torch.bfloat16},
                    device_map='auto',
                )

            sys_prompt = """You are part of a team of bots that creates videos. You work with an assistant bot that will draw anything you say in square brackets.
            For example , outputting " a beautiful morning in the woods with the sun peaking through the trees " will trigger your partner bot to output an video of a forest morning , as described. You will be prompted by people looking to create detailed , amazing videos. The way to accomplish this is to take their short prompts and make them extremely detailed and descriptive.
            There are a few rules to follow:
            You will only ever output a single video description per user request.
            When modifications are requested , you should not simply make the description longer . You should refactor the entire description to integrate the suggestions.
            Other times the user will not want modifications , but instead want a new image . In this case , you should ignore your previous conversation with the user.
            Video descriptions must have the same num of words as examples below. Extra words will be ignored.
            """
            self.enhance_ctx = [
                {
                    'role': 'system',
                    'content': sys_prompt
                },
                {
                    'role':
                    'user',
                    'content':
                    'Create an imaginative video descriptive caption or modify an earlier caption for the user input : "a girl is on the beach"',
                },
                {
                    'role':
                    'assistant',
                    'content':
                    "A radiant woman stands on a deserted beach, arms outstretched, wearing a beige trench coat, white blouse, light blue jeans, and chic boots, against a backdrop of soft sky and sea. Moments later, she is seen mid-twirl, arms exuberant, with the lighting suggesting dawn or dusk. Then, she runs along the beach, her attire complemented by an off-white scarf and black ankle boots, the tranquil sea behind her. Finally, she holds a paper airplane, her pose reflecting joy and freedom, with the ocean's gentle waves and the sky's soft pastel hues enhancing the serene ambiance.",
                },
                {
                    'role':
                    'user',
                    'content':
                    'Create an imaginative video descriptive caption or modify an earlier caption for the user input : "A man jogging on a football field"',
                },
                {
                    'role':
                    'assistant',
                    'content':
                    "A determined man in athletic attire, including a blue long-sleeve shirt, black shorts, and blue socks, jogs around a snow-covered soccer field, showcasing his solitary exercise in a quiet, overcast setting. His long dreadlocks, focused expression, and the serene winter backdrop highlight his dedication to fitness. As he moves, his attire, consisting of a blue sports sweatshirt, black athletic pants, gloves, and sneakers, grips the snowy ground. He is seen running past a chain-link fence enclosing the playground area, with a basketball hoop and children's slide, suggesting a moment of solitary exercise amidst the empty field.",
                },
                {
                    'role':
                    'user',
                    'content':
                    'Create an imaginative video descriptive caption or modify an earlier caption for the user input : " A woman is dancing, HD footage, close-up"',
                },
                {
                    'role':
                    'assistant',
                    'content':
                    'A young woman with her hair in an updo and wearing a teal hoodie stands against a light backdrop, initially looking over her shoulder with a contemplative expression. She then confidently makes a subtle dance move, suggesting rhythm and movement. Next, she appears poised and focused, looking directly at the camera. Her expression shifts to one of introspection as she gazes downward slightly. Finally, she dances with confidence, her left hand over her heart, symbolizing a poignant moment, all while dressed in the same teal hoodie against a plain, light-colored background.',
                },
            ]

    def create_ui(self):

        css = '.chatbot.prose.md {opacity: 1.0 !important} #chatbot {opacity: 1.0 !important}'
        with gr.Blocks(css=css,
                       title='Chatbot',
                       head='Chatbot',
                       analytics_enabled=False):
            self.history = gr.State(value=[])
            self.images = gr.State(value={})
            self.history_result = gr.State(value={})
            self.retry_msg = gr.State(value='')
            with gr.Group():
                self.ui_mode = gr.State(value='legacy')
                with gr.Row(equal_height=True, visible=False) as self.chat_group:
                    with gr.Column(visible=True) as self.chat_page:
                        self.chatbot = gr.Chatbot(
                            height=600,
                            value=[],
                            bubble_full_width=False,
                            show_copy_button=True,
                            container=False,
                            placeholder='<strong>Chat Box</strong>')
                        with gr.Row():
                            self.clear_btn = gr.Button(clear_sty +
                                                       ' Clear Chat',
                                                       size='sm')

                    with gr.Column(visible=False) as self.editor_page:
                        with gr.Tabs(visible=False) as self.upload_tabs:
                            with gr.Tab(id='ImageUploader',
                                        label='Image Uploader',
                                        visible=True) as self.upload_tab:
                                self.image_uploader = gr.Image(
                                    height=550,
                                    interactive=True,
                                    type='pil',
                                    image_mode='RGB',
                                    sources=['upload'],
                                    elem_id='image_uploader',
                                    format='png')
                                with gr.Row():
                                    self.sub_btn_1 = gr.Button(
                                        value='Submit',
                                        elem_id='upload_submit')
                                    self.ext_btn_1 = gr.Button(value='Exit')
                        with gr.Tabs(visible=False) as self.edit_tabs:
                            with gr.Tab(id='ImageEditor',
                                        label='Image Editor') as self.edit_tab:
                                self.mask_type = gr.Dropdown(
                                    label='Mask Type',
                                    choices=[
                                        'Background', 'Composite',
                                        'Outpainting'
                                    ],
                                    value='Background')
                                self.mask_type_info = gr.HTML(
                                    value=
                                    "<div style='background-color: white; padding-left: 15px; color: grey;'>Background mode will not erase the visual content in the mask area</div>"
                                )
                                with gr.Accordion(
                                        label='Outpainting Setting',
                                        open=True,
                                        visible=False) as self.outpaint_tab:
                                    with gr.Row(variant='panel'):
                                        self.top_ext = gr.Slider(
                                            show_label=True,
                                            label='Top Extend Ratio',
                                            minimum=0.0,
                                            maximum=2.0,
                                            step=0.1,
                                            value=0.25)
                                        self.bottom_ext = gr.Slider(
                                            show_label=True,
                                            label='Bottom Extend Ratio',
                                            minimum=0.0,
                                            maximum=2.0,
                                            step=0.1,
                                            value=0.25)
                                    with gr.Row(variant='panel'):
                                        self.left_ext = gr.Slider(
                                            show_label=True,
                                            label='Left Extend Ratio',
                                            minimum=0.0,
                                            maximum=2.0,
                                            step=0.1,
                                            value=0.25)
                                        self.right_ext = gr.Slider(
                                            show_label=True,
                                            label='Right Extend Ratio',
                                            minimum=0.0,
                                            maximum=2.0,
                                            step=0.1,
                                            value=0.25)
                                    with gr.Row(variant='panel'):
                                        self.img_pad_btn = gr.Button(
                                            value='Pad Image')

                                self.image_editor = gr.ImageMask(
                                    value=None,
                                    sources=[],
                                    layers=False,
                                    label='Edit Image',
                                    elem_id='image_editor',
                                    format='png')
                                with gr.Row():
                                    self.sub_btn_2 = gr.Button(
                                        value='Submit', elem_id='edit_submit')
                                    self.ext_btn_2 = gr.Button(value='Exit')

                            with gr.Tab(id='ImageViewer',
                                        label='Image Viewer') as self.image_view_tab:
                                if self.gradio_version >= '5.0.0':
                                    self.image_viewer = gr.Image(
                                        label='Image',
                                        type='pil',
                                        show_download_button=True,
                                        elem_id='image_viewer')
                                else:
                                    try:
                                        from gradio_imageslider import ImageSlider
                                    except Exception as e:
                                        print(f"Import gradio_imageslider failed, please install.")
                                    self.image_viewer = ImageSlider(
                                        label='Image',
                                        type='pil',
                                        show_download_button=True,
                                        elem_id='image_viewer')

                                self.ext_btn_3 = gr.Button(value='Exit')

                            with gr.Tab(id='VideoViewer',
                                        label='Video Viewer',
                                        visible=False) as self.video_view_tab:
                                self.video_viewer = gr.Video(
                                    label='Video',
                                    interactive=False,
                                    sources=[],
                                    format='mp4',
                                    show_download_button=True,
                                    elem_id='video_viewer',
                                    loop=True,
                                    autoplay=True)

                                self.ext_btn_4 = gr.Button(value='Exit')

                with gr.Row(equal_height=True, visible=True) as self.legacy_group:
                    with gr.Column():
                        self.legacy_image_uploader = gr.Image(
                            height=550,
                            interactive=True,
                            type='pil',
                            image_mode='RGB',
                            elem_id='legacy_image_uploader',
                            format='png')
                    with gr.Column():
                        self.legacy_image_viewer = gr.Image(
                            label='Image',
                            height=550,
                            type='pil',
                            interactive=False,
                            show_download_button=True,
                            elem_id='image_viewer')


                with gr.Accordion(label='Setting', open=False):
                    with gr.Row():
                        self.model_name_dd = gr.Dropdown(
                            choices=self.model_choices,
                            value=self.default_model_name,
                            label='Model Version')

                    with gr.Row():
                        self.negative_prompt = gr.Textbox(
                            value='',
                            placeholder=
                            'Negative prompt used for Classifier-Free Guidance',
                            label='Negative Prompt',
                            container=False)

                    with gr.Row():
                        # REFINER_PROMPT
                        self.refiner_prompt = gr.Textbox(
                            value=self.pipe.input.get("refiner_prompt", ""),
                            visible=self.pipe.input.get("refiner_prompt", None) is not None,
                            placeholder=
                            'Prompt used for refiner',
                            label='Refiner Prompt',
                            container=False)


                    with gr.Row():
                        with gr.Column(scale=8, min_width=500):
                            with gr.Row():
                                self.step = gr.Slider(minimum=1,
                                                      maximum=1000,
                                                      value=self.pipe.input.get("sample_steps", 20),
                                                      visible=self.pipe.input.get("sample_steps", None) is not None,
                                                      label='Sample Step')
                                self.cfg_scale = gr.Slider(
                                    minimum=1.0,
                                    maximum=20.0,
                                    value=self.pipe.input.get("guide_scale", 4.5),
                                    visible=self.pipe.input.get("guide_scale", None) is not None,
                                    label='Guidance Scale')
                                self.rescale = gr.Slider(minimum=0.0,
                                                         maximum=1.0,
                                                         value=self.pipe.input.get("guide_rescale", 0.5),
                                                         visible=self.pipe.input.get("guide_rescale", None) is not None,
                                                         label='Rescale')
                                self.refiner_scale = gr.Slider(minimum=-0.1,
                                                         maximum=1.0,
                                                         value=self.pipe.input.get("refiner_scale", -1),
                                                         visible=self.pipe.input.get("refiner_scale", None) is not None,
                                                         label='Refiner Scale')
                                self.seed = gr.Slider(minimum=-1,
                                                      maximum=10000000,
                                                      value=-1,
                                                      label='Seed')
                                self.output_height = gr.Slider(
                                    minimum=256,
                                    maximum=1440,
                                    value=self.pipe.input.get("output_height", 1024),
                                    visible=self.pipe.input.get("output_height", None) is not None,
                                    label='Output Height')
                                self.output_width = gr.Slider(
                                    minimum=256,
                                    maximum=1440,
                                    value=self.pipe.input.get("output_width", 1024),
                                    visible=self.pipe.input.get("output_width", None) is not None,
                                    label='Output Width')
                        with gr.Column(scale=1, min_width=50):
                            self.use_history = gr.Checkbox(value=False,
                                                           label='Use History')
                            self.use_ace = gr.Checkbox(value=self.pipe.input.get("use_ace", True),
                                                       visible=self.pipe.input.get("use_ace", None) is not None,
                                                       label='Use ACE')
                            self.video_auto = gr.Checkbox(
                                value=False,
                                label='Auto Gen Video',
                                visible=self.enable_i2v)

                    with gr.Row(variant='panel',
                                equal_height=True,
                                visible=self.enable_i2v):
                        self.video_fps = gr.Slider(minimum=1,
                                                   maximum=16,
                                                   value=8,
                                                   label='Video FPS',
                                                   visible=True)
                        self.video_frames = gr.Slider(minimum=8,
                                                      maximum=49,
                                                      value=49,
                                                      label='Video Frame Num',
                                                      visible=True)
                        self.video_step = gr.Slider(minimum=1,
                                                    maximum=1000,
                                                    value=50,
                                                    label='Video Sample Step',
                                                    visible=True)
                        self.video_cfg_scale = gr.Slider(
                            minimum=1.0,
                            maximum=20.0,
                            value=6.0,
                            label='Video Guidance Scale',
                            visible=True)
                        self.video_seed = gr.Slider(minimum=-1,
                                                    maximum=10000000,
                                                    value=-1,
                                                    label='Video Seed',
                                                    visible=True)

                with gr.Row():
                    self.chatbot_inst = """
                       **Instruction**:
                       1. Click 'Upload' button to upload one or more images as input images.
                       2. Enter '@' in the text box will exhibit all images in the gallery.
                       3. Select the image you wish to edit from the gallery, and its Image ID will be displayed in the text box.
                       4. Compose the editing instruction for the selected image, incorporating image id '@xxxxxx' into your instruction.
                       For example, you might say, "Change the girl's skirt in @123456 to blue." The '@xxxxx' token will facilitate the identification of the specific image, and will be automatically replaced by a special token '{image}' in the instruction. Furthermore, it is also possible to engage in text-to-image generation without any initial image input.
                       5. Once your instructions are prepared, please click the "Chat" button to view the edited result in the chat window.
                       6. **Important** To render text on an image, please ensure to include a space between each letter. For instance, "add text 'g i r l' on the mask area of @xxxxx".
                       7. To implement local editing based on a specified mask, simply click on the image within the chat window to access the image editor. Here, you can draw a mask and then click the 'Submit' button to upload the edited image along with the mask. For inpainting tasks, select the 'Composite' mask type, while for outpainting tasks, choose the 'Outpainting' mask type. For all other local editing tasks, please select the 'Background' mask type.
                       8. If you find our work valuable, we invite you to refer to the [ACE Page](https://ali-vilab.github.io/ace-page/) for comprehensive information.
                    """

                    self.legacy_inst = """
                       **Instruction**:
                       1. You can edit the image by uploading it; if no image is uploaded, an image will be generated from text..
                       2. Enter '@' in the text box will exhibit all images in the gallery.
                       3. Select the image you wish to edit from the gallery, and its Image ID will be displayed in the text box.
                       4. **Important** To render text on an image, please ensure to include a space between each letter. For instance, "add text 'g i r l' on the mask area of @xxxxx".
                       5. To perform multi-step editing, partial editing, inpainting, outpainting, and other operations, please click the Chatbot Checkbox to enable the conversational editing mode and follow the relevant instructions..
                       6. If you find our work valuable, we invite you to refer to the [ACE Page](https://ali-vilab.github.io/ace-page/) for comprehensive information.
                    """

                    self.instruction = gr.Markdown(value=self.legacy_inst)

                with gr.Row(variant='panel',
                            equal_height=True,
                            show_progress=False):
                    with gr.Column(scale=1, min_width=100, visible=False) as self.upload_panel:
                        self.upload_btn = gr.Button(value=upload_sty +
                                                    ' Upload',
                                                    variant='secondary')
                    with gr.Column(scale=5, min_width=500):
                        self.text = gr.Textbox(
                            placeholder='Input "@" find history of image',
                            label='Instruction',
                            container=False)
                    with gr.Column(scale=1, min_width=100):
                        self.chat_btn = gr.Button(value='Generate',
                                                  variant='primary')
                    with gr.Column(scale=1, min_width=100):
                        self.retry_btn = gr.Button(value=refresh_sty +
                                                   ' Retry',
                                                   variant='secondary')
                    with gr.Column(scale=1, min_width=100):
                        self.mode_checkbox = gr.Checkbox(
                            value=False,
                            label='ChatBot')
                    with gr.Column(scale=(1 if self.enable_i2v else 0),
                                   min_width=0):
                        self.video_gen_btn = gr.Button(value=video_sty +
                                                       ' Gen Video',
                                                       variant='secondary',
                                                       visible=self.enable_i2v)
                    with gr.Column(scale=(1 if self.enable_i2v else 0),
                                   min_width=0):
                        self.extend_prompt = gr.Checkbox(
                            value=True,
                            label='Extend Prompt',
                            visible=self.enable_i2v)

                with gr.Row():
                    self.gallery = gr.Gallery(visible=False,
                                              label='History',
                                              columns=10,
                                              allow_preview=False,
                                              interactive=False)

                self.eg = gr.Column(visible=True)

    def set_callbacks(self, *args, **kwargs):

        ########################################
        #@spaces.GPU(duration=60)
        def change_model(model_name):
            if model_name not in self.model_choices:
                gr.Info('The provided model name is not a valid choice!')
                return model_name, gr.update(), gr.update()

            if model_name != self.model_name:
                lock.acquire()
                del self.pipe
                torch.cuda.empty_cache()
                self.pipe = ACEInference()
                self.pipe.init_from_cfg(self.model_choices[model_name])
                self.model_name = model_name
                lock.release()

            return (model_name, gr.update(), gr.update(),
                    gr.Slider(
                              value=self.pipe.input.get("sample_steps", 20),
                              visible=self.pipe.input.get("sample_steps", None) is not None),
                    gr.Slider(
                        value=self.pipe.input.get("guide_scale", 4.5),
                        visible=self.pipe.input.get("guide_scale", None) is not None),
                    gr.Slider(
                              value=self.pipe.input.get("guide_rescale", 0.5),
                              visible=self.pipe.input.get("guide_rescale", None) is not None),
                    gr.Slider(
                        value=self.pipe.input.get("output_height", 1024),
                        visible=self.pipe.input.get("output_height", None) is not None),
                    gr.Slider(
                        value=self.pipe.input.get("output_width", 1024),
                        visible=self.pipe.input.get("output_width", None) is not None),
                    gr.Textbox(
                        value=self.pipe.input.get("refiner_prompt", ""),
                        visible=self.pipe.input.get("refiner_prompt", None) is not None),
                    gr.Slider(
                              value=self.pipe.input.get("refiner_scale", -1),
                              visible=self.pipe.input.get("refiner_scale", None) is not None
                        ),
                    gr.Checkbox(
                        value=self.pipe.input.get("use_ace", True),
                        visible=self.pipe.input.get("use_ace", None) is not None
                    )
                    )

        self.model_name_dd.change(
            change_model,
            inputs=[self.model_name_dd],
            outputs=[
                self.model_name_dd, self.chatbot, self.text,
                self.step,
                self.cfg_scale, self.rescale, self.output_height,
                self.output_width, self.refiner_prompt, self.refiner_scale,
                self.use_ace])


        def mode_change(mode_check):
            if mode_check:
                # ChatBot
                return (
                    gr.Row(visible=False),
                    gr.Row(visible=True),
                    gr.Button(value='Generate'),
                    gr.State(value='chatbot'),
                    gr.Column(visible=True),
                    gr.Markdown(value=self.chatbot_inst)
                )
            else:
                # Legacy
                return (
                    gr.Row(visible=True),
                    gr.Row(visible=False),
                    gr.Button(value=chat_sty + ' Chat'),
                    gr.State(value='legacy'),
                    gr.Column(visible=False),
                    gr.Markdown(value=self.legacy_inst)
                )
        self.mode_checkbox.change(mode_change, inputs=[self.mode_checkbox],
                                  outputs=[self.legacy_group, self.chat_group,
                                           self.chat_btn, self.ui_mode,
                                           self.upload_panel, self.instruction])


        ########################################
        def generate_gallery(text, images):
            if text.endswith(' '):
                return gr.update(), gr.update(visible=False)
            elif text.endswith('@'):
                gallery_info = []
                for image_id, image_meta in images.items():
                    thumbnail_path = image_meta['thumbnail']
                    gallery_info.append((thumbnail_path, image_id))
                return gr.update(), gr.update(visible=True, value=gallery_info)
            else:
                gallery_info = []
                match = re.search('@([^@ ]+)$', text)
                if match:
                    prefix = match.group(1)
                    for image_id, image_meta in images.items():
                        if not image_id.startswith(prefix):
                            continue
                        thumbnail_path = image_meta['thumbnail']
                        gallery_info.append((thumbnail_path, image_id))

                    if len(gallery_info) > 0:
                        return gr.update(), gr.update(visible=True,
                                                      value=gallery_info)
                    else:
                        return gr.update(), gr.update(visible=False)
                else:
                    return gr.update(), gr.update(visible=False)

        self.text.input(generate_gallery,
                        inputs=[self.text, self.images],
                        outputs=[self.text, self.gallery],
                        show_progress='hidden')

        ########################################
        def select_image(text, evt: gr.SelectData):
            image_id = evt.value['caption']
            text = '@'.join(text.split('@')[:-1]) + f'@{image_id} '
            return gr.update(value=text), gr.update(visible=False, value=None)

        self.gallery.select(select_image,
                            inputs=self.text,
                            outputs=[self.text, self.gallery])

        ########################################
        def generate_video(message,
                           extend_prompt,
                           history,
                           images,
                           num_steps,
                           num_frames,
                           cfg_scale,
                           fps,
                           seed,
                           progress=gr.Progress(track_tqdm=True)):

            from diffusers.utils import export_to_video

            generator = torch.Generator(device='cuda').manual_seed(seed)
            img_ids = re.findall('@(.*?)[ ,;.?$]', message)
            if len(img_ids) == 0:
                history.append((
                    message,
                    'Sorry, no images were found in the prompt to be used as the first frame of the video.'
                ))
                while len(history) >= self.max_msgs:
                    history.pop(0)
                return history, self.get_history(
                    history), gr.update(), gr.update(visible=False)

            img_id = img_ids[0]
            prompt = re.sub(f'@{img_id}\s+', '', message)

            if extend_prompt:
                messages = copy.deepcopy(self.enhance_ctx)
                messages.append({
                    'role':
                    'user',
                    'content':
                    f'Create an imaginative video descriptive caption or modify an earlier caption in ENGLISH for the user input: "{prompt}"',
                })
                lock.acquire()
                outputs = self.enhancer(
                    messages,
                    max_new_tokens=200,
                )

                prompt = outputs[0]['generated_text'][-1]['content']
                print(prompt)
                lock.release()

            img_meta = images[img_id]
            img_path = img_meta['image']
            image = Image.open(img_path).convert('RGB')

            lock.acquire()
            video = self.i2v_pipe(
                prompt=prompt,
                image=image,
                num_videos_per_prompt=1,
                num_inference_steps=num_steps,
                num_frames=num_frames,
                guidance_scale=cfg_scale,
                generator=generator,
            ).frames[0]
            lock.release()

            out_video_path = export_to_video(video, fps=fps)
            history.append((
                f"Based on first frame @{img_id} and description '{prompt}', generate a video",
                'This is generated video:'))
            history.append((None, out_video_path))
            while len(history) >= self.max_msgs:
                history.pop(0)

            return history, self.get_history(history), gr.update(
                value=''), gr.update(visible=False)

        self.video_gen_btn.click(
            generate_video,
            inputs=[
                self.text, self.extend_prompt, self.history, self.images,
                self.video_step, self.video_frames, self.video_cfg_scale,
                self.video_fps, self.video_seed
            ],
            outputs=[self.history, self.chatbot, self.text, self.gallery])

        ########################################
        @spaces.GPU(duration=120)
        def run_chat(
                     message,
                     legacy_image,
                     ui_mode,
                     use_ace,
                     extend_prompt,
                     history,
                     images,
                     use_history,
                     history_result,
                     negative_prompt,
                     cfg_scale,
                     rescale,
                     refiner_prompt,
                     refiner_scale,
                     step,
                     seed,
                     output_h,
                     output_w,
                     video_auto,
                     video_steps,
                     video_frames,
                     video_cfg_scale,
                     video_fps,
                     video_seed,
                     progress=gr.Progress(track_tqdm=True)):
            legacy_img_ids = []
            if ui_mode == 'legacy':
                if legacy_image is not None:
                    history, images, img_id = self.add_uploaded_image_to_history(
                        legacy_image, history, images)
                    legacy_img_ids.append(img_id)
            retry_msg = message
            gen_id = get_md5(message)[:12]
            save_path = os.path.join(self.cache_dir, f'{gen_id}.png')

            img_ids = re.findall('@(.*?)[ ,;.?$]', message)
            history_io = None

            if len(img_ids) < 1:
                img_ids = legacy_img_ids
                for img_id in img_ids:
                    if f'@{img_id}' not in message:
                        message = f'@{img_id} ' + message

            new_message = message

            if len(img_ids) > 0:
                edit_image, edit_image_mask, edit_task = [], [], []
                for i, img_id in enumerate(img_ids):
                    if img_id not in images:
                        gr.Info(
                            f'The input image ID {img_id} is not exist... Skip loading image.'
                        )
                        continue
                    placeholder = '{image}' if i == 0 else '{' + f'image{i}' + '}'
                    new_message = re.sub(f'@{img_id}', placeholder,
                                         new_message)
                    img_meta = images[img_id]
                    img_path = img_meta['image']
                    img_mask = img_meta['mask']
                    img_mask_type = img_meta['mask_type']
                    if img_mask_type is not None and img_mask_type == 'Composite':
                        task = 'inpainting'
                    else:
                        task = ''
                    edit_image.append(Image.open(img_path).convert('RGB'))
                    edit_image_mask.append(
                        Image.open(img_mask).
                        convert('L') if img_mask is not None else None)
                    edit_task.append(task)

                    if use_history and (img_id in history_result):
                        history_io = history_result[img_id]

                buffered = io.BytesIO()
                edit_image[0].save(buffered, format='PNG')
                img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
                img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'
                pre_info = f'Received one or more images, so image editing is conducted.\n The first input image @{img_ids[0]} is:\n {img_str}'
            else:
                pre_info = 'No image ids were found in the provided text prompt, so text-guided image generation is conducted. \n'
                edit_image = None
                edit_image_mask = None
                edit_task = ''

            print(new_message)
            imgs = self.pipe(
                image=edit_image,
                mask=edit_image_mask,
                task=edit_task,
                prompt=[new_message] *
                len(edit_image) if edit_image is not None else [new_message],
                negative_prompt=[negative_prompt] * len(edit_image)
                if edit_image is not None else [negative_prompt],
                history_io=history_io,
                output_height=output_h,
                output_width=output_w,
                sampler='ddim',
                sample_steps=step,
                guide_scale=cfg_scale,
                guide_rescale=rescale,
                seed=seed,
                refiner_prompt=refiner_prompt,
                refiner_scale=refiner_scale,
                use_ace=use_ace
            )

            img = imgs[0]
            img.save(save_path, format='PNG')

            if history_io:
                history_io_new = copy.deepcopy(history_io)
                history_io_new['image'] += edit_image[:1]
                history_io_new['mask'] += edit_image_mask[:1]
                history_io_new['task'] += edit_task[:1]
                history_io_new['prompt'] += [new_message]
                history_io_new['image'] = history_io_new['image'][-5:]
                history_io_new['mask'] = history_io_new['mask'][-5:]
                history_io_new['task'] = history_io_new['task'][-5:]
                history_io_new['prompt'] = history_io_new['prompt'][-5:]
                history_result[gen_id] = history_io_new
            elif edit_image is not None and len(edit_image) > 0:
                history_io_new = {
                    'image': edit_image[:1],
                    'mask': edit_image_mask[:1],
                    'task': edit_task[:1],
                    'prompt': [new_message]
                }
                history_result[gen_id] = history_io_new

            w, h = img.size
            if w > h:
                tb_w = 128
                tb_h = int(h * tb_w / w)
            else:
                tb_h = 128
                tb_w = int(w * tb_h / h)

            thumbnail_path = os.path.join(self.cache_dir,
                                          f'{gen_id}_thumbnail.jpg')
            thumbnail = img.resize((tb_w, tb_h))
            thumbnail.save(thumbnail_path, format='JPEG')

            images[gen_id] = {
                'image': save_path,
                'mask': None,
                'mask_type': None,
                'thumbnail': thumbnail_path
            }

            buffered = io.BytesIO()
            img.convert('RGB').save(buffered, format='PNG')
            img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
            img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'

            history.append(
                (message,
                 f'{pre_info} The generated image @{gen_id} is:\n {img_str}'))

            if video_auto:
                if video_seed is None or video_seed == -1:
                    video_seed = random.randint(0, 10000000)

                lock.acquire()
                generator = torch.Generator(
                    device='cuda').manual_seed(video_seed)
                pixel_values = load_image(img.convert('RGB'),
                                          max_num=self.llm_max_num).to(
                                              torch.bfloat16).cuda()
                prompt = self.captioner.chat(self.llm_tokenizer, pixel_values,
                                             self.llm_prompt,
                                             self.llm_generation_config)
                print(prompt)
                lock.release()

                if extend_prompt:
                    messages = copy.deepcopy(self.enhance_ctx)
                    messages.append({
                        'role':
                        'user',
                        'content':
                        f'Create an imaginative video descriptive caption or modify an earlier caption in ENGLISH for the user input: "{prompt}"',
                    })
                    lock.acquire()
                    outputs = self.enhancer(
                        messages,
                        max_new_tokens=200,
                    )
                    prompt = outputs[0]['generated_text'][-1]['content']
                    print(prompt)
                    lock.release()

                lock.acquire()
                video = self.i2v_pipe(
                    prompt=prompt,
                    image=img,
                    num_videos_per_prompt=1,
                    num_inference_steps=video_steps,
                    num_frames=video_frames,
                    guidance_scale=video_cfg_scale,
                    generator=generator,
                ).frames[0]
                lock.release()

                out_video_path = export_to_video(video, fps=video_fps)
                history.append((
                    f"Based on first frame @{gen_id} and description '{prompt}', generate a video",
                    'This is generated video:'))
                history.append((None, out_video_path))

            while len(history) >= self.max_msgs:
                history.pop(0)

            return (history, images, gr.Image(value=save_path),
                    history_result, self.get_history(
                history), gr.update(), gr.update(
                    visible=False), retry_msg)

        chat_inputs = [
            self.legacy_image_uploader, self.ui_mode, self.use_ace,
            self.extend_prompt, self.history, self.images, self.use_history,
            self.history_result, self.negative_prompt, self.cfg_scale,
            self.rescale, self.refiner_prompt, self.refiner_scale,
            self.step, self.seed, self.output_height,
            self.output_width, self.video_auto, self.video_step,
            self.video_frames, self.video_cfg_scale, self.video_fps,
            self.video_seed
        ]

        chat_outputs = [
            self.history, self.images, self.legacy_image_viewer,
            self.history_result, self.chatbot,
            self.text, self.gallery, self.retry_msg
        ]

        self.chat_btn.click(run_chat,
                            inputs=[self.text] + chat_inputs,
                            outputs=chat_outputs)

        self.text.submit(run_chat,
                         inputs=[self.text] + chat_inputs,
                         outputs=chat_outputs)

        def retry_fn(*args):
            return run_chat(*args)

        self.retry_btn.click(retry_fn,
                             inputs=[self.retry_msg] + chat_inputs,
                             outputs=chat_outputs)

        ########################################
        @spaces.GPU(duration=120)
        def run_example(task, img, img_mask, ref1, prompt, seed):
            edit_image, edit_image_mask, edit_task = [], [], []
            if img is not None:
                w, h = img.size
                if w > 2048:
                    ratio = w / 2048.
                    w = 2048
                    h = int(h / ratio)
                if h > 2048:
                    ratio = h / 2048.
                    h = 2048
                    w = int(w / ratio)
                img = img.resize((w, h))
                edit_image.append(img)
                edit_image_mask.append(
                    img_mask if img_mask is not None else None)
                edit_task.append(task)
                if ref1 is not None:
                    edit_image.append(ref1)
                    edit_image_mask.append(None)
                    edit_task.append('')

                buffered = io.BytesIO()
                img.save(buffered, format='PNG')
                img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
                img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'
                pre_info = f'Received one or more images, so image editing is conducted.\n The first input image is:\n {img_str}'
            else:
                pre_info = 'No image ids were found in the provided text prompt, so text-guided image generation is conducted. \n'
                edit_image = None
                edit_image_mask = None
                edit_task = ''

            img_num = len(edit_image) if edit_image is not None else 1
            imgs = self.pipe(
                image=edit_image,
                mask=edit_image_mask,
                task=edit_task,
                prompt=[prompt] * img_num,
                negative_prompt=[''] * img_num,
                seed=seed,
                refiner_prompt=self.pipe.input.get("refiner_prompt", ""),
                refiner_scale=self.pipe.input.get("refiner_scale", 0.0),
            )

            img = imgs[0]
            buffered = io.BytesIO()
            img.convert('RGB').save(buffered, format='PNG')
            img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
            img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'
            history = [(prompt,
                        f'{pre_info} The generated image is:\n {img_str}')]

            img_id = get_md5(img_b64)[:12]
            save_path = os.path.join(self.cache_dir, f'{img_id}.png')
            img.convert('RGB').save(save_path)
            
            return self.get_history(history), gr.update(value=''), gr.update(
                visible=False), gr.update(value=save_path), gr.update(value=-1)

        with self.eg:
            self.example_task = gr.Text(label='Task Name',
                                        value='',
                                        visible=False)
            self.example_image = gr.Image(label='Edit Image',
                                          type='pil',
                                          image_mode='RGB',
                                          visible=False)
            self.example_mask = gr.Image(label='Edit Image Mask',
                                         type='pil',
                                         image_mode='L',
                                         visible=False)
            self.example_ref_im1 = gr.Image(label='Ref Image',
                                            type='pil',
                                            image_mode='RGB',
                                            visible=False)

            self.examples = gr.Examples(
                fn=run_example,
                examples=self.chatbot_examples,
                inputs=[
                    self.example_task, self.example_image, self.example_mask,
                    self.example_ref_im1, self.text, self.seed
                ],
                outputs=[self.chatbot, self.text, self.gallery, self.legacy_image_viewer, self.seed],
                examples_per_page=4,
                cache_examples=False,
                run_on_click=True)
        ########################################
        def upload_image():
            return (gr.update(visible=True,
                              scale=1), gr.update(visible=True, scale=1),
                    gr.update(visible=True), gr.update(visible=False),
                    gr.update(visible=False), gr.update(visible=False),
                    gr.update(visible=True))

        self.upload_btn.click(upload_image,
                              inputs=[],
                              outputs=[
                                  self.chat_page, self.editor_page,
                                  self.upload_tab, self.edit_tab,
                                  self.image_view_tab, self.video_view_tab,
                                  self.upload_tabs
                              ])

        ########################################
        def edit_image(evt: gr.SelectData):
            if isinstance(evt.value, str):
                img_b64s = re.findall(
                    '<img src="data:image/png;base64,(.*?)" style="pointer-events: none;">',
                    evt.value)
                imgs = [
                    Image.open(io.BytesIO(base64.b64decode(copy.deepcopy(i))))
                    for i in img_b64s
                ]
                if len(imgs) > 0:
                    if len(imgs) == 2:
                        if self.gradio_version >= '5.0.0':
                            view_img = copy.deepcopy(imgs[-1])
                        else:
                            view_img = copy.deepcopy(imgs)
                        edit_img = copy.deepcopy(imgs[-1])
                    else:
                        if self.gradio_version >= '5.0.0':
                            view_img = copy.deepcopy(imgs[-1])
                        else:
                            view_img = [
                                copy.deepcopy(imgs[-1]),
                                copy.deepcopy(imgs[-1])
                            ]
                        edit_img = copy.deepcopy(imgs[-1])

                    return (gr.update(visible=True,
                                      scale=1), gr.update(visible=True,
                                                          scale=1),
                            gr.update(visible=False), gr.update(visible=True),
                            gr.update(visible=True), gr.update(visible=False),
                            gr.update(value=edit_img),
                            gr.update(value=view_img), gr.update(value=None),
                            gr.update(visible=True))
                else:
                    return (gr.update(), gr.update(), gr.update(), gr.update(),
                            gr.update(), gr.update(), gr.update(), gr.update(),
                            gr.update(), gr.update())
            elif isinstance(evt.value, dict) and evt.value.get(
                    'component', '') == 'video':
                value = evt.value['value']['video']['path']
                return (gr.update(visible=True,
                                  scale=1), gr.update(visible=True, scale=1),
                        gr.update(visible=False), gr.update(visible=False),
                        gr.update(visible=False), gr.update(visible=True),
                        gr.update(), gr.update(), gr.update(value=value),
                        gr.update())
            else:
                return (gr.update(), gr.update(), gr.update(), gr.update(),
                        gr.update(), gr.update(), gr.update(), gr.update(),
                        gr.update(), gr.update())

        self.chatbot.select(edit_image,
                            outputs=[
                                self.chat_page, self.editor_page,
                                self.upload_tab, self.edit_tab,
                                self.image_view_tab, self.video_view_tab,
                                self.image_editor, self.image_viewer,
                                self.video_viewer, self.edit_tabs
                            ])

        if self.gradio_version < '5.0.0':
            self.image_viewer.change(lambda x: x,
                                     inputs=self.image_viewer,
                                     outputs=self.image_viewer)

        ########################################
        def submit_upload_image(image, history, images):
            history, images, _ = self.add_uploaded_image_to_history(
                image, history, images)
            return gr.update(visible=False), gr.update(
                visible=True), gr.update(
                    value=self.get_history(history)), history, images

        self.sub_btn_1.click(
            submit_upload_image,
            inputs=[self.image_uploader, self.history, self.images],
            outputs=[
                self.editor_page, self.chat_page, self.chatbot, self.history,
                self.images
            ])

        ########################################
        def submit_edit_image(imagemask, mask_type, history, images):
            history, images = self.add_edited_image_to_history(
                imagemask, mask_type, history, images)
            return gr.update(visible=False), gr.update(
                visible=True), gr.update(
                    value=self.get_history(history)), history, images

        self.sub_btn_2.click(submit_edit_image,
                             inputs=[
                                 self.image_editor, self.mask_type,
                                 self.history, self.images
                             ],
                             outputs=[
                                 self.editor_page, self.chat_page,
                                 self.chatbot, self.history, self.images
                             ])

        ########################################
        def exit_edit():
            return gr.update(visible=False), gr.update(visible=True, scale=3)

        self.ext_btn_1.click(exit_edit,
                             outputs=[self.editor_page, self.chat_page])
        self.ext_btn_2.click(exit_edit,
                             outputs=[self.editor_page, self.chat_page])
        self.ext_btn_3.click(exit_edit,
                             outputs=[self.editor_page, self.chat_page])
        self.ext_btn_4.click(exit_edit,
                             outputs=[self.editor_page, self.chat_page])

        ########################################
        def update_mask_type_info(mask_type):
            if mask_type == 'Background':
                info = 'Background mode will not erase the visual content in the mask area'
                visible = False
            elif mask_type == 'Composite':
                info = 'Composite mode will erase the visual content in the mask area'
                visible = False
            elif mask_type == 'Outpainting':
                info = 'Outpaint mode is used for preparing input image for outpainting task'
                visible = True
            return (gr.update(
                visible=True,
                value=
                f"<div style='background-color: white; padding-left: 15px; color: grey;'>{info}</div>"
            ), gr.update(visible=visible))

        self.mask_type.change(update_mask_type_info,
                              inputs=self.mask_type,
                              outputs=[self.mask_type_info, self.outpaint_tab])

        ########################################
        def extend_image(top_ratio, bottom_ratio, left_ratio, right_ratio,
                         image):
            img = cv2.cvtColor(image['background'], cv2.COLOR_RGBA2RGB)
            h, w = img.shape[:2]
            new_h = int(h * (top_ratio + bottom_ratio + 1))
            new_w = int(w * (left_ratio + right_ratio + 1))
            start_h = int(h * top_ratio)
            start_w = int(w * left_ratio)
            new_img = np.zeros((new_h, new_w, 3), dtype=np.uint8)
            new_mask = np.ones((new_h, new_w, 1), dtype=np.uint8) * 255
            new_img[start_h:start_h + h, start_w:start_w + w, :] = img
            new_mask[start_h:start_h + h, start_w:start_w + w] = 0
            layer = np.concatenate([new_img, new_mask], axis=2)
            value = {
                'background': new_img,
                'composite': new_img,
                'layers': [layer]
            }
            return gr.update(value=value)

        self.img_pad_btn.click(extend_image,
                               inputs=[
                                   self.top_ext, self.bottom_ext,
                                   self.left_ext, self.right_ext,
                                   self.image_editor
                               ],
                               outputs=self.image_editor)

        ########################################
        def clear_chat(history, images, history_result):
            history.clear()
            images.clear()
            history_result.clear()
            return history, images, history_result, self.get_history(history)

        self.clear_btn.click(
            clear_chat,
            inputs=[self.history, self.images, self.history_result],
            outputs=[
                self.history, self.images, self.history_result, self.chatbot
            ])

    def get_history(self, history):
        info = []
        for item in history:
            new_item = [None, None]
            if isinstance(item[0], str) and item[0].endswith('.mp4'):
                new_item[0] = gr.Video(item[0], format='mp4')
            else:
                new_item[0] = item[0]
            if isinstance(item[1], str) and item[1].endswith('.mp4'):
                new_item[1] = gr.Video(item[1], format='mp4')
            else:
                new_item[1] = item[1]
            info.append(new_item)
        return info

    def generate_random_string(self, length=20):
        letters_and_digits = string.ascii_letters + string.digits
        random_string = ''.join(
            random.choice(letters_and_digits) for i in range(length))
        return random_string

    def add_edited_image_to_history(self, image, mask_type, history, images):
        if mask_type == 'Composite':
            img = Image.fromarray(image['composite'])
        else:
            img = Image.fromarray(image['background'])

        img_id = get_md5(self.generate_random_string())[:12]
        save_path = os.path.join(self.cache_dir, f'{img_id}.png')
        img.convert('RGB').save(save_path)

        mask = image['layers'][0][:, :, 3]
        mask = Image.fromarray(mask).convert('RGB')
        mask_path = os.path.join(self.cache_dir, f'{img_id}_mask.png')
        mask.save(mask_path)

        w, h = img.size
        if w > h:
            tb_w = 128
            tb_h = int(h * tb_w / w)
        else:
            tb_h = 128
            tb_w = int(w * tb_h / h)

        if mask_type == 'Background':
            comp_mask = np.array(mask, dtype=np.uint8)
            mask_alpha = (comp_mask[:, :, 0:1].astype(np.float32) *
                          0.6).astype(np.uint8)
            comp_mask = np.concatenate([comp_mask, mask_alpha], axis=2)
            thumbnail = Image.alpha_composite(
                img.convert('RGBA'),
                Image.fromarray(comp_mask).convert('RGBA')).convert('RGB')
        else:
            thumbnail = img.convert('RGB')

        thumbnail_path = os.path.join(self.cache_dir,
                                      f'{img_id}_thumbnail.jpg')
        thumbnail = thumbnail.resize((tb_w, tb_h))
        thumbnail.save(thumbnail_path, format='JPEG')

        buffered = io.BytesIO()
        img.convert('RGB').save(buffered, format='PNG')
        img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'

        buffered = io.BytesIO()
        mask.convert('RGB').save(buffered, format='PNG')
        mask_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        mask_str = f'<img src="data:image/png;base64,{mask_b64}" style="pointer-events: none;">'

        images[img_id] = {
            'image': save_path,
            'mask': mask_path,
            'mask_type': mask_type,
            'thumbnail': thumbnail_path
        }
        history.append((
            None,
            f'This is edited image and mask:\n {img_str} {mask_str} image ID is: {img_id}'
        ))
        return history, images

    def add_uploaded_image_to_history(self, img, history, images):
        img_id = get_md5(self.generate_random_string())[:12]
        save_path = os.path.join(self.cache_dir, f'{img_id}.png')
        w, h = img.size
        if w > 2048:
            ratio = w / 2048.
            w = 2048
            h = int(h / ratio)
        if h > 2048:
            ratio = h / 2048.
            h = 2048
            w = int(w / ratio)
        img = img.resize((w, h))
        img.save(save_path)

        w, h = img.size
        if w > h:
            tb_w = 128
            tb_h = int(h * tb_w / w)
        else:
            tb_h = 128
            tb_w = int(w * tb_h / h)
        thumbnail_path = os.path.join(self.cache_dir,
                                      f'{img_id}_thumbnail.jpg')
        thumbnail = img.resize((tb_w, tb_h))
        thumbnail.save(thumbnail_path, format='JPEG')

        images[img_id] = {
            'image': save_path,
            'mask': None,
            'mask_type': None,
            'thumbnail': thumbnail_path
        }

        buffered = io.BytesIO()
        img.convert('RGB').save(buffered, format='PNG')
        img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        img_str = f'<img src="data:image/png;base64,{img_b64}" style="pointer-events: none;">'

        history.append(
            (None,
             f'This is uploaded image:\n {img_str} image ID is: {img_id}'))
        return history, images, img_id



if __name__ == '__main__':
    cfg = "config/chatbot_ui.yaml"
    with gr.Blocks() as demo:
        chatbot = ChatBotUI(cfg)
        chatbot.create_ui()
        chatbot.set_callbacks()
    demo.launch()