File size: 49,901 Bytes
feee32b
342ec8c
 
 
 
 
 
 
8049492
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42cbb83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
109c1c7
342ec8c
 
 
 
 
 
 
feee32b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342ec8c
6f83a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
6f83a93
 
 
 
 
2d2d6b8
6f83a93
2d2d6b8
342ec8c
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109c1c7
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
feee32b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342ec8c
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97a97db
342ec8c
 
 
 
 
 
 
7ad0a73
342ec8c
 
90b0db1
2d2d6b8
 
 
 
 
 
7ad0a73
2d2d6b8
 
 
342ec8c
2d2d6b8
 
 
 
 
 
7ad0a73
2d2d6b8
 
 
342ec8c
 
 
 
97a97db
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ba29d
 
feee32b
97a97db
81ba29d
97a97db
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97a97db
342ec8c
 
 
 
 
 
 
 
 
 
 
 
42cbb83
 
 
 
 
342ec8c
 
42cbb83
342ec8c
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feee32b
6f83a93
 
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
feee32b
 
 
 
 
 
6f83a93
 
 
 
 
 
2d2d6b8
6f83a93
 
 
 
342ec8c
2d2d6b8
342ec8c
 
 
 
 
6f83a93
342ec8c
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feee32b
 
 
 
 
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feee32b
342ec8c
 
 
 
feee32b
342ec8c
2d2d6b8
6f83a93
342ec8c
feee32b
 
342ec8c
 
 
 
 
 
 
 
 
 
6f83a93
342ec8c
 
 
 
 
 
6f83a93
342ec8c
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feee32b
 
 
 
 
 
 
 
6f83a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342ec8c
 
6f83a93
342ec8c
6f83a93
342ec8c
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d2d6b8
342ec8c
 
 
 
 
 
 
 
 
6f83a93
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109c1c7
 
 
2d2d6b8
109c1c7
 
342ec8c
109c1c7
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feee32b
342ec8c
 
feee32b
 
342ec8c
 
 
 
 
 
 
 
feee32b
 
342ec8c
 
 
 
 
2d2d6b8
 
 
342ec8c
 
 
feee32b
342ec8c
 
 
 
 
 
feee32b
 
 
 
 
 
 
 
 
 
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8049492
 
 
 
342ec8c
 
 
 
 
 
 
feee32b
6f83a93
 
342ec8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97a97db
 
 
 
 
8049492
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
"""Wan 2.2 Space with ZeroGPU, Turbo 4-8 Step Distill, Multi-LoRA (Civitai + HF), Presets, Trigger Words, and 300-Hours Optimizations."""

from __future__ import annotations

import hashlib
import ipaddress
import json
import mimetypes
import os
import random
import re
import shutil
import socket
import tempfile
import time
import traceback
from functools import cache
from urllib.parse import urljoin, urlsplit

import spaces
import gradio as gr
import torch
from PIL import Image, ImageOps
from diffusers.utils import export_to_video
from safetensors import safe_open

# =========================================================================
# Monkey-patch Diffusers Wan LoRA converter for broad community compatibility
# =========================================================================
try:
    import diffusers.loaders.lora_conversion_utils as conv_utils
    import diffusers.loaders.lora_pipeline as lora_pipe

    _orig_wan_converter = getattr(conv_utils, "_convert_non_diffusers_wan_lora_to_diffusers", None)

    def patched_convert_non_diffusers_wan_lora_to_diffusers(original_state_dict):
        # 1. Normalize prefixes: community LoRAs often start directly with "blocks."
        # while diffusers internal converter expects "diffusion_model.blocks."
        prefixed_dict = {}
        for k, v in original_state_dict.items():
            if k.startswith("blocks."):
                prefixed_dict[f"diffusion_model.{k}"] = v
            elif k.startswith("transformer.blocks."):
                prefixed_dict[f"diffusion_model.{k[12:]}"] = v
            else:
                prefixed_dict[k] = v

        if _orig_wan_converter is not None:
            try:
                # Attempt standard conversion with normalized keys
                return _orig_wan_converter(prefixed_dict.copy())
            except Exception as err:
                print(f"[wan] standard converter note: {err}; applying robust fallback mapping", flush=True)

        # 2. Robust fallback mapping for any non-standard Wan LoRA format
        converted_dict = {}
        for k, v in prefixed_dict.items():
            new_k = k
            if new_k.startswith("diffusion_model."):
                new_k = new_k[len("diffusion_model."):]
            if not new_k.startswith("transformer."):
                new_k = f"transformer.{new_k}"
            converted_dict[new_k] = v
        return converted_dict

    if _orig_wan_converter is not None:
        conv_utils._convert_non_diffusers_wan_lora_to_diffusers = patched_convert_non_diffusers_wan_lora_to_diffusers
    if hasattr(lora_pipe, "_convert_non_diffusers_wan_lora_to_diffusers"):
        lora_pipe._convert_non_diffusers_wan_lora_to_diffusers = patched_convert_non_diffusers_wan_lora_to_diffusers
    print("[wan] LoRA converter patch successfully applied.", flush=True)
except Exception as patch_err:
    print(f"[wan] could not apply LoRA converter patch: {patch_err}", flush=True)

DEFAULT_CIVITAI_KEY = "50a9e1bd474c03b856070a7272d8015c"
DEFAULT_MODEL_REPO = os.environ.get("WAN_MODEL_REPO", "Wan-AI/Wan2.2-I2V-A14B-Diffusers")
GPU_SIZE = os.environ.get("WAN_GPU_SIZE", "xlarge")
MAX_GPU_DURATION = int(os.environ.get("WAN_MAX_GPU_DURATION", "300"))
OUTPUT_DIR = os.path.join(tempfile.gettempdir(), "wan-outputs")
LOCAL_LORAS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "loras")
LORA_MAX_BYTES = 2 * 1024**3
DEFAULT_FPS = 16

