File size: 52,724 Bytes
2aeb649
 
 
d95253f
2aeb649
d95253f
9071ed9
2aeb649
d95253f
2a43fc6
d95253f
 
cd5feb8
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cf5f90
 
 
acf180f
 
 
 
 
 
 
 
 
 
 
 
 
 
d95253f
 
9669215
d95253f
 
 
 
 
2aeb649
d95253f
2aeb649
 
 
6e50c0f
d95253f
2aeb649
 
6e50c0f
d95253f
 
 
 
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
6e50c0f
d95253f
bf3802a
d95253f
 
6e50c0f
d95253f
 
 
 
 
 
 
2aeb649
d95253f
 
 
 
 
 
 
 
 
e460389
d95253f
e460389
 
6e50c0f
acf180f
d95253f
 
acf180f
d95253f
 
 
 
 
6e50c0f
d95253f
 
 
 
e460389
c5b9a3f
d95253f
6e50c0f
d95253f
6e50c0f
2aeb649
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e50c0f
 
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e398c6
 
 
 
acf180f
d95253f
 
6e50c0f
acf180f
d95253f
6e50c0f
 
d95253f
acf180f
 
d95253f
 
acf180f
6e50c0f
d95253f
 
 
 
acf180f
 
d95253f
acf180f
d95253f
acf180f
6e50c0f
d95253f
6e50c0f
 
acf180f
6e50c0f
 
 
d95253f
 
6e50c0f
 
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2a0ec8
d95253f
 
 
 
 
 
2aeb649
d95253f
2aeb649
d95253f
 
 
 
 
 
 
 
2aeb649
d95253f
 
 
 
 
a2a0ec8
6e50c0f
d95253f
2aeb649
 
e48859b
d95253f
2aeb649
d95253f
6e50c0f
d95253f
 
 
 
2aeb649
d95253f
2aeb649
d95253f
 
2aeb649
d95253f
 
2aeb649
d95253f
 
 
6389ef8
2aeb649
6e50c0f
d95253f
8d54c58
d95253f
 
 
 
8d54c58
 
d95253f
 
 
 
6e50c0f
d95253f
8d54c58
d95253f
 
0ec77e4
8d54c58
d95253f
 
8d54c58
d95253f
 
8d54c58
6e50c0f
53b0d0a
4f448b7
d95253f
 
 
 
 
 
 
 
8d54c58
d95253f
8177b9c
8d54c58
d95253f
388621e
ff20abd
4f448b7
d95253f
4f448b7
8d54c58
d95253f
 
 
8d54c58
0ec77e4
8d54c58
 
 
d95253f
8d54c58
 
d95253f
 
8d54c58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d95253f
 
8d54c58
 
6e50c0f
8d54c58
6e50c0f
 
 
8d54c58
 
6e50c0f
d95253f
8d54c58
 
d95253f
 
 
 
 
 
 
 
 
6e50c0f
d95253f
6e50c0f
d95253f
8d54c58
d95253f
 
 
0ec77e4
d95253f
 
 
8d54c58
d95253f
 
8d54c58
d95253f
8d54c58
6e50c0f
d95253f
8d54c58
d95253f
8d54c58
d95253f
 
0b1a6cb
d95253f
8d54c58
d95253f
3f71e88
6531480
d95253f
 
6e50c0f
3f71e88
8d54c58
 
2aeb649
f5a2481
2aeb649
4f448b7
 
2aeb649
f5a2481
 
1d06c07
d95253f
8d92190
53b0d0a
1d06c07
 
 
d95253f
1d06c07
f9f9336
d95253f
 
6e50c0f
d95253f
6e50c0f
8d54c58
d95253f
 
8d54c58
1d06c07
d95253f
2a43fc6
6e50c0f
 
 
 
d95253f
 
 
 
2a43fc6
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
6e50c0f
2a43fc6
 
d95253f
 
 
 
 
 
 
6e50c0f
d95253f
 
2a43fc6
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c50ed1f
a0e1b6c
d95253f
 
b189c01
6e50c0f
d95253f
 
 
 
0e77e0b
d95253f
 
 
 
 
 
 
 
 
6e50c0f
d95253f
 
 
6e50c0f
d95253f
 
 
 
 
 
78994f5
d95253f
78994f5
0227cff
d95253f
 
 
 
6e50c0f
d95253f
 
 
6e50c0f
 
 
d95253f
6e50c0f
 
d95253f
 
 
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0817061
d95253f
 
0817061
d95253f
7950bc5
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c21d02
d95253f
2c21d02
d95253f
 
0ec77e4
d95253f
 
 
6e50c0f
d95253f
 
 
 
 
 
 
 
 
6e50c0f
d95253f
d6bfdd6
d95253f
 
 
 
 
9329734
6e50c0f
d95253f
 
 
49bde08
d95253f
 
 
 
 
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
6e50c0f
d95253f
 
6e50c0f
0817061
d95253f
 
 
 
 
 
 
6e50c0f
 
d95253f
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7256938
d95253f
2aeb649
d95253f
 
 
 
 
 
 
 
2aeb649
 
d95253f
da93b9f
2c21d02
d95253f
6e50c0f
d95253f
 
 
 
 
 
 
 
 
6e50c0f
d95253f
2aeb649
d95253f
 
 
 
 
 
 
6e50c0f
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0227cff
d95253f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e50c0f
d95253f
 
 
 
 
6e50c0f
 
d95253f
 
 
 
 
6e50c0f
d95253f
 
 
 
 
 
6e50c0f
d95253f
 
 
 
 
 
6e50c0f
 
d95253f
 
 
 
 
 
0ec77e4
d95253f
 
 
 
 
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
import requests
import os
import gradio as gr
from huggingface_hub import update_repo_visibility, whoami, upload_folder, create_repo, upload_file # Removed duplicate update_repo_visibility
from slugify import slugify
# import gradio as gr # Already imported
import re
import uuid
from typing import Optional, Dict, Any
import json
# from bs4 import BeautifulSoup # Not used

TRUSTED_UPLOADERS = ["KappaNeuro", "CiroN2022", "Norod78", "joachimsallstrom", "blink7630", "e-n-v-y", "DoctorDiffusion", "RalFinger", "artificialguybr"]

