File size: 48,190 Bytes
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2428f13
 
 
 
 
 
 
 
 
 
 
 
c311609
 
 
 
 
 
 
 
71036e1
 
 
 
 
 
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2428f13
 
 
2eff0d8
 
2428f13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e62f2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2428f13
 
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584e173
2eff0d8
584e173
2eff0d8
 
 
584e173
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584e173
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
584e173
 
 
 
 
 
 
 
 
2eff0d8
 
 
 
 
 
 
 
 
 
 
584e173
2eff0d8
 
 
 
 
584e173
 
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33ef2d
 
 
 
 
2eff0d8
 
b33ef2d
 
584e173
 
b33ef2d
 
2eff0d8
b33ef2d
2eff0d8
 
b33ef2d
 
584e173
b33ef2d
2eff0d8
b33ef2d
2eff0d8
584e173
b33ef2d
 
 
 
2eff0d8
b33ef2d
4ec9e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33ef2d
584e173
2eff0d8
b33ef2d
 
 
 
 
 
 
 
 
584e173
b33ef2d
 
584e173
 
b33ef2d
 
 
 
2eff0d8
b33ef2d
 
584e173
 
 
 
 
 
 
b33ef2d
 
584e173
 
b33ef2d
2eff0d8
4ec9e49
 
 
 
 
 
 
 
 
 
 
 
 
 
2eff0d8
584e173
2eff0d8
4ec9e49
b33ef2d
 
2eff0d8
584e173
2eff0d8
4ec9e49
 
 
 
b33ef2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec9e49
 
 
 
 
b33ef2d
 
 
4ec9e49
 
b33ef2d
 
 
 
 
 
 
 
4ec9e49
 
 
b33ef2d
 
 
4ec9e49
 
 
 
 
b33ef2d
 
 
 
 
 
 
 
 
4ec9e49
b33ef2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec9e49
 
 
b33ef2d
 
 
 
 
 
 
 
 
 
 
2eff0d8
 
433318a
 
 
 
 
 
 
 
 
 
4ec9e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433318a
2eff0d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6110eef
 
0205c92
2eff0d8
 
1bb836a
 
 
6110eef
8828356
 
 
 
 
 
 
 
 
 
 
 
6110eef
 
 
0205c92
6110eef
 
2eff0d8
6110eef
 
 
 
0205c92
8828356
6110eef
8828356
 
 
 
 
 
 
 
 
 
 
1bb836a
 
 
8828356
 
2eff0d8
6110eef
8828356
 
2eff0d8
6110eef
8828356
 
6110eef
1bb836a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6110eef
1fd13a0
 
 
 
0205c92
 
 
 
 
 
 
 
 
 
6110eef
8828356
6110eef
8828356
 
 
2eff0d8
8828356
 
6110eef
 
2eff0d8
584e173
 
 
 
 
71036e1
 
 
 
 
 
 
 
 
 
433318a
71036e1
433318a
71036e1
584e173
 
 
 
 
 
 
 
2eff0d8
7c76ec0
b33ef2d
4ec9e49
b33ef2d
2eff0d8
 
584e173
6110eef
2eff0d8
 
b33ef2d
 
 
 
 
2eff0d8
b33ef2d
584e173
 
2eff0d8
584e173
4ec9e49
86348fd
 
7c76ec0
 
 
 
b33ef2d
 
584e173
b33ef2d
7c76ec0
 
 
 
 
 
 
 
 
 
 
 
86348fd
 
0ac1314
7c76ec0
 
 
b33ef2d
 
7c76ec0
b33ef2d
7c76ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec9e49
 
e5d7eac
4ec9e49
 
7c76ec0
4ec9e49
e5d7eac
4ec9e49
 
7c76ec0
4ec9e49
 
 
 
7c76ec0
 
 
 
 
 
 
 
2eff0d8
b80a36c
 
 
 
 
 
 
 
584e173
 
 
 
 
 
 
 
 
 
 
6110eef
584e173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c76ec0
584e173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0205c92
584e173
 
 
 
 
 
0205c92
584e173
 
8828356
 
 
 
 
584e173
8828356
2eff0d8
7c76ec0
584e173
 
 
 
 
 
 
 
 
7c76ec0
584e173
 
 
 
 
 
 
 
 
 
 
 
5aa4162
 
 
 
 
 
 
b33ef2d
 
 
 
 
 
 
0ac1314
86348fd
4ec9e49
86348fd
 
4ec9e49
86348fd
0ac1314
 
 
 
 
 
 
 
 
86348fd
0ac1314
7c76ec0
b33ef2d
7c76ec0
5aa4162
 
7c76ec0
 
b33ef2d
 
 
 
 
 
 
 
 
 
5aa4162
 
b33ef2d
 
584e173
 
 
 
5aa4162
 
584e173
5aa4162
2eff0d8
 
 
 
5aa4162
 
2eff0d8
5aa4162
2eff0d8
 
 
 
5aa4162
 
2eff0d8
 
168fce2
 
 
7c76ec0
 
168fce2
 
 
 
 
6110eef
 
584e173
168fce2
5aa4162
 
168fce2
2eff0d8
c311609
584e173
2eff0d8
 
 
 
584e173
168fce2
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
import tempfile
import time
from collections.abc import Sequence
from typing import Any, cast
import os
from huggingface_hub import login, hf_hub_download

import gradio as gr
import numpy as np
import pillow_heif
import spaces
import torch
from gradio_image_annotation import image_annotator
from gradio_imageslider import ImageSlider
from PIL import Image
from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
from refiners.fluxion.utils import no_grad
from refiners.solutions import BoxSegmenter
from transformers import GroundingDinoForObjectDetection, GroundingDinoProcessor
from diffusers import FluxPipeline
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import gc

from PIL import Image, ImageDraw, ImageFont
from PIL import Image
from gradio_client import Client, handle_file
import uuid

import gradio as gr
import spaces
import torch
from diffusers import AutoencoderKL, TCDScheduler
from diffusers.models.model_loading_utils import load_state_dict
from gradio_imageslider import ImageSlider
from huggingface_hub import hf_hub_download
from transformers import pipeline

