File size: 83,906 Bytes
358e1b5
bc2ae02
7cff785
6dd859c
faf797f
 
6dd859c
faf797f
 
80b040e
faf797f
ab4cf94
cbf9ae2
ab4cf94
cbf9ae2
 
5add6fe
74c812c
7cff785
faf797f
 
 
f97739f
13dff50
f97739f
71656cf
6dd859c
5add6fe
cbf9ae2
5add6fe
cbf9ae2
 
5add6fe
cbf9ae2
 
 
 
 
faf797f
 
 
 
74c812c
faf797f
74c812c
faf797f
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
ab4cf94
80b040e
74c812c
f97739f
0808774
ced6a2a
74c812c
 
cbf9ae2
5add6fe
cbf9ae2
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
ab4cf94
 
 
13dff50
cbf9ae2
6dd859c
74c812c
6dd859c
 
74c812c
 
 
 
 
 
 
 
 
 
 
 
cbf9ae2
6dd859c
 
 
 
74c812c
 
6dd859c
 
13dff50
 
 
 
 
 
7cff785
 
cbf9ae2
 
faf797f
74c812c
 
cbf9ae2
 
 
 
faf797f
 
 
 
 
 
 
 
 
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab4cf94
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
74c812c
 
6dd859c
 
 
 
 
74c812c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e7b045
 
 
74c812c
8e7b045
 
ab4cf94
8e7b045
 
 
 
 
 
 
 
 
 
 
 
 
 
74c812c
8e7b045
 
 
 
 
 
 
 
 
 
 
 
74c812c
8e7b045
74c812c
8e7b045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dd859c
8e7b045
 
 
74c812c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e7b045
 
74c812c
8e7b045
74c812c
 
 
8e7b045
 
 
 
 
 
74c812c
8e7b045
 
 
 
 
 
 
 
 
 
 
 
 
74c812c
8e7b045
 
 
74c812c
eb4b77d
8e7b045
 
 
 
 
 
 
74c812c
 
8e7b045
 
 
 
 
 
 
 
 
74c812c
8e7b045
 
74c812c
8e7b045
 
 
 
 
6dd859c
 
 
 
 
 
 
 
74c812c
6dd859c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb4b77d
6dd859c
 
 
 
 
 
 
 
 
 
 
 
8e7b045
74c812c
 
 
 
 
 
8e7b045
6dd859c
 
 
 
 
74c812c
6dd859c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358e1b5
6dd859c
 
2840c72
74c812c
faf797f
6dd859c
cbf9ae2
 
 
 
da0a17c
cbf9ae2
 
 
 
ab4cf94
cbf9ae2
 
da0a17c
 
74c812c
 
 
 
cbf9ae2
 
 
 
 
74c812c
ab4cf94
 
 
74c812c
 
 
 
 
 
cbf9ae2
ab4cf94
cbf9ae2
 
 
 
 
 
74c812c
ab4cf94
5add6fe
7a9500a
ab4cf94
 
cbf9ae2
 
 
 
 
 
 
da0a17c
cbf9ae2
 
 
 
 
da0a17c
cbf9ae2
 
 
13dff50
cbf9ae2
da0a17c
5add6fe
cbf9ae2
 
 
 
 
 
 
5add6fe
cbf9ae2
 
5add6fe
74c812c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbf9ae2
 
 
 
eb4b77d
 
cbf9ae2
 
5add6fe
cbf9ae2
 
 
 
 
5add6fe
375c110
cbf9ae2
 
 
 
6dd859c
e34b08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf797f
f97739f
 
faf797f
 
 
84268f6
faf797f
84268f6
faf797f
84268f6
faf797f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e34b08b
faf797f
 
 
 
 
 
 
 
f97739f
faf797f
 
 
 
 
 
 
 
 
f97739f
 
faf797f
 
 
84268f6
7cff785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf797f
 
 
 
 
 
 
 
 
84268f6
faf797f
f97739f
faf797f
 
 
71656cf
faf797f
 
7cff785
375c110
7cff785
 
 
f97739f
7cff785
375c110
faf797f
 
375c110
7cff785
faf797f
375c110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc2ae02
375c110
 
 
 
 
 
 
 
 
bc2ae02
375c110
 
 
 
 
 
 
 
 
bc2ae02
 
 
 
 
7cff785
 
 
 
 
 
bc2ae02
faf797f
 
 
375c110
 
 
 
 
 
 
faf797f
7cff785
 
 
 
 
 
faf797f
375c110
 
 
faf797f
 
7cff785
faf797f
 
 
 
 
 
 
84268f6
faf797f
84268f6
 
faf797f
 
 
f97739f
84268f6
faf797f
84268f6
faf797f
 
 
 
 
375c110
faf797f
375c110
faf797f
13dff50
faf797f
 
 
 
84268f6
faf797f
 
84268f6
faf797f
 
 
 
 
 
 
375c110
faf797f
375c110
faf797f
f97739f
 
ced6a2a
6dd859c
 
80b040e
358e1b5
f97739f
80b040e
 
 
 
5add6fe
04daaa1
5add6fe
80b040e
74c812c
80b040e
 
 
 
 
 
 
35bcea0
80b040e
35bcea0
 
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab4cf94
80b040e
faf797f
80b040e
 
 
 
 
 
 
 
e34b08b
74c812c
 
80b040e
e34b08b
 
 
 
 
 
 
 
74c812c
e34b08b
 
 
 
 
74c812c
 
e34b08b
 
 
74c812c
e34b08b
 
74c812c
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74c812c
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
527fcc4
e34b08b
80b040e
eb4b77d
80b040e
 
 
eb4b77d
80b040e
 
 
 
 
 
 
 
 
74c812c
80b040e
 
74c812c
 
 
 
 
 
 
 
 
 
 
80b040e
ab4cf94
 
71656cf
ced6a2a
71656cf
 
 
 
80b040e
 
 
 
f97739f
7a9500a
80b040e
 
 
 
 
 
ced6a2a
7a9500a
ced6a2a
80b040e
 
 
 
 
 
cbf9ae2
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da0a17c
80b040e
ab4cf94
ced6a2a
80b040e
da0a17c
80b040e
358e1b5
 
74c812c
 
80b040e
7cff785
80b040e
 
 
 
ced6a2a
80b040e
da0a17c
80b040e
 
 
 
 
cbf9ae2
1c1d0c5
 
 
 
cbf9ae2
1c1d0c5
 
cbf9ae2
80b040e
74c812c
cbf9ae2
80b040e
 
 
 
 
74c812c
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
1c1d0c5
 
80b040e
 
 
 
1c1d0c5
80b040e
 
 
1c1d0c5
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bddd9f
cbf9ae2
faf797f
cbf9ae2
375c110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf797f
 
7cff785
 
527fcc4
faf797f
 
 
 
 
 
 
 
 
 
375c110
 
faf797f
375c110
 
 
 
 
 
 
faf797f
 
7cff785
 
80b040e
 
 
 
 
 
 
 
 
 
8ee01d1
 
80b040e
 
 
faf797f
 
 
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
74c812c
da0a17c
e34b08b
 
 
 
80b040e
e34b08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74c812c
 
 
 
 
 
 
 
80b040e
 
 
 
 
ab4cf94
80b040e
 
 
74c812c
da0a17c
e34b08b
 
 
 
80b040e
ab4cf94
80b040e
 
 
 
 
 
 
 
 
 
 
 
e34b08b
 
 
 
80b040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf797f
 
 
 
 
 
 
 
 
 
7cff785
faf797f
7cff785
 
 
 
375c110
faf797f
 
 
 
 
 
 
 
 
 
 
375c110
faf797f
 
375c110
faf797f
 
 
 
 
375c110
faf797f
 
375c110
faf797f
6dd859c
358e1b5
ced6a2a
358e1b5
 
 
 
71656cf
 
 
 
 
358e1b5
71656cf
 
 
 
faf797f
 
 
 
74c812c
 
 
 
 
 
7cff785
 
faf797f
74c812c
faf797f
74c812c
faf797f
 
ced6a2a
faf797f
358e1b5
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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
import gradio as gr
import spaces
import os
import numpy as np
os.environ['SPCONV_ALGO'] = 'native'
from typing import *
import torch
import imageio
import shutil
from PIL import Image, ImageFilter
from easydict import EasyDict as edict
import utils.constants as constants
from haishoku.haishoku import Haishoku

from tempfile import NamedTemporaryFile
import atexit
import random
import accelerate 
from transformers import AutoTokenizer, DPTImageProcessor, DPTForDepthEstimation
from trellis.pipelines import TrellisImageTo3DPipeline
from trellis.representations import Gaussian, MeshExtractResult
from trellis.utils import render_utils, postprocessing_utils
from pathlib import Path

import logging
#logging.getLogger("transformers.modeling_utils").setLevel(logging.ERROR)
import gc

IS_SHARED_SPACE = constants.IS_SHARED_SPACE

# Import functions from modules
from utils.file_utils import cleanup_temp_files

from utils.color_utils import (
    hex_to_rgb,
    detect_color_format,
    update_color_opacity,
)
from utils.misc import (
    get_filename, 
    pause, 
    convert_ratio_to_dimensions,
    update_dimensions_on_ratio,
    get_seed,
    get_output_name    
    ) #install_cuda_toolkit,install_torch, _get_output, setup_runtime_env)

from utils.image_utils import (
    change_color,
    open_image,
    upscale_image,
    lerp_imagemath,
    shrink_and_paste_on_blank,
    show_lut,
    apply_lut_to_image_path,
    multiply_and_blend_images,
    alpha_composite_with_control,
    crop_and_resize_image,
    resize_and_crop_image,
    convert_to_rgba_png,
    resize_image_with_aspect_ratio,
    build_prerendered_images_by_quality,
    get_image_from_dict,
    calculate_optimal_fill_dimensions
)

from utils.hex_grid import (
    generate_hexagon_grid,
    generate_hexagon_grid_interface,
)