# --- Model Mappings ---
MODEL_MAPPING_IMAGE = {
    "SDXL 1.0": "stabilityai/stable-diffusion-xl-base-1.0",
    "SDXL 0.9": "stabilityai/stable-diffusion-xl-base-1.0", # Usually mapped to 1.0
    "SD 1.5": "runwayml/stable-diffusion-v1-5",
    "SD 1.4": "CompVis/stable-diffusion-v1-4",
    "SD 2.1": "stabilityai/stable-diffusion-2-1-base",
    "SD 2.0": "stabilityai/stable-diffusion-2-base",
    "SD 2.1 768": "stabilityai/stable-diffusion-2-1",
    "SD 2.0 768": "stabilityai/stable-diffusion-2",
    "SD 3": "stabilityai/stable-diffusion-3-medium-diffusers", # Assuming medium, adjust if others are common
    "SD 3.5": "stabilityai/stable-diffusion-3.5-large", # Assuming large, adjust
    "SD 3.5 Large": "stabilityai/stable-diffusion-3.5-large",
    "SD 3.5 Medium": "stabilityai/stable-diffusion-3.5-medium",
    "SD 3.5 Large Turbo": "stabilityai/stable-diffusion-3.5-large-turbo",
    "Flux.1 D": "black-forest-labs/FLUX.1-dev",
    "Flux.1 S": "black-forest-labs/FLUX.1-schnell",
}

MODEL_MAPPING_VIDEO = {
    "LTXV": "Lightricks/LTX-Video-0.9.7-dev",
    "Wan Video 1.3B t2v": "Wan-AI/Wan2.1-T2V-1.3B-Diffusers",
    "Wan Video 14B t2v": "Wan-AI/Wan2.1-T2V-14B-Diffusers",
    "Wan Video 14B i2v 480p": "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers",
    "Wan Video 14B i2v 720p": "Wan-AI/Wan2.1-I2V-14B-720P-Diffusers",
    "Hunyuan Video": "hunyuanvideo-community/HunyuanVideo-I2V", # Default, will be overridden by choice
}

SUPPORTED_CIVITAI_BASE_MODELS = list(MODEL_MAPPING_IMAGE.keys()) + list(MODEL_MAPPING_VIDEO.keys())


cookie_info = os.environ.get("COOKIE_INFO")

headers = {
        "authority": "civitai.com",
        "accept": "*/*",
        "accept-language": "en-US,en;q=0.9", # Simplified
        "content-type": "application/json",
        "cookie": cookie_info, # Use the env var
        "sec-ch-ua": "\"Chromium\";v=\"118\", \"Not_A Brand\";v=\"99\"", # Example, update if needed
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"", # Example
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" # Example
}

def get_json_data(url):
    url_split = url.split('/')
    if len(url_split) < 5 or not url_split[4].isdigit():
        print(f"Invalid Civitai URL format or model ID not found: {url}")
        gr.Warning(f"Invalid Civitai URL format. Ensure it's like 'https://civitai.com/models/YOUR_MODEL_ID/MODEL_NAME'. Problem with: {url}")
        return None
    api_url = f"https://civitai.com/api/v1/models/{url_split[4]}"
    try:
        response = requests.get(api_url)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error fetching JSON data from {api_url}: {e}")
        gr.Warning(f"Error fetching data from Civitai API for {url_split[4]}: {e}")
        return None

def check_nsfw(json_data: Dict[str, Any], profile: Optional[gr.OAuthProfile]) -> bool:
    if not json_data:
        return False # Should not happen if get_json_data succeeded

    # Overall model boolean flag - highest priority
    if json_data.get("nsfw", False):
        print("Model flagged as NSFW by 'nsfw: true'.")
        gr.Info("Reason: Model explicitly flagged as NSFW on Civitai.")
        return False # Unsafe

    # Overall model numeric nsfwLevel - second priority. Max allowed is 5 (nsfwLevel < 6).
    # nsfwLevel definitions: None (1), Mild (2), Mature (4), Adult (5), X (8), R (16), XXX (32)
    model_nsfw_level = json_data.get("nsfwLevel", 0)
    if model_nsfw_level > 5: # Anything above "Adult"
        print(f"Model's overall nsfwLevel ({model_nsfw_level}) is > 5. Blocking.")
        gr.Info(f"Reason: Model's overall NSFW Level ({model_nsfw_level}) is above the allowed threshold (5).")
        return False # Unsafe

    # If uploader is trusted and the above checks passed, they bypass further version/image checks.
    if profile and profile.username in TRUSTED_UPLOADERS:
        print(f"User {profile.username} is trusted. Model 'nsfw' is false and overall nsfwLevel ({model_nsfw_level}) is <= 5. Allowing.")
        return True

    # For non-trusted users, check nsfwLevel of model versions and individual images/videos
    for model_version in json_data.get("modelVersions", []):
        version_nsfw_level = model_version.get("nsfwLevel", 0)
        if version_nsfw_level > 5:
            print(f"Model version nsfwLevel ({version_nsfw_level}) is > 5 for non-trusted user. Blocking.")
            gr.Info(f"Reason: A model version's NSFW Level ({version_nsfw_level}) is above 5.")
            return False
    return True # Safe for non-trusted user if all checks pass


def get_prompts_from_image(image_id_str: str):
    # image_id_str could be non-numeric if URL parsing failed or format changed
    try:
        image_id = int(image_id_str)
    except ValueError:
        print(f"Invalid image_id_str for TRPC call: {image_id_str}. Skipping prompt fetch.")
        return "", ""

    print(f"Fetching prompts for image_id: {image_id}")
    url = f'https://civitai.com/api/trpc/image.getGenerationData?input={{"json":{{"id":{image_id}}}}}'
    
    prompt = ""
    negative_prompt = ""
    try:
        response = requests.get(url, headers=headers, timeout=10) # Added timeout
        response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
        data = response.json()
        print("Response from image: ", data)
        # Expected structure: {'result': {'data': {'json': {'meta': {'prompt': '...', 'negativePrompt': '...'}}}}}
        meta = data.get('result', {}).get('data', {}).get('json', {}).get('meta')
        if meta: # meta can be None
            prompt = meta.get('prompt', "")
            negative_prompt = meta.get('negativePrompt', "")
    except requests.exceptions.RequestException as e:
        print(f"Could not fetch/parse generation data for image_id {image_id}: {e}")
    except json.JSONDecodeError as e:
        print(f"JSONDecodeError for image_id {image_id}: {e}. Response content: {response.text[:200]}")
    
    return prompt, negative_prompt

