File size: 53,704 Bytes
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8647602
674481b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
try:
    import spaces
except ImportError:
    class spaces:
        @staticmethod
        def GPU(f): return f
sys.modules["spaces"] = sys.modules.get("spaces", spaces)
import os
from camera_control_ui import CameraControl3D, build_camera_prompt, update_prompt_with_camera
import re
import gc
import traceback
import gradio as gr
import numpy as np
import spaces
import torch
import random
from PIL import Image, ImageDraw
from typing import Iterable, Optional

from transformers import (
    AutoImageProcessor,
    AutoModelForDepthEstimation,
)

from huggingface_hub import hf_hub_download
from safetensors.torch import load_file as safetensors_load_file

from gradio.themes import Soft
from gradio.themes.utils import colors, fonts, sizes

# ============================================================
# Theme
# ============================================================

colors.orange_red = colors.Color(
    name="orange_red",
    c50="#FFF0E5",
    c100="#FFE0CC",
    c200="#FFC299",
    c300="#FFA366",
    c400="#FF8533",
    c500="#FF4500",
    c600="#E63E00",
    c700="#CC3700",
    c800="#B33000",
    c900="#992900",
    c950="#802200",
)


class OrangeRedTheme(Soft):
    def __init__(
        self,
        *,
        primary_hue: colors.Color | str = colors.gray,
        secondary_hue: colors.Color | str = colors.orange_red,
        neutral_hue: colors.Color | str = colors.slate,
        text_size: sizes.Size | str = sizes.text_lg,
        font: fonts.Font | str | Iterable[fonts.Font | str] = (
            fonts.GoogleFont("Outfit"),
            "Arial",
            "sans-serif",
        ),
        font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
            fonts.GoogleFont("IBM Plex Mono"),
            "ui-monospace",
            "monospace",
        ),
    ):
        super().__init__(
            primary_hue=primary_hue,
            secondary_hue=secondary_hue,
            neutral_hue=neutral_hue,
            text_size=text_size,
            font=font,
            font_mono=font_mono,
        )
        super().set(
            background_fill_primary="*primary_50",
            background_fill_primary_dark="*primary_900",
            body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
            body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
            button_primary_text_color="white",
            button_primary_text_color_hover="white",
            button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
            button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
            button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
            button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
            button_secondary_text_color="black",
            button_secondary_text_color_hover="white",
            button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
            button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
            button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
            button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
            slider_color="*secondary_500",
            slider_color_dark="*secondary_600",
            block_title_text_weight="600",
            block_border_width="3px",
            block_shadow="*shadow_drop_lg",
            button_primary_shadow="*shadow_drop_lg",
            button_large_padding="11px",
            color_accent_soft="*primary_100",
            block_label_background_fill="*primary_200",
        )


orange_red_theme = OrangeRedTheme()

# ============================================================
# Device
# ============================================================

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
print("torch.__version__ =", torch.__version__)
print("torch.version.cuda =", torch.version.cuda)
print("cuda available:", torch.cuda.is_available())
print("cuda device count:", torch.cuda.device_count())
if torch.cuda.is_available():
    print("current device:", torch.cuda.current_device())
    print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
print("Using device:", device)

# ============================================================
# AIO version (Space variable)
# ============================================================

AIO_REPO_ID = "sdfafdfsdf/Phr00t-Qwen-Rapid-AIO"
DEFAULT_AIO_VERSION = "v19"

_VER_RE = re.compile(r"^v\d+$")
_DIGITS_RE = re.compile(r"^\d+$")


def _normalize_version(raw: str) -> Optional[str]:
    if raw is None:
        return None
    s = str(raw).strip()
    if not s:
        return None
    if _VER_RE.fullmatch(s):
        return s
    # forgiving: allow "21" -> "v21"
    if _DIGITS_RE.fullmatch(s):
        return f"v{s}"
    return None


_AIO_ENV_RAW = os.environ.get("AIO_VERSION", "")
_AIO_ENV_NORM = _normalize_version(_AIO_ENV_RAW)

AIO_VERSION = _AIO_ENV_NORM or DEFAULT_AIO_VERSION
AIO_VERSION_SOURCE = "env" if _AIO_ENV_NORM else "default(v19)"

print(f"AIO_VERSION (env raw) = {_AIO_ENV_RAW!r}")
print(f"AIO_VERSION (normalized) = {_AIO_ENV_NORM!r}")
print(f"Using AIO_VERSION = {AIO_VERSION} ({AIO_VERSION_SOURCE})")

# ============================================================
# Pipeline
# ============================================================

from diffusers import FlowMatchEulerDiscreteScheduler  # noqa: F401
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3

dtype = torch.bfloat16


def _load_pipe_with_version(version: str) -> QwenImageEditPlusPipeline:
    sub = f"{version}/transformer"
    print(f"📦 Loading AIO transformer: {AIO_REPO_ID} / {sub}")
    p = QwenImageEditPlusPipeline.from_pretrained(
        "sdfafdfsdf/Qwen-Image-Edit-2511",
        transformer=QwenImageTransformer2DModel.from_pretrained(
            AIO_REPO_ID,
            subfolder=sub,
            torch_dtype=dtype,
            device_map="auto",
            low_cpu_mem_usage=True,
        ),
        torch_dtype=dtype,
    )
    p.enable_model_cpu_offload()
    return p


# Forgiving load: try env/default version, fallback to v19 if it fails
try:
    pipe = _load_pipe_with_version(AIO_VERSION)
except Exception as e:
    print("❌ Failed to load requested AIO_VERSION. Falling back to v19.")
    print("---- exception ----")
    print(traceback.format_exc())
    print("-------------------")
    AIO_VERSION = DEFAULT_AIO_VERSION
    AIO_VERSION_SOURCE = "fallback_to_v19"
    pipe = _load_pipe_with_version(AIO_VERSION)

# Apply FA3 Optimization
try:
    print("Skipping FA3 optimization for stability.")
    print("Flash Attention 3 Processor set successfully.")
except Exception as e:
    print(f"Warning: Could not set FA3 processor: {e}")

MAX_SEED = np.iinfo(np.int32).max

# ============================================================
# VAE tiling toggle (UI-controlled; OFF by default)
# ============================================================

