File size: 53,643 Bytes
c63bb82
 
 
 
55045a6
 
 
 
c6c0d90
55045a6
c63bb82
 
 
55045a6
 
c63bb82
 
 
 
 
 
 
 
 
 
55045a6
 
c63bb82
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
ec7fc1c
c63bb82
 
 
6d65f3f
 
 
 
 
 
 
 
c6c0d90
55045a6
 
 
 
 
6d65f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
c6c0d90
55045a6
 
c63bb82
 
55045a6
c63bb82
55045a6
4c985dd
3d29507
c63bb82
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
ec7fc1c
c63bb82
55045a6
c63bb82
 
 
55045a6
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
 
 
c63bb82
 
 
 
 
 
 
 
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
 
 
c63bb82
 
55045a6
 
 
 
 
 
 
c63bb82
 
55045a6
 
 
 
c63bb82
55045a6
 
 
 
 
c63bb82
 
55045a6
 
 
c63bb82
 
55045a6
 
3d29507
55045a6
 
 
 
 
 
 
 
 
c63bb82
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
55045a6
 
 
 
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc51fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
dc51fb0
c63bb82
 
 
 
 
 
 
 
 
 
9db1e37
c63bb82
 
 
9db1e37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f325bb3
 
 
 
 
 
 
 
dc51fb0
 
 
 
 
9db1e37
dc51fb0
9db1e37
dc51fb0
 
 
 
 
 
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9db1e37
c63bb82
 
9db1e37
 
 
c63bb82
9db1e37
 
dc51fb0
c63bb82
9db1e37
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc51fb0
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
dc51fb0
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
55045a6
c63bb82
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
d85d301
c63bb82
 
 
 
 
d85d301
c63bb82
 
 
 
3d29507
c63bb82
 
 
 
 
 
 
55045a6
c63bb82
 
 
55045a6
c63bb82
 
 
 
 
55045a6
c63bb82
 
 
 
55045a6
c63bb82
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
55045a6
c63bb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55045a6
bea56e8
c63bb82
55045a6
c63bb82
bea56e8
 
 
 
 
 
 
 
55045a6
 
f51e2c4
3dfa903
 
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f325bb3
 
55045a6
 
 
 
 
 
f325bb3
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9db1e37
55045a6
9db1e37
 
55045a6
 
 
f325bb3
55045a6
9db1e37
55045a6
9db1e37
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8386cc
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
55045a6
dc51fb0
55045a6
 
 
 
 
 
c63bb82
55045a6
 
 
 
 
 
 
c63bb82
55045a6
 
 
 
c63bb82
55045a6
 
c63bb82
 
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bb82
55045a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea56e8
55045a6
 
 
 
 
 
 
4c985dd
 
c63bb82
 
 
0d28f6f
f7a4cfb
c63bb82
 
 
 
 
 
 
 
 
 
f55e007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
"""
Pro AI Headshot Generator - Public Access Version
Transforms any selfie into professional headshots using AI.
"""
import cv2
import torch
import random
import numpy as np
import os
import time
import glob
from pathlib import Path
from typing import Tuple, Optional
from datetime import datetime, timedelta

try:
    import spaces
    SPACES_AVAILABLE = True
except ImportError:
    SPACES_AVAILABLE = False
    # Create a dummy decorator for local development
    class spaces:
        @staticmethod
        def GPU(func):
            return func

import PIL
from PIL import Image

import diffusers
from diffusers.utils import load_image
from diffusers.models import ControlNetModel
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
from huggingface_hub import hf_hub_download
from insightface.app import FaceAnalysis

from style_template import styles
from pipeline_stable_diffusion_xl_instantid_full import (
    StableDiffusionXLInstantIDPipeline,
    draw_kps,
)
import gradio as gr
import json
import sys
import gc
import os

# Ensure the current directory and parent directory are in Python path (for Hugging Face Spaces)
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
for dir_path in [current_dir, parent_dir]:
    if dir_path not in sys.path:
        sys.path.insert(0, dir_path)

from depth_anything.dpt import DepthAnything
from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
import torch.nn.functional as F
from torchvision.transforms import Compose

# Try to import config, with fallback values if it fails
try:
    from config import (
        TEMP_DIR,
        MAX_FILE_AGE_HOURS,
        MAX_IMAGE_SIZE_MB,
        ALLOWED_IMAGE_FORMATS,
        MIN_IMAGE_DIMENSION,
        MAX_IMAGE_DIMENSION,
        MAX_PROMPT_LENGTH,
        MAX_NEGATIVE_PROMPT_LENGTH,
        DEFAULT_NUM_STEPS,
        DEFAULT_GUIDANCE_SCALE,
        DEFAULT_SEED,
    )
except ImportError:
    # Fallback configuration if config.py is not found
    from pathlib import Path
    TEMP_DIR = Path(os.getenv("TEMP_DIR", "temp_downloads"))
    TEMP_DIR.mkdir(exist_ok=True, parents=True)
    MAX_FILE_AGE_HOURS = int(os.getenv("MAX_FILE_AGE_HOURS", "24"))
    MAX_IMAGE_SIZE_MB = int(os.getenv("MAX_IMAGE_SIZE_MB", "10"))
    ALLOWED_IMAGE_FORMATS = {".jpg", ".jpeg", ".png", ".webp"}
    MIN_IMAGE_DIMENSION = 128
    MAX_IMAGE_DIMENSION = 4096
    DEFAULT_NUM_STEPS = int(os.getenv("DEFAULT_NUM_STEPS", "30"))
    DEFAULT_GUIDANCE_SCALE = float(os.getenv("DEFAULT_GUIDANCE_SCALE", "5.0"))
    DEFAULT_SEED = int(os.getenv("DEFAULT_SEED", "42"))
    MAX_PROMPT_LENGTH = int(os.getenv("MAX_PROMPT_LENGTH", "500"))
    MAX_NEGATIVE_PROMPT_LENGTH = int(os.getenv("MAX_NEGATIVE_PROMPT_LENGTH", "500"))
    print("⚠ Warning: config.py not found, using default configuration values")


# ============ GLOBAL CONFIG ============
MAX_SEED = np.iinfo(np.int32).max

# Device detection - fixed logic
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
STYLE_NAMES = list(styles.keys())