from utils.excluded_colors import (
    add_color,
    delete_color,
    build_dataframe,
    on_input,
    excluded_color_list,
    on_color_display_select
)

# from utils.ai_generator import (
#     generate_ai_image,
# )

from utils.lora_details import (
    upd_prompt_notes,
    upd_prompt_notes_by_index,
    split_prompt_precisely,
    approximate_token_count,
    get_trigger_words,
    is_lora_loaded,
    get_lora_models
)
from diffusers import (
    FluxPipeline,
    FluxImg2ImgPipeline,
    FluxControlPipeline,
    FluxControlPipeline,
    DiffusionPipeline, 
    AutoencoderTiny, 
    AutoencoderKL
)

PIPELINE_CLASSES = {
    "FluxPipeline": FluxPipeline,
    "FluxImg2ImgPipeline": FluxImg2ImgPipeline,
    "FluxControlPipeline": FluxControlPipeline,
    "FluxFillPipeline": FluxControlPipeline
}

from utils.version_info import (
    versions_html,
    #initialize_cuda,
    #release_torch_resources,
    #get_torch_info
)
#from utils.depth_estimation import (get_depth_map_from_state)

input_image_palette = []
current_prerendered_image = gr.State("./images/images/Beeuty-1.png")
user_dir = constants.TMPDIR
lora_models = get_lora_models()
selected_index = gr.State(value=-1)

# Register the cleanup function
atexit.register(cleanup_temp_files)

def start_session(req: gr.Request):
    user_dir = os.path.join(constants.TMPDIR, str(req.session_hash))
    os.makedirs(user_dir, exist_ok=True)
    
    
def end_session(req: gr.Request):
    user_dir = os.path.join(constants.TMPDIR, str(req.session_hash))
    shutil.rmtree(user_dir)

def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
    global input_image_palette

    try:
        # Load and process the input image
        input_image = Image.open(input_image_path).convert("RGBA")
    except Exception as e:
        print(f"Failed to convert image to RGBA: {e}")
        # Open the original image without conversion
        input_image = Image.open(input_image_path)
        # Ensure the canvas is at least 1344x768 pixels
        min_width, min_height = 1344, 768
        canvas_width = max(min_width, input_image.width)
        canvas_height = max(min_height, input_image.height)

        # Create a transparent canvas with the required dimensions
        new_canvas = Image.new("RGBA", (canvas_width, canvas_height), (0, 0, 0, 0))

        # Calculate position to center the input image on the canvas
        paste_x = (canvas_width - input_image.width) // 2
        paste_y = (canvas_height - input_image.height) // 2

        # Paste the input image onto the canvas
        new_canvas.paste(input_image, (paste_x, paste_y))

        # Save the 'RGBA' image to a temporary file and update 'input_image_path'
        with NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
            new_canvas.save(tmp_file.name, format="PNG")
            input_image_path = tmp_file.name
            constants.temp_files.append(tmp_file.name)

        # Update 'input_image' with the new image as a file path
        input_image = Image.open(input_image_path)
    
    # Use Haishoku to get the palette from the new image
    input_palette = Haishoku.loadHaishoku(input_image_path)
    input_image_palette = input_palette.palette

    # Update colors with opacity
    background_color = update_color_opacity(
        hex_to_rgb(background_color_hex),
        int(background_opacity * (255 / 100))
    )
    border_color = update_color_opacity(
        hex_to_rgb(border_color_hex),
        int(border_opacity * (255 / 100))
    )

    # Prepare excluded colors list
    excluded_color_list = [tuple(lst) for lst in excluded_colors_var]

    # Generate the hexagon grid images
    grid_image = generate_hexagon_grid_interface(
        hex_size,
        border_size,
        input_image,
        start_x,
        start_y,
        end_x,
        end_y,
        rotation,
        background_color,
        border_color,
        fill_hex,
        excluded_color_list,
        filter_color,
        x_spacing,
        y_spacing,
        add_hex_text_option,
        custom_text_list,
        custom_text_color_list
    )    
    return grid_image

def get_model_and_lora(model_textbox):
    """
    Determines the model and LoRA weights based on the model_textbox input.
    wieghts must be in an array ["Borcherding/FLUX.1-dev-LoRA-FractalLand-v0.1"]
    """
    # If the input is in the list of models, return it with None as LoRA weights
    if model_textbox in constants.MODELS:
        return model_textbox, []
    # If the input is in the list of LoRA weights, get the corresponding model
    elif model_textbox in constants.LORA_WEIGHTS:
        model = constants.LORA_TO_MODEL.get(model_textbox)
        return model, model_textbox.split()
    else:
        # Default to a known model if input is unrecognized
        default_model = model_textbox
        return default_model, []

@torch.inference_mode()
def set_pipeline(
    model_name="black-forest-labs/FLUX.1-dev",
    lora_weights=None,
    pipeline_name="FluxPipeline",
    progress=gr.Progress(track_tqdm=True)
):  
    global pipe
    if pipe.name_or_path != model_name:
        del pipe
        #from torch import cuda, bfloat16, float32, Generator, no_grad, backends
        # Retrieve the pipeline class from the mapping
        pipeline_class = PIPELINE_CLASSES.get(pipeline_name)
        if not pipeline_class:
            raise ValueError(f"Unsupported pipeline type '{pipeline_name}'. "
                            f"Available options: {list(PIPELINE_CLASSES.keys())}")

        #initialize_cuda()
        device = "cuda" if torch.cuda.is_available() else "cpu"
        #from src.condition import Condition

        print(f"device:{device}\nmodel_name:{model_name}\nlora_weights:{lora_weights}\n")
        #print(f"\n {get_torch_info()}\n")
        # Initialize the pipeline inside the context manager
        pipe = pipeline_class.from_pretrained(
            model_name,
            torch_dtype=torch.bfloat16 if device == "cuda" else torch.float16
        ).to(device)
        # Optionally, don't use CPU offload if not necessary
        
        # alternative version that may be more efficient
        # pipe.enable_sequential_cpu_offload()
        if pipeline_name == "FluxPipeline":
            pipe.enable_model_cpu_offload()
            pipe.vae.enable_slicing()
            #pipe.vae.enable_tiling()
        else:
            pipe.enable_model_cpu_offload()

        # Access the tokenizer from the pipeline
        tokenizer = pipe.tokenizer

        # Check if add_prefix_space is set and convert to slow tokenizer if necessary
        if getattr(tokenizer, 'add_prefix_space', False):
            tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, device_map = 'cpu')
            # Update the pipeline's tokenizer
            pipe.tokenizer = tokenizer
            pipe.to(device)

        flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
        if flash_attention_enabled == False:
            #Enable xFormers memory-efficient attention (optional)
            #pipe.enable_xformers_memory_efficient_attention()
            print("\nEnabled xFormers memory-efficient attention.\n")
        else:            
            pipe.attn_implementation="flash_attention_2"
            print("\nEnabled flash_attention_2.\n")
    if not is_lora_loaded(pipe, lora_weights): 
        # Load LoRA weights
        # note: does not yet handle multiple LoRA weights with different names, needs .set_adapters(["depth", "hyper-sd"], adapter_weights=[0.85, 0.125])
        if lora_weights:
            for lora_weight in lora_weights:
                lora_configs = constants.LORA_DETAILS.get(lora_weight, [])
                lora_weight_set = False
                if lora_configs:
                    for config in lora_configs:
                        # Load LoRA weights with optional weight_name and adapter_name
                        if 'weight_name' in config:
                            weight_name = config.get("weight_name")
                            adapter_name = config.get("adapter_name")
                            lora_collection = config.get("lora_collection")
                            if weight_name and adapter_name and lora_collection and lora_weight_set == False:
                                pipe.load_lora_weights(
                                    lora_collection,
                                    weight_name=weight_name,
                                    adapter_name=adapter_name,
                                    token=constants.HF_API_TOKEN
                                )
                                lora_weight_set = True
                                print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}, lora_collection={lora_collection}\n")  
                            elif weight_name and adapter_name==None and lora_collection and lora_weight_set == False:
                                pipe.load_lora_weights(
                                    lora_collection,
                                    weight_name=weight_name,
                                    token=constants.HF_API_TOKEN
                                )
                                lora_weight_set = True
                                print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}, lora_collection={lora_collection}\n")  
                            elif weight_name and adapter_name and lora_weight_set == False:
                                pipe.load_lora_weights(
                                    lora_weight,
                                    weight_name=weight_name,
                                    adapter_name=adapter_name,
                                    token=constants.HF_API_TOKEN
                                )
                                lora_weight_set = True
                                print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
                            elif weight_name and adapter_name==None and lora_weight_set == False:
                                pipe.load_lora_weights(
                                    lora_weight,
                                    weight_name=weight_name,
                                    token=constants.HF_API_TOKEN
                                )
                                lora_weight_set = True
                                print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
                            elif lora_weight_set == False:
                                pipe.load_lora_weights(
                                    lora_weight,
                                    token=constants.HF_API_TOKEN
                                )  
                                lora_weight_set = True
                                print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
                        # Apply 'pipe' configurations if present
                        if 'pipe' in config:
                            pipe_config = config['pipe']
                            for method_name, params in pipe_config.items():
                                method = getattr(pipe, method_name, None)
                                if method:
                                    print(f"Applying pipe method: {method_name} with params: {params}")
                                    method(**params)
                                else:
                                    print(f"Method {method_name} not found in pipe.")
                        if 'condition_type' in config:
                            condition_type = config['condition_type']
                            if condition_type == "coloring":
                                #pipe.enable_coloring()
                                print("\nEnabled coloring.\n")
                            elif condition_type == "deblurring":
                                #pipe.enable_deblurring()
                                print("\nEnabled deblurring.\n")
                            elif condition_type == "fill":
                                #pipe.enable_fill()
                                print("\nEnabled fill.\n")
                            elif condition_type == "depth":
                                #pipe.enable_depth()
                                print("\nEnabled depth.\n")
                            elif condition_type == "canny":
                                #pipe.enable_canny()
                                print("\nEnabled canny.\n")
                            elif condition_type == "subject":
                                #pipe.enable_subject()
                                print("\nEnabled subject.\n")
                            else:
                                print(f"Condition type {condition_type} not implemented.")
                else:
                    pipe.load_lora_weights(lora_weight, use_auth_token=constants.HF_API_TOKEN)