# =========================================================================
# Turbo Distillation LoRAs (4-Step & 8-Step Acceleration)
# =========================================================================
TURBO_PRESETS = {
    "⚡ Turbo 4-Passos (LightX2V Distill - 4 Steps Ultra Rápido)": {
        "wan2.2_i2v": ("lightx2v/Wan2.2-Distill-Loras", "wan2.2_i2v_A14b_high_noise_lora_rank64_lightx2v_4step_1022.safetensors"),
        "wan2.2_t2v": ("lightx2v/Wan2.2-Distill-Loras", "wan2.2_t2v_A14b_high_noise_lora_rank64_lightx2v_4step_1217.safetensors"),
        "wan2.1_i2v": ("lightx2v/Wan2.1-Distill-Loras", "wan2.1_i2v_lora_rank64_lightx2v_4step.safetensors"),
        "wan2.1_t2v": ("lightx2v/Wan2.1-Distill-Loras", "wan2.1_t2v_14b_lora_rank64_lightx2v_4step.safetensors"),
        "default_steps": 4,
        "default_cfg": 1.5,
        "description": "Destilação 4-passos LightX2V. Renderiza o vídeo em ~15-25 segundos mantendo alta qualidade!",
    },
    "⚡ Turbo 8-Passos (8 Steps Balanceado)": {
        "wan2.2_i2v": ("lightx2v/Wan2.2-Distill-Loras", "wan2.2_i2v_A14b_low_noise_lora_rank64_lightx2v_4step_1022.safetensors"),
        "wan2.2_t2v": ("lightx2v/Wan2.2-Distill-Loras", "wan2.2_t2v_A14b_low_noise_lora_rank64_lightx2v_4step_1217.safetensors"),
        "wan2.1_i2v": ("lightx2v/Wan2.1-Distill-Loras", "wan2.1_i2v_lora_rank64_lightx2v_4step.safetensors"),
        "wan2.1_t2v": ("lightx2v/Wan2.1-Distill-Loras", "wan2.1_t2v_14b_lora_rank64_lightx2v_4step.safetensors"),
        "default_steps": 8,
        "default_cfg": 2.0,
        "description": "Destilação 8-passos para equilíbrio perfeito entre velocidade e detalhes.",
    },
    "🚫 Desativado (Modo Normal - 15 a 25 Passos)": {
        "default_steps": 20,
        "default_cfg": 5.0,
        "description": "Modo padrão sem aceleração Turbo (requer 15-25 passos).",
    },
}

# =========================================================================
# Base NSFW Adapters (Unlocks adult generation on Wan Base Models)
# =========================================================================
NSFW_BASE_ADAPTERS = {
    "WAN General NSFW Model (Civitai 1307155 - Recomendado)": {
        "source": "https://civitai.com/models/1307155",
        "trigger_words": "nsfwsks, uncensored, naked, explicit",
        "default_strength": 1.0,
        "description": "Desbloqueia anatomia e geração explícita no modelo base Wan (Civitai Model 1307155).",
    },
    "HMNSFW AIO V2 Base (Civitai 2834417)": {
        "source": "https://civitai.com/api/download/models/3206518",
        "trigger_words": "hmmotion",
        "default_strength": 0.5,
        "description": "Adaptador anatômico geral HMNSFW V2 para Wan 2.1/2.2.",
    },
    "Desativado / Apenas Base SFW": {
        "source": "",
        "trigger_words": "",
        "default_strength": 0.0,
        "description": "Usa apenas o modelo base padrão sem adaptador NSFW adicional.",
    },
}

# =========================================================================
# Preset Catalog for Wan 2.2 LoRAs (Actions, Motions & Styles)
# =========================================================================
LORA_PRESETS = {
    "None / Desativado": {
        "type": "none",
        "trigger_words": "",
        "default_strength": 1.0,
        "description": "Nenhum LoRA selecionado neste slot.",
    },
    "WAN General NSFW model (Civitai 1307155)": {
        "type": "civitai",
        "source": "https://civitai.com/models/1307155",
        "trigger_words": "nsfwsks, uncensored, naked, explicit",
        "default_strength": 1.0,
        "description": "LoRA Geral NSFW para Wan 2.2 (Civitai 1307155).",
    },
    "HMNSFW AIO V2 / hmmotion (Wan 2.2)": {
        "type": "civitai",
        "source": "https://civitai.com/api/download/models/3206518",
        "trigger_words": "hmmotion, missionary, side, fast, third-person side view, medium shot.",
        "default_strength": 0.5,
        "description": "LoRA All-in-One de anatomia e movimento realista (Civitai 2834417 / 3206518). Use força <= 0.5 com prompts descritivos.",
    },
    "Icy Twerk Pro Max (Wan 2.2)": {
        "type": "civitai",
        "source": "https://civitai.com/api/download/models/3201584",
        "trigger_words": "icytw3rk, twerking, booty shake, rhythmic hip movement, dynamic motion, bouncing buttocks, high quality",
        "default_strength": 0.9,
        "description": "LoRA de animação e movimento de twerk / booty shake para Wan (Civitai 2836640 / 3201584).",
    },
    "Cumouf - Oral Creampie / CIM with Spasms": {
        "type": "civitai",
        "source": "https://civitai.com/api/download/models/3223411",
        "trigger_words": "cum in mouth, oral creampie, cum overflow, spasms, throat bulge, choking on cum, messy facial, open mouth",
        "default_strength": 0.85,
        "description": "Oral creampie com espasmos faciais e garganta (Civitai 2846978 / 3223411).",
    },
    "Epic Cumshots & Facials": {
        "type": "civitai",
        "source": "https://civitai.com/api/download/models/3052864",
        "trigger_words": "cumshot, thick semen, facial, climax, sticky ejaculation, messy dripping, high viscosity",
        "default_strength": 0.9,
        "description": "Ejaculação realista de alta viscosidade com respingos faciais e corporais.",
    },
    "Dynamic Cinematic Camera Motion": {
        "type": "prompt_only",
        "trigger_words": "dynamic cinematic camera, slow orbit shot, dramatic lighting, sweeping drone view, motion blur",
        "default_strength": 0.85,
        "description": "Movimento de câmera fluído e cinematográfico.",
    },
    "Cyberpunk / Sci-Fi Neon Realism": {
        "type": "prompt_only",
        "trigger_words": "cyberpunk, holographic HUD, volumetric neon reflections, cybernetic glow, futuristic city, 8k cinematic",
        "default_strength": 0.9,
        "description": "Estilo cyberpunk hiper-detalhado com iluminação volumétrica e neons.",
    },
}