# ============ ZEROGPU HELPER FUNCTIONS ============
def serialize_for_zerogpu(obj):
    """
    Serialize objects for ZeroGPU return values.
    Converts numpy arrays, pandas DataFrames, and other non-serializable objects
    to JSON-serializable formats.
    """
    try:
        import pandas as pd
        HAS_PANDAS = True
    except ImportError:
        HAS_PANDAS = False
    
    if isinstance(obj, np.ndarray):
        return obj.tolist()
    elif isinstance(obj, (np.integer, np.floating)):
        return float(obj)
    elif isinstance(obj, (np.bool_)):
        return bool(obj)
    elif HAS_PANDAS and isinstance(obj, pd.DataFrame):
        # Convert DataFrame to dict with limited rows to avoid large returns
        max_rows = 1000  # Limit to prevent large returns
        df_limited = obj.head(max_rows) if len(obj) > max_rows else obj
        return {
            'data': df_limited.to_dict('records'),
            'columns': df_limited.columns.tolist(),
            'total_rows': len(obj),
            'limited': len(obj) > max_rows
        }
    elif HAS_PANDAS and isinstance(obj, pd.Series):
        return obj.tolist()
    elif isinstance(obj, dict):
        return {k: serialize_for_zerogpu(v) for k, v in obj.items()}
    elif isinstance(obj, (list, tuple)):
        return [serialize_for_zerogpu(item) for item in obj]
    elif isinstance(obj, (str, int, float, bool, type(None))):
        return obj
    else:
        # Try to convert to string as fallback
        try:
            return str(obj)
        except:
            return None


def safe_zerogpu_return(*args, **kwargs):
    """
    Wrapper to safely return values from ZeroGPU functions.
    Ensures all return values are serializable and not too large.
    """
    results = []
    if args:
        if len(args) == 1:
            results = serialize_for_zerogpu(args[0])
        else:
            results = [serialize_for_zerogpu(arg) for arg in args]
    
    # Clean up GPU memory before returning
    if device == "cuda":
        try:
            torch.cuda.empty_cache()
        except:
            pass
    gc.collect()
    
    return results


# ============ FILE MANAGEMENT ============
def cleanup_old_files():
    """Remove files older than MAX_FILE_AGE_HOURS from temp directory."""
    if not TEMP_DIR.exists():
        return
    
    cutoff_time = time.time() - (MAX_FILE_AGE_HOURS * 3600)
    deleted_count = 0
    
    for file_path in TEMP_DIR.glob("*"):
        if file_path.is_file():
            try:
                if file_path.stat().st_mtime < cutoff_time:
                    file_path.unlink()
                    deleted_count += 1
            except Exception as e:
                print(f"Warning: Could not delete {file_path}: {e}")
    
    if deleted_count > 0:
        print(f"Cleaned up {deleted_count} old files from temp directory")


def save_as_png(image: Image.Image, filename: str = "professional_headshot") -> str:
    """Save image as PNG in temp directory with cleanup."""
    # Cleanup old files before saving new ones
    cleanup_old_files()
    
    TEMP_DIR.mkdir(exist_ok=True, parents=True)
    timestamp = int(time.time())
    filepath = TEMP_DIR / f"{filename}_{timestamp}.png"

    # Ensure image is in RGB mode
    if image.mode in ("RGBA", "LA"):
        background = Image.new("RGB", image.size, (255, 255, 255))
        background.paste(image, mask=image.split()[-1])
        image = background
    elif image.mode != "RGB":
        image = image.convert("RGB")

    image.save(filepath, "PNG", optimize=True)
    return str(filepath)


# ============ INPUT VALIDATION ============
def validate_image_file(file_path: str) -> Tuple[bool, Optional[str]]:
    """Validate uploaded image file."""
    if not file_path:
        return False, "Please upload an image file."
    
    # Check file exists
    if not os.path.exists(file_path):
        return False, "Uploaded file not found. Please try again."
    
    # Check file extension
    ext = Path(file_path).suffix.lower()
    if ext not in ALLOWED_IMAGE_FORMATS:
        return False, f"Unsupported file format. Allowed formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
    
    # Check file size
    file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
    if file_size_mb > MAX_IMAGE_SIZE_MB:
        return False, f"File too large. Maximum size: {MAX_IMAGE_SIZE_MB}MB"
    
    # Try to open and validate image
    try:
        with Image.open(file_path) as img:
            width, height = img.size
            if width < MIN_IMAGE_DIMENSION or height < MIN_IMAGE_DIMENSION:
                return False, f"Image too small. Minimum size: {MIN_IMAGE_DIMENSION}x{MIN_IMAGE_DIMENSION}px"
            if width > MAX_IMAGE_DIMENSION or height > MAX_IMAGE_DIMENSION:
                return False, f"Image too large. Maximum size: {MAX_IMAGE_DIMENSION}x{MAX_IMAGE_DIMENSION}px"
    except Exception as e:
        return False, f"Invalid image file: {str(e)}"
    
    return True, None


def validate_prompt(prompt: str) -> Tuple[bool, Optional[str]]:
    """Validate prompt input."""
    if not prompt or not prompt.strip():
        return True, None  # Empty prompt is allowed (will use default)
    
    if len(prompt) > MAX_PROMPT_LENGTH:
        return False, f"Prompt too long. Maximum length: {MAX_PROMPT_LENGTH} characters"
    
    return True, None


def validate_negative_prompt(negative_prompt: str) -> Tuple[bool, Optional[str]]:
    """Validate negative prompt input."""
    if not negative_prompt:
        return True, None
    
    if len(negative_prompt) > MAX_NEGATIVE_PROMPT_LENGTH:
        return False, f"Negative prompt too long. Maximum length: {MAX_NEGATIVE_PROMPT_LENGTH} characters"
    
    return True, None


# ============ MODEL LOADING ============
print("Loading AI models... This may take a few minutes on first run.")
print("=" * 60)

def check_file_exists(local_path):
    """Check if a file exists locally."""
    return Path(local_path).exists()