@spaces.GPU(duration=200, progress=gr.Progress(track_tqdm=True))
def generate_image_lowmem(
    text,
    neg_prompt=None,
    model_name="black-forest-labs/FLUX.1-dev",
    lora_weights=None,
    conditioned_image=None,
    mask_image=None,
    image_width=1368,
    image_height=848,
    guidance_scale=3.5,
    num_inference_steps=30,
    seed=0,
    true_cfg_scale=1.0,
    pipeline_name="FluxPipeline",
    strength=0.75,
    additional_parameters=None,
    progress=gr.Progress(track_tqdm=True)
):  
#     #from torch import cuda, bfloat16, float32, Generator, no_grad, backends
#     # Retrieve the pipeline class from the mapping
#     pipeline_class = PIPELINE_CLASSES.get(pipeline_name)
#     if not pipeline_class:
#         raise ValueError(f"Unsupported pipeline type '{pipeline_name}'. "
#                         f"Available options: {list(PIPELINE_CLASSES.keys())}")

#     #initialize_cuda()
#     device = "cuda" if torch.cuda.is_available() else "cpu"
#     #from src.condition import Condition

#     print(f"device:{device}\nmodel_name:{model_name}\nlora_weights:{lora_weights}\n")
#     #print(f"\n {get_torch_info()}\n")
#     # Disable gradient calculations
#    with torch.no_grad():
#         # Initialize the pipeline inside the context manager
#         pipe = pipeline_class.from_pretrained(
#             model_name,
#             torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
#         ).to(device)
#         # Optionally, don't use CPU offload if not necessary
        
#         # alternative version that may be more efficient
#         # pipe.enable_sequential_cpu_offload()
#         if pipeline_name == "FluxPipeline":
#             pipe.enable_model_cpu_offload()
#             pipe.vae.enable_slicing()
#             #pipe.vae.enable_tiling()
#         else:
#             pipe.enable_model_cpu_offload()

#         # Access the tokenizer from the pipeline
#         tokenizer = pipe.tokenizer

#         # Check if add_prefix_space is set and convert to slow tokenizer if necessary
#         if getattr(tokenizer, 'add_prefix_space', False):
#             tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, device_map = 'cpu')
#             # Update the pipeline's tokenizer
#             pipe.tokenizer = tokenizer
#             pipe.to(device)

#         flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
#         if flash_attention_enabled == False:
#             #Enable xFormers memory-efficient attention (optional)
#             #pipe.enable_xformers_memory_efficient_attention()
#             print("\nEnabled xFormers memory-efficient attention.\n")
#         else:            
#             pipe.attn_implementation="flash_attention_2"
#             print("\nEnabled flash_attention_2.\n")

#         # Load LoRA weights
#         # note: does not yet handle multiple LoRA weights with different names, needs .set_adapters(["depth", "hyper-sd"], adapter_weights=[0.85, 0.125])
#         if lora_weights:
#             for lora_weight in lora_weights:
#                 lora_configs = constants.LORA_DETAILS.get(lora_weight, [])
#                 lora_weight_set = False
#                 if lora_configs:
#                     for config in lora_configs:
#                         # Load LoRA weights with optional weight_name and adapter_name
#                         if 'weight_name' in config:
#                             weight_name = config.get("weight_name")
#                             adapter_name = config.get("adapter_name")
#                             lora_collection = config.get("lora_collection")
#                             if weight_name and adapter_name and lora_collection and lora_weight_set == False:
#                                 pipe.load_lora_weights(
#                                     lora_collection,
#                                     weight_name=weight_name,
#                                     adapter_name=adapter_name,
#                                     token=constants.HF_API_TOKEN
#                                 )
#                                 lora_weight_set = True
#                                 print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}, lora_collection={lora_collection}\n")  
#                             elif weight_name and adapter_name==None and lora_collection and lora_weight_set == False:
#                                 pipe.load_lora_weights(
#                                     lora_collection,
#                                     weight_name=weight_name,
#                                     token=constants.HF_API_TOKEN
#                                 )
#                                 lora_weight_set = True
#                                 print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}, lora_collection={lora_collection}\n")  
#                             elif weight_name and adapter_name and lora_weight_set == False:
#                                 pipe.load_lora_weights(
#                                     lora_weight,
#                                     weight_name=weight_name,
#                                     adapter_name=adapter_name,
#                                     token=constants.HF_API_TOKEN
#                                 )
#                                 lora_weight_set = True
#                                 print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
#                             elif weight_name and adapter_name==None and lora_weight_set == False:
#                                 pipe.load_lora_weights(
#                                     lora_weight,
#                                     weight_name=weight_name,
#                                     token=constants.HF_API_TOKEN
#                                 )
#                                 lora_weight_set = True
#                                 print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
#                             elif lora_weight_set == False:
#                                 pipe.load_lora_weights(
#                                     lora_weight,
#                                     token=constants.HF_API_TOKEN
#                                 )  
#                                 lora_weight_set = True
#                                 print(f"\npipe.load_lora_weights({lora_weight}, weight_name={weight_name}, adapter_name={adapter_name}\n")  
#                         # Apply 'pipe' configurations if present
#                         if 'pipe' in config:
#                             pipe_config = config['pipe']
#                             for method_name, params in pipe_config.items():
#                                 method = getattr(pipe, method_name, None)
#                                 if method:
#                                     print(f"Applying pipe method: {method_name} with params: {params}")
#                                     method(**params)
#                                 else:
#                                     print(f"Method {method_name} not found in pipe.")
#                         if 'condition_type' in config:
#                             condition_type = config['condition_type']
#                             if condition_type == "coloring":
#                                 #pipe.enable_coloring()
#                                 print("\nEnabled coloring.\n")
#                             elif condition_type == "deblurring":
#                                 #pipe.enable_deblurring()
#                                 print("\nEnabled deblurring.\n")
#                             elif condition_type == "fill":
#                                 #pipe.enable_fill()
#                                 print("\nEnabled fill.\n")
#                             elif condition_type == "depth":
#                                 #pipe.enable_depth()
#                                 print("\nEnabled depth.\n")
#                             elif condition_type == "canny":
#                                 #pipe.enable_canny()
#                                 print("\nEnabled canny.\n")
#                             elif condition_type == "subject":
#                                 #pipe.enable_subject()
#                                 print("\nEnabled subject.\n")
#                             else:
#                                 print(f"Condition type {condition_type} not implemented.")
#                 else:
#                     pipe.load_lora_weights(lora_weight, use_auth_token=constants.HF_API_TOKEN)
    with torch.no_grad():
        global pipe
        mask_parameters = {}
        # Load the mask image if provided
        if (pipeline_name == "FluxFillPipeline"):
            mask_image = open_image(mask_image).convert("RGBA")
            mask_condition_type = constants.condition_type[5]
            guidance_scale = 30
            num_inference_steps=50
            max_sequence_length=512
            print(f"\nAdded mask image.\n {mask_image.size}")
            mask_parameters ={
                "mask_image": mask_image,
            }

        # Set the random seed for reproducibility
        generator = torch.Generator(device=device).manual_seed(seed)
        #conditions = []
        if conditioned_image is not None:
            conditioned_image = resize_and_crop_image(conditioned_image, image_width, image_height)
            #condition = Condition(constants.condition_type[2], conditioned_image)
            #conditions.append(condition)
            print(f"\nAdded conditioned image.\n {conditioned_image.size}")
            # Prepare the parameters for image generation
            additional_parameters ={
                "strength": strength,
                "image": conditioned_image,
            }
            additional_parameters.update(mask_parameters)
        else:
            print("\nNo conditioned image provided.")
            if neg_prompt!=None:
                true_cfg_scale=1.1
            additional_parameters ={
                "negative_prompt": neg_prompt,
                "true_cfg_scale": true_cfg_scale,
            }
        # handle long prompts by splitting them
        if approximate_token_count(text) > 76:
            prompt, prompt2 = split_prompt_precisely(text)
            prompt_parameters = {
                "prompt" : prompt,
                "prompt_2": prompt2,
            }
        else:
            prompt_parameters = {
                "prompt" :text,
        }
        additional_parameters.update(prompt_parameters)
        # Combine all parameters
        generate_params = {
            "height": image_height,
            "width": image_width,
            "guidance_scale": guidance_scale,
            "num_inference_steps": num_inference_steps,
            "generator": generator,
        }
        if additional_parameters:
            generate_params.update(additional_parameters)
        generate_params = {k: v for k, v in generate_params.items() if v is not None}
        print(f"generate_params: {generate_params}")
        # Generate the image
        result = pipe(**generate_params) #generate_image(pipe,generate_params)
        image = result.images[0]
        # Clean up
        del result
        #del conditions
        del generator
    # Delete the pipeline and clear cache
    #del pipe
    torch.cuda.empty_cache()
    torch.cuda.ipc_collect()
    print(torch.cuda.memory_summary(device=None, abbreviated=False))
    
    return image