from controlnet_union import ControlNetModel_Union
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline

def initialize_ui():
    """UI ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜"""
    return {
        "position": "bottom-center",
        "processing_status": "idle",
        "status_message": gr.update(visible=False)
    }
    
def debug_event(event_name, *args):
    """์ด๋ฒคํŠธ ๋””๋ฒ„๊น… ์œ ํ‹ธ๋ฆฌํ‹ฐ"""
    print(f"Event '{event_name}' triggered at {time.strftime('%H:%M:%S')}")
    print(f"Arguments: {args}")
    return args
    
def clear_memory():
    """๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ํ•จ์ˆ˜"""
    gc.collect()
    try:
        if torch.cuda.is_available():
            with torch.cuda.device(0):  # ๋ช…์‹œ์ ์œผ๋กœ device 0 ์‚ฌ์šฉ
                torch.cuda.empty_cache()
    except:
        pass

# GPU ์„ค์ •
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")  # ๋ช…์‹œ์ ์œผ๋กœ cuda:0 ์ง€์ •

# GPU ์„ค์ •์„ try-except๋กœ ๊ฐ์‹ธ๊ธฐ
if torch.cuda.is_available():
    try:
        with torch.cuda.device(0):
            torch.cuda.empty_cache()
            torch.backends.cudnn.benchmark = True
            torch.backends.cuda.matmul.allow_tf32 = True
    except:
        print("Warning: Could not configure CUDA settings")

# ๋ฒˆ์—ญ ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
model_name = "Helsinki-NLP/opus-mt-ko-en"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to('cpu')
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)

def translate_to_english(text: str) -> str:
    """ํ•œ๊ธ€ ํ…์ŠคํŠธ๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญ"""
    try:
        if any(ord('๊ฐ€') <= ord(char) <= ord('ํžฃ') for char in text):
            translated = translator(text, max_length=128)[0]['translation_text']
            print(f"Translated '{text}' to '{translated}'")
            return translated
        return text
    except Exception as e:
        print(f"Translation error: {str(e)}")
        return text

BoundingBox = tuple[int, int, int, int]

pillow_heif.register_heif_opener()
pillow_heif.register_avif_opener()

# HF ํ† ํฐ ์„ค์ •
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN is None:
    raise ValueError("Please set the HF_TOKEN environment variable")

try:
    login(token=HF_TOKEN)
except Exception as e:
    raise ValueError(f"Failed to login to Hugging Face: {str(e)}")

# ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
segmenter = BoxSegmenter(device="cpu")
segmenter.device = device
segmenter.model = segmenter.model.to(device=segmenter.device)

gd_model_path = "IDEA-Research/grounding-dino-base"
gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
gd_model = GroundingDinoForObjectDetection.from_pretrained(gd_model_path, torch_dtype=torch.float32)
gd_model = gd_model.to(device=device)
assert isinstance(gd_model, GroundingDinoForObjectDetection)

# FLUX ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.float16,
    use_auth_token=HF_TOKEN
)
pipe.enable_attention_slicing(slice_size="auto")

# LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ
pipe.load_lora_weights(
    hf_hub_download(
        "ByteDance/Hyper-SD",
        "Hyper-FLUX.1-dev-8steps-lora.safetensors",
        use_auth_token=HF_TOKEN
    )
)
pipe.fuse_lora(lora_scale=0.125)

# GPU ์„ค์ •์„ try-except๋กœ ๊ฐ์‹ธ๊ธฐ
try:
    if torch.cuda.is_available():
        pipe = pipe.to("cuda:0")  # ๋ช…์‹œ์ ์œผ๋กœ cuda:0 ์ง€์ •
except Exception as e:
    print(f"Warning: Could not move pipeline to CUDA: {str(e)}")


#------------------------------- ์ด๋ฏธ์ง€ ์ธํŽ˜์ธํŒ… ----------------------

client = Client("NabeelShar/BiRefNet_for_text_writing")

MODELS = {
    "RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
}

config_file = hf_hub_download(
    "xinsir/controlnet-union-sdxl-1.0",
    filename="config_promax.json",
)

config = ControlNetModel_Union.load_config(config_file)
controlnet_model = ControlNetModel_Union.from_config(config)
model_file = hf_hub_download(
    "xinsir/controlnet-union-sdxl-1.0",
    filename="diffusion_pytorch_model_promax.safetensors",
)
state_dict = load_state_dict(model_file)
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
    controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
)
model.to(device="cuda", dtype=torch.float16)

vae = AutoencoderKL.from_pretrained(
    "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
).to("cuda")

pipe = StableDiffusionXLFillPipeline.from_pretrained(
    "SG161222/RealVisXL_V5.0_Lightning",
    torch_dtype=torch.float16,
    vae=vae,
    controlnet=model,
    variant="fp16",
).to("cuda")

pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)

def translate_if_korean(text):
    # ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๊ฐ€ ํ•œ๊ธ€์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
    if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in text):
        # ํ•œ๊ธ€์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฒˆ์—ญ
        translated = translator(text)[0]['translation_text']
        print(f"Translated prompt: {translated}")  # ๋””๋ฒ„๊น…์„ ์œ„ํ•œ ์ถœ๋ ฅ
        return translated
    return text

@spaces.GPU
def fill_image(prompt, image, model_selection):
    # ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ
    translated_prompt = translate_if_korean(prompt)
    
    (
        prompt_embeds,
        negative_prompt_embeds,
        pooled_prompt_embeds,
        negative_pooled_prompt_embeds,
    ) = pipe.encode_prompt(translated_prompt, "cuda", True)

    source = image["background"]
    mask = image["layers"][0]

    alpha_channel = mask.split()[3]
    binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
    cnet_image = source.copy()
    cnet_image.paste(0, (0, 0), binary_mask)

    for image in pipe(
        prompt_embeds=prompt_embeds,
        negative_prompt_embeds=negative_prompt_embeds,
        pooled_prompt_embeds=pooled_prompt_embeds,
        negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
        image=cnet_image,
    ):
        yield image, cnet_image

    image = image.convert("RGBA")
    cnet_image.paste(image, (0, 0), binary_mask)

    yield source, cnet_image