try:
    # InstantID checkpoints - only download if missing
    print("Checking InstantID checkpoints...")
    checkpoint_dir = Path("./checkpoints/ControlNetModel")
    config_path = checkpoint_dir / "config.json"
    model_path = checkpoint_dir / "diffusion_pytorch_model.safetensors"
    adapter_path = Path("./checkpoints/ip-adapter.bin")
    
    if not config_path.exists():
        print("  Downloading config.json...")
        hf_hub_download(
            repo_id="InstantX/InstantID",
            filename="ControlNetModel/config.json",
            local_dir="./checkpoints",
        )
        print("  ✓ config.json downloaded")
    else:
        print("  ✓ config.json already exists")
    
    if not model_path.exists():
        print("  Downloading diffusion_pytorch_model.safetensors (this may take a while)...")
        hf_hub_download(
            repo_id="InstantX/InstantID",
            filename="ControlNetModel/diffusion_pytorch_model.safetensors",
            local_dir="./checkpoints",
        )
        print("  ✓ diffusion_pytorch_model.safetensors downloaded")
    else:
        print("  ✓ diffusion_pytorch_model.safetensors already exists")
    
    if not adapter_path.exists():
        print("  Downloading ip-adapter.bin...")
        hf_hub_download(
            repo_id="InstantX/InstantID", 
            filename="ip-adapter.bin", 
            local_dir="./checkpoints"
        )
        print("  ✓ ip-adapter.bin downloaded")
    else:
        print("  ✓ ip-adapter.bin already exists")
    
    print("✓ InstantID checkpoints ready")
except Exception as e:
    print(f"⚠ Warning: Error downloading InstantID checkpoints: {e}")
    print("  The app will attempt to use cached models if available.")
    import traceback
    traceback.print_exc()

try:
    # Face encoder
    print("\nLoading face recognition model...")
    models_dir = Path("./models/antelopev2")
    if not models_dir.exists():
        print("  Warning: Face models directory not found. Models will be downloaded automatically.")
    
    app = FaceAnalysis(
        name="antelopev2",
        root="./",
        providers=["CPUExecutionProvider"],
    )
    app.prepare(ctx_id=0, det_size=(640, 640))
    print("✓ Face recognition model loaded")
except Exception as e:
    print(f"✗ Error loading face recognition model: {e}")
    import traceback
    traceback.print_exc()
    raise RuntimeError("Failed to load face recognition model. Please ensure models are downloaded correctly.")

try:
    # DepthAnything
    print("\nLoading depth estimation model...")
    print("  This may take a few minutes on first run (downloading ~1.5GB)...")
    depth_anything = DepthAnything.from_pretrained(
        "LiheYoung/depth_anything_vitl14"
    ).to(device).eval()
    print("✓ Depth estimation model loaded")
except Exception as e:
    print(f"✗ Error loading depth estimation model: {e}")
    import traceback
    traceback.print_exc()
    raise RuntimeError("Failed to load depth estimation model. Please check your internet connection and try again.")

transform = Compose(
    [
        Resize(
            width=518,
            height=518,
            resize_target=False,
            keep_aspect_ratio=True,
            ensure_multiple_of=14,
            resize_method="lower_bound",
            image_interpolation_method=cv2.INTER_CUBIC,
        ),
        NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
        PrepareForNet(),
    ]
)

face_adapter = "./checkpoints/ip-adapter.bin"
controlnet_path = "./checkpoints/ControlNetModel"

try:
    print("\nLoading ControlNet models...")
    print("  Loading Identity ControlNet...")
    controlnet_identitynet = ControlNetModel.from_pretrained(
        controlnet_path, torch_dtype=dtype
    )
    print("  ✓ Identity ControlNet loaded")
    
    controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
    controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
    
    print("  Loading Canny ControlNet (this may take a while)...")
    controlnet_canny = ControlNetModel.from_pretrained(
        controlnet_canny_model, torch_dtype=dtype
    ).to(device)
    print("  ✓ Canny ControlNet loaded")
    
    print("  Loading Depth ControlNet (this may take a while)...")
    controlnet_depth = ControlNetModel.from_pretrained(
        controlnet_depth_model, torch_dtype=dtype
    ).to(device)
    print("  ✓ Depth ControlNet loaded")
    print("✓ All ControlNet models loaded")
except Exception as e:
    print(f"✗ Error loading ControlNet models: {e}")
    import traceback
    traceback.print_exc()
    raise RuntimeError("Failed to load ControlNet models. Please check your internet connection and disk space.")


def get_depth_map(image):
    """Generate depth map from image."""
    try:
        print("    Processing depth estimation...")
        image = np.array(image) / 255.0
        h, w = image.shape[:2]
        print(f"    Input size: {w}x{h}")
        
        print("    Applying transforms...")
        image = transform({"image": image})["image"]
        image = torch.from_numpy(image).unsqueeze(0).to(device)
        
        print("    Running depth model (this may take a while on CPU)...")
        with torch.no_grad():
            depth = depth_anything(image)
        
        print("    Post-processing depth map...")
        depth = F.interpolate(
            depth[None], (h, w), mode="bilinear", align_corners=False
        )[0, 0]
        depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
        depth = depth.cpu().numpy().astype(np.uint8)
        depth_image = Image.fromarray(depth)
        print("    ✓ Depth map generated")
        return depth_image
    except Exception as e:
        print(f"    ✗ Error generating depth map: {e}")
        import traceback
        traceback.print_exc()
        # Return a blank depth map as fallback
        return Image.new("L", (512, 512), 128)


def get_canny_image(image, t1=100, t2=200):
    """Generate canny edge map from image."""
    try:
        image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
        edges = cv2.Canny(image, t1, t2)
        return Image.fromarray(edges, "L")
    except Exception as e:
        print(f"Warning: Error generating canny edges: {e}")
        # Return a blank canny map as fallback
        return Image.new("L", (512, 512), 0)


controlnet_map = {
    "canny": controlnet_canny,
    "depth": controlnet_depth,
}
controlnet_map_fn = {
    "canny": get_canny_image,
    "depth": get_depth_map,
}

pretrained_model_name_or_path = "wangqixun/YamerMIX_v8"