def extract_info(json_data: Dict[str, Any], hunyuan_type: Optional[str] = None) -> Optional[Dict[str, Any]]:
    if json_data.get("type") != "LORA":
        print("Model type is not LORA.")
        return None

    for model_version in json_data.get("modelVersions", []):
        civitai_base_model_name = model_version.get("baseModel")
        if civitai_base_model_name in SUPPORTED_CIVITAI_BASE_MODELS:
            base_model_hf = ""
            is_video = False

            if civitai_base_model_name == "Hunyuan Video":
                is_video = True
                if hunyuan_type == "Text-to-Video":
                    base_model_hf = "hunyuanvideo-community/HunyuanVideo"
                else: # Default or "Image-to-Video"
                    base_model_hf = "hunyuanvideo-community/HunyuanVideo-I2V"
            elif civitai_base_model_name in MODEL_MAPPING_VIDEO:
                is_video = True
                base_model_hf = MODEL_MAPPING_VIDEO[civitai_base_model_name]
            elif civitai_base_model_name in MODEL_MAPPING_IMAGE:
                base_model_hf = MODEL_MAPPING_IMAGE[civitai_base_model_name]
            else:
                print(f"Logic error: {civitai_base_model_name} in supported list but not mapped.")
                continue 

            primary_file_info = None
            for file_entry in model_version.get("files", []):
                if file_entry.get("primary", False) and file_entry.get("type") == "Model":
                    primary_file_info = file_entry
                    break
            
            if not primary_file_info:
                for file_entry in model_version.get("files", []):
                    if file_entry.get("type") == "Model" and file_entry.get("name","").endswith(".safetensors"):
                        primary_file_info = file_entry
                        print(f"Using first safetensors file as primary: {primary_file_info['name']}")
                        break
                if not primary_file_info:
                    print(f"No primary or suitable safetensors model file found for version {model_version.get('name')}")
                    continue

            urls_to_download = [{"url": primary_file_info["downloadUrl"], "filename": primary_file_info["name"], "type": "weightName"}]
            
            for image_obj in model_version.get("images", []):
                image_url = image_obj.get("url")
                if not image_url:
                    continue

                image_nsfw_level = image_obj.get("nsfwLevel", 0)
                if image_nsfw_level > 5:
                    continue
                    
                filename_part = os.path.basename(image_url)
                image_id_str = filename_part.split('.')[0]

                prompt, negative_prompt = "", ""
                if image_obj.get("hasMeta", False):
                     prompt, negative_prompt = get_prompts_from_image(image_id_str)

                urls_to_download.append({
                    "url": image_url,
                    "filename": filename_part, 
                    "type": "imageName", 
                    "prompt": prompt,
                    "negative_prompt": negative_prompt,
                    "media_type": image_obj.get("type", "image") 
                })

            info = {
                "urls_to_download": urls_to_download,
                "id": model_version["id"],
                "baseModel": base_model_hf, 
                "civitai_base_model_name": civitai_base_model_name,
                "is_video_model": is_video,
                "modelId": json_data.get("id", ""),
                "name": json_data["name"],
                "description": json_data.get("description", ""),
                "trainedWords": model_version.get("trainedWords", []),
                "creator": json_data.get("creator", {}).get("username", "Unknown"),
                "tags": json_data.get("tags", []),
                "allowNoCredit": json_data.get("allowNoCredit", True),
                "allowCommercialUse": json_data.get("allowCommercialUse", "Sell"),
                "allowDerivatives": json_data.get("allowDerivatives", True),
                "allowDifferentLicense": json_data.get("allowDifferentLicense", True)
            }
            return info
    print("No suitable model version found with a supported base model.")
    return None

def download_files(info, folder="."):
    downloaded_files = {
        "imageName": [], # Will contain both image and video filenames
        "imagePrompt": [],
        "imageNegativePrompt": [],
        "weightName": [],
        "mediaType": [] # To distinguish image/video for gallery if needed later
    }
    for item in info["urls_to_download"]:
        # Ensure filename is safe for filesystem
        safe_filename = slugify(item["filename"].rsplit('.', 1)[0]) + '.' + item["filename"].rsplit('.', 1)[-1] if '.' in item["filename"] else slugify(item["filename"])
        
        # Civitai URLs might need auth for direct download if not public
        try:
            download_file_with_auth(item["url"], safe_filename, folder) # Changed to use the auth-aware download
            downloaded_files[item["type"]].append(safe_filename)
            if item["type"] == "imageName": # This list now includes videos too
                prompt_clean = re.sub(r'<.*?>', '', item.get("prompt", ""))
                negative_prompt_clean = re.sub(r'<.*?>', '', item.get("negative_prompt", ""))
                downloaded_files["imagePrompt"].append(prompt_clean)
                downloaded_files["imageNegativePrompt"].append(negative_prompt_clean)
                downloaded_files["mediaType"].append(item.get("media_type", "image"))
        except gr.Error as e: # Catch Gradio errors from download_file_with_auth
            print(f"Skipping file {safe_filename} due to download error: {e.message}")
            gr.Warning(f"Skipping file {safe_filename} due to download error: {e.message}")

    return downloaded_files