# 300 Hours Civitai Guide: Strict multiples-of-16 resolutions
CANVASES = {
    # 16:9 Landscape
    "832x480 · 16:9 Landscape (Fast 480p)": (480, 832),
    "960x544 · 16:9 Landscape (Balanced)": (544, 960),
    "1280x720 · 16:9 Landscape (HD 720p)": (720, 1280),
    # 9:16 Portrait / Reels
    "480x832 · 9:16 Portrait (Fast 480p)": (832, 480),
    "544x960 · 9:16 Portrait (Balanced)": (960, 544),
    "720x1280 · 9:16 Portrait (HD 720p)": (1280, 720),
    # 1:1 Square
    "640x640 · 1:1 Square (Fast)": (640, 640),
    "768x768 · 1:1 Square (HD)": (768, 768),
    # 4:3 / 3:4
    "768x576 · 4:3 Standard": (576, 768),
    "576x768 · 3:4 Portrait": (768, 576),
    # 21:9 Ultrawide
    "1152x512 · 21:9 Ultrawide": (512, 1152),
}
DEFAULT_CANVAS = "832x480 · 16:9 Landscape (Fast 480p)"

PIPE = None
CURRENT_MODEL_REPO = None
LOAD_ERROR: str | None = None
LOADED_IN: float | None = None


def get_local_loras() -> list[str]:
    """Scans the local loras/ folder for .safetensors files."""
    if not os.path.exists(LOCAL_LORAS_DIR):
        try:
            os.makedirs(LOCAL_LORAS_DIR, exist_ok=True)
        except Exception:
            return []
    files = [f for f in os.listdir(LOCAL_LORAS_DIR) if f.endswith(".safetensors")]
    return sorted(files)


def normalize_civitai_url(url: str) -> str:
    """Extracts direct download link from any Civitai model or version URL."""
    url = url.strip()
    if not url:
        return url
    
    match_version = re.search(r"modelVersionId=(\d+)", url)
    if match_version:
        version_id = match_version.group(1)
        return f"https://civitai.com/api/download/models/{version_id}"
    
    if "api/download/models/" in url:
        return re.sub(r"https?://[^/]+", "https://civitai.com", url)

    match_model = re.search(r"civitai\.(?:com|red|org|blue|work)/models/(\d+)", url)
    if match_model:
        model_id = match_model.group(1)
        try:
            import requests
            r = requests.get(f"https://civitai.com/api/v1/models/{model_id}", timeout=8)
            if r.ok:
                data = r.json()
                versions = data.get("modelVersions", [])
                if versions and "id" in versions[0]:
                    return f"https://civitai.com/api/download/models/{versions[0]['id']}"
        except Exception as err:
            print(f"[civitai] failed to resolve model {model_id} metadata: {err}", flush=True)
    return url


def resolve_canvas(value: str) -> str:
    canvas = str(value).strip()
    if canvas in CANVASES:
        return canvas
    return DEFAULT_CANVAS