try:
    print("\nLoading Stable Diffusion XL pipeline...")
    print("  This is the largest model (~6-7GB) and may take 10-20 minutes on first run...")
    print("  Please be patient and ensure you have a stable internet connection.")
    pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
        pretrained_model_name_or_path,
        controlnet=[controlnet_identitynet],
        torch_dtype=dtype,
        safety_checker=None,
        feature_extractor=None,
    ).to(device)
    print("✓ Stable Diffusion XL pipeline loaded")
    
    # Standard scheduler
    pipe.scheduler = diffusers.EulerDiscreteScheduler.from_config(
        pipe.scheduler.config
    )
    
    print("\nLoading IP-Adapter...")
    if device == "cuda":
        pipe.cuda()
        pipe.load_ip_adapter_instantid(face_adapter)
        # Ensure all components are on CUDA
        pipe.image_proj_model.to("cuda")
        pipe.unet.to("cuda")
        # Verify models have data (not meta tensors)
        test_param = next(pipe.unet.parameters())
        if hasattr(test_param, 'is_meta') and test_param.is_meta:
            print("  ⚠ Warning: UNet appears to have meta tensors")
        else:
            print(f"  ✓ UNet verified on device: {test_param.device}")
    else:
        pipe.load_ip_adapter_instantid(face_adapter)
        # Verify CPU models have data
        test_param = next(pipe.unet.parameters())
        if hasattr(test_param, 'is_meta') and test_param.is_meta:
            print("  ⚠ Warning: UNet appears to have meta tensors")
        else:
            print(f"  ✓ UNet verified on device: {test_param.device}")
    print("✓ IP-Adapter loaded")
    
    print("\n" + "=" * 60)
    print("✅ ALL MODELS LOADED SUCCESSFULLY!")
    print("=" * 60)
    print("The application is now ready to use.")
except Exception as e:
    print(f"\n✗ Error loading Stable Diffusion pipeline: {e}")
    import traceback
    traceback.print_exc()
    raise RuntimeError(f"Failed to load Stable Diffusion pipeline: {e}. Please check your internet connection and disk space.")


# ============ UTILS ============
def toggle_lcm_ui(value: bool) -> Tuple[dict, dict]:
    """Toggle UI for LCM mode."""
    if value:
        return (
            gr.update(minimum=0, maximum=100, step=1, value=5),
            gr.update(minimum=0.1, maximum=20.0, step=0.1, value=1.5),
        )
    else:
        return (
            gr.update(minimum=5, maximum=100, step=1, value=DEFAULT_NUM_STEPS),
            gr.update(minimum=0.1, maximum=20.0, step=0.1, value=DEFAULT_GUIDANCE_SCALE),
        )


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    """Randomize seed if requested."""
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed


def convert_from_cv2_to_image(img: np.ndarray) -> Image.Image:
    """Convert OpenCV image to PIL Image."""
    return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))


def convert_from_image_to_cv2(img: Image.Image) -> np.ndarray:
    """Convert PIL Image to OpenCV format."""
    return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)


def resize_img(
    input_image,
    max_side=1280,
    min_side=1024,
    size=None,
    pad_to_max_side=False,
    mode=PIL.Image.BILINEAR,
    base_pixel_number=64,
):
    """Resize image maintaining aspect ratio."""
    w, h = input_image.size
    if size is not None:
        w_resize_new, h_resize_new = size
    else:
        ratio = min_side / min(h, w)
        w, h = round(ratio * w), round(ratio * h)
        ratio = max_side / max(h, w)
        input_image = input_image.resize([round(ratio * w), round(ratio * h)], mode)
        w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
        h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
    input_image = input_image.resize([w_resize_new, h_resize_new], mode)

    if pad_to_max_side:
        res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
        offset_x = (max_side - w_resize_new) // 2
        offset_y = (max_side - h_resize_new) // 2
        res[offset_y : offset_y + h_resize_new, offset_x : offset_x + w_resize_new] = (
            np.array(input_image)
        )
        input_image = Image.fromarray(res)
    return input_image


def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
    """Apply style template to prompts."""
    if style_name == "No Style":
        return positive, negative
    p, n = styles.get(style_name, ("{prompt}", ""))
    return p.replace("{prompt}", positive), n + " " + negative