def clear_result():
    return gr.update(value=None)

def process_inpainting(image, mask_input, prompt):
    """์ด๋ฏธ์ง€ ์ธํŽ˜์ธํŒ… ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
    try:
        if image is None or mask_input is None or not prompt:
            raise gr.Error("Please provide image, mask, and prompt")
            
        # ํ”„๋กฌํ”„ํŠธ ๋ฒˆ์—ญ (ํ•œ๊ธ€์ธ ๊ฒฝ์šฐ)
        translated_prompt = translate_if_korean(prompt)
        
        # ๋งˆ์Šคํฌ ์ฒ˜๋ฆฌ
        source = image
        if isinstance(mask_input, dict):
            mask = mask_input["layers"][0]
            alpha_channel = mask.split()[3]
            binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
        else:
            raise gr.Error("Invalid mask input")

        # ์ธํŽ˜์ธํŒ…์„ ์œ„ํ•œ ์ด๋ฏธ์ง€ ์ค€๋น„
        cnet_image = source.copy()
        cnet_image.paste(0, (0, 0), binary_mask)

        # ํ”„๋กฌํ”„ํŠธ ์ž„๋ฒ ๋”ฉ
        (
            prompt_embeds,
            negative_prompt_embeds,
            pooled_prompt_embeds,
            negative_pooled_prompt_embeds,
        ) = pipe.encode_prompt(translated_prompt, "cuda", True)

        # ์ธํŽ˜์ธํŒ… ์‹คํ–‰
        result = None
        for image in pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
            image=cnet_image,
        ):
            result = image

        if result is None:
            raise gr.Error("Inpainting failed")

        # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ
        result = result.convert("RGBA")
        cnet_image.paste(result, (0, 0), binary_mask)
        
        return cnet_image

    except Exception as e:
        print(f"Inpainting error: {str(e)}")
        raise gr.Error(f"Inpainting failed: {str(e)}")
    finally:
        clear_memory()
        
#--------------- ์ด๋ฏธ์ง€ ์ธํŽ˜์ธํŒ… ๋ ----------------    

class timer:
    def __init__(self, method_name="timed process"):
        self.method = method_name
    def __enter__(self):
        self.start = time.time()
        print(f"{self.method} starts")
    def __exit__(self, exc_type, exc_val, exc_tb):
        end = time.time()
        print(f"{self.method} took {str(round(end - self.start, 2))}s")

def bbox_union(bboxes: Sequence[list[int]]) -> BoundingBox | None:
    if not bboxes:
        return None
    for bbox in bboxes:
        assert len(bbox) == 4
        assert all(isinstance(x, int) for x in bbox)
    return (
        min(bbox[0] for bbox in bboxes),
        min(bbox[1] for bbox in bboxes),
        max(bbox[2] for bbox in bboxes),
        max(bbox[3] for bbox in bboxes),
    )

def corners_to_pixels_format(bboxes: torch.Tensor, width: int, height: int) -> torch.Tensor:
    x1, y1, x2, y2 = bboxes.round().to(torch.int32).unbind(-1)
    return torch.stack((x1.clamp_(0, width), y1.clamp_(0, height), x2.clamp_(0, width), y2.clamp_(0, height)), dim=-1)

def gd_detect(img: Image.Image, prompt: str) -> BoundingBox | None:
    inputs = gd_processor(images=img, text=f"{prompt}.", return_tensors="pt").to(device=device)
    with no_grad():
        outputs = gd_model(**inputs)
    width, height = img.size
    results: dict[str, Any] = gd_processor.post_process_grounded_object_detection(
        outputs,
        inputs["input_ids"],
        target_sizes=[(height, width)],
    )[0]
    assert "boxes" in results and isinstance(results["boxes"], torch.Tensor)
    bboxes = corners_to_pixels_format(results["boxes"].cpu(), width, height)
    return bbox_union(bboxes.numpy().tolist())

def apply_mask(img: Image.Image, mask_img: Image.Image, defringe: bool = True) -> Image.Image:
    assert img.size == mask_img.size
    img = img.convert("RGB")
    mask_img = mask_img.convert("L")
    if defringe:
        rgb, alpha = np.asarray(img) / 255.0, np.asarray(mask_img) / 255.0
        foreground = cast(np.ndarray[Any, np.dtype[np.uint8]], estimate_foreground_ml(rgb, alpha))
        img = Image.fromarray((foreground * 255).astype("uint8"))
    result = Image.new("RGBA", img.size)
    result.paste(img, (0, 0), mask_img)
    return result


def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
    """์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •ํ•˜๋Š” ํ•จ์ˆ˜"""
    new_width = ((width + 7) // 8) * 8
    new_height = ((height + 7) // 8) * 8
    return new_width, new_height

def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
    """์„ ํƒ๋œ ๋น„์œจ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ„์‚ฐ"""
    if aspect_ratio == "1:1":
        return base_size, base_size
    elif aspect_ratio == "16:9":
        return base_size * 16 // 9, base_size
    elif aspect_ratio == "9:16":
        return base_size, base_size * 16 // 9
    elif aspect_ratio == "4:3":
        return base_size * 4 // 3, base_size
    return base_size, base_size

@spaces.GPU(duration=20)  # 40์ดˆ์—์„œ 20์ดˆ๋กœ ๊ฐ์†Œ
def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
    try:
        width, height = calculate_dimensions(aspect_ratio)
        width, height = adjust_size_to_multiple_of_8(width, height)
        
        max_size = 768
        if width > max_size or height > max_size:
            ratio = max_size / max(width, height)
            width = int(width * ratio)
            height = int(height * ratio)
            width, height = adjust_size_to_multiple_of_8(width, height)

        with timer("Background generation"):
            try:
                with torch.inference_mode():
                    image = pipe(
                        prompt=prompt,
                        width=width,
                        height=height,
                        num_inference_steps=8,
                        guidance_scale=4.0
                    ).images[0]
            except Exception as e:
                print(f"Pipeline error: {str(e)}")
                return Image.new('RGB', (width, height), 'white')
                
        return image
    except Exception as e:
        print(f"Background generation error: {str(e)}")
        return Image.new('RGB', (512, 512), 'white')

def create_position_grid():
    return """
    <div class="position-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">
        <button class="position-btn" data-pos="top-left">โ†–</button>
        <button class="position-btn" data-pos="top-center">โ†‘</button>
        <button class="position-btn" data-pos="top-right">โ†—</button>
        <button class="position-btn" data-pos="middle-left">โ†</button>
        <button class="position-btn" data-pos="middle-center">โ€ข</button>
        <button class="position-btn" data-pos="middle-right">โ†’</button>
        <button class="position-btn" data-pos="bottom-left">โ†™</button>
        <button class="position-btn" data-pos="bottom-center" data-default="true">โ†“</button>
        <button class="position-btn" data-pos="bottom-right">โ†˜</button>
    </div>
    """

def calculate_object_position(position: str, bg_size: tuple[int, int], obj_size: tuple[int, int]) -> tuple[int, int]:
    """์˜ค๋ธŒ์ ํŠธ์˜ ์œ„์น˜ ๊ณ„์‚ฐ"""
    bg_width, bg_height = bg_size
    obj_width, obj_height = obj_size
    
    positions = {
        "top-left": (0, 0),
        "top-center": ((bg_width - obj_width) // 2, 0),
        "top-right": (bg_width - obj_width, 0),
        "middle-left": (0, (bg_height - obj_height) // 2),
        "middle-center": ((bg_width - obj_width) // 2, (bg_height - obj_height) // 2),
        "middle-right": (bg_width - obj_width, (bg_height - obj_height) // 2),
        "bottom-left": (0, bg_height - obj_height),
        "bottom-center": ((bg_width - obj_width) // 2, bg_height - obj_height),
        "bottom-right": (bg_width - obj_width, bg_height - obj_height)
    }
    
    return positions.get(position, positions["bottom-center"])

def resize_object(image: Image.Image, scale_percent: float) -> Image.Image:
    """์˜ค๋ธŒ์ ํŠธ ํฌ๊ธฐ ์กฐ์ •"""
    width = int(image.width * scale_percent / 100)
    height = int(image.height * scale_percent / 100)
    return image.resize((width, height), Image.Resampling.LANCZOS)

def combine_with_background(foreground: Image.Image, background: Image.Image, 
                          position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
    """์ „๊ฒฝ๊ณผ ๋ฐฐ๊ฒฝ ํ•ฉ์„ฑ ํ•จ์ˆ˜"""
    print(f"Combining with position: {position}, scale: {scale_percent}")
    
    result = background.convert('RGBA')
    scaled_foreground = resize_object(foreground, scale_percent)
    
    x, y = calculate_object_position(position, result.size, scaled_foreground.size)
    print(f"Calculated position coordinates: ({x}, {y})")
    
    result.paste(scaled_foreground, (x, y), scaled_foreground)
    return result

@spaces.GPU(duration=30)  # 120์ดˆ์—์„œ 30์ดˆ๋กœ ๊ฐ์†Œ
def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Image.Image, BoundingBox | None, list[str]]:
    time_log: list[str] = []
    try:
        if isinstance(prompt, str):
            t0 = time.time()
            bbox = gd_detect(img, prompt)
            time_log.append(f"detect: {time.time() - t0}")
            if not bbox:
                print(time_log[0])
                raise gr.Error("No object detected")
        else:
            bbox = prompt
        t0 = time.time()
        mask = segmenter(img, bbox)
        time_log.append(f"segment: {time.time() - t0}")
        return mask, bbox, time_log
    except Exception as e:
        print(f"GPU process error: {str(e)}")
        raise

def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str | None = None, aspect_ratio: str = "1:1") -> tuple[tuple[Image.Image, Image.Image, Image.Image], gr.DownloadButton]:
    try:
        # ์ž…๋ ฅ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์ œํ•œ
        max_size = 1024
        if img.width > max_size or img.height > max_size:
            ratio = max_size / max(img.width, img.height)
            new_size = (int(img.width * ratio), int(img.height * ratio))
            img = img.resize(new_size, Image.LANCZOS)

        # CUDA ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ์ˆ˜์ •
        try:
            if torch.cuda.is_available():
                current_device = torch.cuda.current_device()
                with torch.cuda.device(current_device):
                    torch.cuda.empty_cache()
        except Exception as e:
            print(f"CUDA memory management failed: {e}")

        with torch.cuda.amp.autocast(enabled=torch.cuda.is_available()):
            mask, bbox, time_log = _gpu_process(img, prompt)
            masked_alpha = apply_mask(img, mask, defringe=True)

        if bg_prompt:
            background = generate_background(bg_prompt, aspect_ratio)
            combined = background
        else:
            combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)

        clear_memory()

        with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp:
            combined.save(temp.name)
            return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
    except Exception as e:
        clear_memory()
        print(f"Processing error: {str(e)}")
        raise gr.Error(f"Processing failed: {str(e)}")

def on_change_bbox(prompts: dict[str, Any] | None):
    return gr.update(interactive=prompts is not None)


def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
    return gr.update(interactive=bool(img and prompt))


def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None, 
                  aspect_ratio: str = "1:1", position: str = "bottom-center", 
                  scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
    try:
        if img is None or prompt.strip() == "":
            raise gr.Error("Please provide both image and prompt")
        
        print(f"Processing with position: {position}, scale: {scale_percent}")  # ๋””๋ฒ„๊น…์šฉ
        
        try:
            prompt = translate_to_english(prompt)
            if bg_prompt:
                bg_prompt = translate_to_english(bg_prompt)
        except Exception as e:
            print(f"Translation error (continuing with original text): {str(e)}")
        
        results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
        
        if bg_prompt:
            try:
                print(f"Using position: {position}")  # ๋””๋ฒ„๊น…์šฉ
                # ์œ„์น˜ ๊ฐ’ ๊ฒ€์ฆ
                valid_positions = ["top-left", "top-center", "top-right",
                                 "middle-left", "middle-center", "middle-right",
                                 "bottom-left", "bottom-center", "bottom-right"]
                if position not in valid_positions:
                    position = "bottom-center"
                    print(f"Invalid position, using default: {position}")
                
                combined = combine_with_background(
                    foreground=results[2],
                    background=results[1],
                    position=position,
                    scale_percent=scale_percent
                )
                return combined, results[2]
            except Exception as e:
                print(f"Combination error: {str(e)}")
                return results[1], results[2]
        
        return results[1], results[2]  # ๊ธฐ๋ณธ ๋ฐ˜ํ™˜ ์ถ”๊ฐ€
    except Exception as e:
        print(f"Error in process_prompt: {str(e)}")
        raise gr.Error(str(e))
    finally:
        clear_memory()

    
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
    try:
        if img is None or box_input.strip() == "":
            raise gr.Error("Please provide both image and bounding box coordinates")
        
        try:
            coords = eval(box_input)
            if not isinstance(coords, list) or len(coords) != 4:
                raise ValueError("Invalid box format")
            bbox = tuple(int(x) for x in coords)
        except:
            raise gr.Error("Invalid box format. Please provide [xmin, ymin, xmax, ymax]")
        
        # Process the image
        results, _ = _process(img, bbox)
        
        # ํ•ฉ์„ฑ๋œ ์ด๋ฏธ์ง€์™€ ์ถ”์ถœ๋œ ์ด๋ฏธ์ง€๋งŒ ๋ฐ˜ํ™˜
        return results[1], results[2]
    except Exception as e:
        raise gr.Error(str(e))

# Event handler functions ์ˆ˜์ •
def update_process_button(img, prompt):
    return gr.update(
        interactive=bool(img and prompt),
        variant="primary" if bool(img and prompt) else "secondary"
    )

def update_box_button(img, box_input):
    try:
        if img and box_input:
            coords = eval(box_input)
            if isinstance(coords, list) and len(coords) == 4:
                return gr.update(interactive=True, variant="primary")
        return gr.update(interactive=False, variant="secondary")
    except:
        return gr.update(interactive=False, variant="secondary")


css = """
/* ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ */
footer {display: none !important}
body {background: #f5f7fa !important}

/* ๋ฉ”์ธ ํƒ€์ดํ‹€ */
.main-title {
    text-align: center;
    margin: 1.5em auto;
    padding: 2em;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    border-radius: 15px;
    box-shadow: 0 8px 16px rgba(0,0,0,0.1);
    max-width: 1200px;
}

.main-title h1 {
    color: #2196F3;
    font-size: 3em;
    margin-bottom: 0.5em;
    font-weight: 700;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}

.main-title p {
    color: #555;
    font-size: 1.4em;
    line-height: 1.6;
    max-width: 800px;
    margin: 0 auto;
}

/* ํƒญ ์Šคํƒ€์ผ๋ง */
.tabs-container {
    margin-top: 1em;
}

.tab-nav {
    pointer-events: auto !important;
    cursor: pointer !important;
    border-bottom: 2px solid #e0e0e0;
    margin-bottom: 1em;
}

.tab-nav button {
    pointer-events: auto !important;
    cursor: pointer !important;
    padding: 0.8em 1.2em;
    margin-right: 0.5em;
    border: none;
    background: none;
    color: #666;
    transition: all 0.3s ease;
}

.tab-nav button:hover {
    color: #2196F3;
    background: rgba(33, 150, 243, 0.1);
}

.tab-nav button.selected {
    color: #2196F3;
    border-bottom: 2px solid #2196F3;
    font-weight: bold;
}

/* ํŒจ๋„ ์Šคํƒ€์ผ๋ง */
.input-panel, .output-panel {
    background: white;
    padding: 2em;
    border-radius: 15px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    margin-bottom: 1.5em;
    transition: all 0.3s ease;
}

.input-panel:hover, .output-panel:hover {
    box-shadow: 0 6px 16px rgba(0,0,0,0.1);
}

/* ์ปจํŠธ๋กค ํŒจ๋„ */
.controls-panel {
    background: #f8f9fa;
    padding: 1.5em;
    border-radius: 12px;
    margin: 1.5em 0;
    border: 1px solid #e9ecef;
}

/* ์ด๋ฏธ์ง€ ๋””์Šคํ”Œ๋ ˆ์ด */
.image-display {
    min-height: 512px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fafafa;
    border-radius: 12px;
    margin: 1.5em 0;
    border: 2px dashed #e0e0e0;
}

/* ๋ฒ„ํŠผ ์Šคํƒ€์ผ๋ง */
.position-btn {
    padding: 12px !important;
    border: 2px solid #ddd !important;
    border-radius: 8px !important;
    background: white !important;
    cursor: pointer !important;
    transition: all 0.2s ease !important;
    width: 48px !important;
    height: 48px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 1.2em !important;
    margin: 4px !important;
    pointer-events: auto !important;
}

.position-btn:hover {
    background: #e3f2fd !important;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.position-btn.selected {
    background-color: #2196F3 !important;
    color: white !important;
    border-color: #1976D2 !important;
    box-shadow: 0 4px 12px rgba(33,150,243,0.3) !important;
}

/* ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ */
.position-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin: 1.5em 0;
    padding: 10px;
    background: #f5f5f5;
    border-radius: 12px;
}