# Renamed original download_file to download_file_with_auth
def download_file_with_auth(url, filename, folder="."):
    headers = {}
    # Add CIVITAI_API_TOKEN if available, for potentially restricted downloads
    # Note: The prompt example didn't use it for image URLs, only for the model file via API.
    # However, some image/video URLs might also require it if they are not fully public.
    if "CIVITAI_API_TOKEN" in os.environ: # Changed from CIVITAI_API
         headers['Authorization'] = f'Bearer {os.environ["CIVITAI_API_TOKEN"]}'

    try:
        response = requests.get(url, headers=headers, stream=True, timeout=60) # Added stream and timeout
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        print(f"HTTPError downloading {url}: {e}")
        # No automatic retry with token here as it was specific to the primary file in original code
        # If it was related to auth, the initial header should have helped.
        raise gr.Error(f"Error downloading file {filename}: {e}")
    except requests.exceptions.RequestException as e:
        print(f"RequestException downloading {url}: {e}")
        raise gr.Error(f"Error downloading file {filename}: {e}")

    filepath = os.path.join(folder, filename)
    with open(filepath, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    print(f"Successfully downloaded {filepath}")


def process_url(url, profile, do_download=True, folder=".", hunyuan_type: Optional[str] = None):
    json_data = get_json_data(url)
    if json_data:
        if check_nsfw(json_data, profile):
            info = extract_info(json_data, hunyuan_type=hunyuan_type)
            if info:
                downloaded_files_summary = {}
                if do_download:
                    gr.Info(f"Downloading files for {info['name']}...")
                    downloaded_files_summary = download_files(info, folder)
                    gr.Info(f"Finished downloading files for {info['name']}.")
                return info, downloaded_files_summary
            else:
                raise gr.Error("LoRA extraction failed. The base model might not be supported, or it's not a LoRA model, or no suitable files found in the version.")
        else:
            # check_nsfw now prints detailed reasons via gr.Info/print
            raise gr.Error("This model has content tagged as unsafe by CivitAI or exceeds NSFW level limits.")
    else:
        raise gr.Error("Failed to fetch model data from CivitAI API. Please check the URL and Civitai's status.")


def create_readme(info: Dict[str, Any], downloaded_files: Dict[str, Any], user_repo_id: str, link_civit: bool = False, is_author: bool = True, folder: str = "."):
    readme_content = ""
    original_url = f"https://civitai.com/models/{info['modelId']}" if info.get('modelId') else "CivitAI (ID not found)"
    link_civit_disclaimer = f'([CivitAI]({original_url}))'
    non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
    
    is_video = info.get("is_video_model", False)
    base_hf_model = info["baseModel"] # This is the HF model ID
    civitai_bm_name_lower = info.get("civitai_base_model_name", "").lower()

    if is_video:
        default_tags = ["lora", "diffusers", "migrated", "video"]
        if "template:" not in " ".join(info.get("tags", [])):
             default_tags.append("template:video-lora")
        if "t2v" in civitai_bm_name_lower or (civitai_bm_name_lower == "hunyuan video" and base_hf_model.endswith("HunyuanVideo")):
            default_tags.append("text-to-video")
        elif "i2v" in civitai_bm_name_lower or (civitai_bm_name_lower == "hunyuan video" and base_hf_model.endswith("HunyuanVideo-I2V")):
            default_tags.append("image-to-video")
    else:
        default_tags = ["text-to-image", "stable-diffusion", "lora", "diffusers", "migrated"]
        if "template:" not in " ".join(info.get("tags", [])):
            default_tags.append("template:sd-lora")

    civit_tags_raw = info.get("tags", [])
    civit_tags_clean = [t.replace(":", "").strip() for t in civit_tags_raw if t.replace(":", "").strip()]
    final_civit_tags = [tag for tag in civit_tags_clean if tag not in default_tags and tag.lower() not in default_tags]
    tags = default_tags + final_civit_tags
    unpacked_tags = "\n- ".join(sorted(list(set(tags))))

    trained_words = info.get('trainedWords', [])
    formatted_words = ', '.join(f'`{word}`' for word in trained_words if word)
    trigger_words_section = f"## Trigger words\nYou should use {formatted_words} to trigger the generation." if formatted_words else ""
    
    widget_content = ""
    max_widget_items = 5
    items_for_widget = list(zip(
        downloaded_files.get("imagePrompt", []),
        downloaded_files.get("imageNegativePrompt", []),
        downloaded_files.get("imageName", [])
    ))[:max_widget_items]

    for index, (prompt, negative_prompt, media_filename) in enumerate(items_for_widget):
        escaped_prompt = prompt.replace("'", "''") if prompt else ' '
        base_media_filename = os.path.basename(media_filename)
        negative_prompt_content = f"negative_prompt: {negative_prompt}\n" if negative_prompt else ""
        # Corrected YAML for widget:
        widget_content += f"""- text: '{escaped_prompt}'
  {negative_prompt_content}
  output:
    url: >-
      {base_media_filename}
"""

    if base_hf_model in ["black-forest-labs/FLUX.1-dev", "black-forest-labs/FLUX.1-schnell"]:
        dtype = "torch.bfloat16"
    else:
        dtype = "torch.float16" # Default for others, Hunyuan examples specify this.
    
    main_prompt_for_snippet_raw = formatted_words if formatted_words else 'Your custom prompt'
    if items_for_widget and items_for_widget[0][0]:
        main_prompt_for_snippet_raw = items_for_widget[0][0]
    
    # Escape single quotes for Python string literals
    main_prompt_for_snippet = main_prompt_for_snippet_raw.replace("'", "\\'")


    lora_loader_line = f"pipe.load_lora_weights('{user_repo_id}', weight_name='{downloaded_files.get('weightName', ['your_lora.safetensors'])[0]}')"

    diffusers_example = ""
    if is_video:
        if base_hf_model == "hunyuanvideo-community/HunyuanVideo-I2V":
            diffusers_example = f"""
```py
import torch
from diffusers import HunyuanVideoImageToVideoPipeline, HunyuanVideoTransformer3DModel
from diffusers.utils import load_image, export_to_video

# Available checkpoints: "hunyuanvideo-community/HunyuanVideo-I2V" and "hunyuanvideo-community/HunyuanVideo-I2V-33ch"
model_id = "{base_hf_model}"
transformer = HunyuanVideoTransformer3DModel.from_pretrained(
    model_id, subfolder="transformer", torch_dtype=torch.bfloat16 # Explicitly bfloat16 for transformer
)
pipe = HunyuanVideoImageToVideoPipeline.from_pretrained(
    model_id, transformer=transformer, torch_dtype=torch.float16 # float16 for pipeline
)
pipe.vae.enable_tiling()
{lora_loader_line}
pipe.to("cuda")

prompt = "{main_prompt_for_snippet if main_prompt_for_snippet else 'A detailed scene description'}"
# Replace with your image path or URL
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png"
image = load_image(image_url)

output = pipe(image=image, prompt=prompt).frames[0]
export_to_video(output, "output.mp4", fps=15)
```
"""
        elif base_hf_model == "hunyuanvideo-community/HunyuanVideo":
            diffusers_example = f"""
```py
import torch
from diffusers import HunyuanVideoPipeline, HunyuanVideoTransformer3DModel
from diffusers.utils import export_to_video

model_id = "{base_hf_model}"
transformer = HunyuanVideoTransformer3DModel.from_pretrained(
    model_id, subfolder="transformer", torch_dtype=torch.bfloat16
)
pipe = HunyuanVideoPipeline.from_pretrained(model_id, transformer=transformer, torch_dtype=torch.float16)
{lora_loader_line}
# Enable memory savings
pipe.vae.enable_tiling()
pipe.enable_model_cpu_offload() # Optional: if VRAM is limited

output = pipe(
    prompt="{main_prompt_for_snippet if main_prompt_for_snippet else 'A cinematic video scene'}",
    height=320, # Adjust as needed
    width=512,  # Adjust as needed
    num_frames=61, # Adjust as needed
    num_inference_steps=30, # Adjust as needed
).frames[0]
export_to_video(output, "output.mp4", fps=15)
```
"""
        elif base_hf_model == "Lightricks/LTX-Video-0.9.7-dev" or base_hf_model == "Lightricks/LTX-Video-0.9.7-distilled": # Assuming -dev is the one from mapping
            # Note: The LTX example is complex. We'll simplify a bit for a LoRA example.
            # The user might need to adapt the full pipeline if they used the distilled one directly.
            # We assume the LoRA is trained on the main LTX pipeline.
            diffusers_example = f"""
```py
import torch
from diffusers import LTXConditionPipeline, LTXLatentUpsamplePipeline
from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXVideoCondition
from diffusers.utils import export_to_video, load_image, load_video

# Use the base LTX model your LoRA was trained on. The example below uses the distilled version.
# Adjust if your LoRA is for the non-distilled "Lightricks/LTX-Video-0.9.7-dev".
pipe = LTXConditionPipeline.from_pretrained("Lightricks/LTX-Video-0.9.7-distilled", torch_dtype=torch.bfloat16)
{lora_loader_line}
# The LTX upsampler is separate and typically doesn't have LoRAs loaded into it directly.
pipe_upsample = LTXLatentUpsamplePipeline.from_pretrained("Lightricks/ltxv-spatial-upscaler-0.9.7", vae=pipe.vae, torch_dtype=torch.bfloat16)

pipe.to("cuda")
pipe_upsample.to("cuda")
pipe.vae.enable_tiling()

def round_to_nearest_resolution_acceptable_by_vae(height, width, vae_spatial_compression_ratio):
    height = height - (height % vae_spatial_compression_ratio)
    width = width - (width % vae_spatial_compression_ratio)
    return height, width

# Example image for condition (replace with your own)
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/penguin.png")
video_for_condition = load_video(export_to_video([image])) # Create a dummy video for conditioning
condition1 = LTXVideoCondition(video=video_for_condition, frame_index=0)

prompt = "{main_prompt_for_snippet if main_prompt_for_snippet else 'A cute little penguin takes out a book and starts reading it'}"
negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted" # Example
expected_height, expected_width = 480, 832 # Target final resolution
downscale_factor = 2 / 3
num_frames = 32 # Reduced for quicker example

# Part 1. Generate video at smaller resolution
downscaled_height, downscaled_width = int(expected_height * downscale_factor), int(expected_width * downscale_factor)
downscaled_height, downscaled_width = round_to_nearest_resolution_acceptable_by_vae(downscaled_height, downscaled_width, pipe.vae_spatial_compression_ratio)

latents = pipe(
    conditions=[condition1],
    prompt=prompt,
    negative_prompt=negative_prompt,
    width=downscaled_width,
    height=downscaled_height,
    num_frames=num_frames,
    num_inference_steps=7, # Example steps
    guidance_scale=1.0,    # Example guidance
    decode_timestep = 0.05,
    decode_noise_scale = 0.025,
    generator=torch.Generator().manual_seed(0),
    output_type="latent",
).frames

# Part 2. Upscale generated video
upscaled_latents = pipe_upsample(
    latents=latents,
    output_type="latent"
).frames

# Part 3. Denoise the upscaled video (optional, but recommended)
video_frames = pipe(
    conditions=[condition1],
    prompt=prompt,
    negative_prompt=negative_prompt,
    width=downscaled_width * 2, # Upscaled width
    height=downscaled_height * 2, # Upscaled height
    num_frames=num_frames,
    denoise_strength=0.3,
    num_inference_steps=10,
    guidance_scale=1.0,
    latents=upscaled_latents,
    decode_timestep = 0.05,
    decode_noise_scale = 0.025,
    image_cond_noise_scale=0.025, # if using image condition
    generator=torch.Generator().manual_seed(0),
    output_type="pil",
).frames[0]

# Part 4. Downscale to target resolution if upscaler overshot
final_video = [frame.resize((expected_width, expected_height)) for frame in video_frames]
export_to_video(final_video, "output.mp4", fps=16) # Example fps
```
"""
        elif base_hf_model.startswith("Wan-AI/Wan2.1-T2V-"):
            diffusers_example = f"""
```py
import torch
from diffusers import AutoencoderKLWan, WanPipeline
from diffusers.utils import export_to_video

model_id = "{base_hf_model}"
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) # As per example
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
{lora_loader_line}
pipe.to("cuda")

prompt = "{main_prompt_for_snippet if main_prompt_for_snippet else 'A cat walks on the grass, realistic'}"
negative_prompt = "worst quality, low quality, blurry" # Simplified for LoRA example

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=480, # Adjust as needed
    width=832,  # Adjust as needed
    num_frames=30, # Adjust for LoRA, original example had 81
    guidance_scale=5.0 # Adjust as needed
).frames[0]
export_to_video(output, "output.mp4", fps=15)
```
"""
        elif base_hf_model.startswith("Wan-AI/Wan2.1-I2V-"):
            diffusers_example = f"""
```py
import torch
import numpy as np
from diffusers import AutoencoderKLWan, WanImageToVideoPipeline
from diffusers.utils import export_to_video, load_image
from transformers import CLIPVisionModel

model_id = "{base_hf_model}"
# These components are part of the base model, LoRA is loaded into the pipeline
image_encoder = CLIPVisionModel.from_pretrained(model_id, subfolder="image_encoder", torch_dtype=torch.float32)
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
pipe = WanImageToVideoPipeline.from_pretrained(model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16)
{lora_loader_line}
pipe.to("cuda")

# Replace with your image path or URL
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
image = load_image(image_url)

# Adjust resolution based on model capabilities (480p or 720p variants)
# This is a simplified example; refer to original Wan I2V docs for precise resolution handling
if "480P" in model_id:
    max_height, max_width = 480, 832 # Example for 480p
elif "720P" in model_id:
    max_height, max_width = 720, 1280 # Example for 720p
else: # Fallback
    max_height, max_width = 480, 832

# Simple resize for example, optimal resizing might need to maintain aspect ratio & VAE constraints
h, w = image.height, image.width
if w > max_width or h > max_height:
    aspect_ratio = w / h
    if w > h:
        new_w = max_width
        new_h = int(new_w / aspect_ratio)
    else:
        new_h = max_height
        new_w = int(new_h * aspect_ratio)
    # Ensure dimensions are divisible by VAE scale factors (typically 8 or 16)
    # This is a basic adjustment, model specific patch sizes might also matter.
    patch_size_factor = 16 # Common factor
    new_h = (new_h // patch_size_factor) * patch_size_factor
    new_w = (new_w // patch_size_factor) * patch_size_factor
    if new_h > 0 and new_w > 0:
         image = image.resize((new_w, new_h))
    else: # Fallback if calculations lead to zero
        image = image.resize((max_width//2, max_height//2)) # A smaller safe default
else:
    patch_size_factor = 16 
    h = (h // patch_size_factor) * patch_size_factor
    w = (w // patch_size_factor) * patch_size_factor
    if h > 0 and w > 0:
        image = image.resize((w,h))


prompt = "{main_prompt_for_snippet if main_prompt_for_snippet else 'An astronaut in a dynamic scene'}"
negative_prompt = "worst quality, low quality, blurry" # Simplified

output = pipe(
    image=image, 
    prompt=prompt, 
    negative_prompt=negative_prompt, 
    height=image.height, # Use resized image height
    width=image.width,   # Use resized image width
    num_frames=30,       # Adjust for LoRA
    guidance_scale=5.0   # Adjust as needed
).frames[0]
export_to_video(output, "output.mp4", fps=16)
```
"""
        else: # Fallback for other video LoRAs
            diffusers_example = f"""
```py
# This is a video LoRA. Diffusers usage for video models can vary.
# You may need to install/import specific pipeline classes from diffusers or the model's community.
# Below is a generic placeholder.
import torch
from diffusers import AutoPipelineForTextToVideo # Or the appropriate video pipeline

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

pipeline = AutoPipelineForTextToVideo.from_pretrained('{base_hf_model}', torch_dtype={dtype}).to(device)
{lora_loader_line}

# The following generation command is an example and may need adjustments
# based on the specific pipeline and its required parameters for '{base_hf_model}'.
# video_frames = pipeline(prompt='{main_prompt_for_snippet}', num_frames=16).frames
# For more details, consult the Hugging Face Hub page for {base_hf_model}
# and the Diffusers documentation on LoRAs and video pipelines.
```
"""
    else: # Image model
        diffusers_example = f"""
```py
from diffusers import AutoPipelineForText2Image
import torch

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

pipeline = AutoPipelineForText2Image.from_pretrained('{base_hf_model}', torch_dtype={dtype}).to(device)
{lora_loader_line}
image = pipeline('{main_prompt_for_snippet}').images[0]
```
"""

    license_map_simple = {
        "Public Domain": "public-domain",
        "CreativeML Open RAIL-M": "creativeml-openrail-m",
        "CreativeML Open RAIL++-M": "creativeml-openrail-m", 
        "openrail": "creativeml-openrail-m",
    }
    commercial_use = info.get("allowCommercialUse", "None") 
    license_identifier = "other"
    license_name = "bespoke-lora-trained-license"
    
    if isinstance(commercial_use, str) and commercial_use.lower() == "none" and not info.get("allowDerivatives", True):
        license_identifier = "creativeml-openrail-m" 
        license_name = "CreativeML OpenRAIL-M" 
    
    bespoke_license_link = f"https://multimodal.art/civitai-licenses?allowNoCredit={info['allowNoCredit']}&allowCommercialUse={commercial_use[0] if isinstance(commercial_use, list) and commercial_use else (commercial_use if isinstance(commercial_use, str) else 'None')}&allowDerivatives={info['allowDerivatives']}&allowDifferentLicense={info['allowDifferentLicense']}"

    content = f"""---
license: {license_identifier}
license_name: "{license_name}"
license_link: {bespoke_license_link}
tags:
- {unpacked_tags}

base_model: {base_hf_model}
instance_prompt: {trained_words[0] if trained_words else ''}
widget:
{widget_content.strip()}
---

# {info["name"]} 

<Gallery />

{non_author_disclaimer if not is_author else ''}
{link_civit_disclaimer if link_civit else ''}

## Model description
{info["description"] if info["description"] else "No description provided."}

{trigger_words_section}

## Download model
Weights for this model are available in Safetensors format.
[Download](/{user_repo_id}/tree/main) them in the Files & versions tab.

## Use it with the [🧨 diffusers library](https://github.com/huggingface/diffusers)
{diffusers_example}
For more details, including weighting, merging and fusing LoRAs, check the [documentation on loading LoRAs in diffusers](https://huggingface.co/docs/diffusers/main/en/using-diffusers/loading_adapters)
"""
    readme_content += content + "\n"
    readme_path = os.path.join(folder, "README.md")
    with open(readme_path, "w", encoding="utf-8") as file:
        file.write(readme_content)
    print(f"README.md created at {readme_path}")
    # print(f"README.md content:\n{readme_content}") # For debugging

def get_creator(username):
    url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
    try:
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error fetching creator data for {username}: {e}")
        gr.Warning(f"Could not verify Civitai creator's HF link: {e}")
        return None


def extract_huggingface_username(username_civitai):
    data = get_creator(username_civitai)
    if not data:
        return None
        
    links = data.get('result', {}).get('data', {}).get('json', {}).get('links', [])
    for link in links:
        url = link.get('url', '')
        if 'huggingface.co/' in url:
            # Extract username, handling potential variations like www. or trailing slashes
            hf_username = url.split('huggingface.co/')[-1].split('/')[0]
            if hf_username:
                return hf_username
    return None


def check_civit_link(profile: Optional[gr.OAuthProfile], url: str):
    # Initial return structure: instructions_html, submit_interactive, try_again_visible, other_submit_visible, hunyuan_radio_visible
    # Default to disabling/hiding things if checks fail early
    default_fail_updates = ("", gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False))

    if not profile: # Should be handled by demo.load and login button
        return "Please log in with Hugging Face.", gr.update(interactive=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

    if not url or not url.startswith("https://civitai.com/models/"):
        return "Please enter a valid Civitai model URL.", gr.update(interactive=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

    try:
        # We need hunyuan_type for extract_info, but we don't know it yet.
        # Call get_json_data first to check if it's Hunyuan.
        json_data_preview = get_json_data(url)
        if not json_data_preview:
            return ("Failed to fetch basic model info from Civitai. Check URL.",
                    gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False))

        is_hunyuan = False
        original_civitai_base_model = ""
        if json_data_preview.get("type") == "LORA":
            for mv in json_data_preview.get("modelVersions", []):
                # Try to find a relevant model version to check its base model
                # This is a simplified check; extract_info does a more thorough search
                cbm = mv.get("baseModel")
                if cbm and cbm in SUPPORTED_CIVITAI_BASE_MODELS:
                    original_civitai_base_model = cbm
                    if cbm == "Hunyuan Video":
                        is_hunyuan = True
                    break 
        
        # Now call process_url with a default hunyuan_type for other checks
        # The actual hunyuan_type choice will be used during the main upload.
        info, _ = process_url(url, profile, do_download=False, hunyuan_type="Image-to-Video") # Use default for check
        
        # If process_url raises an error (e.g. NSFW, not supported), it will be caught by Gradio
        # and displayed as a gr.Error. Here, we assume it passed if no exception.

    except gr.Error as e: # Catch errors from process_url (like NSFW, not supported)
        return (f"Cannot process this model: {e.message}",
                gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=is_hunyuan)) # Show hunyuan if detected
    except Exception as e: # Catch any other unexpected error during preview
        print(f"Unexpected error in check_civit_link: {e}")
        return (f"An unexpected error occurred: {str(e)}",
                gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=is_hunyuan))


    hf_username_on_civitai = extract_huggingface_username(info['creator'])
    
    if profile.username in TRUSTED_UPLOADERS:
        return ('Admin/Trusted user override: Upload enabled.', 
                gr.update(interactive=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_hunyuan))
        
    if not hf_username_on_civitai:
        no_username_text = (f'If you are {info["creator"]} on Civitai, hi! Your CivitAI profile does not seem to have a link to your Hugging Face account. '
                            f'Please visit <a href="https://civitai.com/user/account" target="_blank">https://civitai.com/user/account</a>, '
                            f'go to "Edit profile" and add your Hugging Face profile URL (e.g., https://huggingface.co/{profile.username}) to the "Links" section. '
                            f'<br><img width="60%" src="https://i.imgur.com/hCbo9uL.png" alt="Civitai profile links example"/><br>'
                            f'(If you are not {info["creator"]}, you cannot submit their model at this time.)')
        return no_username_text, gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=is_hunyuan)

    if profile.username.lower() != hf_username_on_civitai.lower():
        unmatched_username_text = (f'Oops! The Hugging Face username found on the CivitAI profile of {info["creator"]} is '
                                   f'"{hf_username_on_civitai}", but you are logged in as "{profile.username}". '
                                   f'Please ensure your CivitAI profile links to the correct Hugging Face account: '
                                   f'<a href="https://civitai.com/user/account" target="_blank">https://civitai.com/user/account</a> (Edit profile -> Links section).'
                                   f'<br><img width="60%" src="https://i.imgur.com/hCbo9uL.png" alt="Civitai profile links example"/>')
        return unmatched_username_text, gr.update(interactive=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=is_hunyuan)
    
    # All checks passed
    return ('Username verified! You can now upload this model.', 
            gr.update(interactive=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_hunyuan))

        