# ============ GENERATION FUNCTION ============
@spaces.GPU  # ZeroGPU will allocate GPU for this function (Hugging Face Spaces only)
def generate_image(
    face_image_path: str,
    prompt: str,
    negative_prompt: str,
    style_name: str,
    num_steps: int,
    identitynet_strength_ratio: float,
    adapter_strength_ratio: float,
    canny_strength: float,
    depth_strength: float,
    controlnet_selection: list,
    guidance_scale: float,
    seed: int,
    scheduler: str,
    enable_LCM: bool,
    enhance_face_region: bool,
    progress=gr.Progress(track_tqdm=True),
):
    """Generate professional headshot from face image."""
    try:
        print("\n" + "=" * 60)
        print("Starting image generation...")
        print("=" * 60)
        # Validate inputs
        print("Step 1/8: Validating inputs...")
        is_valid, error_msg = validate_image_file(face_image_path)
        if not is_valid:
            raise gr.Error(error_msg)
        
        is_valid, error_msg = validate_prompt(prompt)
        if not is_valid:
            raise gr.Error(error_msg)
        
        is_valid, error_msg = validate_negative_prompt(negative_prompt)
        if not is_valid:
            raise gr.Error(error_msg)
        print("  ✓ Inputs validated")

        # Randomize seed if needed
        if seed < 0:
            seed = random.randint(0, MAX_SEED)

        # Load and process face image
        print("Step 2/8: Loading and processing face image...")
        face_image = load_image(face_image_path)
        face_image = resize_img(face_image, max_side=1024)
        face_image_cv2 = convert_from_image_to_cv2(face_image)
        height, width, _ = face_image_cv2.shape
        print(f"  ✓ Image loaded: {width}x{height}")

        # Detect face
        print("Step 3/8: Detecting face...")
        face_info_list = app.get(face_image_cv2)
        if len(face_info_list) == 0:
            raise gr.Error(
                "Unable to detect a face in the image. Please upload a different photo with a clear face."
            )
        print(f"  ✓ Face detected")

        # Use largest detected face
        print("Step 4/8: Processing face features...")
        face_info = sorted(
            face_info_list,
            key=lambda x: (x["bbox"][2] - x["bbox"][0]) * (x["bbox"][3] - x["bbox"][1]),
        )[-1]
        
        # Detect gender from face info if available (for identity preservation)
        detected_gender = None
        if "gender" in face_info:
            detected_gender = face_info["gender"]
            print(f"  Detected gender: {'Female' if detected_gender == 0 else 'Male' if detected_gender == 1 else 'Unknown'}")
        elif hasattr(face_info, "get") and face_info.get("gender") is not None:
            detected_gender = face_info.get("gender")
            print(f"  Detected gender: {'Female' if detected_gender == 0 else 'Male' if detected_gender == 1 else 'Unknown'}")

        # Configure scheduler
        print("Step 5/8: Configuring scheduler...")
        scheduler_class_name = scheduler.split("-")[0]
        add_kwargs = {}
        if len(scheduler.split("-")) > 1:
            add_kwargs["use_karras_sigmas"] = True
        if len(scheduler.split("-")) > 2:
            add_kwargs["algorithm_type"] = "sde-dpmsolver++"
        scheduler_cls = getattr(diffusers, scheduler_class_name)
        pipe.scheduler = scheduler_cls.from_config(pipe.scheduler.config, **add_kwargs)
        print(f"  ✓ Scheduler: {scheduler_class_name}")

        # Apply style and process prompts (AFTER face detection so we can use gender info)
        if not prompt:
            prompt = "a person"
        
        # Add explicit gender to prompt if detected (InstantID works better with explicit gender)
        if detected_gender is not None:
            prompt_lower = prompt.lower()
            # Only add gender if not already in prompt
            if "man" not in prompt_lower and "woman" not in prompt_lower and "male" not in prompt_lower and "female" not in prompt_lower and "person" not in prompt_lower:
                if detected_gender == 0:  # Female
                    prompt = f"a woman, {prompt}"
                    print(f"  ✓ Added 'a woman' to prompt for gender preservation")
                elif detected_gender == 1:  # Male
                    prompt = f"a man, {prompt}"
                    print(f"  ✓ Added 'a man' to prompt for gender preservation")
            elif "person" in prompt_lower:
                # Replace "person" with specific gender
                if detected_gender == 0:  # Female
                    prompt = prompt.replace("person", "woman").replace("Person", "Woman")
                    print(f"  ✓ Replaced 'person' with 'woman' in prompt")
                elif detected_gender == 1:  # Male
                    prompt = prompt.replace("person", "man").replace("Person", "Man")
                    print(f"  ✓ Replaced 'person' with 'man' in prompt")
        
        # Warn if prompt contains physical feature descriptions that might override identity
        physical_keywords = ["hair", "blonde", "brown hair", "black hair", "red hair", "beard", "mustache", 
                            "wearing", "shirt", "jacket", "suit", "blazer", "tie", "glasses"]
        prompt_lower = prompt.lower()
        if any(keyword in prompt_lower for keyword in physical_keywords):
            print("  ⚠ Warning: Prompt contains physical feature descriptions. These may override face identity.")
            print("  💡 Tip: Focus on style/setting only (e.g., 'professional headshot, studio lighting') for better identity preservation.")
        
        # Add gender preservation to negative prompt if gender was detected
        gender_negative_terms = "wrong gender, gender swap, different person, different face, face swap, identity change, different identity"
        if detected_gender is not None:
            # Add opposite gender terms to negative prompt
            if detected_gender == 0:  # Female
                gender_negative_terms += ", man, male, masculine, boy"
            elif detected_gender == 1:  # Male
                gender_negative_terms += ", woman, female, feminine, girl"
            print(f"  ✓ Gender preservation enabled in negative prompt")
        
        # Add gender preservation terms to negative prompt
        if gender_negative_terms not in negative_prompt:
            negative_prompt = f"{negative_prompt}, {gender_negative_terms}" if negative_prompt else gender_negative_terms
        
        prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
        print(f"  ✓ Style applied: {style_name}")
        
        # Extract face embedding and ensure it's a proper tensor
        face_emb_raw = face_info["embedding"]
        print(f"  Face embedding raw type: {type(face_emb_raw)}")
        
        # Convert to torch tensor if it's numpy
        if isinstance(face_emb_raw, np.ndarray):
            face_emb = torch.from_numpy(face_emb_raw).to(device).to(dtype)
        elif isinstance(face_emb_raw, torch.Tensor):
            face_emb = face_emb_raw.to(device).to(dtype)
        else:
            # Fallback: try to convert
            face_emb = torch.tensor(face_emb_raw, device=device, dtype=dtype)
        
        # Ensure proper shape for InstantID (should be [1, 512] or [512])
        if len(face_emb.shape) == 1:
            face_emb = face_emb.unsqueeze(0)  # Add batch dimension: [1, 512]
        elif len(face_emb.shape) == 2 and face_emb.shape[0] > 1:
            # If multiple faces, take the first one
            face_emb = face_emb[0:1]
        
        # DO NOT normalize - InstantID expects raw embeddings from InsightFace
        # Normalization breaks the identity preservation
        
        print(f"  Face embedding final shape: {face_emb.shape}, dtype: {face_emb.dtype}, device: {face_emb.device}")
        print(f"  Face embedding range: [{face_emb.min().item():.4f}, {face_emb.max().item():.4f}]")
        
        face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
        print(f"  Face keypoints image size: {face_kps.size}")
        img_controlnet = face_image
        print("  ✓ Face features extracted")
        import sys
        sys.stdout.flush()

        # Create control mask if requested
        print("Step 5/8: Preparing control images...")
        if enhance_face_region:
            control_mask = np.zeros([height, width, 3])
            x1, y1, x2, y2 = face_info["bbox"]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            control_mask[y1:y2, x1:x2] = 255
            control_mask = Image.fromarray(control_mask.astype(np.uint8))
        else:
            control_mask = None

        # Configure ControlNet
        print("Step 6/8: Configuring ControlNet...")
        if len(controlnet_selection) > 0:
            controlnet_scales = {
                "canny": canny_strength,
                "depth": depth_strength,
            }
            
            # Generate control images with progress updates first
            control_images = [face_kps]
            successful_controls = []
            
            for s in controlnet_selection:
                print(f"  Generating {s} control image...")
                try:
                    if s == "depth":
                        # Depth generation can be slow, show progress
                        print("    (This may take 10-20 seconds on CPU...)")
                    control_img = controlnet_map_fn[s](img_controlnet).resize((width, height))
                    control_images.append(control_img)
                    successful_controls.append(s)
                    print(f"  ✓ {s} control image generated")
                except Exception as e:
                    print(f"  ⚠ Warning: Failed to generate {s} control image: {e}")
                    print(f"    Continuing without {s} control...")
            
            # Configure ControlNet based on successful controls
            if len(successful_controls) > 0:
                pipe.controlnet = MultiControlNetModel(
                    [controlnet_identitynet]
                    + [controlnet_map[s] for s in successful_controls]
                )
                control_scales = [float(identitynet_strength_ratio)] + [
                    controlnet_scales[s] for s in successful_controls
                ]
                print(f"  ✓ ControlNet configured with: {successful_controls}")
            else:
                # Fallback to identity only if all controls failed
                pipe.controlnet = controlnet_identitynet
                control_scales = float(identitynet_strength_ratio)
                control_images = face_kps
                print("  ✓ ControlNet configured: identity only (control generation failed)")
        else:
            pipe.controlnet = controlnet_identitynet
            control_scales = float(identitynet_strength_ratio)
            control_images = face_kps
            print("  ✓ ControlNet configured: identity only")

        # Adjust steps for LCM if enabled
        if enable_LCM:
            num_steps = max(5, min(num_steps, 10))
            guidance_scale = max(1.0, min(guidance_scale, 2.0))
            print("  ✓ Fast generation mode enabled")

        generator = torch.Generator(device=device).manual_seed(seed)
        print(f"  Generator created on {device}, seed: {seed}")

        pipe.set_ip_adapter_scale(adapter_strength_ratio)
        print(f"  IP-Adapter scale set to: {adapter_strength_ratio}")
        import sys
        sys.stdout.flush()
        
        # Generate image
        print("Step 7/8: Generating image (this may take 30-60 seconds)...")
        print(f"  Steps: {num_steps}, Guidance: {guidance_scale}, Seed: {seed}")
        print(f"  Device: {device}, Image size: {width}x{height}")
        print(f"  Control images type: {type(control_images)}, length: {len(control_images) if isinstance(control_images, list) else 'single'}")
        print("  Starting diffusion process...")
        import sys
        sys.stdout.flush()  # Force output
        
        # Create callback for progress updates
        step_count = [0]  # Use list to allow modification in nested function
        
        def progress_callback(step, timestep, latents):
            step_count[0] = step + 1
            print(f"  Progress: Step {step_count[0]}/{num_steps} ({(step_count[0]/num_steps)*100:.1f}%)")
            sys.stdout.flush()
            if progress:
                try:
                    progress(step / num_steps, desc=f"Generating... {step}/{num_steps}")
                except:
                    pass
        
        print("  Calling pipeline...")
        sys.stdout.flush()
        
        try:
            # Ensure all inputs are on correct device
            print("  Preparing inputs...")
            print(f"    Prompt length: {len(prompt)} chars")
            print(f"    Negative prompt length: {len(negative_prompt)} chars")
            print(f"    Face embedding: shape={face_emb.shape}, dtype={face_emb.dtype}, device={face_emb.device}")
            print(f"    Face embedding has data: {face_emb.numel() > 0}")
            if isinstance(control_images, list):
                print(f"    Control images: {len(control_images)} images")
                for i, img in enumerate(control_images):
                    print(f"      Image {i}: {type(img)}, size: {img.size if hasattr(img, 'size') else 'N/A'}")
            else:
                print(f"    Control image: {type(control_images)}, size: {control_images.size if hasattr(control_images, 'size') else 'N/A'}")
            sys.stdout.flush()
            
            # Verify face_emb is not a meta tensor
            if hasattr(face_emb, 'is_meta') and face_emb.is_meta:
                print("  ✗ ERROR: Face embedding is a meta tensor!")
                raise RuntimeError("Face embedding is a meta tensor. This usually means the model wasn't loaded correctly.")
            
            # Ensure face_emb has actual data
            if face_emb.numel() == 0:
                print("  ✗ ERROR: Face embedding is empty!")
                raise RuntimeError("Face embedding is empty. Face detection may have failed.")
            
            # Check if pipeline is ready
            print("  Checking pipeline state...")
            print(f"    Pipeline device: {next(pipe.unet.parameters()).device}")
            print(f"    Pipeline dtype: {next(pipe.unet.parameters()).dtype}")
            sys.stdout.flush()
            
            # Force garbage collection before generation
            import gc
            if device == "cuda":
                torch.cuda.empty_cache()
            gc.collect()
            print("  Memory cleared, starting generation...")
            sys.stdout.flush()
            
            # Call pipeline with explicit error handling
            print("  Entering pipeline call (this may take a moment to start)...")
            print("  NOTE: First step may take 10-30 seconds for encoding/preprocessing")
            sys.stdout.flush()
            
            # Try to enable attention slicing for memory efficiency
            try:
                if hasattr(pipe, 'enable_attention_slicing'):
                    pipe.enable_attention_slicing(slice_size="max")
                    print("  ✓ Attention slicing enabled")
            except:
                pass
            
            # Try to enable CPU offload if on CPU
            if device == "cpu":
                try:
                    if hasattr(pipe, 'enable_sequential_cpu_offload'):
                        pipe.enable_sequential_cpu_offload()
                        print("  ✓ Sequential CPU offload enabled")
                except:
                    pass
            
            sys.stdout.flush()
            
            # Add a print right before the actual call
            print("  Starting pipeline inference NOW...")
            sys.stdout.flush()
            
            # Pass IP-Adapter scale explicitly to ensure it's used
            images = pipe(
                prompt=prompt,
                negative_prompt=negative_prompt,
                image_embeds=face_emb,
                image=control_images,
                control_mask=control_mask,
                controlnet_conditioning_scale=control_scales,
                num_inference_steps=num_steps,
                guidance_scale=guidance_scale,
                height=height,
                width=width,
                generator=generator,
                callback=progress_callback,
                callback_steps=1,  # Update every step
                ip_adapter_scale=adapter_strength_ratio,  # Explicitly pass IP-Adapter scale
            ).images
            
            print(f"  ✓ Pipeline completed, generated {len(images)} image(s)")
            sys.stdout.flush()
        except Exception as pipe_error:
            print(f"  ✗ Pipeline error: {pipe_error}")
            import traceback
            traceback.print_exc()
            sys.stdout.flush()
            raise

        final_image = images[0]
        print("Step 8/8: Saving image...")
        save_as_png(final_image)
        print("✓ Image generation complete!")
        print("=" * 60)
        
        # Clean up GPU memory before returning (important for ZeroGPU)
        if device == "cuda":
            try:
                torch.cuda.empty_cache()
                print("  GPU memory cleared")
            except Exception as e:
                print(f"  Warning: Could not clear GPU cache: {e}")
        
        # Force garbage collection
        gc.collect()
        
        # Ensure stdout is flushed before returning (helps with ZeroGPU)
        sys.stdout.flush()
        
        return final_image

    except gr.Error:
        raise
    except Exception as e:
        print(f"\n✗ Error during generation: {e}")
        import traceback
        traceback.print_exc()
        raise gr.Error(f"An error occurred during generation: {str(e)}")