def _sha256(path: str) -> str:
    digest = hashlib.sha256()
    with open(path, "rb") as source:
        for chunk in iter(lambda: source.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def _lora_cache_directory(source: str) -> str:
    root = os.path.join(tempfile.gettempdir(), "wan-lora-downloads")
    cache_key = hashlib.sha256(source.encode()).hexdigest()[:20]
    local_dir = os.path.join(root, cache_key)
    os.makedirs(local_dir, exist_ok=True)
    others = sorted(
        (entry for entry in os.scandir(root) if entry.is_dir() and entry.path != local_dir),
        key=lambda entry: entry.stat().st_mtime,
        reverse=True,
    )
    for stale in others[4:]:
        shutil.rmtree(stale.path, ignore_errors=True)
    return local_dir


def _validate_public_lora_url(url: str) -> str:
    if len(url) > 2048:
        raise ValueError("Direct LoRA URL is too long.")
    parsed = urlsplit(url)
    if parsed.scheme.lower() != "https" or not parsed.hostname:
        raise ValueError("Direct LoRA URLs must use public HTTPS.")
    if parsed.username or parsed.password or parsed.port not in (None, 443):
        raise ValueError("Direct LoRA URLs cannot contain credentials or non-standard ports.")
    try:
        addresses = {item[4][0] for item in socket.getaddrinfo(parsed.hostname, 443, type=socket.SOCK_STREAM)}
    except socket.gaierror as error:
        raise ValueError("Direct LoRA URL hostname could not be resolved.") from error
    for raw_address in addresses:
        address = ipaddress.ip_address(raw_address)
        if isinstance(address, ipaddress.IPv6Address) and address.ipv4_mapped is not None:
            address = address.ipv4_mapped
        if not address.is_global:
            raise ValueError("Direct LoRA URLs cannot access private or local networks.")
    return url


def _download_lora_url(url: str, civitai_token: str = "") -> tuple[str, str]:
    import requests

    token = (civitai_token or os.environ.get("CIVITAI_API_KEY", "") or os.environ.get("CIVITAI_TOKEN", "") or DEFAULT_CIVITAI_KEY).strip()

    if "civitai." in url and token and "token=" not in url:
        sep = "&" if "?" in url else "?"
        url = f"{url}{sep}token={token}"

    original = _validate_public_lora_url(url)
    local_dir = _lora_cache_directory(original)
    path = os.path.join(local_dir, "adapter.safetensors")
    if os.path.isfile(path) and 0 < os.path.getsize(path) <= LORA_MAX_BYTES:
        try:
            with safe_open(path, framework="pt", device="cpu") as handle:
                if handle.keys():
                    os.utime(local_dir, None)
                    parsed = urlsplit(original)
                    return path, f"{parsed.hostname}{parsed.path}"[:180]
        except Exception:
            os.unlink(path)

    temporary = path + ".download"
    current = original
    for _ in range(6):
        current = _validate_public_lora_url(current)
        parsed_current = urlsplit(current)
        req_headers = {"User-Agent": "Wan-Studio-Space/1.0"}
        if token and ("civitai.com" in parsed_current.netloc or "civitai.red" in parsed_current.netloc):
            req_headers["Authorization"] = f"Bearer {token}"

        try:
            with requests.get(
                current,
                stream=True,
                allow_redirects=False,
                timeout=(15, 300),
                headers=req_headers,
            ) as response:
                if response.is_redirect or response.is_permanent_redirect:
                    location = response.headers.get("location")
                    if not location:
                        raise ValueError("Direct LoRA URL returned an empty redirect.")
                    current = urljoin(current, location)
                    continue
                if response.status_code in (401, 403):
                    raise gr.Error(
                        "🔒 O Civitai bloqueou o download deste modelo (401 Unauthorized / NSFW). "
                        "Verifique sua Civitai API Key nas configurações."
                    )
                response.raise_for_status()
                total = 0
                with open(temporary, "wb") as output:
                    for chunk in response.iter_content(1024 * 1024):
                        if not chunk:
                            continue
                        total += len(chunk)
                        if total > LORA_MAX_BYTES:
                            raise ValueError("Direct LoRA exceeds the 2 GiB safety limit.")
                        output.write(chunk)
                with safe_open(temporary, framework="pt", device="cpu") as handle:
                    if not handle.keys():
                        raise ValueError("Direct LoRA contains no safetensors tensors.")
                os.replace(temporary, path)
                os.utime(local_dir, None)
                parsed = urlsplit(original)
                return path, f"{parsed.hostname}{parsed.path}"[:180]
        except requests.exceptions.HTTPError as err:
            if "response" in locals() and response.status_code in (401, 403):
                raise gr.Error(
                    "🔒 O Civitai bloqueou o download deste modelo (401 Unauthorized / NSFW). "
                    "Verifique sua Civitai API Key nas configurações."
                ) from err
            raise
    raise ValueError("Too many redirects downloading LoRA.")


def resolve_turbo_lora(turbo_choice: str, model_repo: str) -> tuple[str | None, float]:
    """Resolves and downloads the appropriate 4-step or 8-step distillation LoRA."""
    if turbo_choice not in TURBO_PRESETS or "Desativado" in turbo_choice:
        return None, 0.0

    spec = TURBO_PRESETS[turbo_choice]
    is_i2v = "I2V" in model_repo or "i2v" in model_repo or "TI2V" in model_repo
    is_wan22 = "Wan2.2" in model_repo or "wan2.2" in model_repo

    if is_wan22:
        key = "wan2.2_i2v" if is_i2v else "wan2.2_t2v"
    else:
        key = "wan2.1_i2v" if is_i2v else "wan2.1_t2v"

    if key not in spec:
        return None, 0.0

    repo_id, filename = spec[key]
    from huggingface_hub import hf_hub_download
    local_dir = _lora_cache_directory(f"hf://{repo_id}/{filename}")
    path = hf_hub_download(repo_id=repo_id, filename=filename, token=False, local_dir=local_dir)
    os.utime(local_dir, None)
    return path, 1.0


def resolve_single_lora(
    preset_type: str, custom_url: str, hf_repo: str, hf_file: str, local_file: str, strength: float, civitai_token: str = ""
) -> tuple[str | None, str, float]:
    """Resolves one LoRA file path, label and scale for Wan 2.2."""
    if float(strength) == 0.0 or preset_type in ("None / Desativado", "None", ""):
        return None, "None", 0.0

    if preset_type in LORA_PRESETS:
        spec = LORA_PRESETS[preset_type]
        if spec.get("type") == "hf":
            from huggingface_hub import hf_hub_download
            repo_id = spec["hf_repo"]
            filename = spec["hf_file"]
            local_dir = _lora_cache_directory(f"hf://{repo_id}/{filename}")
            path = hf_hub_download(repo_id=repo_id, filename=filename, token=False, local_dir=local_dir)
            os.utime(local_dir, None)
            return path, preset_type, strength

        if spec.get("source"):
            url = normalize_civitai_url(spec["source"])
            if url:
                path, label = _download_lora_url(url, civitai_token)
                return path, preset_type, strength
        return None, "None", 0.0

    if preset_type == "Custom URL / Civitai":
        url = normalize_civitai_url(custom_url)
        if not url:
            return None, "None", 0.0
        path, label = _download_lora_url(url, civitai_token)
        return path, f"URL: {label}", strength

    if preset_type == "Custom Hugging Face":
        repo_id = str(hf_repo or "").strip()
        filename = str(hf_file or "").strip()
        if not repo_id or not filename:
            return None, "None", 0.0
        if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repo_id):
            raise ValueError("Custom LoRA repo must be in `owner/repository` format.")
        if not filename.endswith(".safetensors"):
            raise ValueError("Custom LoRA file must be a `.safetensors` file.")

        from huggingface_hub import get_hf_file_metadata, hf_hub_download, hf_hub_url
        metadata = get_hf_file_metadata(hf_hub_url(repo_id, filename), token=False)
        if metadata.size is None or metadata.size > LORA_MAX_BYTES:
            raise ValueError("LoRA file exceeds the 2 GiB limit.")
        local_dir = _lora_cache_directory(f"hf://{repo_id}/{filename}")
        path = hf_hub_download(repo_id=repo_id, filename=filename, token=False, local_dir=local_dir)
        os.utime(local_dir, None)
        return path, f"{repo_id}/{filename}", strength

    if preset_type == "Local File (loras/ folder)":
        if not local_file:
            return None, "None", 0.0
        local_path = os.path.join(LOCAL_LORAS_DIR, local_file)
        if not os.path.isfile(local_path):
            raise ValueError(f"Arquivo local {local_file} não encontrado na pasta loras/.")
        return local_path, f"local:{local_file}", strength

    return None, "None", 0.0


def get_or_load_pipeline(model_repo: str = DEFAULT_MODEL_REPO):
    global PIPE, CURRENT_MODEL_REPO, LOAD_ERROR, LOADED_IN

    if PIPE is not None and CURRENT_MODEL_REPO == model_repo:
        return PIPE

    started = time.time()
    try:
        from diffusers import DiffusionPipeline, WanImageToVideoPipeline, WanPipeline

        print(f"[wan] loading pipeline from {model_repo} ...", flush=True)
        if "I2V" in model_repo or "i2v" in model_repo or "TI2V" in model_repo:
            try:
                pipe = WanImageToVideoPipeline.from_pretrained(
                    model_repo,
                    torch_dtype=torch.bfloat16,
                )
            except Exception:
                pipe = DiffusionPipeline.from_pretrained(
                    model_repo,
                    torch_dtype=torch.bfloat16,
                )
        else:
            try:
                pipe = WanPipeline.from_pretrained(
                    model_repo,
                    torch_dtype=torch.bfloat16,
                )
            except Exception:
                pipe = DiffusionPipeline.from_pretrained(
                    model_repo,
                    torch_dtype=torch.bfloat16,
                )

        PIPE = pipe
        CURRENT_MODEL_REPO = model_repo
        LOADED_IN = time.time() - started
        print(f"[wan] ready in {LOADED_IN:.0f}s on CPU", flush=True)
    except Exception as error:
        traceback.print_exc()
        LOAD_ERROR = f"**Loading `{model_repo}` failed**: `{type(error).__name__}: {error}`"
        raise RuntimeError(LOAD_ERROR) from error
    return PIPE