def generate_ai_image_local (
    map_option,
    prompt_textbox_value,
    neg_prompt_textbox_value,
    model="black-forest-labs/FLUX.1-dev",
    lora_weights=None,
    conditioned_image=None,
    mask_image=None,
    height=512,
    width=912,
    num_inference_steps=30,
    guidance_scale=3.5,
    seed=777,
    pipeline_name="FluxPipeline",
    strength=0.75,
    progress=gr.Progress(track_tqdm=True)
):        
    print(f"Generating image with lowmem")
    try:
        if map_option != "Prompt":
            prompt = constants.PROMPTS[map_option]
            negative_prompt = constants.NEGATIVE_PROMPTS.get(map_option, "")
        else:
            prompt = prompt_textbox_value
            negative_prompt = neg_prompt_textbox_value or ""
        #full_prompt = f"{prompt} {negative_prompt}"
        additional_parameters = {}
        if lora_weights:
            for lora_weight in lora_weights:
                lora_configs = constants.LORA_DETAILS.get(lora_weight, [])
                for config in lora_configs:
                    if 'parameters' in config:
                        additional_parameters.update(config['parameters'])
                    elif 'trigger_words' in config:
                        trigger_words = get_trigger_words(lora_weight)
                        prompt = f"{trigger_words} {prompt}"
        for key, value in additional_parameters.items():
            if key in ['height', 'width', 'num_inference_steps', 'max_sequence_length']:
                additional_parameters[key] = int(value)
            elif key in ['guidance_scale','true_cfg_scale']:
                additional_parameters[key] = float(value)
        height = additional_parameters.pop('height', height)
        width = additional_parameters.pop('width', width)
        num_inference_steps = additional_parameters.pop('num_inference_steps', num_inference_steps)
        guidance_scale = additional_parameters.pop('guidance_scale', guidance_scale)
        print("Generating image with the following parameters:\n")
        print(f"Model: {model}")
        print(f"LoRA Weights: {lora_weights}")
        print(f"Prompt: {prompt}")
        print(f"Neg Prompt: {negative_prompt}")
        print(f"Height: {height}")
        print(f"Width: {width}")
        print(f"Number of Inference Steps: {num_inference_steps}")
        print(f"Guidance Scale: {guidance_scale}")
        print(f"Seed: {seed}")
        print(f"Additional Parameters: {additional_parameters}")
        print(f"Conditioned Image: {conditioned_image}")
        print(f"Conditioned Image Strength: {strength}")
        print(f"pipeline: {pipeline_name}")
        set_pipeline(
            model_name=model,
            lora_weights=lora_weights,
            pipeline_name=pipeline_name,
            progress=progress
        )
        image = generate_image_lowmem(
            text=prompt,
            model_name=model,
            neg_prompt=negative_prompt,
            lora_weights=lora_weights,
            conditioned_image=conditioned_image,
            mask_image=mask_image,
            image_width=width,
            image_height=height,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            seed=seed,
            pipeline_name=pipeline_name,
            strength=strength,
            additional_parameters=additional_parameters
        )
        with NamedTemporaryFile(delete=False, suffix=".png") as tmp:
            image.save(tmp.name, format="PNG")
            constants.temp_files.append(tmp.name)
            print(f"Image saved to {tmp.name}")
            return tmp.name
    except Exception as e:
        print(f"Error generating AI image: {e}")
        #gc.collect()
        return None


def generate_input_image_click(image_input, map_option, prompt_textbox_value, negative_prompt_textbox_value, model_textbox_value, randomize_seed=True, seed=None, use_conditioned_image=False, mask_image=None, strength=0.5, image_format="16:9", scale_factor=constants.SCALE_FACTOR, progress=gr.Progress(track_tqdm=True)):
    seed = get_seed(randomize_seed, seed)

    # Get the model and LoRA weights
    model, lora_weights = get_model_and_lora(model_textbox_value)
    global current_prerendered_image
    conditioned_image=None
    formatted_map_option = map_option.lower().replace(' ', '_')

    if use_conditioned_image:
        print(f"Conditioned path: {current_prerendered_image.value}.. converting to RGB\n")
        # ensure the conditioned image is an image and not a string, cannot use RGBA
        if isinstance(current_prerendered_image.value, str):
            conditioned_image = open_image(current_prerendered_image.value).convert("RGB")
            print(f"Conditioned Image: {conditioned_image.size}.. converted to RGB\n")
    # use image_input as the conditioned_image if it is not None
    elif image_input is not None:
        file_path, is_dict = get_image_from_dict(image_input)
        conditioned_image =  open_image(file_path).convert("RGB")
        print(f"Conditioned Image set to modify Input Image!\nClear to generate new image from layered image: {is_dict}\n")
        gr.Info(f"Conditioned Image set to modify Input Image! Clear to generate new image. Layered: {is_dict}",duration=5)

    # Convert image_format from a string split by ":" into two numbers divided
    width_ratio, height_ratio = map(int, image_format.split(":"))
    aspect_ratio = width_ratio / height_ratio
    
    width, height = convert_ratio_to_dimensions(aspect_ratio, constants.BASE_HEIGHT)
    pipeline = "FluxPipeline"
    if conditioned_image is not None:
        pipeline = "FluxImg2ImgPipeline"

    if (model == "black-forest-labs/FLUX.1-Fill-dev"):
        pipeline = "FluxFillPipeline"
        width, height = calculate_optimal_fill_dimensions(conditioned_image)
        print(f"Optimal Dimensions: {width} x {height} \n")

    # Generate the AI image and get the image path
    image_path = generate_ai_image_local(
        map_option,
        prompt_textbox_value,
        negative_prompt_textbox_value,
        model,
        lora_weights,
        conditioned_image,
        mask_image,
        strength=strength,
        height=height,
        width=width,
        seed=seed,
        pipeline_name=pipeline,
    )
       
    # Open the generated image
    try:
        image = Image.open(image_path).convert("RGBA")
    except Exception as e:
        print(f"Failed to open generated image: {e}")
        return image_path, seed  # Return the original image path if opening fails
       
    # Upscale the image
    upscaled_image = upscale_image(image, scale_factor)
       
    # Save the upscaled image to a temporary file
    with NamedTemporaryFile(delete=False, suffix=".png", prefix=f"{formatted_map_option}_") as tmp_upscaled:
        upscaled_image.save(tmp_upscaled.name, format="PNG")
        constants.temp_files.append(tmp_upscaled.name)
        print(f"Upscaled image saved to {tmp_upscaled.name}")
    gc.collect()
    # Return the path of the upscaled image
    return tmp_upscaled.name, seed

def update_prompt_visibility(map_option):
      is_visible = (map_option == "Prompt")
      return (
          gr.update(visible=is_visible),
          gr.update(visible=is_visible),
          gr.update(visible=is_visible)
      )

def update_prompt_notes(model_textbox_value):
    return upd_prompt_notes(model_textbox_value)

def update_selection(evt: gr.SelectData, aspect_ratio):
    selected_lora = constants.LORAS[evt.index]
    new_placeholder = f"Type a prompt for {selected_lora['title']}"
    new_aspect_ratio = aspect_ratio  # default to the currently selected aspect ratio
    lora_repo = selected_lora["repo"]
    #updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✅"
    # If the selected LoRA model specifies an aspect ratio, use it to update dimensions.
    if "aspect" in selected_lora:
        try:
            new_aspect_ratio = selected_lora["aspect"]
            # Recalculate dimensions using constants.BASE_HEIGHT as the height reference.
            new_width, new_height = update_dimensions_on_ratio(new_aspect_ratio, constants.BASE_HEIGHT)
            # (Optionally, you could log or use new_width/new_height as needed)
        except Exception as e:
            print(f"\nError in update selection aspect ratios: {e}\nSkipping")
    return [gr.update(value=lora_repo), gr.update(value=lora_repo), evt.index, new_aspect_ratio, upd_prompt_notes_by_index(evt.index)]

def on_prerendered_gallery_selection(event_data: gr.SelectData):
    global current_prerendered_image
    selected_index = event_data.index
    selected_image = constants.pre_rendered_maps_paths[selected_index]
    print(f"Template Image Selected: {selected_image} ({event_data.index})\n")
    gr.Info(f"Template Image Selected: {selected_image} ({event_data.index})",duration=5)
    current_prerendered_image.value = selected_image
    return current_prerendered_image

def combine_images_with_lerp(input_image, output_image, alpha):
    in_image = open_image(input_image)
    out_image = open_image(output_image)
    print(f"Combining images with alpha: {alpha}")
    return lerp_imagemath(in_image, out_image, alpha)

def add_border(image, mask_width, mask_height, blank_color):
    bordered_image_output = Image.open(image).convert("RGBA")
    margin_color = detect_color_format(blank_color)
    print(f"Adding border to image with width: {mask_width}, height: {mask_height}, color: {margin_color}")
    return shrink_and_paste_on_blank(bordered_image_output, mask_width, mask_height, margin_color)

def on_input_image_change(image_path):
    if image_path is None:
        gr.Warning("Please upload an Input Image to get started.")
        return None, gr.update()
    img, img_path = convert_to_rgba_png(image_path)
    with Image.open(img_path) as pil_img:
        width, height = pil_img.size
    return [img_path, gr.update(width=width, height=height)]

def update_sketch_dimensions(input_image, sketch_image):
    # Load the images using open_image() if they are provided as file paths.
    in_img = open_image(input_image) if isinstance(input_image, str) else input_image
    sk_img_path, _ = get_image_from_dict(sketch_image)
    sk_img = open_image(sk_img_path)
    # Resize sketch image if dimensions don't match input image.
    if in_img.size != sk_img.size:
        sk_img = sk_img.resize(in_img.size, Image.LANCZOS)
    return [sk_img, gr.update(width=in_img.width, height=in_img.height)]

def composite_with_control_sync(input_image, sketch_image, slider_value):
    # Load the images using open_image() if they are provided as file paths.
    in_img = open_image(input_image) if isinstance(input_image, str) else input_image
    sk_img_path, _ = get_image_from_dict(sketch_image)
    sk_img = open_image(sk_img_path)

    # Resize sketch image if dimensions don't match input image.
    if in_img.size != sk_img.size:
        sk_img = sk_img.resize(in_img.size, Image.LANCZOS)

    # Now composite using the original alpha_composite_with_control function.
    result_img = alpha_composite_with_control(in_img, sk_img, slider_value)
    return result_img