# ============ CSS STYLING ============
css = """
/* Main container styling */
.main-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* Hero section */
.hero-title {
    font-size: 2.5em;
    font-weight: 700;
    margin-bottom: 10px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.hero-subtitle {
    font-size: 1.1em;
    color: #666;
    margin-bottom: 30px;
}

/* Control cards */
.control-card {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.control-header {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
}

.control-icon {
    font-size: 1.5em;
    margin-right: 10px;
}

.control-title {
    font-size: 1.2em;
    font-weight: 600;
    margin: 0;
}

/* Upload area */
.upload-area {
    border: 2px dashed #667eea;
    border-radius: 8px;
    padding: 20px;
    text-align: center;
}

/* Tips card */
.tips-card {
    background: #fff3cd;
    border-left: 4px solid #ffc107;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 20px;
}

.tips-header {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.tips-icon {
    font-size: 1.3em;
    margin-right: 8px;
}

/* Result card */
.result-card {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.result-header {
    margin-bottom: 20px;
}

.result-title {
    font-size: 1.5em;
    font-weight: 600;
    margin-bottom: 5px;
}

.result-subtitle {
    color: #666;
    font-size: 0.95em;
}

/* Image container */
.image-container {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

/* Success banner */
.success-banner {
    background: #d4edda;
    border: 1px solid #c3e6cb;
    border-radius: 8px;
    padding: 15px;
    margin-top: 15px;
}

/* Primary button */
.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    font-weight: 600;
    padding: 12px 30px;
}

.btn-primary:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
"""