def swap_fill(profile: Optional[gr.OAuthProfile]):
    if profile is None: # Not logged in
        return gr.update(visible=True), gr.update(visible=False)
    else: # Logged in
        return gr.update(visible=False), gr.update(visible=True)

def show_output():
    return gr.update(visible=True)

def list_civit_models(username_civitai: str):
    if not username_civitai:
        return ""
    url = f"https://civitai.com/api/v1/models?username={username_civitai}&limit=100&sort=Newest" # Added sort
    
    all_model_urls = ""
    page_count = 0
    max_pages = 5 # Limit number of pages to fetch to avoid very long requests

    while url and page_count < max_pages:
        try:
            response = requests.get(url, timeout=10)
            response.raise_for_status()
            data = response.json()
        except requests.exceptions.RequestException as e:
            print(f"Error fetching model list for {username_civitai}: {e}")
            gr.Warning(f"Could not fetch full model list for {username_civitai}.")
            break 
        
        items = data.get('items', [])
        if not items:
            break

        for model in items:
            # Only list LORAs of supported base model types to avoid cluttering with unsupported ones
            is_supported_lora = False
            if model.get("type") == "LORA":
                # Check modelVersions for baseModel compatibility
                for mv in model.get("modelVersions", []):
                    if mv.get("baseModel") in SUPPORTED_CIVITAI_BASE_MODELS:
                        is_supported_lora = True
                        break
            if is_supported_lora:
                model_slug = slugify(model.get("name", f"model-{model['id']}"))
                all_model_urls += f'https://civitai.com/models/{model["id"]}/{model_slug}\n'
        
        metadata = data.get('metadata', {})
        url = metadata.get('nextPage', None)
        page_count += 1
        if page_count >= max_pages and url:
            print(f"Reached max page limit for fetching models for {username_civitai}.")
            gr.Info(f"Showing first {max_pages*100} models. There might be more.")

    if not all_model_urls:
        gr.Info(f"No compatible LoRA models found for user {username_civitai} or user not found.")
    return all_model_urls.strip()