def replace_input_with_sketch_image(sketch_image):
    print(f"Sketch Image: {sketch_image}\n")
    sketch, is_dict = get_image_from_dict(sketch_image)
    return sketch
####################################### DEPTH ESTIMATION #######################################


def preprocess_image(image: Image.Image) -> Image.Image:
    """
    Preprocess the input image.
    Args:
        image (Image.Image): The input image.
    Returns:
        Image.Image: The preprocessed image.
    """
    processed_image = TRELLIS_PIPELINE.preprocess_image(image)
    return processed_image

def pack_state(gs: Gaussian, mesh: MeshExtractResult, name: str) -> dict:
    return {
        'gaussian': {
            **gs.init_params,
            '_xyz': gs._xyz.cpu().numpy(),
            '_features_dc': gs._features_dc.cpu().numpy(),
            '_scaling': gs._scaling.cpu().numpy(),
            '_rotation': gs._rotation.cpu().numpy(),
            '_opacity': gs._opacity.cpu().numpy(),
        },
        'mesh': {
            'vertices': mesh.vertices.cpu().numpy(),
            'faces': mesh.faces.cpu().numpy(),
        },
        'name': name
    }

def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
    gs = Gaussian(
        aabb=state['gaussian']['aabb'],
        sh_degree=state['gaussian']['sh_degree'],
        mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
        scaling_bias=state['gaussian']['scaling_bias'],
        opacity_bias=state['gaussian']['opacity_bias'],
        scaling_activation=state['gaussian']['scaling_activation'],
    )
    gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
    gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
    gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
    gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
    gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
    
    mesh = edict(
        vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
        faces=torch.tensor(state['mesh']['faces'], device='cuda'),
    )

    name = state['name']
    
    return gs, mesh, name

@spaces.GPU()
def depth_process_image(image_path, resized_width=800, z_scale=208):
    """
    Processes the input image to generate a depth map.

    Args:
        image_path (str): The file path to the input image.
        resized_width (int, optional): The width to which the image is resized. Defaults to 800.
        z_scale (int, optional): Z-axis scale factor. Defaults to 208.

    Returns:
        list: A list containing the depth image.
    """
    image_path = Path(image_path)
    if not image_path.exists():
        raise ValueError("Image file not found")

    # Load and resize the image
    image_raw = Image.open(image_path).convert("RGB")
    print(f"Original size: {image_raw.size}")
    resized_height = int(resized_width * image_raw.size[1] / image_raw.size[0])
    image = image_raw.resize((resized_width, resized_height), Image.Resampling.LANCZOS)
    print(f"Resized size: {image.size}")

    # Prepare image for the model
    encoding = image_processor(image, return_tensors="pt")

    # Perform depth estimation
    with torch.no_grad():
        outputs = depth_model(**encoding)
        predicted_depth = outputs.predicted_depth

    # Interpolate depth to match the image size
    prediction = torch.nn.functional.interpolate(
        predicted_depth.unsqueeze(1),
        size=(image.height, image.width),
        mode="bicubic",
        align_corners=False,
    ).squeeze()

    # Normalize the depth image to 8-bit
    if torch.cuda.is_available():
        prediction = prediction.numpy()
    else:
        prediction = prediction.cpu().numpy()
    depth_min, depth_max = prediction.min(), prediction.max()
    depth_image = ((prediction - depth_min) / (depth_max - depth_min) * 255).astype("uint8")
    img = Image.fromarray(depth_image)

    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.ipc_collect()
    return img

def generate_3d_asset_part1(depth_image_source, randomize_seed, seed, input_image, output_image, overlay_image, bordered_image_output, progress=gr.Progress(track_tqdm=True)):
    # Choose the image based on source
    if depth_image_source == "Input Image":
        image_path = input_image
    elif depth_image_source == "Output Image":
        image_path = output_image
    elif depth_image_source == "Image with Margins":
        image_path = bordered_image_output
    else:  # "Overlay Image"
        image_path = overlay_image

    output_name = get_output_name(input_image, output_image, overlay_image, bordered_image_output)

    # Ensure the file exists
    if not Path(image_path).exists():
        raise ValueError("Image file not found.")

    # Determine the final seed using default MAX_SEED from constants
    final_seed = np.random.randint(0, constants.MAX_SEED) if randomize_seed else seed
    # Process the image for depth estimation
    depth_img = depth_process_image(image_path, resized_width=1536, z_scale=336)
    depth_img = resize_image_with_aspect_ratio(depth_img, 1536, 1536)

    return depth_img, image_path, output_name, final_seed

@spaces.GPU(duration=150,progress=gr.Progress(track_tqdm=True))
def generate_3d_asset_part2(depth_img, image_path, output_name, seed, steps, model_resolution, video_resolution, req: gr.Request, progress=gr.Progress(track_tqdm=True)):
    # Open image using standardized defaults
    image_raw = Image.open(image_path).convert("RGB")
    resized_image = resize_image_with_aspect_ratio(image_raw, model_resolution, model_resolution)
    depth_img = Image.open(depth_img).convert("RGBA")
    # Preprocess and run the Trellis pipeline with fixed sampler settings
    try:
        processed_image = TRELLIS_PIPELINE.preprocess_image(resized_image, max_resolution=model_resolution)
        outputs = TRELLIS_PIPELINE.run(
            processed_image,
            seed=seed,
            formats=["gaussian", "mesh"],
            preprocess_image=False,
            sparse_structure_sampler_params={
                "steps": steps,
                "cfg_strength": 7.5,
            },
            slat_sampler_params={
                "steps": steps,
                "cfg_strength": 3.0,
            },
        )

        # Validate the mesh
        mesh = outputs['mesh'][0]
        meshisdict = isinstance(mesh, dict)
        if meshisdict:
            vertices = mesh['vertices']
            faces = mesh['faces']
        else:
            vertices = mesh.vertices
            faces = mesh.faces

        print(f"Mesh vertices: {vertices.shape}, faces: {faces.shape}")
        if faces.max() >= vertices.shape[0]:
            raise ValueError(f"Invalid mesh: face index {faces.max()} exceeds vertex count {vertices.shape[0]}")
    except Exception as e:
        gr.Warning(f"Error generating 3D asset: {e}")
        print(f"Error generating 3D asset: {e}")
        torch.cuda.empty_cache()
        torch.cuda.ipc_collect()
        return None,None, depth_img

    # Ensure data is on GPU and has correct type
    if not vertices.is_cuda or not faces.is_cuda:
        raise ValueError("Mesh data must be on GPU")
    if vertices.dtype != torch.float32 or faces.dtype != torch.int32:
        if meshisdict:
            mesh['faces'] = faces.to(torch.int32)
            mesh['vertices'] = vertices.to(torch.float32)
        else:
            mesh.faces = faces.to(torch.int32)
            mesh.vertices = vertices.to(torch.float32)

    user_dir = os.path.join(constants.TMPDIR, str(req.session_hash))
    os.makedirs(user_dir, exist_ok=True)

    video = render_utils.render_video(outputs['gaussian'][0], resolution=video_resolution, num_frames=64, r=1, fov=45)['color']
    try:
        video_geo = render_utils.render_video(outputs['mesh'][0], resolution=video_resolution, num_frames=64, r=1, fov=45)['normal']
        video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
    except Exception as e:
        gr.Info(f"Error rendering video: {e}")
        print(f"Error rendering video: {e}")
    video_path = os.path.join(user_dir, f'{output_name}.mp4')
    imageio.mimsave(video_path, video, fps=8)

    #snapshot_results = render_utils.render_snapshot_depth(outputs['mesh'][0], resolution=1280, r=1, fov=80)
    #depth_snapshot = Image.fromarray(snapshot_results['normal'][0]).convert("L")
    depth_snapshot = depth_img

    state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], output_name)
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.ipc_collect()
    return [state, video_path, depth_snapshot]


@spaces.GPU(duration=90,progress=gr.Progress(track_tqdm=True))
def extract_glb(
    state: dict,
    mesh_simplify: float,
    texture_size: int,
    req: gr.Request,progress=gr.Progress(track_tqdm=True)
) -> Tuple[str, str]:
    """
    Extract a GLB file from the 3D model.

    Args:
        state (dict): The state of the generated 3D model.
        mesh_simplify (float): The mesh simplification factor.
        texture_size (int): The texture resolution.

    Returns:
        str: The path to the extracted GLB file.
    """
    user_dir = os.path.join(constants.TMPDIR, str(req.session_hash))
    gs, mesh, name = unpack_state(state)
    glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
    glb_path = os.path.join(user_dir, f'{name}.glb')
    glb.export(glb_path)

    torch.cuda.empty_cache()
    torch.cuda.ipc_collect()
    return glb_path, glb_path

@spaces.GPU(progress=gr.Progress(track_tqdm=True))
def extract_gaussian(state: dict, req: gr.Request, progress=gr.Progress(track_tqdm=True)) -> Tuple[str, str]:
    """
    Extract a Gaussian file from the 3D model.

    Args:
        state (dict): The state of the generated 3D model.

    Returns:
        str: The path to the extracted Gaussian file.
    """
    user_dir = os.path.join(constants.TMPDIR, str(req.session_hash))
    gs, _, name = unpack_state(state)
    gaussian_path = os.path.join(user_dir, f'{name}.ply')
    gs.save_ply(gaussian_path)

    torch.cuda.empty_cache()
    torch.cuda.ipc_collect()
    return gaussian_path, gaussian_path


@spaces.GPU()
def getVersions():
    return versions_html()

#generate_input_image_click.zerogpu = True
#generate_depth_button_click.zerogpu = True
#def main(debug=False):
title = "HexaGrid Creator"
#description = "Customizable Hexagon Grid Image Generator"
examples = [["assets//examples//hex_map_p1.png", 32, 1, 0, 0, 0, 0, 0, "#ede9ac44","#12165380", True]]