# ============ UI / GRADIO ============
def show_success():
    """Show success message after generation."""
    return gr.update(
        value="""
        <div class="success-banner">
            <h4 style="margin: 0 0 8px 0;">✅ Success! Your Professional Headshot is Ready</h4>
            <p style="margin: 0; opacity: 0.9;">Download your high-quality PNG file for LinkedIn, professional profiles, or portfolios.</p>
        </div>
        """,
    )


with gr.Blocks() as demo:
    # Add custom CSS via HTML component (compatible with Gradio 4.44.0+)
    gr.HTML(f"<style>{css}</style>")
    
    with gr.Column(elem_classes="main-container"):
        with gr.Column(elem_classes="hero-section"):
            gr.HTML(
                """
                <div style="position: relative; z-index: 2;">
                    <h1 class="hero-title">🎯 Pro AI Headshot Generator</h1>
                    <p class="hero-subtitle">Transform any selfie into professional headshots in seconds. Perfect for LinkedIn, corporate profiles, and professional portfolios.</p>
                </div>
                """
            )

        with gr.Row(equal_height=True):
            with gr.Column(scale=1, min_width=400):
                with gr.Column(elem_classes="control-card"):
                    gr.HTML(
                        """
                        <div class="control-header">
                            <div class="control-icon">📸</div>
                            <h3 class="control-title">Upload Your Photo</h3>
                        </div>
                        """
                    )
                    gr.HTML(
                        """
                        <p style="color: var(--text-secondary); margin-bottom: 20px; font-size: 0.95em;">
                            For best results, use a clear, well-lit photo where your face is clearly visible.
                        </p>
                        """
                    )
                    face_file = gr.Image(
                        label="",
                        type="filepath",
                        height=200,
                        show_label=False,
                        elem_classes="upload-area",
                    )

                with gr.Column(elem_classes="control-card"):
                    gr.HTML(
                        """
                        <div class="control-header">
                            <div class="control-icon">✍️</div>
                            <h3 class="control-title">Describe Your Look</h3>
                        </div>
                        """
                    )
                    prompt = gr.Textbox(
                        label="",
                        placeholder="Describe the style and setting (avoid describing physical features)...",
                        value="professional headshot, business portrait, soft natural lighting, high quality",
                        show_label=False,
                        lines=3,
                    )
                    gr.HTML(
                        """
                        <div style="font-size: 0.85em; color: var(--text-secondary); margin-top: 8px;">
                            💡 Tips: Describe style/setting only. Don't mention hair color, clothing, or physical features - your face will be preserved automatically.
                            <br>Examples: "professional business headshot", "corporate portrait", "studio lighting"
                        </div>
                        """
                    )

                with gr.Column(elem_classes="control-card"):
                    gr.HTML(
                        """
                        <div class="control-header">
                            <div class="control-icon">🎨</div>
                            <h3 class="control-title">Style Options</h3>
                        </div>
                        """
                    )
                    style = gr.Dropdown(
                        label="Style Theme",
                        choices=["No Style"] + STYLE_NAMES,
                        value="No Style",
                        info="'No Style' recommended for natural professional results",
                    )

                with gr.Column(elem_classes="control-card"):
                    gr.HTML(
                        """
                        <div class="control-header">
                            <div class="control-icon">⚙️</div>
                            <h3 class="control-title">Quality Settings</h3>
                        </div>
                        """
                    )

                    identitynet_strength_ratio = gr.Slider(
                        label="Face Similarity",
                        minimum=0.5,
                        maximum=1.5,
                        step=0.05,
                        value=1.2,
                        info="How closely the headshot resembles your photo (higher = more similar, recommended: 1.0-1.2)",
                    )

                    adapter_strength_ratio = gr.Slider(
                        label="Face Detail Strength",
                        minimum=0.3,
                        maximum=1.5,
                        step=0.05,
                        value=1.0,
                        info="Strength of face features preservation (higher = better identity match, recommended: 0.8-1.0)",
                    )

                    enable_LCM = gr.Checkbox(
                        label="Enable Fast Generation Mode",
                        value=False,
                        info="Faster results with slightly lower quality",
                    )

                with gr.Column(elem_classes="tips-card"):
                    gr.HTML(
                        """
                        <div class="tips-header">
                            <div class="tips-icon">💡</div>
                            <h4 style="margin: 0; color: #92400e;">Pro Tips for Best Results</h4>
                        </div>
                        <ul style="margin: 0; color: #92400e; font-size: 0.9em;">
                            <li>Use clear, well-lit face photos</li>
                            <li>Face should be visible and not too small</li>
                            <li>Avoid blurry or dark images</li>
                            <li>Single person in photo works best</li>
                        </ul>
                        """
                    )

                submit = gr.Button(
                    "✨ Generate Professional Headshot",
                    variant="primary",
                    size="lg",
                    elem_classes="btn-primary",
                    scale=1,
                )

            with gr.Column(scale=1, min_width=500):
                with gr.Column(elem_classes="result-card"):
                    gr.HTML(
                        """
                        <div class="result-header">
                            <h2 class="result-title">Your Professional Headshot</h2>
                            <p class="result-subtitle">Your AI-generated headshot will appear here. Download as high-quality PNG for professional use.</p>
                        </div>
                        """
                    )

                    gallery = gr.Image(
                        label="Output",
                        height=400,
                        show_label=False,
                        type="pil",
                        elem_classes="image-container",
                    )

                    success_msg = gr.HTML(
                        """
                        <div class="success-banner" style="display: none;">
                            <h4 style="margin: 0 0 8px 0;">✅ Success! Your Professional Headshot is Ready</h4>
                            <p style="margin: 0; opacity: 0.9;">Download your high-quality PNG file for LinkedIn, professional profiles, or portfolios.</p>
                        </div>
                        """
                    )

                    progress_info = gr.HTML(
                        """
                        <div class="progress-container">
                            <div style="font-size: 0.9em; color: var(--text-secondary);">
                                ⏱️ Generation takes 20-30 seconds
                            </div>
                        </div>
                        """
                    )

    # Hidden advanced settings
    negative_prompt = gr.Textbox(
        value="(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green, wrong gender, gender swap, different person, different face, face swap, identity change, different identity",
        visible=False,
    )
    num_steps = gr.Slider(
        minimum=5,
        maximum=100,
        step=1,
        value=DEFAULT_NUM_STEPS,
        label="Number of steps",
        visible=False,
    )
    guidance_scale = gr.Slider(
        minimum=0.1,
        maximum=20.0,
        step=0.1,
        value=DEFAULT_GUIDANCE_SCALE,
        label="Guidance scale",
        visible=False,
    )
    seed = gr.Slider(
        minimum=-1,
        maximum=MAX_SEED,
        step=1,
        value=-1,
        label="Seed (-1 for random)",
        visible=False,
    )
    scheduler = gr.Dropdown(
        value="EulerDiscreteScheduler",
        choices=[
            "EulerDiscreteScheduler",
            "EulerAncestralDiscreteScheduler",
            "DPMSolverMultistepScheduler",
        ],
        visible=False,
    )
    randomize_seed = gr.Checkbox(value=True, visible=False)
    enhance_face_region = gr.Checkbox(value=True, visible=False)
    controlnet_selection = gr.CheckboxGroup(
        choices=["canny", "depth"], value=[], label="Controlnet", visible=False  # Changed default to empty - depth can be slow
    )
    canny_strength = gr.Slider(
        minimum=0,
        maximum=1.5,
        step=0.01,
        value=0.4,
        label="Canny strength",
        visible=False,
    )
    depth_strength = gr.Slider(
        minimum=0,
        maximum=1.5,
        step=0.01,
        value=0.4,
        label="Depth strength",
        visible=False,
    )

    submit.click(
        fn=generate_image,
        inputs=[
            face_file,
            prompt,
            negative_prompt,
            style,
            num_steps,
            identitynet_strength_ratio,
            adapter_strength_ratio,
            canny_strength,
            depth_strength,
            controlnet_selection,
            guidance_scale,
            seed,
            scheduler,
            enable_LCM,
            enhance_face_region,
        ],
        outputs=[gallery],
    ).then(fn=show_success, outputs=success_msg)

    enable_LCM.input(
        fn=toggle_lcm_ui,
        inputs=[enable_LCM],
        outputs=[num_steps, guidance_scale],
        queue=False,
    )

    # Cleanup on startup
    cleanup_old_files()