def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: Optional[gr.OAuthToken], url: str, link_civit: bool, hunyuan_type: str):
    if not profile or not profile.username: # Check profile and username
        raise gr.Error("You must be logged in to Hugging Face to upload.")
    if not oauth_token or not oauth_token.token:
        raise gr.Error("Hugging Face authentication token is missing or invalid. Please log out and log back in.")
    
    folder = str(uuid.uuid4())
    os.makedirs(folder, exist_ok=True) # exist_ok=True is safer if folder might exist
    
    gr.Info(f"Starting processing for model {url}")
    try:
        # Pass hunyuan_type to process_url
        info, downloaded_files_summary = process_url(url, profile, do_download=True, folder=folder, hunyuan_type=hunyuan_type)
    except gr.Error as e: # Catch errors from process_url (NSFW, not supported, API fail)
        # Cleanup created folder if download failed or was skipped
        if os.path.exists(folder):
            try:
                import shutil
                shutil.rmtree(folder)
            except Exception as clean_e:
                print(f"Error cleaning up folder {folder}: {clean_e}")
        raise e # Re-raise the Gradio error to display it

    if not downloaded_files_summary.get("weightName"):
        raise gr.Error("No model weight file was downloaded. Cannot proceed with upload.")

    # Determine if user is the author for README generation
    # This relies on extract_huggingface_username which needs COOKIE_INFO
    is_author = False
    if "COOKIE_INFO" in os.environ:
        hf_username_on_civitai = extract_huggingface_username(info['creator'])
        if hf_username_on_civitai and profile.username.lower() == hf_username_on_civitai.lower():
            is_author = True
    elif profile.username.lower() == info['creator'].lower(): # Fallback if cookie not set, direct match
        is_author = True


    slug_name = slugify(info["name"])
    user_repo_id = f"{profile.username}/{slug_name}"
    
    gr.Info(f"Creating README for {user_repo_id}...")
    create_readme(info, downloaded_files_summary, user_repo_id, link_civit, is_author, folder=folder)
    
    try:
        gr.Info(f"Creating repository {user_repo_id} on Hugging Face...")
        create_repo(repo_id=user_repo_id, private=True, exist_ok=True, token=oauth_token.token)
        
        gr.Info(f"Starting upload of all files to {user_repo_id}...")
        upload_folder(
            folder_path=folder,
            repo_id=user_repo_id,
            repo_type="model",
            token=oauth_token.token,
            commit_message=f"Upload LoRA: {info['name']} from Civitai model ID {info['modelId']}" # Add commit message
        )
        
        gr.Info(f"Setting repository {user_repo_id} to public...")
        update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
        gr.Info(f"Model {info['name']} uploaded successfully to {user_repo_id}!")
    except Exception as e:
        print(f"Error during Hugging Face repo operations for {user_repo_id}: {e}")
        # Attempt to provide a more specific error message for token issues
        if "401" in str(e) or "Unauthorized" in str(e):
             raise gr.Error("Hugging Face authentication failed (e.g. token expired or insufficient permissions). Please log out and log back in with a token that has write permissions.")
        raise gr.Error(f"Error during Hugging Face upload: {str(e)}")
    finally:
        # Clean up the temporary folder
        if os.path.exists(folder):
            try:
                import shutil
                shutil.rmtree(folder)
                print(f"Cleaned up temporary folder: {folder}")
            except Exception as clean_e:
                print(f"Error cleaning up folder {folder}: {clean_e}")
        
    return f"""# Model uploaded to 🤗!
Access it here: [{user_repo_id}](https://huggingface.co/{user_repo_id})
"""