def _apply_vae_tiling(enabled: bool):
    """
    Toggle VAE tiling on the global pipeline.

    This does NOT require a Space restart; it applies to the next pipe(...) call.
    Note: this is global process state, so concurrent users could flip it between runs.
    """
    try:
        if enabled:
            if hasattr(pipe, "enable_vae_tiling"):
                pipe.enable_vae_tiling()
                print("✅ VAE tiling ENABLED (per UI).")
            elif hasattr(pipe, "vae") and hasattr(pipe.vae, "enable_tiling"):
                pipe.vae.enable_tiling()
                print("✅ VAE tiling ENABLED via pipe.vae.enable_tiling() (per UI).")
            else:
                print("⚠️ No enable_vae_tiling()/vae.enable_tiling() found; cannot enable.")
        else:
            if hasattr(pipe, "disable_vae_tiling"):
                pipe.disable_vae_tiling()
                print("🛑 VAE tiling DISABLED (per UI).")
            elif hasattr(pipe, "vae") and hasattr(pipe.vae, "disable_tiling"):
                pipe.vae.disable_tiling()
                print("🛑 VAE tiling DISABLED via pipe.vae.disable_tiling() (per UI).")
            else:
                # If no disable method exists, we leave current state unchanged.
                print("⚠️ No disable_vae_tiling()/vae.disable_tiling() found; leaving current state unchanged.")
    except Exception as e:
        print(f"⚠️ VAE tiling toggle failed: {e}")

# ============================================================
# Derived conditioning (Transformers): Depth
# ============================================================
# Depth uses Depth Anything V2 Small (Transformers-compatible):
# https://huggingface.co/depth-anything/Depth-Anything-V2-Small-hf

DEPTH_MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf"

# Lazy cache keyed by device string ("cpu" / "cuda")
_DEPTH_CACHE = {}

def _derived_device(use_gpu: bool) -> torch.device:
    return torch.device("cuda" if (use_gpu and torch.cuda.is_available()) else "cpu")

def _load_depth_models(dev: torch.device):
    key = str(dev)
    if key in _DEPTH_CACHE:
        return _DEPTH_CACHE[key]
    proc = AutoImageProcessor.from_pretrained(DEPTH_MODEL_ID)
    model = AutoModelForDepthEstimation.from_pretrained(DEPTH_MODEL_ID).to(dev)
    model.eval()
    _DEPTH_CACHE[key] = (proc, model)
    return _DEPTH_CACHE[key]

@torch.inference_mode()
def make_depth_map(img: Image.Image, *, use_gpu: bool) -> Image.Image:
    dev = _derived_device(use_gpu)
    proc, model = _load_depth_models(dev)

    w, h = img.size
    inputs = proc(images=img.convert("RGB"), return_tensors="pt").to(dev)
    outputs = model(**inputs)
    predicted = outputs.predicted_depth  # [B, H, W]

    depth = torch.nn.functional.interpolate(
        predicted.unsqueeze(1),
        size=(h, w),
        mode="bicubic",
        align_corners=False,
    ).squeeze(1)[0]

    depth = depth - depth.min()
    depth = depth / (depth.max() + 1e-8)
    depth = (depth * 255.0).clamp(0, 255).to(torch.uint8).cpu().numpy()
    return Image.fromarray(depth).convert("RGB")

# ============================================================
# LoRA adapters + presets
# ============================================================

NONE_LORA = "None"