/* ์ž…๋ ฅ ํ•„๋“œ ์Šคํƒ€์ผ๋ง */
input[type="text"], textarea {
    border: 2px solid #e0e0e0 !important;
    border-radius: 8px !important;
    padding: 12px !important;
    font-size: 1.1em !important;
    transition: all 0.3s ease !important;
}

input[type="text"]:focus, textarea:focus {
    border-color: #2196F3 !important;
    box-shadow: 0 0 0 3px rgba(33,150,243,0.2) !important;
}

/* ์Šฌ๋ผ์ด๋” ์Šคํƒ€์ผ๋ง */
.slider-container {
    margin: 1.5em 0;
}

.slider {
    height: 6px !important;
    background: #e0e0e0 !important;
    border-radius: 3px !important;
}

.slider-handle {
    width: 20px !important;
    height: 20px !important;
    background: #2196F3 !important;
    border: 2px solid white !important;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2) !important;
}

/* ์ƒํƒœ ๋ฉ”์‹œ์ง€ */
.status-message {
    padding: 10px;
    border-radius: 8px;
    margin: 10px 0;
    font-size: 0.9em;
    transition: all 0.3s ease;
    animation: slideIn 0.3s ease-out;
}

.status-success {
    background: #e8f5e9;
    color: #2e7d32;
    border: 1px solid #a5d6a7;
}