def _fit_keyframe(image_input, target_width: int, target_height: int) -> Image.Image:
    if isinstance(image_input, str):
        img = Image.open(image_input)
    elif isinstance(image_input, Image.Image):
        img = image_input
    else:
        raise ValueError("Invalid image input")

    img = ImageOps.exif_transpose(img).convert("RGB")
    target_aspect = target_width / target_height
    img_aspect = img.width / img.height

    if abs(img_aspect - target_aspect) > 1e-3:
        if img_aspect > target_aspect:
            new_w = int(img.height * target_aspect)
            left = (img.width - new_w) // 2
            img = img.crop((left, 0, left + new_w, img.height))
        else:
            new_h = int(img.width / target_aspect)
            top = (img.height - new_h) // 2
            img = img.crop((0, top, img.width, top + new_h))

    img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
    return img


def get_duration(*args, **kwargs):
    try:
        steps = kwargs.get("steps", args[6] if len(args) > 6 else 4)
        return max(180, min(MAX_GPU_DURATION, int(steps) * 8 + 60))
    except Exception:
        return 240


@spaces.GPU(duration=get_duration, size=GPU_SIZE)
def _generate_video_gpu(
    prompt: str,
    negative_prompt: str,
    image_input: Image.Image | None,
    height: int,
    width: int,
    num_frames: int,
    steps: int,
    guidance_scale: float,
    seed: int,
    lora_configs: list[tuple[str, float]],
    model_repo: str,
):
    pipe = get_or_load_pipeline(model_repo)
    pipe.to("cuda")

    active_lora_names = []
    if lora_configs:
        try:
            pipe.unload_lora_weights()
        except Exception:
            pass

        for lora_path, lora_scale in lora_configs:
            if lora_path and lora_scale > 0:
                adapter_name = f"lora_{len(active_lora_names)}"
                try:
                    pipe.load_lora_weights(lora_path, adapter_name=adapter_name)
                    active_lora_names.append(adapter_name)
                except Exception as lora_err:
                    print(f"[wan] warning: skipping incompatible LoRA `{lora_path}`: {lora_err}", flush=True)

        if active_lora_names:
            scales = [scale for _, scale in lora_configs if scale > 0][:len(active_lora_names)]
            pipe.set_adapters(active_lora_names, adapter_weights=scales)

    generator = torch.Generator("cuda").manual_seed(int(seed))

    try:
        with torch.inference_mode():
            if image_input is not None and ("I2V" in model_repo or "i2v" in model_repo or "TI2V" in model_repo):
                output = pipe(
                    image=image_input,
                    prompt=prompt,
                    negative_prompt=negative_prompt if negative_prompt else None,
                    height=height,
                    width=width,
                    num_frames=num_frames,
                    num_inference_steps=int(steps),
                    guidance_scale=float(guidance_scale),
                    generator=generator,
                )
            else:
                output = pipe(
                    prompt=prompt,
                    negative_prompt=negative_prompt if negative_prompt else None,
                    height=height,
                    width=width,
                    num_frames=num_frames,
                    num_inference_steps=int(steps),
                    guidance_scale=float(guidance_scale),
                    generator=generator,
                )
        frames = output.frames[0]
    finally:
        if active_lora_names:
            try:
                pipe.unload_lora_weights()
            except Exception:
                pass

    return frames


def generate_video(
    prompt: str,
    negative_prompt: str,
    input_image: Image.Image | str | None,
    turbo_choice: str,
    nsfw_base_choice: str,
    nsfw_base_strength: float,
    canvas: str,
    num_frames: int,
    fps: int,
    steps: int,
    guidance_scale: float,
    seed: int,
    randomize_seed: bool,
    model_choice: str,
    lora1_preset: str,
    lora1_custom_url: str,
    lora1_hf_repo: str,
    lora1_hf_file: str,
    lora1_local_file: str,
    lora1_strength: float,
    lora2_preset: str,
    lora2_custom_url: str,
    lora2_hf_repo: str,
    lora2_hf_file: str,
    lora2_local_file: str,
    lora2_strength: float,
    civitai_api_key: str,
    progress=gr.Progress(track_tqdm=True),
):
    if not prompt or not prompt.strip():
        raise gr.Error("Por favor, digite um prompt descrevendo a cena do vídeo.")

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

    canvas = resolve_canvas(canvas)
    height, width = CANVASES[canvas]

    processed_image = None
    if input_image is not None:
        processed_image = _fit_keyframe(input_image, width, height)

    token_to_use = (civitai_api_key or DEFAULT_CIVITAI_KEY).strip()
    lora_configs = []
    active_labels = []

    # 0. Turbo LoRA (4-Step or 8-Step Distill)
    t_path, t_scale = resolve_turbo_lora(turbo_choice, model_choice)
    if t_path and t_scale > 0:
        lora_configs.append((t_path, t_scale))
        active_labels.append(f"⚡ Turbo: {turbo_choice.split('(')[0].strip()}")

    # 1. Base NSFW Adapter
    if nsfw_base_choice in NSFW_BASE_ADAPTERS and nsfw_base_strength > 0:
        base_spec = NSFW_BASE_ADAPTERS[nsfw_base_choice]
        if base_spec.get("source"):
            b_url = normalize_civitai_url(base_spec["source"])
            if b_url:
                b_path, b_label = _download_lora_url(b_url, token_to_use)
                lora_configs.append((b_path, float(nsfw_base_strength)))
                active_labels.append(f"🔞 NSFW Base: {nsfw_base_choice.split('(')[0].strip()} (@ {nsfw_base_strength:g})")

    # 2. Slot 1 LoRA
    l1_path, l1_label, l1_scale = resolve_single_lora(
        lora1_preset, lora1_custom_url, lora1_hf_repo, lora1_hf_file, lora1_local_file, lora1_strength, token_to_use
    )
    if l1_path and l1_scale > 0:
        lora_configs.append((l1_path, l1_scale))
        active_labels.append(f"{l1_label} (@ {lora1_strength:g})")

    # 3. Slot 2 LoRA
    l2_path, l2_label, l2_scale = resolve_single_lora(
        lora2_preset, lora2_custom_url, lora2_hf_repo, lora2_hf_file, lora2_local_file, lora2_strength, token_to_use
    )
    if l2_path and l2_scale > 0:
        lora_configs.append((l2_path, l2_scale))
        active_labels.append(f"{l2_label} (@ {lora2_strength:g})")

    progress(0.1, desc=f"Gerando {steps} passos a {width}x{height} ({num_frames} frames)...")
    started = time.time()

    frames = _generate_video_gpu(
        prompt=prompt,
        negative_prompt=negative_prompt,
        image_input=processed_image,
        height=height,
        width=width,
        num_frames=int(num_frames),
        steps=int(steps),
        guidance_scale=float(guidance_scale),
        seed=int(seed),
        lora_configs=lora_configs,
        model_repo=model_choice,
    )

    gen_time = time.time() - started
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    out_video_path = os.path.join(OUTPUT_DIR, f"wan_{int(time.time() * 1000)}.mp4")
    export_to_video(frames, out_video_path, fps=int(fps))

    loras_str = " + ".join(active_labels) if active_labels else "None (Base Model)"
    report = (
        f"**Modelo**: `{model_choice.split('/')[-1]}` | **Resolução**: `{width}x{height}` (Múltiplo de 16) | "
        f"**Frames**: {num_frames} ({num_frames / fps:.2f}s @ {fps}fps) | **Passos**: {steps} | **Seed**: {seed}\n\n"
        f"🎯 **Active LoRAs**: `{loras_str}`\n\n"
        f"⏱️ **Tempo de Renderização**: {gen_time:.1f}s"
    )

    return out_video_path, report, seed