if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Pro AI Headshot Generator")
    parser.add_argument("--share", action="store_true", help="Create a public Gradio link")
    parser.add_argument("--server-name", type=str, default="127.0.0.1", help="Server name (default: 127.0.0.1)")
    parser.add_argument("--server-port", type=int, default=7860, help="Server port (default: 7860)")
    parser.add_argument("--no-queue", action="store_true", help="Disable queueing")
    args = parser.parse_args()
    
    demo.queue(api_open=False, max_size=3 if not args.no_queue else None)
    
    # Launch the application
    try:
        print("\n" + "=" * 60)
        print("🚀 Launching Gradio interface...")
        print("=" * 60)
        
        # For Hugging Face Spaces, use 0.0.0.0 to allow external access
        server_name = "0.0.0.0" if os.getenv("SPACE_ID") else args.server_name
        
        print(f"  Server: {server_name}:{args.server_port}")
        print(f"  Queue enabled: {not args.no_queue}")
        print("  Interface should be available shortly...")
        print("=" * 60)
        
        demo.launch(
            share=args.share,
            server_name=server_name,
            server_port=args.server_port,
            show_error=True,
            # Note: theme parameter removed for Gradio 4.44.0+ compatibility
            # CSS is injected via HTML component instead
        )
        
        print("\n✅ Gradio interface launched successfully!")
    except Exception as e:
        print(f"\n✗ Error launching Gradio interface: {e}")
        import traceback
        traceback.print_exc()
        raise