ADAPTER_SPECS = {
    "3D-Camera": {
        "type": "single",
        "repo": "sdfafdfsdf/Qwen-Image-Edit-2511-Multiple-Angles-LoRA",
        "weights": "qwen-image-edit-2511-multiple-angles-lora.safetensors",
        "adapter_name": "angles",
        "strength": 1.0,
    },

    "Qwen-lora-nsfw": {
        "type": "single",
        "repo": "wiikoo/Qwen-lora-nsfw",
        "weights": "loras/qwen_image_edit_remove-clothing_v1.0.safetensors",
        "adapter_name": "qwen-lora-nsfw",
        "strength": 1.0,
    },

    "Consistance": {
        "type": "single",
        "repo": "sdfafdfsdf/QIE_2511_Consistency_Lora",
        "weights": "qe2511_consis_alpha_patched.safetensors",
        "adapter_name": "Consistency",
        "strength": 0.6,
    },
    "Semirealistic-photo-detailer": {
        "type": "single",
        "repo": "rzgar/Qwen-Image-Edit-semi-realistic-detailer",
        "weights": "Qwen-Image-Edit-Anime-Semi-Realistic-Detailer-v1.safetensors",
        "adapter_name": "semirealistic",
        "strength": 1.0,
    },
    "AnyPose": {
        "type": "package",
        "requires_two_images": True,
        "image2_label": "Upload Pose Reference (Image 2)",
        "parts": [
            {
                "repo": "lilylilith/AnyPose",
                "weights": "2511-AnyPose-base-000006250.safetensors",
                "adapter_name": "anypose-base",
                "strength": 0.7,
            },
            {
                "repo": "lilylilith/AnyPose",
                "weights": "2511-AnyPose-helper-00006000.safetensors",
                "adapter_name": "anypose-helper",
                "strength": 0.7,
            },
        ],
    },
    "Any2Real_2601": {
        "type": "single",
        "repo": "lrzjason/Anything2Real_2601",
        "weights": "anything2real_2601_A_final_patched.safetensors",
        "adapter_name": "photoreal",
        "strength": 1.0,
    },
    "Hyperrealistic-Portrait": {
        "type": "single",
        "repo": "prithivMLmods/Qwen-Image-Edit-2511-Hyper-Realistic-Portrait",
        "weights": "HRP_20.safetensors",
        "adapter_name": "HRPortrait",
        "strength": 1.0,
    },
    "Ultrarealistic-Portrait": {
        "type": "single",
        "repo": "prithivMLmods/Qwen-Image-Edit-2511-Ultra-Realistic-Portrait",
        "weights": "URP_20.safetensors",
        "adapter_name": "URPortrait",
        "strength": 1.0,
    },
    "BFS-Best-FaceSwap": {
        "type": "single",
        "requires_two_images": True,
        "image2_label": "Upload Head/Face Donor (Image 2)",
        "repo": "sdfafdfsdf/BFS-Best-Face-Swap",
        "weights": "bfs_head_v5_2511_original.safetensors",
        "adapter_name": "BFS-Best-Faceswap",
        "strength": 1.0,
        "needs_alpha_fix": True,  # <-- fixes KeyError 'img_in.alpha'
    },
    "BFS-Best-FaceSwap-merge": {
        "type": "single",
        "requires_two_images": True,
        "image2_label": "Upload Head/Face Donor (Image 2)",
        "repo": "sdfafdfsdf/BFS-Best-Face-Swap",
        "weights": "bfs_head_v5_2511_merged_version_rank_32_fp32.safetensors",
        "adapter_name": "BFS-Best-Faceswap-merge",
        "strength": 1.1,
        "needs_alpha_fix": True,  # <-- fixes KeyError 'img_in.alpha'
    },
    "F2P": {
        "type": "single",
        "repo": "DiffSynth-Studio/Qwen-Image-Edit-F2P",
        "weights": "edit_0928_lora_step40000.safetensors",
        "adapter_name": "F2P",
        "strength": 1.0,
    },
    "Multiple-Angles": {
        "type": "single",
        "repo": "dx8152/Qwen-Edit-2509-Multiple-angles",
        "weights": "镜头转换.safetensors",
        "adapter_name": "multiple-angles",
        "strength": 1.0,
    },
    "Light-Restoration": {
        "type": "single",
        "repo": "dx8152/Qwen-Image-Edit-2509-Light_restoration",
        "weights": "移除光影.safetensors",
        "adapter_name": "light-restoration",
        "strength": 1.0,
    },
    "Relight": {
        "type": "single",
        "repo": "dx8152/Qwen-Image-Edit-2509-Relight",
        "weights": "Qwen-Edit-Relight.safetensors",
        "adapter_name": "relight",
        "strength": 1.0,
    },
    "Multi-Angle-Lighting": {
        "type": "single",
        "repo": "dx8152/Qwen-Edit-2509-Multi-Angle-Lighting",
        "weights": "多角度灯光-251116.safetensors",
        "adapter_name": "multi-angle-lighting",
        "strength": 1.0,
    },
    "Edit-Skin": {
        "type": "single",
        "repo": "tlennon-ie/qwen-edit-skin",
        "weights": "qwen-edit-skin_1.1_000002750.safetensors",
        "adapter_name": "edit-skin",
        "strength": 1.0,
    },
    "Next-Scene": {
        "type": "single",
        "repo": "lovis93/next-scene-qwen-image-lora-2509",
        "weights": "next-scene_lora-v2-3000.safetensors",
        "adapter_name": "next-scene",
        "strength": 1.0,
    },
    "Flat-Log": {
        "type": "single",
        "repo": "tlennon-ie/QwenEdit2509-FlatLogColor",
        "weights": "QwenEdit2509-FlatLogColor.safetensors",
        "adapter_name": "flat-log",
        "strength": 1.0,
    },
    "Upscale-Image": {
        "type": "single",
        "repo": "vafipas663/Qwen-Edit-2509-Upscale-LoRA",
        "weights": "qwen-edit-enhance_64-v3_000001000.safetensors",
        "adapter_name": "upscale-image",
        "strength": 1.0,
    },
    "Upscale2K": {
        "type": "single",
        "repo": "valiantcat/Qwen-Image-Edit-2509-Upscale2K",
        "weights": "qwen_image_edit_2509_upscale.safetensors",
        "adapter_name": "upscale-2k",
        "strength": 1.0,
        "target_long_edge": 2048,
    },
}

LORA_PRESET_PROMPTS = {
    "Any2Real_2601": "change the picture 1 to realistic photograph",
    "Semirealistic-photo-detailer": "transform the image to semi-realistic image",
    "AnyPose": "Make the person in image 1 do the exact same pose of the person in image 2. Changing the style and background of the image of the person in image 1 is undesirable, so don't do it. The new pose should be pixel accurate to the pose we are trying to copy. The position of the arms and head and legs should be the same as the pose we are trying to copy. Change the field of view and angle to match exactly image 2. Head tilt and eye gaze pose should match the person in image 2.",
    "Hyperrealistic-Portrait": "Transform the image into an ultra-realistic photorealistic portrait with strict identity preservation, facing straight to the camera. Enhance pore-level skin textures, realistic moisture effects, and natural wet hair clumping against the skin. Apply cool-toned soft-box lighting with subtle highlights and shadows, maintain realistic green-hazel eye catchlights without synthetic gloss, and preserve soft natural lip texture. Use shallow depth of field with a clean bokeh background, an 85mm macro photographic look, and raw photo grading without retouching to maintain realism and original details.",
    "Ultrarealistic-Portrait": "Transform the image into an ultra-realistic glamour portrait while strictly preserving the subject’s identity. Apply a close-up composition with a slight head tilt and a hand near the face, enhance cinematic directional lighting with dramatic fashion-style highlights, and refine makeup details including glowing skin, glossy lips, luminous highlighter, and defined eyes. Increase skin realism with detailed epidermal textures such as micropores, microhairs, subtle oil sheen, natural highlights, soft wrinkles, and subsurface scattering. Maintain a luxury fashion-magazine look in a 9:16 aspect ratio, preserving realism, facial structure, and original details without over-smoothing or retouching.",
    "Upscale2K": "Upscale this picture to 4K resolution.",
    "BFS-Best-FaceSwap": "head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2, strictly preserving the hair, eye color, and nose structure of Picture 2. copy the eye direction, head rotation, and micro-expressions from Picture 1. high quality, sharp details, 4k",
    "BFS-Best-FaceSwap-merge": "head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2, strictly preserving the hair, eye color, and nose structure of Picture 2. copy the eye direction, head rotation, and micro-expressions from Picture 1. high quality, sharp details, 4k",
}

# Track what is currently loaded in memory (adapter_name values)
LOADED_ADAPTERS = set()

# ============================================================
# Helpers: resolution
# ============================================================

# We prefer *area-based* sizing (≈ megapixels) over long-edge sizing.
# This aligns better with Qwen-Image-Edit's internal assumptions and reduces FOV drift.