.status-error {
    background: #ffebee;
    color: #c62828;
    border: 1px solid #ef9a9a;
}

/* ๋ฐ˜์‘ํ˜• ๋””์ž์ธ */
@media (max-width: 768px) {
    .main-title h1 {
        font-size: 2em;
    }
    
    .main-title p {
        font-size: 1.1em;
    }
    
    .input-panel, .output-panel {
        padding: 1em;
    }
    
    .position-btn {
        width: 40px !important;
        height: 40px !important;
        font-size: 1em !important;
    }
}

/* ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ */
@keyframes fadeIn {
    from {opacity: 0; transform: translateY(10px);}
    to {opacity: 1; transform: translateY(0);}
}

.fade-in {
    animation: fadeIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        transform: translateY(-10px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ์š”์†Œ ๊ณตํ†ต ์Šคํƒ€์ผ */
.interactive-element {
    pointer-events: auto !important;
    cursor: pointer !important;
    user-select: none !important;
}

/* ๋ฒ„ํŠผ ๊ณตํ†ต ์Šคํƒ€์ผ */
button, .button {
    pointer-events: auto !important;
    cursor: pointer !important;
    user-select: none !important;
}

/* ํƒญ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
.tabs-container {
    pointer-events: auto !important;
}

.tab-item {
    pointer-events: auto !important;
}

/* ํ˜ธ๋ฒ„ ํšจ๊ณผ */
.interactive-element:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

/* ํด๋ฆญ ํšจ๊ณผ */
.interactive-element:active {
    transform: translateY(1px);
}
"""

js_code = """
<script>
document.addEventListener('DOMContentLoaded', function() {
    function enableInteraction(selector) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(el => {
            el.style.pointerEvents = 'auto';
            el.style.cursor = 'pointer';
        });
    }

    // ํƒญ ํ™œ์„ฑํ™”
    enableInteraction('.tab-nav button');
    enableInteraction('.position-btn');
    enableInteraction('.interactive-element');

    // ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ
    document.querySelectorAll('.position-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            document.querySelectorAll('.position-btn').forEach(b => b.classList.remove('selected'));
            this.classList.add('selected');
        });
    });
});
</script>
"""

def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
    """Helper function to draw text with stroke"""
    # Draw the stroke/outline
    for adj_x in range(-stroke_width, stroke_width + 1):
        for adj_y in range(-stroke_width, stroke_width + 1):
            draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)

def remove_background(image):
    # Save the image to a specific location
    filename = f"image_{uuid.uuid4()}.png"  # Generates a universally unique identifier (UUID) for the filename
    image.save(filename)
    # Call gradio client for background removal
    result = client.predict(images=handle_file(filename), api_name="/image")
    return Image.open(result[0])

def superimpose(image_with_text, overlay_image):
    # Open image as RGBA to handle transparency
    overlay_image = overlay_image.convert("RGBA")
    # Paste overlay on the background
    image_with_text.paste(overlay_image, (0, 0), overlay_image)
    # Save the final image
    # image_with_text.save("output_image.png")
    return image_with_text

def add_text_to_image(
    input_image,
    text,
    font_size,
    color,
    opacity,
    x_position,
    y_position,
    thickness,
    text_position_type,
    font_choice
):
    try:
        if input_image is None or text.strip() == "":
            return input_image

        # PIL Image ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
        if not isinstance(input_image, Image.Image):
            if isinstance(input_image, np.ndarray):
                image = Image.fromarray(input_image)
            else:
                raise ValueError("Unsupported image type")
        else:
            image = input_image.copy()

        # ์ด๋ฏธ์ง€๋ฅผ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
        if image.mode != 'RGBA':
            image = image.convert('RGBA')

        # ํฐํŠธ ์„ค์ •
        font_files = {
            "Default": "DejaVuSans.ttf",
            "Korean Regular": "ko-Regular.ttf"
        }
        
        try:
            font_file = font_files.get(font_choice, "DejaVuSans.ttf")
            font = ImageFont.truetype(font_file, int(font_size))
        except Exception as e:
            print(f"Font loading error ({font_choice}): {str(e)}")
            font = ImageFont.load_default()
        
        # ์ƒ‰์ƒ ์„ค์ •
        color_map = {
            'White': (255, 255, 255),
            'Black': (0, 0, 0),
            'Red': (255, 0, 0),
            'Green': (0, 255, 0),
            'Blue': (0, 0, 255),
            'Yellow': (255, 255, 0),
            'Purple': (128, 0, 128)
        }
        rgb_color = color_map.get(color, (255, 255, 255))
        
        # ์ž„์‹œ Draw ๊ฐ์ฒด ์ƒ์„ฑํ•˜์—ฌ ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
        temp_draw = ImageDraw.Draw(image)
        text_bbox = temp_draw.textbbox((0, 0), text, font=font)
        text_width = text_bbox[2] - text_bbox[0]
        text_height = text_bbox[3] - text_bbox[1]

        # ์œ„์น˜ ๊ณ„์‚ฐ
        actual_x = int((image.width - text_width) * (x_position / 100))
        actual_y = int((image.height - text_height) * (y_position / 100))

        # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ •
        text_color = (*rgb_color, int(opacity))
        
        if text_position_type == "Text Behind Image":
            try:
                # ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ์ „๊ฒฝ ๊ฐ์ฒด๋งŒ ์ถ”์ถœ
                foreground = remove_background(image)
                
                # ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์ƒ์„ฑ (์›๋ณธ ์ด๋ฏธ์ง€ ๋ณต์‚ฌ)
                background = image.copy()
                
                # ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ฆด ์ž„์‹œ ๋ ˆ์ด์–ด ์ƒ์„ฑ
                text_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
                draw_text = ImageDraw.Draw(text_layer)
                
                # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ
                add_text_with_stroke(
                    draw_text,
                    text,
                    actual_x,
                    actual_y,
                    font,
                    text_color,
                    int(thickness)
                )
                
                # ๋ฐฐ๊ฒฝ์— ํ…์ŠคํŠธ ํ•ฉ์„ฑ
                background = Image.alpha_composite(background, text_layer)
                
                # ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋Š” ๋ฐฐ๊ฒฝ ์œ„์— ์ „๊ฒฝ ๊ฐ์ฒด ํ•ฉ์„ฑ
                output_image = Image.alpha_composite(background, foreground)
            except Exception as e:
                print(f"Error in Text Behind Image processing: {str(e)}")
                return input_image
        else:
            # ํ…์ŠคํŠธ ์˜ค๋ฒ„๋ ˆ์ด ์ƒ์„ฑ
            txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
            draw = ImageDraw.Draw(txt_overlay)
            
            # ํ…์ŠคํŠธ๋ฅผ ์ด๋ฏธ์ง€ ์œ„์— ๊ทธ๋ฆฌ๊ธฐ
            add_text_with_stroke(
                draw,
                text,
                actual_x,
                actual_y,
                font,
                text_color,
                int(thickness)
            )
            output_image = Image.alpha_composite(image, txt_overlay)
        
        # RGB๋กœ ๋ณ€ํ™˜
        output_image = output_image.convert('RGB')
        
        return output_image

    except Exception as e:
        print(f"Error in add_text_to_image: {str(e)}")
        return input_image

    
def update_position(new_position):
    """์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜"""
    print(f"Position updated to: {new_position}")
    return new_position

def update_position_and_ui(pos):
    """์œ„์น˜ ์—…๋ฐ์ดํŠธ ๋ฐ UI ๋ฐ˜์˜"""
    updates = {btn: gr.update(value="selected" if pos_val == pos else "") 
              for btn, pos_val in position_mapping.items()}
    updates['position'] = pos
    return [pos] + [updates[btn] for btn in position_mapping.keys()]
    
def process_inpainting_with_feedback(image, mask, prompt):
    try:
        result = process_inpainting(image, mask, prompt)
        return result, update_status_message("Inpainting completed successfully!")
    except Exception as e:
        return None, update_status_message(f"Error: {str(e)}", is_error=True)
        
def update_controls(bg_prompt):
    """๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ปจํŠธ๋กค ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ"""
    is_visible = bool(bg_prompt)
    return [
        gr.update(visible=is_visible),  # aspect_ratio
        gr.update(visible=is_visible),  # object_controls
    ]

with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
    position = gr.State(value="bottom-center")
    processing_status = gr.State(value="idle")
    gr.HTML(js_code)  # JavaScript ์ฝ”๋“œ ์ถ”๊ฐ€
    
    gr.HTML("""
        <div class="main-title">
            <h1>๐ŸŽจ GiniGen Canvas-o3</h1>
            <p>Remove background of specified objects, generate new backgrounds, and insert text over or behind images with prompts.</p>
        </div>
    """)
    
    status_message = gr.HTML(
        value='<div class="status-message"></div>',
        visible=False
    )

    
    with gr.Row(equal_height=True):
        # ์™ผ์ชฝ ํŒจ๋„ (์ž…๋ ฅ)
        with gr.Column(scale=1):
            with gr.Group(elem_classes="input-panel"):
                with gr.Tabs(elem_classes="tabs-container") as tabs:
                    with gr.Tab("Image Upload & Inpainting", elem_classes="tab-item"):
 
                        input_image = gr.Image(
                            type="pil",
                            label="Upload Image",
                            interactive=True,
                            height=400,
                            elem_classes="fade-in"
                        )

                        with gr.Group():
                            inpaint_prompt = gr.Textbox(
                                label="Inpainting Prompt",
                                placeholder="Describe what you want to add in the masked area..."
                            )
                            mask_input = image_annotator(
                                label="Draw mask for inpainting",
                                height=400
                            )
                            inpaint_btn = gr.Button("Apply Inpainting", variant="primary")

                    # ๋‘ ๋ฒˆ์งธ ํƒญ: ๋ฐฐ๊ฒฝ ์ œ๊ฑฐ ๋ฐ ์ƒ์„ฑ
                    with gr.Tab("Background Removal", elem_classes="tab-item"):


                        text_prompt = gr.Textbox(
                            label="Object to Extract",
                            placeholder="Enter what you want to extract...",
                            interactive=True,
                            elem_classes="fade-in"
                        )

                        with gr.Row():
                            bg_prompt = gr.Textbox(
                                label="Background Prompt (optional)",
                                placeholder="Describe the background...",
                                interactive=True,
                                scale=3
                            )
                            aspect_ratio = gr.Dropdown(
                                choices=["1:1", "16:9", "9:16", "4:3"],
                                value="1:1",
                                label="Aspect Ratio",
                                interactive=True,
                                visible=True,
                                scale=1
                            )

                        with gr.Group(elem_classes="controls-panel", visible=False) as object_controls:
                            with gr.Column(scale=1):
                                with gr.Row():

                                    btn_top_left = gr.Button("โ†–", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    btn_top_center = gr.Button("โ†‘",elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    btn_top_right = gr.Button("โ†—", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")                                    

                                with gr.Row():
                                    btn_middle_left = gr.Button("โ†", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    btn_middle_center = gr.Button("โ€ข",elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    btn_middle_right = gr.Button("โ†’", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    
                                with gr.Row():
                                    btn_bottom_left = gr.Button("โ†™", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                                    btn_bottom_center = gr.Button("โ†“", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary", value="selected")
                                    btn_bottom_right = gr.Button("โ†˜", elem_classes=["position-btn", "interactive-element"],interactive=True,variant="secondary")
                
                            with gr.Column(scale=1):
                                scale_slider = gr.Slider(
                                    minimum=10,
                                    maximum=200,
                                    value=50,
                                    step=5,
                                    label="Object Size (%)"
                                )

                            process_btn = gr.Button(
                                "Process",
                                variant="primary",
                                interactive=False,
                                size="lg"
                            )



        # ์˜ค๋ฅธ์ชฝ ํŒจ๋„ (์ถœ๋ ฅ)
        with gr.Column(scale=1):
            with gr.Group(elem_classes="output-panel"):
                with gr.Tab("Result"):
                    combined_image = gr.Image(
                        label="Combined Result",
                        show_download_button=True,
                        type="pil",
                        height=400
                    )
                    
                    with gr.Accordion("Text Insertion Options", open=False):
                        with gr.Group():
                            with gr.Row():
                                text_input = gr.Textbox(
                                    label="Text Content",
                                    placeholder="Enter text to add..."
                                )
                                text_position_type = gr.Radio(
                                    choices=["Text Over Image", "Text Behind Image"],
                                    value="Text Over Image",
                                    label="Text Position"
                                )
                            
                            with gr.Row():
                                with gr.Column(scale=1):
                                    font_choice = gr.Dropdown(
                                        choices=["Default", "Korean Regular"],
                                        value="Default",
                                        label="Font Selection",
                                        interactive=True
                                    )
                                    font_size = gr.Slider(
                                        minimum=10,
                                        maximum=200,
                                        value=40,
                                        step=5,
                                        label="Font Size"
                                    )
                                    color_dropdown = gr.Dropdown(
                                        choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
                                        value="White",
                                        label="Text Color"
                                    )
                                    thickness = gr.Slider(
                                        minimum=0,
                                        maximum=10,
                                        value=1,
                                        step=1,
                                        label="Text Thickness"
                                    )
                                with gr.Column(scale=1):
                                    opacity_slider = gr.Slider(
                                        minimum=0,
                                        maximum=255,
                                        value=255,
                                        step=1,
                                        label="Opacity"
                                    )
                                    x_position = gr.Slider(
                                        minimum=0,
                                        maximum=100,
                                        value=50,
                                        step=1,
                                        label="Left(0%)~Right(100%)"
                                    )
                                    y_position = gr.Slider(
                                        minimum=0,
                                        maximum=100,
                                        value=50,
                                        step=1,
                                        label="High(0%)~Low(100%)"
                                    )
                            add_text_btn = gr.Button("Apply Text", variant="primary")

                    extracted_image = gr.Image(
                        label="Extracted Object",
                        show_download_button=True,
                        type="pil",
                        height=200
                    )

    # CSS ์Šคํƒ€์ผ
    gr.HTML("""
        <style>
        .position-btn.selected {
            background-color: #2196F3 !important;
            color: white !important;
        }
        </style>
    """)

    # ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
    position_mapping = {
        btn_top_left: "top-left",
        btn_top_center: "top-center",
        btn_top_right: "top-right",
        btn_middle_left: "middle-left",
        btn_middle_center: "middle-center",
        btn_middle_right: "middle-right",
        btn_bottom_left: "bottom-left",
        btn_bottom_center: "bottom-center",
        btn_bottom_right: "bottom-right"
    }

    
    def update_status_message(message: str, is_error: bool = False) -> dict:
        return {
            "visible": True,
            "value": f'<div class="status-message {"status-error" if is_error else "status-success"}">{message}</div>'
        }

    def update_ui_state(component_id, value, is_error=False):
        class_name = "status-error" if is_error else "status-success"
        return gr.update(
            value=f'<div class="status-message {class_name}">{value}</div>',
            visible=True
        )

    def handle_button_click(btn_pos):
        """๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ"""
        print(f"Button clicked: {btn_pos}")
        updates = {btn: gr.update(variant="secondary", value="") for btn in position_mapping.keys()}
        updates[btn_pos] = gr.update(variant="primary", value="selected")
        return [btn_pos] + [updates[btn] for btn in position_mapping.keys()]
    
    # ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
    for btn, pos in position_mapping.items():
        btn.click(
            fn=handle_button_click,
            inputs=[gr.State(pos)],
            outputs=[position] + list(position_mapping.keys()),
            api_name=f"btn_click_{pos}"
        )


    
    inpaint_btn.click(
        fn=process_inpainting_with_feedback,
        inputs=[input_image, mask_input, inpaint_prompt],
        outputs=[input_image, status_message],
        api_name="inpaint"
    )

    process_btn.click(
        fn=process_prompt,
        inputs=[
            input_image,
            text_prompt,
            bg_prompt,
            aspect_ratio,
            position,
            scale_slider
        ],
        outputs=[combined_image, extracted_image, status_message],
        api_name="process"
    )

    bg_prompt.change(
        fn=update_controls,
        inputs=bg_prompt,
        outputs=[aspect_ratio, object_controls],
        queue=False,
        api_name="update_controls"
    )

    input_image.change(
        fn=update_process_button,
        inputs=[input_image, text_prompt],
        outputs=process_btn,
        queue=False,
        api_name="update_process_button"
    )

    text_prompt.change(
        fn=update_process_button,
        inputs=[input_image, text_prompt],
        outputs=process_btn,
        queue=False,
        api_name="update_text_prompt"
    )

    add_text_btn.click(
        fn=add_text_to_image,
        inputs=[
            combined_image,
            text_input,
            font_size,
            color_dropdown,
            opacity_slider,
            x_position,
            y_position,
            thickness,
            text_position_type,
            font_choice
        ],
        outputs=[combined_image, status_message],
        api_name="add_text"
    )


demo.queue(max_size=5)
demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
    share=False,
    max_threads=2
)