gr.set_static_paths(paths=["images/","images/images","images/prerendered","LUT/","fonts/","assets/"])

# Gradio Blocks Interface
with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',delete_cache=(21600,86400)) as hexaGrid:
    with gr.Row():
        gr.Markdown("""
        # HexaGrid Creator
        ## Transform Your Images into Mesmerizing Hexagon Grid Masterpieces! ⬢""", elem_classes="intro")
    with gr.Row():
        with gr.Accordion("Welcome to HexaGrid Creator, the ultimate tool for transforming your images into stunning hexagon grid artworks. Whether you're a tabletop game enthusiast, a digital artist, or someone who loves unique patterns, HexaGrid Creator has something for you.", open=False, elem_classes="intro"):
            gr.Markdown ("""

            ## Drop an image into the Input Image and get started!

        

            ## What is HexaGrid Creator?
            HexaGrid Creator is a web-based application that allows you to apply a hexagon grid overlay to any image. You can customize the size, color, and opacity of the hexagons, as well as the background and border colors. The result is a visually striking image that looks like it was made from hexagonal tiles!

            ### What Can You Do?
            - **Generate Hexagon Grids:** Create beautiful hexagon grid overlays on any image with fully customizable parameters.
            - **AI-Powered Image Generation:** Use advanced AI models to generate images based on your prompts and apply hexagon grids to them.
            - **Color Exclusion:** Select and exclude specific colors from your hexagon grid for a cleaner and more refined look.
            - **Interactive Customization:** Adjust hexagon size, border size, rotation, background color, and more in real-time.
            - **Depth and 3D Model Generation:** Generate depth maps and 3D models from your images for enhanced visualization.
            - **Image Filter [Look-Up Table (LUT)] Application:** Apply filters (LUTs) to your images for color grading and enhancement.
            - **Pre-rendered Maps:** Access a library of pre-rendered hexagon maps for quick and easy customization.
            - **Add Margins:** Add customizable margins around your images for a polished finish.

            ### Why You'll Love It
            - **Fun and Easy to Use:** With an intuitive interface and real-time previews, creating hexagon grids has never been this fun!
            - **Endless Creativity:** Unleash your creativity with endless customization options and see your images transform in unique ways.
            - **Hexagon-Inspired Theme:** Enjoy a delightful yellow and purple theme inspired by hexagons! ⬢
            - **Advanced AI Models:** Leverage advanced AI models and LoRA weights for high-quality image generation and customization.

            ### Get Started
            1. **Upload or Generate an Image:** Start by uploading your own image or generate one using our AI-powered tool.
            2. **Customize Your Grid:** Play around with the settings to create the perfect hexagon grid overlay.
            3. **Download and Share:** Once you're happy with your creation, download it and share it with the world!

            ### Advanced Features
            - **Generative AI Integration:** Utilize models like `black-forest-labs/FLUX.1-dev` and various LoRA weights for generating unique images.
            - **Pre-rendered Maps:** Access a library of pre-rendered hexagon maps for quick and easy customization.
            - **Image Filter [Look-Up Table (LUT)] Application:** Apply filters (LUTs) to your images for color grading and enhancement.
            - **Depth and 3D Model Generation:** Create depth maps and 3D models from your images for enhanced visualization.
            - **Add Margins:** Customize margins around your images for a polished finish.

            Join the hive and start creating with HexaGrid Creator today!
        
            """, elem_classes="intro")
    with gr.Row():
        with gr.Column(scale=2):
            input_image = gr.Image(
                label="Input Image",
                type="filepath",
                interactive=True,
                elem_classes="centered solid imgcontainer",
                key="imgInput",
                image_mode=None,
                format="PNG",
                height=450,
                width=800
            )
            with gr.Accordion("Sketch Pad", open = False, elem_id="sketchpd"):
                with gr.Row():
                    sketch_image = gr.Sketchpad(
                        label="Sketch Image",
                        type="filepath",
                        #invert_colors=True,
                        #sources=['upload','canvas'],
                        #tool=['editor','select','color-sketch'],
                        placeholder="Draw a sketch or upload an image.",
                        interactive=True,
                        elem_classes="centered solid imgcontainer",
                        key="imgSketch",
                        image_mode="RGBA",
                        format="PNG",
                        brush=gr.Brush(),
                        canvas_size=(640,360)
                    )
                with gr.Row():
                    with gr.Column(scale=1):
                        sketch_replace_input_image_button = gr.Button("Replace Input Image with sketch", elem_id="sketch_replace_input_image_button", elem_classes="solid")
                        sketch_alpha_composite_slider = gr.Slider(0,100,50,0.5, label="Sketch Transparancy", elem_id="alpha_composite_slider")
                        btn_sketch_alpha_composite = gr.Button("Overlay Sketch on Input Image", elem_id="btn_sketchninput", elem_classes="solid")
                        gr.Markdown("### Do Not add to image if using a fill model")

        with gr.Column():
            with gr.Accordion("Hex Coloring and Exclusion", open = False):
                with gr.Row():
                    with gr.Column():
                        color_picker = gr.ColorPicker(label="Pick a color to exclude",value="#505050")
                    with gr.Column():
                        filter_color = gr.Checkbox(label="Filter Excluded Colors from Sampling", value=False,)
                exclude_color_button = gr.Button("Exclude Color", elem_id="exlude_color_button", elem_classes="solid")
                color_display = gr.DataFrame(label="List of Excluded RGBA Colors", headers=["R", "G", "B", "A"], elem_id="excluded_colors", type="array", value=build_dataframe(excluded_color_list), interactive=True, elem_classes="solid centered")
                selected_row = gr.Number(0, label="Selected Row", visible=False)
                delete_button = gr.Button("Delete Row", elem_id="delete_exclusion_button", elem_classes="solid")
                fill_hex = gr.Checkbox(label="Fill Hex with color from Image", value=True)
            with gr.Accordion("Image Filters", open = False):
                with gr.Row():
                    with gr.Column():
                        composite_color = gr.ColorPicker(label="Color", value="#ede9ac44")
                    with gr.Column():
                        composite_opacity = gr.Slider(label="Opacity %", minimum=0, maximum=100, value=50, interactive=True)
                with gr.Row():
                    composite_button = gr.Button("Composite", elem_classes="solid")
                with gr.Row():
                    with gr.Column():
                        lut_filename = gr.Textbox(
                            value="", 
                            label="Look Up Table (LUT) File Name",
                            elem_id="lutFileName")
                    with gr.Column():
                        lut_file = gr.File(
                            value=None,
                            file_count="single",
                            file_types=[".cube"],
                            type="filepath",
                            label="LUT cube File")
                with gr.Row():
                    lut_example_image = gr.Image(type="pil", label="Filter (LUT) Example Image", value=constants.default_lut_example_img)
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("""
                        ### Included Filters (LUTs)
                        There are several included Filters:

                        Try them on the example image before applying to your Input Image.
                        """, elem_id="lut_markdown")
                    with gr.Column():
                        gr.Examples(elem_id="lut_examples",
                            examples=[[f] for f in constants.lut_files],
                            inputs=[lut_filename],
                            outputs=[lut_filename],
                            label="Select a Filter (LUT) file. Populate the LUT File Name field",
                            examples_per_page = 15,
                        )
                    
                with gr.Row():
                    apply_lut_button = gr.Button("Apply Filter (LUT)", elem_classes="solid", elem_id="apply_lut_button")
                    
                lut_file.change(get_filename, inputs=[lut_file], outputs=[lut_filename])
                lut_filename.change(show_lut, inputs=[lut_filename, lut_example_image], outputs=[lut_example_image])
                apply_lut_button.click(
                    lambda lut_filename, input_image: gr.Warning("Please upload an Input Image to get started.") if input_image is None else apply_lut_to_image_path(lut_filename, input_image)[0], 
                    inputs=[lut_filename, input_image], 
                    outputs=[input_image], 
                    scroll_to_output=True
                )
            
    with gr.Row():
        with gr.Accordion("Generate AI Image (click here for options)", open = False):
            with gr.Row():
                with gr.Column():
                    model_options = gr.Dropdown(
                        label="Choose an AI Model*",
                        choices=constants.MODELS + constants.LORA_WEIGHTS + ["Manual Entry"],
                        value="Cossale/Frames2-Flex.1",
                        elem_classes="solid"
                    )
                    model_textbox = gr.Textbox(
                        label="LORA/Model",
                        value="Cossale/Frames2-Flex.1",
                        elem_classes="solid",
                        elem_id="inference_model",
                        lines=2,
                        visible=False
                    )
                    with gr.Accordion("Choose Style Model*", open=False):
                        lora_gallery = gr.Gallery(
                            [(open_image(image_path), title) for image_path, title in lora_models],
                            label="Styles",
                            allow_preview=False, preview=False ,
                            columns=2,
                            elem_id="lora_gallery",
                            show_share_button=False,
                            elem_classes="solid", type="filepath", 
                            object_fit="contain", height="auto", format="png",
                        )
                    # Update map_options to a Dropdown with choices from constants.PROMPTS keys
                    with gr.Row():
                        with gr.Column():
                            map_options = gr.Dropdown(
                            label="Map Options*",
                            choices=list(constants.PROMPTS.keys()),
                            value="Alien Landscape",
                            elem_classes="solid",
                            scale=0
                            )
                            # Add Dropdown for sizing of Images, height and width based on selection. Options are 16x9, 16x10, 4x5, 1x1
                            # The values of height and width are based on common resolutions for each aspect ratio
                            # Default to 16x9, 912x512
                            image_size_ratio = gr.Dropdown(label="Image Aspect Ratio", choices=["16:9", "16:10", "4:5", "4:3", "2:1","3:2","1:1", "9:16", "10:16", "5:4", "3:4","1:2", "2:3"], value="16:9", elem_classes="solid", type="value", scale=0, interactive=True)
                        with gr.Column():
                            seed_slider = gr.Slider(
                                label="Seed",
                                minimum=0,
                                maximum=constants.MAX_SEED,
                                step=1,
                                value=0,
                                scale=0, randomize=True, elem_id="rnd_seed"
                            )
                            randomize_seed = gr.Checkbox(label="Randomize seed", value=False, scale=0, interactive=True)
                    prompt_textbox = gr.Textbox(
                        label="Prompt",
                        visible=False,
                        elem_classes="solid",
                        value="top-down, (rectangular tabletop_map) alien planet map, Battletech_boardgame scifi world with forests, lakes, oceans, continents and snow at the top and bottom, (middle is dark, no_reflections, no_shadows), from directly above. From 100,000 feet looking straight down",
                        lines=4
                    )
                    negative_prompt_textbox = gr.Textbox(
                        label="Negative Prompt",
                        visible=False,
                        elem_classes="solid",
                        value="Earth, low quality, bad anatomy, blurry, cropped, worst quality, shadows, people, humans, reflections, shadows, realistic map of the Earth, isometric, text"
                    )
                    prompt_notes_label = gr.Label(
                        "You should use FRM$ as trigger words. @1.5 minutes",
                        elem_classes="solid centered small",
                        show_label=False,
                        visible=False
                    )
                    # Keep the change event to maintain functionality
                    map_options.change(
                        fn=update_prompt_visibility,
                        inputs=[map_options],
                        outputs=[prompt_textbox, negative_prompt_textbox, prompt_notes_label]
                    )
                    with gr.Row():
                        generate_input_image = gr.Button(
                            "Generate from Input Image & Options ",
                            elem_id="generate_input_image",
                            elem_classes="solid"
                        )
                with gr.Column(scale=2):
                        with gr.Accordion("Template Images", open = False):
                            with gr.Row():
                                with gr.Column(scale=2):
                                    # Gallery from PRE_RENDERED_IMAGES GOES HERE
                                    prerendered_image_gallery = gr.Gallery(label="Image Gallery", show_label=True, value=build_prerendered_images_by_quality(3,'thumbnail'), elem_id="gallery", 
                                        elem_classes="solid", type="filepath", columns=[3], rows=[3], preview=False ,object_fit="contain", height="auto", format="png",allow_preview=False)
                                with gr.Column():
                                    image_guidance_stength = gr.Slider(label="Image Guidance Strength (prompt percentage)", minimum=0, maximum=1.0, value=0.85, step=0.01, interactive=True)
                                    replace_input_image_button = gr.Button(
                                        "Replace Input Image",
                                        elem_id="prerendered_replace_input_image_button",
                                        elem_classes="solid"
                                    )
                                    generate_input_image_from_gallery = gr.Button(
                                        "Generate AI Image from Template Image & Options",
                                        elem_id="generate_input_image_from_gallery",
                                        elem_classes="solid"
                                    )

    with gr.Accordion("Advanced Hexagon Settings", open = False):
        with gr.Row():
            start_x = gr.Number(label="Start X", value=20, minimum=-512, maximum= 512, precision=0)
            start_y = gr.Number(label="Start Y", value=20, minimum=-512, maximum= 512, precision=0)
            end_x = gr.Number(label="End X", value=-20, minimum=-512, maximum= 512, precision=0)
            end_y = gr.Number(label="End Y", value=-20, minimum=-512, maximum= 512, precision=0)
        with gr.Row():
            x_spacing = gr.Number(label="Adjust Horizontal spacing", value=-8, minimum=-200, maximum=200, precision=1)
            y_spacing = gr.Number(label="Adjust Vertical spacing", value=3, minimum=-200, maximum=200, precision=1)
        with gr.Row():
            rotation = gr.Slider(-90, 180, 0.0, 0.1, label="Hexagon Rotation (degree)")
            add_hex_text = gr.Dropdown(label="Add Text to Hexagons", choices=[None, "Row-Column Coordinates", "Column Letter, Row Number", "Column Number, Row Letter", "Sequential Numbers", "Playing Cards Sequential", "Playing Cards Alternate Red and Black", "Custom List"], value=None)
        with gr.Row():
            custom_text_list = gr.TextArea(label="Custom Text List", value=constants.cards_alternating, visible=False,)
            custom_text_color_list = gr.TextArea(label="Custom Text Color List", value=constants.card_colors_alternating, visible=False)
        with gr.Row():
            hex_text_info = gr.Markdown("""
            ### Text Color uses the Border Color and Border Opacity, unless you use a custom list.
            ### The Custom Text List and Custom Text Color List are repeating comma separated lists.
            ### The custom color list is a comma separated list of hex colors.
            #### Example: "A,2,3,4,5,6,7,8,9,10,J,Q,K", "red,#0000FF,#00FF00,red,#FFFF00,#00FFFF,#FF8000,#FF00FF,#FF0080,#FF8000,#FF0080,lightblue"
            """, elem_id="hex_text_info", visible=False)
        add_hex_text.change(
        fn=lambda x: (
            gr.update(visible=(x == "Custom List")), 
            gr.update(visible=(x == "Custom List")),
            gr.update(visible=(x != None))
            ),
            inputs=add_hex_text,
            outputs=[custom_text_list, custom_text_color_list, hex_text_info]
        )
    with gr.Row():
        hex_size = gr.Number(label="Hexagon Size", value=90, minimum=1, maximum=768)
        border_size = gr.Slider(-5,25,value=2,step=1,label="Border Size")
    with gr.Row():        
        background_color = gr.ColorPicker(label="Background Color", value="#000000", interactive=True)
        background_opacity = gr.Slider(0,100,0,1,label="Background Opacity %")
        border_color = gr.ColorPicker(label="Border Color", value="#7b7b7b", interactive=True)
        border_opacity = gr.Slider(0,100,50,1,label="Border Opacity %")
    with gr.Row():
        hex_button = gr.Button("Generate Hex Grid!", elem_classes="solid", elem_id="btn-generate")
    with gr.Row():
        output_image = gr.Image(label="Hexagon Grid Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOutput",interactive=True)
        overlay_image = gr.Image(label="Hexagon Overlay Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOverlay",interactive=True)
    with gr.Row():
        output_overlay_composite = gr.Slider(0,100,50,0.5, label="Interpolate Intensity")
        output_blend_multiply_composite = gr.Slider(0,100,50,0.5, label="Overlay Intensity")
        output_alpha_composite = gr.Slider(0,100,50,0.5, label="Alpha Composite Intensity")
    with gr.Accordion("Add Margins (bleed)", open=False):
        with gr.Row():
            border_image_source = gr.Radio(label="Add Margins around which Image", choices=["Input Image", "Overlay Image"], value="Overlay Image")
        with gr.Row():
            mask_width = gr.Number(label="Margins Width", value=10, minimum=0, maximum=100, precision=0)
            mask_height = gr.Number(label="Margins Height", value=10, minimum=0, maximum=100, precision=0)
        with gr.Row():
                margin_color = gr.ColorPicker(label="Margin Color", value="#333333FF", interactive=True)
                margin_opacity = gr.Slider(0,100,95,0.5,label="Margin Opacity %")
        with gr.Row():
            add_border_button = gr.Button("Add Margins", elem_classes="solid", variant="secondary")
        with gr.Row():
            bordered_image_output = gr.Image(label="Image with Margins", image_mode="RGBA",  elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgBordered",interactive=False, show_download_button=True, show_fullscreen_button=True, show_share_button=True)

    with gr.Accordion("Height Maps and 3D", open=False):
        with gr.Row():
            depth_image_source = gr.Radio(
                label="Depth Image Source",
                choices=["Input Image", "Hexagon Grid Image", "Overlay Image", "Image with Margins"],
                value="Input Image"
            )
        with gr.Accordion("Advanced 3D Generation Settings", open=False):
            with gr.Row():
                with gr.Column():
                    # Use standard seed settings only
                    seed_3d = gr.Slider(0, constants.MAX_SEED, label="Seed (3D Generation)", value=0, step=1, randomize=True)
                    randomize_seed_3d = gr.Checkbox(label="Randomize Seed (3D Generation)", value=True)
                with gr.Column():
                    steps = gr.Slider(6, 36, value=12, step=1, label="Image Sampling Steps", interactive=True)
                    video_resolution = gr.Slider(384, 768, value=480, step=32, label="Video Resolution (*danger*)", interactive=True)
                    model_resolution = gr.Slider(512, 2304, value=1024, step=64, label="3D Model Resolution", interactive=True)
        with gr.Row():
            generate_3d_asset_button = gr.Button("Generate 3D Asset", elem_classes="solid", variant="secondary")
        with gr.Row():
            depth_output = gr.Image(label="Depth Map", image_mode="L", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="DepthOutput",interactive=False, show_download_button=True, show_fullscreen_button=True, show_share_button=True, height=400)
        with gr.Row():
            # For display: video output and 3D model preview (GLTF)
            video_output = gr.Video(label="3D Asset Video", autoplay=True, loop=True, height=400)
        with gr.Accordion("GLB Extraction Settings", open=False):
            with gr.Row():
                mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
                texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
            with gr.Row():
                extract_glb_btn = gr.Button("Extract GLB", interactive=False)
                extract_gaussian_btn = gr.Button("Extract Gaussian", interactive=False)
            with gr.Row():
                with gr.Column(scale=2):
                    model_output = gr.Model3D(label="Extracted 3D Model", clear_color=[1.0, 1.0, 1.0, 1.0], 
                                            elem_classes="centered solid imgcontainer", interactive=True)
                with gr.Column(scale=1):
                    glb_file = gr.File(label="3D GLTF", elem_classes="solid small centered", height=250)
                    gaussian_file = gr.File(label="Gaussian", elem_classes="solid small centered", height=250)
                    gr.Markdown("""
                    ### Files over 10 MB may not display in the 3D model viewer
                    """, elem_id="file_size_info", elem_classes="intro" )

    is_multiimage = gr.State(False)
    output_buf = gr.State()
    ddd_image_path = gr.State("./images/images/Beeuty-1.png")
    ddd_file_name = gr.State("Hexagon_file")
    with gr.Row():
        gr.Examples(examples=[
            ["assets//examples//hex_map_p1.png", False, True, -32,-31,80,80,-1.8,0,35,0,1,"#FFD0D0", 15],
            ["assets//examples//hex_map_p1_overlayed.png", False, False, -32,-31,80,80,-1.8,0,35,0,1,"#FFD0D0", 75],
            ["assets//examples//hex_flower_logo.png", False, True, -95,-95,100,100,-24,-2,190,30,2,"#FF8951", 50],
            ["assets//examples//hexed_fract_1.png", False, True, 0,0,0,0,0,0,10,0,0,"#000000", 5],
            ["assets//examples//tmpzt3mblvk.png", False, True, -20,10,0,0,-6,-2,35,30,1,"#ffffff", 0],
            ],
            inputs=[input_image, filter_color, fill_hex, start_x, start_y, end_x, end_y, x_spacing, y_spacing, hex_size, rotation, border_size, border_color, border_opacity],
            elem_id="examples")
    # with gr.Row():
    #     login_button = gr.LoginButton(size="sm", elem_classes="solid centered", elem_id="hf_login_btn")
    with gr.Row():
        gr.HTML(value=getVersions(), visible=True, elem_id="versions")

    # Handlers
    hexaGrid.load(start_session)
    hexaGrid.unload(end_session)

    color_display.select(on_color_display_select,inputs=[color_display], outputs=[selected_row])
    color_display.input(on_input,inputs=[color_display], outputs=[color_display, gr.State(excluded_color_list)])

    delete_button.click(fn=delete_color, inputs=[selected_row, color_display], outputs=[color_display])
    exclude_color_button.click(fn=add_color, inputs=[color_picker, gr.State(excluded_color_list)], outputs=[color_display, gr.State(excluded_color_list)])
    hex_button.click(
        fn=lambda hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list: 
            gr.Warning("Please upload an Input Image to get started.") if input_image is None else hex_create(hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list),
        inputs=[hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list],
        outputs=[output_image, overlay_image],
        scroll_to_output=True
    )
    generate_input_image.click(
        fn=generate_input_image_click,
        inputs=[input_image,map_options, prompt_textbox, negative_prompt_textbox, model_textbox, randomize_seed, seed_slider, gr.State(False), sketch_image, image_guidance_stength, image_size_ratio],
        outputs=[input_image, seed_slider], scroll_to_output=True
    ).then(
        fn=update_sketch_dimensions,
        inputs=[input_image, sketch_image],
        outputs=[sketch_image, sketch_image]
    )
    input_image.input(
        fn=on_input_image_change,
        inputs=[input_image],
        outputs=[input_image,sketch_image], scroll_to_output=True,
    )
    ###################### sketchpad ############################
    btn_sketch_alpha_composite.click(
        fn=composite_with_control_sync,
        inputs=[input_image, sketch_image, sketch_alpha_composite_slider],
        outputs=[input_image],
        scroll_to_output=True
    )
    sketch_replace_input_image_button.click(
        lambda sketch_image: replace_input_with_sketch_image(sketch_image),
        inputs=[sketch_image],
        outputs=[input_image], scroll_to_output=True
    )
    ##################### model #######################################
    model_textbox.change(
        fn=update_prompt_notes,
        inputs=model_textbox,
        outputs=prompt_notes_label,preprocess=False
    )
    model_options.change(
        fn=lambda x: (gr.update(visible=(x == "Manual Entry")), gr.update(value=x) if x != "Manual Entry" else gr.update()),
        inputs=model_options,
        outputs=[model_textbox, model_textbox]
    )
    model_options.change(
        fn=update_prompt_notes,
        inputs=model_options,
        outputs=prompt_notes_label
    )
    lora_gallery.select(
        fn=update_selection,
        inputs=[image_size_ratio],
        outputs=[prompt_textbox, model_options, gr.State(selected_index), image_size_ratio, prompt_notes_label]
    )

    #################### model end ########################################

    composite_button.click(
        fn=lambda input_image, composite_color, composite_opacity: gr.Warning("Please upload an Input Image to get started.") if input_image is None else change_color(input_image, composite_color, composite_opacity),
        inputs=[input_image, composite_color, composite_opacity],
        outputs=[input_image]
    )

    #use conditioned_image as the input_image for generate_input_image_click
    generate_input_image_from_gallery.click(
        fn=generate_input_image_click,
        inputs=[input_image, map_options, prompt_textbox, negative_prompt_textbox, model_textbox,randomize_seed, seed_slider, gr.State(True), sketch_image , image_guidance_stength, image_size_ratio],
        outputs=[input_image, seed_slider], scroll_to_output=True
    ).then(
        fn=update_sketch_dimensions,
        inputs=[input_image, sketch_image],
        outputs=[sketch_image, sketch_image]
    )

    # Update the state variable with the prerendered image filepath when an image is selected
    prerendered_image_gallery.select(
        fn=on_prerendered_gallery_selection, 
        inputs=None, 
        outputs=[gr.State(current_prerendered_image)],  # Update the state with the selected image
        show_api=False
    )
    # replace input image with selected gallery image
    replace_input_image_button.click(
        lambda: current_prerendered_image.value,
        inputs=None,
        outputs=[input_image], scroll_to_output=True
    ).then(
        fn=update_sketch_dimensions,
        inputs=[input_image, sketch_image],
        outputs=[sketch_image, sketch_image]
    )
    output_overlay_composite.change(
        fn=combine_images_with_lerp,
        inputs=[input_image, output_image, output_overlay_composite],
        outputs=[overlay_image], scroll_to_output=True
    )
    output_blend_multiply_composite.change(
        fn=multiply_and_blend_images,
        inputs=[input_image, output_image, output_blend_multiply_composite],
        outputs=[overlay_image],
        scroll_to_output=True
    )
    output_alpha_composite.change(
        fn=alpha_composite_with_control,
        inputs=[input_image, output_image, output_alpha_composite],
        outputs=[overlay_image],
        scroll_to_output=True
    )
    add_border_button.click(
        fn=lambda image_source, mask_w, mask_h, color, opacity, input_img, overlay_img: add_border(input_img if image_source == "Input Image" else overlay_img, mask_w, mask_h, update_color_opacity(detect_color_format(color), opacity * 2.55)),
        inputs=[border_image_source, mask_width, mask_height, margin_color, margin_opacity, input_image, overlay_image],
        outputs=[bordered_image_output],
        scroll_to_output=True
    )
    # 3D Generation

    # generate_depth_button.click(
    #     fn=generate_depth_button_click,
    #     inputs=[depth_image_source, resized_width_slider, z_scale_slider, input_image, output_image, overlay_image, bordered_image_output],
    #     outputs=[depth_map_output, model_output, model_file], scroll_to_output=True
    # )

    # Chain the buttons
    generate_3d_asset_button.click(
        fn=generate_3d_asset_part1,
        inputs=[depth_image_source, randomize_seed_3d, seed_3d, input_image, output_image, overlay_image, bordered_image_output],
        outputs=[depth_output, ddd_image_path, ddd_file_name, seed_3d ],
        scroll_to_output=True
    ).then(
        fn=generate_3d_asset_part2,
        inputs=[depth_output, ddd_image_path, ddd_file_name, seed_3d, steps, model_resolution, video_resolution ],
        outputs=[output_buf, video_output, depth_output],
        scroll_to_output=True
    ).then(
        lambda: (gr.Button(interactive=True), gr.Button(interactive=True)),
        outputs=[extract_glb_btn, extract_gaussian_btn]
    )

    # Extraction callbacks remain unchanged from previous behavior
    extract_glb_btn.click(
        fn=extract_glb,
        inputs=[output_buf, mesh_simplify, texture_size],
        outputs=[model_output, glb_file]
    ).then(
        lambda: gr.Button(interactive=True),
        outputs=[glb_file]
    )

    extract_gaussian_btn.click(
        fn=extract_gaussian,
        inputs=[output_buf],
        outputs=[model_output, gaussian_file]
    ).then(
        lambda: gr.Button(interactive=True),
        outputs=[gaussian_file]
    )

if __name__ == "__main__":
    constants.load_env_vars(constants.dotenv_path)
    logging.basicConfig(
        format="[%(levelname)s] %(asctime)s %(message)s", level=logging.INFO
    )
    logging.info("Environment Variables: %s" % os.environ)
    # if _get_output(["nvcc", "--version"]) is None:
    #     logging.info("Installing CUDA toolkit...")
    #     install_cuda_toolkit()
    # else:
    #     logging.info("Detected CUDA: %s" % _get_output(["nvcc", "--version"]))

    # logging.info("Installing CUDA extensions...")
    # setup_runtime_env()
    #main(os.getenv("DEBUG") == "1")
    #main()


    #-------------- ------------------------------------------------MODEL INITIALIZATION------------------------------------------------------------#
    # Load models once during module import
    dtype = torch.bfloat16
    device = "cuda" if torch.cuda.is_available() else "cpu"
    base_model = "black-forest-labs/FLUX.1-dev"
    good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
    pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=good_vae).to(device)

    image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
    depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large", ignore_mismatched_sizes=True)
    TRELLIS_PIPELINE = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
    TRELLIS_PIPELINE.to(device)
    try:
       TRELLIS_PIPELINE.preprocess_image(Image.fromarray(np.zeros((256, 256, 3), dtype=np.uint8)))    # Preload rembg
    except:
        pass
    hexaGrid.queue(default_concurrency_limit=1,max_size=12,api_open=False)
    hexaGrid.launch(allowed_paths=["assets","/","./assets","images","./images", "./images/prerendered", 'e:/TMP'], favicon_path="./assets/favicon.ico", max_file_size="10mb")