def _round_to_multiple(x: int, m: int) -> int:
    return max(m, (int(x) // m) * m)

def compute_canvas_dimensions_from_area(
    image: Image.Image,
    target_area: int,
    multiple_of: int,
) -> tuple[int, int]:
    """Compute (width, height) that matches image aspect ratio and approximates target_area.

    The result is floored to be divisible by multiple_of (typically vae_scale_factor*2).
    """
    w, h = image.size
    aspect = w / h if h else 1.0

    # Use the pipeline's own area->(w,h) helper for consistency.
    from qwenimage.pipeline_qwenimage_edit_plus import calculate_dimensions

    width, height = calculate_dimensions(int(target_area), float(aspect))
    width = _round_to_multiple(int(width), int(multiple_of))
    height = _round_to_multiple(int(height), int(multiple_of))
    return width, height

def get_target_area_for_lora(
    image: Image.Image,
    lora_adapter: str,
    user_target_megapixels: float,
) -> int:
    """Return target pixel area for the canvas.

    Priority:
      1) Adapter spec: target_area (pixels) or target_megapixels
      2) Adapter spec: target_long_edge (legacy) -> converted to area using image aspect
      3) User slider target megapixels
    """
    spec = ADAPTER_SPECS.get(lora_adapter, {})

    if "target_area" in spec:
        try:
            return int(spec["target_area"])
        except Exception:
            pass

    if "target_megapixels" in spec:
        try:
            mp = float(spec["target_megapixels"])
            return int(mp * 1024 * 1024)
        except Exception:
            pass

    # Legacy support (e.g. Upscale2K)
    if "target_long_edge" in spec:
        try:
            long_edge = int(spec["target_long_edge"])
            w, h = image.size
            if w >= h:
                new_w = long_edge
                new_h = int(round(long_edge * (h / w)))
            else:
                new_h = long_edge
                new_w = int(round(long_edge * (w / h)))
            return int(new_w * new_h)
        except Exception:
            pass

    # User default
    try:
        mp = float(user_target_megapixels)
    except Exception:
        mp = 1.0

    # Treat 0 MP as "match input area"
    if mp <= 0:
        w, h = image.size
        return int(w * h)

    return int(mp * 1024 * 1024)

# ============================================================
# Helpers: multi-input routing + gallery normalization
# ============================================================


def lora_requires_two_images(lora_adapter: str) -> bool:
    return bool(ADAPTER_SPECS.get(lora_adapter, {}).get("requires_two_images", False))


def image2_label_for_lora(lora_adapter: str) -> str:
    return str(ADAPTER_SPECS.get(lora_adapter, {}).get("image2_label", "Upload Reference (Image 2)"))


def _to_pil_rgb(x) -> Optional[Image.Image]:
    """
    Accepts PIL / numpy / (image, caption) tuples from gr.Gallery and returns PIL RGB.
    Gradio Gallery commonly yields tuples like (image, caption).
    """
    if x is None:
        return None

    # Gallery often returns (image, caption)
    if isinstance(x, tuple) and len(x) >= 1:
        x = x[0]
        if x is None:
            return None

    if isinstance(x, Image.Image):
        return x.convert("RGB")

    if isinstance(x, np.ndarray):
        return Image.fromarray(x).convert("RGB")

    # Best-effort fallback
    try:
        return Image.fromarray(np.array(x)).convert("RGB")
    except Exception:
        return None


def build_labeled_images(
    img1: Image.Image,
    img2: Optional[Image.Image],
    extra_imgs: Optional[list[Image.Image]],
) -> dict[str, Image.Image]:
    """
    Creates labels image_1, image_2, image_3... based on what is actually uploaded:
      - img1 is always image_1
      - img2 becomes image_2 only if present
      - extras start immediately after the last present base box
    The pipeline receives images in this exact order.
    """
    labeled: dict[str, Image.Image] = {}
    idx = 1

    labeled[f"image_{idx}"] = img1
    idx += 1

    if img2 is not None:
        labeled[f"image_{idx}"] = img2
        idx += 1

    if extra_imgs:
        for im in extra_imgs:
            if im is None:
                continue
            labeled[f"image_{idx}"] = im
            idx += 1

    return labeled


# ============================================================
# Helpers: BFS alpha key fix
# ============================================================


def _inject_missing_alpha_keys(state_dict: dict) -> dict:
    """
    Diffusers' Qwen LoRA converter expects '<module>.alpha' keys.
    BFS safetensors omits them. We inject alpha = rank (neutral scaling).

    IMPORTANT: diffusers may strip 'diffusion_model.' before lookup, so we
    inject BOTH:
      - diffusion_model.xxx.alpha
      - xxx.alpha
    """
    bases = {}

    for k, v in state_dict.items():
        if not isinstance(v, torch.Tensor):
            continue
        if k.endswith(".lora_down.weight") and v.ndim >= 1:
            base = k[: -len(".lora_down.weight")]
            rank = int(v.shape[0])
            bases[base] = rank

    for base, rank in bases.items():
        alpha_tensor = torch.tensor(float(rank), dtype=torch.float32)

        full_alpha = f"{base}.alpha"
        if full_alpha not in state_dict:
            state_dict[full_alpha] = alpha_tensor

        if base.startswith("diffusion_model."):
            stripped_base = base[len("diffusion_model.") :]
            stripped_alpha = f"{stripped_base}.alpha"
            if stripped_alpha not in state_dict:
                state_dict[stripped_alpha] = alpha_tensor

    return state_dict


def _filter_to_diffusers_lora_keys(state_dict: dict) -> tuple[dict, dict]:
    """Return (filtered_state_dict, stats).

    Some ComfyUI/Qwen safetensors (especially "merged" variants) include non-LoRA
    delta/patch keys like `*.diff` and `*.diff_b` alongside real LoRA tensors.
    Diffusers' internal Qwen LoRA converter is strict: any leftover keys cause an
    error (`state_dict should be empty...`).

    This helper keeps only the keys Diffusers can consume as a LoRA:
      - `*.lora_up.weight`
      - `*.lora_down.weight`
      - (rare) `*.lora_mid.weight`
      - alpha keys: `*.alpha` (or `*.lora_alpha` which we normalize to `*.alpha`)

    It also drops known patch keys (`*.diff`, `*.diff_b`) and everything else.
    """

    keep_suffixes = (
        ".lora_up.weight",
        ".lora_down.weight",
        ".lora_mid.weight",
        ".alpha",
        ".lora_alpha",
    )

    dropped_patch = 0
    dropped_other = 0
    kept = 0
    normalized_alpha = 0

    out: dict[str, torch.Tensor] = {}
    for k, v in state_dict.items():
        if not isinstance(v, torch.Tensor):
            # Ignore non-tensor entries if any.
            dropped_other += 1
            continue

        # Drop ComfyUI "delta" keys that Diffusers' LoRA loader will never consume.
        if k.endswith(".diff") or k.endswith(".diff_b"):
            dropped_patch += 1
            continue

        if not k.endswith(keep_suffixes):
            dropped_other += 1
            continue

        if k.endswith(".lora_alpha"):
            # Normalize common alt name to what Diffusers expects.
            base = k[: -len(".lora_alpha")]
            k2 = f"{base}.alpha"
            out[k2] = v.float() if v.dtype != torch.float32 else v
            normalized_alpha += 1
            kept += 1
            continue

        out[k] = v
        kept += 1

    stats = {
        "kept": kept,
        "dropped_patch": dropped_patch,
        "dropped_other": dropped_other,
        "normalized_alpha": normalized_alpha,
    }
    return out, stats


def _duplicate_stripped_prefix_keys(state_dict: dict, prefix: str = "diffusion_model.") -> dict:
    """Ensure both prefixed and unprefixed variants exist for LoRA-related keys.

    Diffusers' Qwen LoRA conversion may strip `diffusion_model.` when looking up
    modules. Some exports only include prefixed keys. To be maximally compatible,
    we duplicate LoRA keys (and alpha) in stripped form when missing.
    """

    out = dict(state_dict)
    for k, v in list(state_dict.items()):
        if not k.startswith(prefix):
            continue
        stripped = k[len(prefix) :]
        if stripped not in out:
            out[stripped] = v
    return out


def _load_lora_weights_with_fallback(repo: str, weight_name: str, adapter_name: str, needs_alpha_fix: bool = False):
    """
    Normal path: pipe.load_lora_weights(repo, weight_name=..., adapter_name=...)
    BFS fallback: download safetensors, inject missing alpha keys, then load from dict.
    """
    try:
        pipe.load_lora_weights(repo, weight_name=weight_name, adapter_name=adapter_name)
        return
    except (KeyError, ValueError) as e:
        # KeyError: missing required alpha keys (common in BFS)
        # ValueError: Diffusers Qwen converter found leftover keys (e.g. .diff/.diff_b)
        if not needs_alpha_fix:
            raise

        print(
            "⚠️ LoRA load failed (will try safe dict fallback). "
            f"Adapter={adapter_name!r} file={weight_name!r} error={type(e).__name__}: {e}"
        )

        local_path = hf_hub_download(repo_id=repo, filename=weight_name)
        sd = safetensors_load_file(local_path)

        # 1) Inject required `<module>.alpha` keys (neutral scaling alpha=rank).
        sd = _inject_missing_alpha_keys(sd)

        # 2) Keep only LoRA + alpha keys; drop ComfyUI patch/delta keys.
        sd, stats = _filter_to_diffusers_lora_keys(sd)

        # 3) Duplicate stripped keys (remove `diffusion_model.`) for compatibility.
        sd = _duplicate_stripped_prefix_keys(sd)

        print(
            "🧹 LoRA dict cleanup stats: "
            f"kept={stats['kept']} dropped_patch={stats['dropped_patch']} "
            f"dropped_other={stats['dropped_other']} normalized_alpha={stats['normalized_alpha']}"
        )

        pipe.load_lora_weights(sd, adapter_name=adapter_name)
        return


# ============================================================
# LoRA loader: single/package + strengths
# ============================================================


def _ensure_loaded_and_get_active_adapters(selected_lora: str):
    spec = ADAPTER_SPECS.get(selected_lora)
    if not spec:
        raise gr.Error(f"Configuration not found for: {selected_lora}")

    adapter_names = []
    adapter_weights = []

    if spec.get("type") == "package":
        parts = spec.get("parts", [])
        if not parts:
            raise gr.Error(f"Package spec has no parts: {selected_lora}")

        for part in parts:
            repo = part["repo"]
            weights = part["weights"]
            adapter_name = part["adapter_name"]
            strength = float(part.get("strength", 1.0))
            needs_alpha_fix = bool(part.get("needs_alpha_fix", False))

            if adapter_name not in LOADED_ADAPTERS:
                print(f"--- Downloading and Loading Adapter Part: {selected_lora} / {adapter_name} ---")
                try:
                    _load_lora_weights_with_fallback(
                        repo=repo,
                        weight_name=weights,
                        adapter_name=adapter_name,
                        needs_alpha_fix=needs_alpha_fix,
                    )
                    LOADED_ADAPTERS.add(adapter_name)
                except Exception as e:
                    raise gr.Error(f"Failed to load adapter part {selected_lora}/{adapter_name}: {e}")
            else:
                print(f"--- Adapter part already loaded: {selected_lora} / {adapter_name} ---")

            adapter_names.append(adapter_name)
            adapter_weights.append(strength)

    else:
        repo = spec["repo"]
        weights = spec["weights"]
        adapter_name = spec["adapter_name"]
        strength = float(spec.get("strength", 1.0))
        needs_alpha_fix = bool(spec.get("needs_alpha_fix", False))

        if adapter_name not in LOADED_ADAPTERS:
            print(f"--- Downloading and Loading Adapter: {selected_lora} ---")
            try:
                _load_lora_weights_with_fallback(
                    repo=repo,
                    weight_name=weights,
                    adapter_name=adapter_name,
                    needs_alpha_fix=needs_alpha_fix,
                )
                LOADED_ADAPTERS.add(adapter_name)
            except Exception as e:
                raise gr.Error(f"Failed to load adapter {selected_lora}: {e}")
        else:
            print(f"--- Adapter {selected_lora} is already loaded. ---")

        adapter_names = [adapter_name]
        adapter_weights = [strength]

    return adapter_names, adapter_weights


# ============================================================
# UI handlers
# ============================================================



def on_lora_change_ui(selected_lora, current_prompt, extras_condition_only):
    prompt_val = current_prompt
    if selected_lora != NONE_LORA:
        preset = LORA_PRESET_PROMPTS.get(selected_lora, "")
        if preset:
             prompt_val = preset
        else:
             prompt_val = "" # CLEAR THE PROMPT IF ACTIVE BUT NO PRESET
    
    prompt_update = gr.update(value=prompt_val)
    camera_update = gr.update(visible=(selected_lora == "3D-Camera"))

    # Image2 visibility/label
    if lora_requires_two_images(selected_lora):
        img2_update = gr.update(visible=True, label=image2_label_for_lora(selected_lora))
    else:
        img2_update = gr.update(visible=False, value=None, label='Upload Reference (Image 2)')

    # Extra references routing default
    if selected_lora in ('BFS-Best-FaceSwap', 'BFS-Best-FaceSwap-merge', 'AnyPose'):
        extras_update = gr.update(value=True)
    else:
        extras_update = gr.update(value=extras_condition_only)

    return prompt_update, img2_update, extras_update, camera_update
# ============================================================
# UI helpers: output routing + derived conditioning

def _append_to_gallery(existing_gallery, new_image):
    if existing_gallery is None:
        return [new_image]
    if not isinstance(existing_gallery, list):
        existing_gallery = [existing_gallery]
    existing_gallery.append(new_image)
    return existing_gallery

# ============================================================

def set_output_as_image1(last):
    if last is None:
        raise gr.Error("No output available yet.")
    return gr.update(value=last)


def set_output_as_image2(last):
    if last is None:
        raise gr.Error("No output available yet.")
    return gr.update(value=last)


def set_output_as_extra(last, existing_extra):
    if last is None:
        raise gr.Error("No output available yet.")
    return _append_to_gallery(existing_extra, last)


@spaces.GPU
def add_derived_ref(img1, existing_extra, derived_type, derived_use_gpu):
    if img1 is None:
        raise gr.Error("Please upload Image 1 first.")

    if derived_type == "None":
        return gr.update(value=existing_extra), gr.update(visible=False, value=None)

    base = img1.convert("RGB")

    if derived_type == "Depth (Depth Anything V2 Small)":
        derived = make_depth_map(base, use_gpu=bool(derived_use_gpu))
    else:
        raise gr.Error(f"Unknown derived type: {derived_type}")

    new_gallery = _append_to_gallery(existing_extra, derived)
    return gr.update(value=new_gallery), gr.update(visible=True, value=derived)


# ============================================================
# Inference
# ============================================================


@spaces.GPU
def infer(
    input_image_1,
    input_image_2,
    input_images_extra,  # gallery multi-image box
    prompt,
    lora_adapter,
    seed,
    randomize_seed,
    guidance_scale,
    steps,
    target_megapixels,
    extras_condition_only,
    pad_to_canvas,
    vae_tiling,  # VAE tiling toggle
    resolution_multiple,
    vae_ref_megapixels,
    decoder_vae,
    keep_decoder_2x,
    progress=gr.Progress(track_tqdm=True),
):
    gc.collect()
    if torch.cuda.is_available():
        torch.cuda.empty_cache()

    if input_image_1 is None:
        raise gr.Error("Please upload Image 1.")

    # Handle "None"
    if lora_adapter == NONE_LORA:
        try:
            pipe.set_adapters([], adapter_weights=[])
        except Exception:
            if LOADED_ADAPTERS:
                pipe.set_adapters(list(LOADED_ADAPTERS), adapter_weights=[0.0] * len(LOADED_ADAPTERS))
    else:
        adapter_names, adapter_weights = _ensure_loaded_and_get_active_adapters(lora_adapter)
        pipe.set_adapters(adapter_names, adapter_weights=adapter_weights)

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    generator = torch.Generator(device=device).manual_seed(seed)
    negative_prompt = (
        "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, "
        "extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
    )

    img1 = input_image_1.convert("RGB")
    img2 = input_image_2.convert("RGB") if input_image_2 is not None else None

    # Normalize extra images (Gallery) to PIL RGB (handles tuples from Gallery)
    extra_imgs: list[Image.Image] = []
    if input_images_extra:
        for item in input_images_extra:
            pil = _to_pil_rgb(item)
            if pil is not None:
                extra_imgs.append(pil)

    # Enforce existing 2-image LoRA behavior (image_1 + image_2 required)
    if lora_requires_two_images(lora_adapter) and img2 is None:
        raise gr.Error("This LoRA needs two images. Please upload Image 2 as well.")

    # Label images as image_1, image_2, image_3...
    labeled = build_labeled_images(img1, img2, extra_imgs)

    # Pass to pipeline in labeled order. Keep single-image call when only one is present.
    pipe_images = list(labeled.values())
    if len(pipe_images) == 1:
        pipe_images = pipe_images[0]

    # Resolution derived from Image 1 (base/body/target)
    # Use target *area* (≈ megapixels) rather than long-edge sizing to reduce FOV drift.
    target_area = get_target_area_for_lora(img1, lora_adapter, float(target_megapixels))
    width, height = compute_canvas_dimensions_from_area(
        img1,
        target_area=target_area,
        multiple_of=int(resolution_multiple),
    )

    # Decide which images participate in the VAE latent stream.
    # If enabled, extra references beyond (Img_1, Img_2) become conditioning-only.
    vae_image_indices = None
    if extras_condition_only:
        if isinstance(pipe_images, list) and len(pipe_images) > 2:
            vae_image_indices = [0, 1] if len(pipe_images) >= 2 else [0]

    try:
        print(
             "[DEBUG][infer] submitting request | "
             f"lora_adapter={lora_adapter!r} seed={seed} prompt={prompt!r}"
        )
        print(f"[DEBUG][infer] canvas={width}x{height} (~{(width*height)/1_048_576:.3f} MP) vae_tiling={bool(vae_tiling)}")

        # ✅ Apply UI toggle per-request (OFF by default)
        # Lattice multiple passed to pipeline too (anti-drift / valid size grid)
        res_mult = int(resolution_multiple) if resolution_multiple is not None else int(pipe.vae_scale_factor * 2)

        # Optional: override VAE sizing for *extra* references (beyond Image 1 / Image 2)
        # Interpreted as megapixels; 0 disables override (uses canvas).
        try:
            mp_ref = float(vae_ref_megapixels)
        except Exception:
            mp_ref = 0.0

        vae_ref_area = int(mp_ref * 1024 * 1024) if mp_ref and mp_ref > 0 else None

        # Extras start index depends on whether Image 2 exists
        base_ref_count = 2 if img2 is not None else 1

        _apply_vae_tiling(bool(vae_tiling))

        result = pipe(
            image=pipe_images,
            prompt=prompt,
            negative_prompt=negative_prompt,
            height=height,
            width=width,
            num_inference_steps=steps,
            generator=generator,
            true_cfg_scale=guidance_scale,
            vae_image_indices=vae_image_indices,
            pad_to_canvas=bool(pad_to_canvas),
            resolution_multiple=res_mult,
            vae_ref_area=vae_ref_area,
            vae_ref_start_index=base_ref_count,
            decoder_vae=str(decoder_vae).lower(),
            keep_decoder_2x=bool(keep_decoder_2x),
        ).images[0]
        return result, seed, result
    finally:
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()


@spaces.GPU
def infer_example(input_image, prompt, lora_adapter):
    if input_image is None:
        return None, 0, None
    input_pil = input_image.convert("RGB")
    guidance_scale = 1.0
    steps = 4
    # Examples don't supply Image 2 or extra images; and example list doesn't include AnyPose/BFS.
    # Keep VAE tiling OFF in examples (matches default).
    result, seed, last = infer(
        input_pil,
        None,
        None,
        prompt,
        lora_adapter,
        0,
        True,
        guidance_scale,
        steps,
        1.0,
        True,
        True,
        False,  # vae_tiling
    )
    return result, seed, last


# ============================================================
# UI
# ============================================================

css = """
#col-container {
    margin: 0 auto;
    max-width: 960px;
}
#main-title h1 {font-size: 2.1em !important;}
"""

aio_status_line = (
    f"**AIO transformer version:** `{AIO_VERSION}`  "
    f"({AIO_VERSION_SOURCE}; env `AIO_VERSION`={_AIO_ENV_RAW!r})"
)

with gr.Blocks() as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# **Qwen-Image-Edit-2511-LoRAs-Fast**", elem_id="main-title")
        gr.Markdown(
            f"""This **experimental** space for [QIE-2511](https://huggingface.co/Qwen/Qwen-Image-Edit-2511) utilizes [extracted transformers](https://huggingface.co/sdfafdfsdf/Phr00t-Qwen-Rapid-AIO) of [Phr00t’s Rapid AIO merge](https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO) and FA3-optimization with [LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2511) support and a couple of extra features:

- Optional conditioning-only routing for extra reference latents
- Uncapped canvas resolution
- Optional VAE tiling for high resolutions
- Optional depth mapping for conditioning
- Optional routing of output to input for further iterations
- Optional alternative decoder [VAE](https://huggingface.co/spacepxl/Wan2.1-VAE-upscale2x/tree/main/diffusers/Wan2.1_VAE_upscale2x_imageonly_real_v1)

Current environment is running **{AIO_VERSION}** of the Rapid AIO. Duplicate the space and set the **AIO_VERSION** space variable to use a different version."""
        )
        gr.Markdown(aio_status_line)

        with gr.Row(equal_height=True):
            with gr.Column():
                input_image_1 = gr.Image(label="Upload Image 1 (Base / Target)", type="pil", )

                input_image_2 = gr.Image(label="Upload Reference (Image 2)", type="pil", height=290, visible=False)
                
                with gr.Column(visible=False) as camera_container:
                    gr.Markdown("### 🎮 3D Camera Control\n*Drag handles: 🟢 Azimuth, 🩷 Elevation, 🟠 Distance*")
                    camera_3d = CameraControl3D(value={"azimuth": 0, "elevation": 0, "distance": 1.0}, elem_id="camera-3d-control")
                    gr.Markdown("### 🎚️ Slider Controls")
                    azimuth_slider = gr.Slider(label="Azimuth", minimum=0, maximum=315, step=45, value=0, info="0°=front, 90°=right, 180°=back, 270°=left")
                    elevation_slider = gr.Slider(label="Elevation", minimum=-30, maximum=60, step=30, value=0, info="-30°=low angle, 0°=eye, 60°=high angle")
                    distance_slider = gr.Slider(label="Distance", minimum=0.6, maximum=1.4, step=0.4, value=1.0, info="0.6=close, 1.0=medium, 1.4=wide")


                input_images_extra = gr.Gallery(
                    label="Upload Additional Images (auto-indexed after Image 1/2)",
                    type="pil",
                    height=290,
                    columns=4,
                    rows=2,
                    interactive=True,
                )

                prompt = gr.Text(
                    label="Edit Prompt",
                    show_label=True,
                    placeholder="e.g., transform into photo..",
                )

                run_button = gr.Button("Edit Image", variant="primary")

            with gr.Column():
                output_image = gr.Image(label="Output Image", interactive=False, format="png", height=353)

                last_output = gr.State(value=None)

                with gr.Row():
                    btn_out_to_img1 = gr.Button("⬅️ Output → Image 1", variant="secondary")
                    btn_out_to_img2 = gr.Button("⬅️ Output → Image 2", variant="secondary")
                    btn_out_to_extra = gr.Button("➕ Output → Extra Ref", variant="secondary")

                derived_preview = gr.Image(
                    label="Derived Conditioning Preview",
                    interactive=False,
                    format="png",
                    height=200,
                    visible=False,
                )

                with gr.Row():
                    lora_choices = [NONE_LORA] + list(ADAPTER_SPECS.keys())
                    lora_adapter = gr.Dropdown(
                        label="Choose Editing Style",
                        choices=lora_choices,
                        value=NONE_LORA,
                    )

                with gr.Accordion("Advanced Settings", open=False, visible=True):
                    with gr.Accordion("Derived Conditioning (Pose / Depth)", open=False):
                        derived_type = gr.Dropdown(
                            label="Derived Type (from Image 1)",
                            choices=["None", "Depth (Depth Anything V2 Small)"],
                            value="None",
                        )
                        derived_use_gpu = gr.Checkbox(label="Use GPU for derived model", value=False)
                        add_derived_btn = gr.Button("➕ Add derived ref to Extras (conditioning-only recommended)")

                    seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
                    randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
                    guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
                    steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
                    target_megapixels = gr.Slider(
                        label="Target Megapixels (canvas, 0 = match input area)",
                        minimum=0.0,
                        maximum=6.0,
                        step=0.1,
                        value=1.0,
                    )
                    resolution_multiple = gr.Dropdown(
                        label="Resolution lattice multiple (anti-drift)",
                        choices=[32, 56, 112],
                        value=32,
                        interactive=True,
                    )
                    vae_ref_megapixels = gr.Slider(
                        label="Extra refs VAE megapixels override (0 = use canvas)",
                        minimum=0.0,
                        maximum=6.0,
                        step=0.1,
                        value=0.0,
                    )
                    decoder_vae = gr.Dropdown(
                        label="Decoder VAE",
                        choices=["qwen", "wan2x"],
                        value="qwen",
                        interactive=True,
                    )
                    keep_decoder_2x = gr.Checkbox(
                        label="Keep 2× output (wan2x only)",
                        value=False,
                    )
                    extras_condition_only = gr.Checkbox(
                        label="Extra references are conditioning-only (exclude from VAE)",
                        value=True,
                    )
                    pad_to_canvas = gr.Checkbox(
                        label="Pad images to canvas aspect (avoid warping)",
                        value=True,
                    )

                    # ✅ NEW: VAE tiling toggle (OFF by default)
                    vae_tiling = gr.Checkbox(
                        label="VAE tiling (lower VRAM, slower)",
                        value=False,
                    )

        # On LoRA selection: preset prompt + toggle Image 2
        lora_adapter.change(
            fn=on_lora_change_ui,
            inputs=[lora_adapter, prompt, extras_condition_only],
            outputs=[prompt, input_image_2, extras_condition_only, camera_container],
        )

        # Examples removed automatically by setup_manager


    # --- 3D Camera Events ---
    def update_prompt_from_sliders(az, el, dist, curr_prompt):
        return update_prompt_with_camera(az, el, dist, curr_prompt)
        
    def sync_3d_to_sliders(cv, curr_prompt):
        if cv and isinstance(cv, dict):
            az = cv.get('azimuth', 0)
            el = cv.get('elevation', 0)
            dist = cv.get('distance', 1.0)
            return az, el, dist, update_prompt_with_camera(az, el, dist, curr_prompt)
        return gr.update(), gr.update(), gr.update(), gr.update()
        
    def sync_sliders_to_3d(az, el, dist):
        return {"azimuth": az, "elevation": el, "distance": dist}

        
    def update_3d_image(img):
        if img is None: return gr.update(imageUrl=None)
        import base64
        from io import BytesIO
        buf = BytesIO()
        img.save(buf, format="PNG")
        durl = f"data:image/png;base64,{base64.b64encode(buf.getvalue()).decode()}"
        return gr.update(imageUrl=durl)

    for slider in [azimuth_slider, elevation_slider, distance_slider]:
        slider.change(fn=update_prompt_from_sliders, inputs=[azimuth_slider, elevation_slider, distance_slider, prompt], outputs=[prompt])
        slider.release(fn=sync_sliders_to_3d, inputs=[azimuth_slider, elevation_slider, distance_slider], outputs=[camera_3d])
        
    camera_3d.change(fn=sync_3d_to_sliders, inputs=[camera_3d, prompt], outputs=[azimuth_slider, elevation_slider, distance_slider, prompt])
    
    input_image_1.upload(fn=update_3d_image, inputs=[input_image_1], outputs=[camera_3d])
    input_image_1.clear(fn=lambda: gr.update(imageUrl=None), outputs=[camera_3d])

    run_button.click(
        fn=infer,
        inputs=[
            input_image_1,
            input_image_2,
            input_images_extra,
            prompt,
            lora_adapter,
            seed,
            randomize_seed,
            guidance_scale,
            steps,
            target_megapixels,
            extras_condition_only,
            pad_to_canvas,
            vae_tiling,
            resolution_multiple,
            vae_ref_megapixels,
            decoder_vae,
            keep_decoder_2x,
        ],
        outputs=[output_image, seed, last_output],
    )

    # Output routing buttons
    btn_out_to_img1.click(fn=set_output_as_image1, inputs=[last_output], outputs=[input_image_1])
    btn_out_to_img2.click(fn=set_output_as_image2, inputs=[last_output], outputs=[input_image_2])
    btn_out_to_extra.click(fn=set_output_as_extra, inputs=[last_output, input_images_extra], outputs=[input_images_extra])

    # Derived conditioning: append pose/depth map as extra ref (UI shows preview)
    add_derived_btn.click(
        fn=add_derived_ref,
        inputs=[input_image_1, input_images_extra, derived_type, derived_use_gpu],
        outputs=[input_images_extra, derived_preview],
    )

if __name__ == "__main__":
    head = '<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>'
    demo.queue(max_size=30).launch(head=head, server_name="0.0.0.0", share=True, 
        css=css,
        theme=orange_red_theme,
        mcp_server=True,
        ssr_mode=False,
        show_error=True,
    )

# Manual Patch for missing prompts
try:
    LORA_PRESET_PROMPTS.update({
        "Consistance": "improve consistency and quality of the generated image",
        "F2P": "transform the image into a high-quality photo with realistic details",
        "Multiple-Angles": "change the camera angle of the image",
        "Light-Restoration": "Remove shadows and relight the image using soft lighting",
        "Relight": "Relight the image with cinematic lighting",
        "Multi-Angle-Lighting": "Change the lighting direction and intensity",
        "Edit-Skin": "Enhance skin textures and natural details",
        "Next-Scene": "Generate the next scene based on the current image",
        "Flat-Log": "Desaturate and lower contrast for a flat log look",
        "Upscale-Image": "Enhance and sharpen the image details",
        "BFS-Best-FaceSwap": "head_swap : start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2, strictly preserving the hair, eye color, and nose structure, mouth, lips and front head of Picture 2. copy the eye direction, head rotation, and micro-expressions from Picture 1. high quality, sharp details, 4k",
        "BFS-Best-FaceSwap-merge": "head_swap : start with Picture 1 as the base image, keeping its lighting, environment, and background. remove the head from Picture 1 completely and replace it with the head from Picture 2, strictly preserving the hair, eye color, and nose structure, mouth, lips and front head of Picture 2. copy the eye direction, head rotation, and micro-expressions from Picture 1. high quality, sharp details, 4k",
        "Qwen-lora-nsfw": "Convert this picture to artistic style.",
    })
except NameError:
    pass