# =========================================================================
# Gradio UI Interface
# =========================================================================

custom_css = """
.gradio-container {
    max-width: 1350px !important;
    margin: 0 auto !important;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
.header-card {
    background: linear-gradient(135deg, #1e1b4b 0%, #3b0764 50%, #0f172a 100%);
    border: 1px solid rgba(168, 85, 247, 0.3);
    border-radius: 16px;
    padding: 24px 32px;
    margin-bottom: 20px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.4);
}
.header-card h1 {
    font-size: 2.2rem;
    font-weight: 800;
    margin: 0 0 8px 0;
    background: linear-gradient(90deg, #c084fc, #38bdf8, #818cf8);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 9999px;
    font-size: 0.8rem;
    font-weight: 600;
    margin-right: 6px;
}
.badge-turbo {
    background: rgba(245, 158, 11, 0.2);
    color: #fbbf24;
    border: 1px solid rgba(245, 158, 11, 0.4);
}
.badge-zerogpu {
    background: rgba(16, 185, 129, 0.2);
    color: #34d399;
    border: 1px solid rgba(16, 185, 129, 0.4);
}
.badge-model {
    background: rgba(99, 102, 241, 0.2);
    color: #a5b4fc;
    border: 1px solid rgba(99, 102, 241, 0.4);
}
.badge-multilora {
    background: rgba(236, 72, 153, 0.2);
    color: #f472b6;
    border: 1px solid rgba(236, 72, 153, 0.4);
}
.trigger-btn {
    background: rgba(168, 85, 247, 0.2) !important;
    border: 1px solid rgba(168, 85, 247, 0.5) !important;
    color: #e9d5ff !important;
    font-size: 0.85rem !important;
    font-weight: 600 !important;
    padding: 4px 12px !important;
    border-radius: 6px !important;
}
.generate-btn {
    background: linear-gradient(135deg, #9333ea 0%, #4f46e5 100%) !important;
    color: white !important;
    font-weight: 700 !important;
    font-size: 1.15rem !important;
    border-radius: 12px !important;
    padding: 12px 24px !important;
    box-shadow: 0 4px 15px rgba(147, 51, 234, 0.4) !important;
    border: none !important;
    transition: all 0.2s ease !important;
}
.generate-btn:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 6px 20px rgba(147, 51, 234, 0.6) !important;
}
"""

all_preset_choices = (
    list(LORA_PRESETS.keys())
    + ["Custom URL / Civitai", "Custom Hugging Face"]
    + (["Local File (loras/ folder)"] if get_local_loras() else [])
)