def bulk_upload(profile: Optional[gr.OAuthProfile], oauth_token: Optional[gr.OAuthToken], urls_text: str, link_civit: bool, hunyuan_type: str):
    if not urls_text.strip():
        return "No URLs provided for bulk upload."
        
    urls = [url.strip() for url in urls_text.split("\n") if url.strip()]
    if not urls:
        return "No valid URLs found in the input."

    upload_results_md = "## Bulk Upload Results:\n\n"
    success_count = 0
    failure_count = 0

    for i, url in enumerate(urls):
        gr.Info(f"Processing URL {i+1}/{len(urls)}: {url}")
        try:
            result = upload_civit_to_hf(profile, oauth_token, url, link_civit, hunyuan_type)
            upload_results_md += f"**SUCCESS**: {url}\n{result}\n\n---\n\n"
            success_count +=1
        except gr.Error as e: # Catch Gradio-raised errors (expected failures)
            upload_results_md += f"**FAILED**: {url}\n*Reason*: {e.message}\n\n---\n\n"
            gr.Warning(f"Failed to upload {url}: {e.message}")
            failure_count +=1
        except Exception as e: # Catch unexpected Python errors
            upload_results_md += f"**FAILED**: {url}\n*Unexpected Error*: {str(e)}\n\n---\n\n"
            gr.Warning(f"Unexpected error uploading {url}: {str(e)}")
            failure_count +=1
            
    summary = f"Finished bulk upload: {success_count} successful, {failure_count} failed."
    gr.Info(summary)
    upload_results_md = f"## {summary}\n\n" + upload_results_md
    return upload_results_md