with gr.Blocks(css=custom_css, title="Wan 2.2 Turbo Video Studio") as app:
    gr.HTML(
        """
        <div class="header-card">
            <div style="margin-bottom: 12px;">
                <span class="badge badge-turbo">⚡ Turbo 4-Step Distill</span>
                <span class="badge badge-zerogpu">⚡ ZeroGPU</span>
                <span class="badge badge-model">🎬 Wan 2.2 (MoE Diffusers)</span>
                <span class="badge badge-multilora">🧩 Base NSFW + Multi-LoRA Engine</span>
            </div>
            <h1>Wan 2.2 Turbo Video Studio</h1>
            <p>Aceleração Turbo de 4 a 8 Passos com Arquitetura Wan 2.2 MoE, Adaptador Base NSFW e Multi-LoRA.</p>
        </div>
        """
    )

    with gr.Row():
        with gr.Column(scale=6):
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Descreva a cena detalhada do vídeo...",
                lines=4,
                value="nsfwsks, a cinematic shot of a beautiful woman with glowing eyes, dramatic lighting, 8k masterpiece",
            )

            negative_prompt = gr.Textbox(
                label="Negative Prompt",
                placeholder="Elementos indesejados (distorções, membros extras, baixa qualidade)...",
                lines=2,
                value="deformed, bad anatomy, extra limbs, blurry, low resolution, bad quality, distortion, censorship",
            )

            with gr.Accordion("📖 Guia & Modelos de Prompt HMNSFW / hmmotion (Wan 2.2)", open=False):
                gr.Markdown(
                    """
                    **Estrutura Recomendada pelo Guia de 300 Horas / Autor do LoRA (`hmmotion`)**:
                    - **Força recomendada**: `0.3` a `0.5` (use `<= 0.5`).
                    - **Cabeçalho Obrigatório**: `hmmotion, <class>, <viewpoint>, <pace>, <shot>.`
                      - *Class*: `missionary` / `cowgirl` / `blowjob` / `doggy` / `handjob` / `insertion`
                      - *Viewpoint*: `pov` ou `side`
                      - *Pace*: `fast` ou `slow`
                      - *Shot*: `close-up` / `medium shot` / `third-person side view` / `high-angle downward shot`
                    - **Dica**: Escreva um parágrafo contínuo descritivo de 180 a 260 palavras detalhando a pose, anatomia, movimento (*"The motion is..."*), superfícies (*"sheen"*) e áudio (*"The audio consists of..."*).
                    """
                )
                hmnsfw_example_btn = gr.Button("💡 Inserir Exemplo Completo de Prompt HMNSFW", elem_classes=["trigger-btn"])

            with gr.Tabs():
                with gr.TabItem("⚡ Aceleração Turbo (4-8 Passos)"):
                    gr.Markdown("Acelera a geração em até **8x** aplicando destilação LightX2V. Permite renderizar vídeos em ~15 segundos!")
                    turbo_choice = gr.Dropdown(
                        label="Modo Turbo",
                        choices=list(TURBO_PRESETS.keys()),
                        value="⚡ Turbo 4-Passos (LightX2V Distill - 4 Steps Ultra Rápido)",
                    )

                with gr.TabItem("🔞 Adaptador Base NSFW (Essencial)"):
                    gr.Markdown("Como o modelo base do Wan é treinado sem conteúdo explícito, este adaptador é carregado como base para desbloquear anatomia sem gastar seus slots de LoRA!")
                    with gr.Row():
                        nsfw_base_choice = gr.Dropdown(
                            label="Adaptador Base NSFW",
                            choices=list(NSFW_BASE_ADAPTERS.keys()),
                            value="WAN General NSFW Model (Civitai 1307155 - Recomendado)",
                        )
                        nsfw_base_strength = gr.Slider(
                            label="Força do Adaptador Base NSFW",
                            minimum=0.0,
                            maximum=1.5,
                            step=0.05,
                            value=1.0,
                        )
                    nsfw_base_trigger_btn = gr.Button("📋 Inserir Trigger `nsfwsks` no Prompt", elem_classes=["trigger-btn"])

                with gr.TabItem("🖼️ Image-to-Video (I2V)"):
                    gr.Markdown("Faça upload de uma imagem inicial (`First Frame`). O sistema ajustará o corte automaticamente para a proporção múltipla de 16 selecionada.")
                    input_image = gr.Image(label="First Frame (Imagem Inicial)", type="pil")

                with gr.TabItem("🎨 LoRA Slot 1 (Ação / Pose)"):
                    lora1_preset = gr.Dropdown(
                        label="LoRA Slot 1 Preset / Fonte",
                        choices=all_preset_choices,
                        value="HMNSFW AIO V2 / hmmotion (Wan 2.2)",
                    )
                    with gr.Group(visible=False) as lora1_custom_url_grp:
                        lora1_custom_url = gr.Textbox(
                            label="URL de Download do Civitai / SafeTensor",
                            placeholder="https://civitai.red/models/... ou https://civitai.com/api/download/models/...",
                        )
                    with gr.Group(visible=False) as lora1_hf_grp:
                        with gr.Row():
                            lora1_hf_repo = gr.Textbox(label="HF Repo ID", placeholder="owner/repo")
                            lora1_hf_file = gr.Textbox(label="HF Filename", placeholder="model.safetensors")
                    with gr.Group(visible=False) as lora1_local_grp:
                        lora1_local_file = gr.Dropdown(label="Arquivo Local (pasta loras/)", choices=get_local_loras())

                    lora1_trigger_display = gr.Textbox(
                        label="Trigger Words",
                        value=LORA_PRESETS.get("HMNSFW AIO V2 / hmmotion (Wan 2.2)", {}).get("trigger_words", ""),
                        interactive=False,
                    )
                    add_lora1_triggers_btn = gr.Button("📋 Inserir Trigger Words no Prompt", elem_classes=["trigger-btn"])

                    lora1_strength = gr.Slider(
                        label="LoRA 1 Força (Scale)",
                        minimum=0.0,
                        maximum=2.0,
                        step=0.05,
                        value=0.5,
                    )

                with gr.TabItem("🎬 LoRA Slot 2 (Movimento / Câmera)"):
                    lora2_preset = gr.Dropdown(
                        label="LoRA Slot 2 Preset / Fonte",
                        choices=all_preset_choices,
                        value="None / Desativado",
                    )
                    with gr.Group(visible=False) as lora2_custom_url_grp:
                        lora2_custom_url = gr.Textbox(
                            label="URL de Download do Civitai / SafeTensor",
                            placeholder="https://civitai.com/api/download/models/...",
                        )
                    with gr.Group(visible=False) as lora2_hf_grp:
                        with gr.Row():
                            lora2_hf_repo = gr.Textbox(label="HF Repo ID", placeholder="owner/repo")
                            lora2_hf_file = gr.Textbox(label="HF Filename", placeholder="model.safetensors")
                    with gr.Group(visible=False) as lora2_local_grp:
                        lora2_local_file = gr.Dropdown(label="Arquivo Local (pasta loras/)", choices=get_local_loras())

                    lora2_trigger_display = gr.Textbox(
                        label="Trigger Words",
                        value="",
                        interactive=False,
                    )
                    add_lora2_triggers_btn = gr.Button("📋 Inserir Trigger Words no Prompt", elem_classes=["trigger-btn"])

                    lora2_strength = gr.Slider(
                        label="LoRA 2 Força (Scale)",
                        minimum=0.0,
                        maximum=2.0,
                        step=0.05,
                        value=0.85,
                    )

            with gr.Accordion("⚙️ Configurações de Resolução & Renderização (Guia 300h)", open=True):
                with gr.Row():
                    model_choice = gr.Dropdown(
                        label="Modelo Base Wan",
                        choices=[
                            ("Wan 2.2 I2V A14B (MoE Image-to-Video - Recomendado)", "Wan-AI/Wan2.2-I2V-A14B-Diffusers"),
                            ("Wan 2.2 TI2V 5B (Híbrido Texto & Imagem)", "Wan-AI/Wan2.2-TI2V-5B-Diffusers"),
                            ("Wan 2.2 T2V A14B (MoE Texto para Vídeo)", "Wan-AI/Wan2.2-T2V-A14B-Diffusers"),
                            ("Wan 2.1 I2V 14B 480P (Versão Anterior)", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"),
                            ("Wan 2.1 I2V 14B 720P (Versão Anterior HD)", "Wan-AI/Wan2.1-I2V-14B-720P-Diffusers"),
                            ("Wan 2.1 T2V 14B (Versão Anterior T2V)", "Wan-AI/Wan2.1-T2V-14B-Diffusers"),
                        ],
                        value="Wan-AI/Wan2.2-I2V-A14B-Diffusers",
                    )
                    canvas = gr.Dropdown(
                        label="Proporção & Resolução (Múltiplos de 16)",
                        choices=list(CANVASES.keys()),
                        value=DEFAULT_CANVAS,
                    )

                with gr.Row():
                    num_frames = gr.Slider(
                        label="Número de Frames",
                        minimum=17,
                        maximum=81,
                        step=16,
                        value=81,
                        info="81 frames = ~5 segundos a 16 fps; 49 frames = ~3 segundos.",
                    )
                    fps = gr.Slider(
                        label="FPS do Vídeo",
                        minimum=8,
                        maximum=30,
                        step=1,
                        value=16,
                    )
                    steps = gr.Slider(
                        label="Passos de Inferência (Steps)",
                        minimum=2,
                        maximum=40,
                        step=1,
                        value=4,
                        info="Com Turbo 4-Passos ativado, 4 passos fornecem render ultrarrápido.",
                    )

                with gr.Row():
                    guidance_scale = gr.Slider(
                        label="Guidance Scale (CFG)",
                        minimum=1.0,
                        maximum=10.0,
                        step=0.5,
                        value=1.5,
                        info="No modo Turbo, recomenda-se CFG baixo (1.0 a 2.0).",
                    )
                    seed = gr.Number(label="Seed", value=42, precision=0)
                    randomize_seed = gr.Checkbox(label="🎲 Randomizar Seed", value=True)

                civitai_api_key = gr.Textbox(
                    label="🔑 Civitai API Key",
                    value=DEFAULT_CIVITAI_KEY,
                    placeholder="Chave de API do Civitai configurada",
                    type="password",
                )

            generate_btn = gr.Button("🚀 Gerar Vídeo Wan 2.2 Turbo", variant="primary", elem_classes=["generate-btn"])

        with gr.Column(scale=6):
            output_video = gr.Video(label="Vídeo Gerado", autoplay=True, loop=True)
            output_report = gr.Markdown(label="Detalhes da Geração", value="Pronto para renderizar vídeo.")

    # Event handlers
    def on_turbo_change(turbo_val):
        spec = TURBO_PRESETS.get(turbo_val, {})
        return spec.get("default_steps", 4), spec.get("default_cfg", 1.5)

    turbo_choice.change(
        fn=on_turbo_change,
        inputs=[turbo_choice],
        outputs=[steps, guidance_scale],
    )

    def on_lora1_change(preset_val):
        spec = LORA_PRESETS.get(preset_val, {})
        triggers = spec.get("trigger_words", "")
        default_s = spec.get("default_strength", 1.0)
        return (
            gr.update(visible=preset_val == "Custom URL / Civitai"),
            gr.update(visible=preset_val == "Custom Hugging Face"),
            gr.update(visible=preset_val == "Local File (loras/ folder)"),
            triggers,
            default_s,
        )

    lora1_preset.change(
        fn=on_lora1_change,
        inputs=[lora1_preset],
        outputs=[lora1_custom_url_grp, lora1_hf_grp, lora1_local_grp, lora1_trigger_display, lora1_strength],
    )

    def on_lora2_change(preset_val):
        spec = LORA_PRESETS.get(preset_val, {})
        triggers = spec.get("trigger_words", "")
        default_s = spec.get("default_strength", 1.0)
        return (
            gr.update(visible=preset_val == "Custom URL / Civitai"),
            gr.update(visible=preset_val == "Custom Hugging Face"),
            gr.update(visible=preset_val == "Local File (loras/ folder)"),
            triggers,
            default_s,
        )

    lora2_preset.change(
        fn=on_lora2_change,
        inputs=[lora2_preset],
        outputs=[lora2_custom_url_grp, lora2_hf_grp, lora2_local_grp, lora2_trigger_display, lora2_strength],
    )

    def append_triggers(curr_prompt, triggers):
        if not triggers:
            return curr_prompt
        curr = curr_prompt.strip()
        if not curr:
            return triggers
        if triggers.lower() in curr.lower():
            return curr
        return f"{curr}, {triggers}"

    def load_hmnsfw_example():
        return (
            "hmmotion, missionary, side, fast, third-person side view, medium shot. "
            "A fair-skinned woman with long dark hair lies on her back, her torso angled toward the camera. "
            "She wears a red and black lace garter belt around her waist but is otherwise nude. "
            "Her left leg is raised and bent while her right leg is spread wide. "
            "The man is positioned above her, his torso and arms visible as he thrusts. "
            "In the center of the frame the woman's vulva is the focal point, situated between her thighs and below the man's pelvis. "
            "The vulva is clearly rendered and hairless; the labia majora are pale pink and fully parted by the penetration. "
            "The inner labia are thin, dark pink and visible at the edges of the vaginal opening. "
            "The clitoral hood is visible and flushed. "
            "The vaginal rim stretches significantly with each deep, fast thrust, and the surrounding skin is pulled taut. "
            "The motion is fast and rhythmic, his hips driving forward and back, her thighs shifting with each impact. "
            "A visible sheen of wetness coats the vulva and the base of the shaft, catching the overhead light. "
            "His hands grip her raised thigh, holding her leg open. Her head is tilted back with her mouth open. "
            "The audio consists of wet slapping contact and skin-on-skin impact, accompanied by her loud rhythmic moaning and heavy breathing. "
            "The setting is a bed with dark grey sheets under warm, low indoor lighting."
        )

    hmnsfw_example_btn.click(fn=load_hmnsfw_example, outputs=[prompt])
    nsfw_base_trigger_btn.click(fn=lambda p: append_triggers(p, "nsfwsks, uncensored, explicit"), inputs=[prompt], outputs=[prompt])
    add_lora1_triggers_btn.click(fn=append_triggers, inputs=[prompt, lora1_trigger_display], outputs=[prompt])
    add_lora2_triggers_btn.click(fn=append_triggers, inputs=[prompt, lora2_trigger_display], outputs=[prompt])

    generate_btn.click(
        fn=generate_video,
        inputs=[
            prompt,
            negative_prompt,
            input_image,
            turbo_choice,
            nsfw_base_choice,
            nsfw_base_strength,
            canvas,
            num_frames,
            fps,
            steps,
            guidance_scale,
            seed,
            randomize_seed,
            model_choice,
            lora1_preset,
            lora1_custom_url,
            lora1_hf_repo,
            lora1_hf_file,
            lora1_local_file,
            lora1_strength,
            lora2_preset,
            lora2_custom_url,
            lora2_hf_repo,
            lora2_hf_file,
            lora2_local_file,
            lora2_strength,
            civitai_api_key,
        ],
        outputs=[output_video, output_report, seed],
    )

if __name__ == "__main__":
    try:
        print("[wan] pre-initializing default pipeline on CPU...", flush=True)
        get_or_load_pipeline(DEFAULT_MODEL_REPO)
    except Exception as err:
        print(f"[wan] initial preload deferred to first call: {err}", flush=True)
    app.launch(show_error=True, allowed_paths=[OUTPUT_DIR])