# --- Gradio UI ---
css = '''
#login_button_row button { /* Target login button specifically */
    width: 100% !important;
    margin: 0 auto;
}
#disabled_upload_area { /* ID for the disabled area */
    opacity: 0.5;
    pointer-events: none;
}
'''

with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo: # Added a theme
    gr.Markdown('''# Upload your CivitAI LoRA to Hugging Face 🤗
By uploading your LoRAs to Hugging Face you get diffusers compatibility, a free GPU-based Inference Widget (for many models)
''')
    
    with gr.Row(elem_id="login_button_row"):
        login_button = gr.LoginButton() # Moved login_button definition here

    # Area shown when not logged in (or login fails)
    with gr.Column(elem_id="disabled_upload_area", visible=True) as disabled_area:
        gr.HTML("<i>Please log in with Hugging Face to enable uploads.</i>")
        # Add some dummy placeholders to mirror the enabled_area structure if needed for consistent layout
        gr.Textbox(label="CivitAI model URL (Log in to enable)", interactive=False)
        gr.Button("Upload (Log in to enable)", interactive=False)

    # Area shown when logged in
    with gr.Column(visible=False) as enabled_area:
        with gr.Row():
            submit_source_civit_enabled = gr.Textbox(
                placeholder="https://civitai.com/models/144684/pixelartredmond-pixel-art-loras-for-sd-xl",
                label="CivitAI model URL",
                info="URL of the CivitAI LoRA model page.",
                elem_id="submit_source_civit_main" # Unique ID
            )
        
        hunyuan_type_radio = gr.Radio(
            choices=["Image-to-Video", "Text-to-Video"],
            label="HunyuanVideo Type (Select if model is Hunyuan Video)",
            value="Image-to-Video", # Default as per prompt
            visible=False, # Initially hidden
            interactive=True
        )
        
        link_civit_checkbox = gr.Checkbox(label="Link back to original CivitAI page in README?", value=False)

        with gr.Accordion("Bulk Upload (Multiple LoRAs)", open=False):
            civit_username_to_bulk = gr.Textbox(
                label="Your CivitAI Username (Optional)",
                info="Type your CivitAI username here to automatically populate the list below with your compatible LoRAs."
            )
            submit_bulk_civit_urls = gr.Textbox(
                label="CivitAI Model URLs (One per line)",
                info="Add one CivitAI model URL per line for bulk processing.",
                lines=6,
            )
            bulk_button = gr.Button("Start Bulk Upload")
                
        instructions_html = gr.HTML("") # For messages from check_civit_link
        
        # Buttons for single upload
        # try_again_button is shown if username check fails
        try_again_button_single = gr.Button("I've updated my CivitAI profile, check again", visible=False)
        # submit_button_single is the main upload button for single model
        submit_button_single = gr.Button("Upload Model to Hugging Face", interactive=False, variant="primary")
        
        output_markdown = gr.Markdown(label="Upload Progress & Results", visible=False)

    # Event Handling
    # When login status changes (login_button implicitly handles profile state for demo.load)
    # demo.load updates visibility of disabled_area and enabled_area based on login.
    # The `profile` argument is implicitly passed by Gradio to functions that declare it.
    # `oauth_token` is also implicitly passed if `login_button` is used and function expects `gr.OAuthToken`.

    # When URL changes in the enabled area
    submit_source_civit_enabled.change(
        fn=check_civit_link,
        inputs=[submit_source_civit_enabled], # profile is implicitly passed
        outputs=[instructions_html, submit_button_single, try_again_button_single, submit_button_single, hunyuan_type_radio],
        # Outputs map to: instructions, submit_interactive, try_again_visible, (submit_visible - seems redundant here, check_civit_link logic ensures one is visible), hunyuan_radio_visible
        # For submit_button_single: 2nd output controls 'interactive', 4th controls 'visible' (often paired with try_again_button's visibility)
    )

    # Try again button for single upload (re-checks the same URL)
    try_again_button_single.click(
        fn=check_civit_link,
        inputs=[submit_source_civit_enabled],
        outputs=[instructions_html, submit_button_single, try_again_button_single, submit_button_single, hunyuan_type_radio],
    )

    # Autofill bulk URLs from CivitAI username
    civit_username_to_bulk.change(
        fn=list_civit_models,
        inputs=[civit_username_to_bulk],
        outputs=[submit_bulk_civit_urls]
    )

    # Single model upload button click
    submit_button_single.click(fn=show_output, outputs=[output_markdown]).then(
        fn=upload_civit_to_hf,
        inputs=[submit_source_civit_enabled, link_civit_checkbox, hunyuan_type_radio], # profile, oauth_token implicit
        outputs=[output_markdown]
    )

    # Bulk model upload button click
    bulk_button.click(fn=show_output, outputs=[output_markdown]).then(
        fn=bulk_upload,
        inputs=[submit_bulk_civit_urls, link_civit_checkbox, hunyuan_type_radio], # profile, oauth_token implicit
        outputs=[output_markdown]
    )
    
    # Initial state of visible areas based on login status
    demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area], queue=False)

demo.queue(default_concurrency_limit=5) # Reduced concurrency from 50, can be demanding
demo.launch(debug=True) # Added debug=True for development