File size: 55,852 Bytes
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a22c36
ed8503f
 
 
 
 
0a22c36
ed8503f
 
0a22c36
6f19275
 
 
 
 
 
 
 
 
0b453ea
 
 
 
 
 
 
 
 
 
 
6f19275
0b453ea
 
 
 
0a22c36
 
 
0b453ea
 
 
 
 
0a22c36
ed8503f
 
15c1a47
 
6f19275
e1be17d
6f19275
15c1a47
0a22c36
6f19275
1c7bfc6
ed8503f
e1be17d
 
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b9e53e
 
 
 
 
 
 
 
 
 
ed8503f
4bfb187
 
 
6f19275
 
 
 
 
 
 
 
5857256
6f19275
 
 
 
 
 
 
 
5857256
 
6f19275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8503f
 
 
89fe941
0a22c36
6f19275
0a22c36
ed8503f
 
 
0a22c36
89fe941
 
 
 
ed8503f
0a22c36
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a22c36
ed8503f
 
 
 
6f19275
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a22c36
ed8503f
 
 
0a22c36
ed8503f
 
 
 
 
 
6f19275
ed8503f
 
 
 
 
 
 
89fe941
ed8503f
 
89fe941
 
 
ed8503f
 
89fe941
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
ed8503f
89fe941
ed8503f
 
 
 
6f19275
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
ed8503f
 
0a22c36
6f19275
ed8503f
 
 
 
 
 
 
89fe941
57fdccb
ed8503f
6f19275
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
564b9db
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
6f19275
 
 
ed8503f
 
0b453ea
89fe941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b453ea
89fe941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
ed8503f
6f19275
ed8503f
 
89fe941
ed8503f
 
 
 
 
 
 
6f19275
ed8503f
 
 
 
 
 
564b9db
6f19275
 
ed8503f
 
4bfb187
ed8503f
 
 
 
 
 
 
 
 
 
 
 
89fe941
ed8503f
 
6f19275
 
 
 
 
4bfb187
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
4bfb187
 
6f19275
 
4bfb187
6f19275
 
 
 
 
 
 
ed8503f
4bfb187
ed8503f
 
0b453ea
89fe941
 
 
 
 
 
 
 
 
 
 
 
 
 
5857256
 
89fe941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1be17d
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5857256
6f19275
5857256
 
 
6f19275
ed8503f
 
 
 
6f19275
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f19275
 
 
 
 
 
 
 
 
ed8503f
 
 
6f19275
ed8503f
 
 
 
 
 
 
5857256
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
6f19275
 
 
 
 
 
 
 
ed8503f
 
0b453ea
ed8503f
 
 
 
6f19275
ed8503f
 
 
 
 
6f19275
 
ed8503f
6f19275
 
 
ed8503f
 
89fe941
 
 
 
6f19275
89fe941
 
 
5857256
 
 
 
89fe941
5857256
 
89fe941
6f19275
89fe941
 
6f19275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bfb187
ed8503f
 
 
 
 
6f19275
5857256
 
6f19275
 
 
 
5857256
6f19275
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57dda4d
 
 
 
6f19275
57dda4d
 
6f19275
4bfb187
 
 
57fdccb
18eb73e
6f19275
4bfb187
ed8503f
 
 
 
 
 
 
 
 
 
 
 
89fe941
 
 
 
 
 
 
 
6f19275
 
 
89fe941
5857256
89fe941
 
 
 
 
 
6f19275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
 
 
 
 
6f19275
 
 
 
 
 
 
 
 
 
89fe941
 
 
 
6f19275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89fe941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57dda4d
4bfb187
 
ed8503f
 
89fe941
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
57dda4d
4bfb187
 
 
 
 
 
 
 
 
ed8503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1be17d
 
4bfb187
1c7bfc6
ed8503f
 
 
 
 
564b9db
ed8503f
 
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
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# ==============================================================================
# Original app.py (for diff reference):
# ==============================================================================
# import gradio as gr
#
# def greet(name):
#     return "Hello " + name + "!!"
#
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
# demo.launch()
# ==============================================================================

# HF Spaces compatibility: Add src to Python path for heartlib import
import sys
import os
import uuid
import subprocess
import tempfile
import wave
from pathlib import Path
from typing import Dict, Any, Iterator, Optional, Tuple, TYPE_CHECKING

import numpy as np

# HF Spaces: import spaces before any CUDA-related packages (e.g., torch).
try:
    import spaces
except Exception:
    spaces = None

IS_SPACE = spaces is not None and os.environ.get("SPACE_ID")

_gpu_duration_env = os.environ.get("GPU_MAX_DURATION")
if _gpu_duration_env is None:
    aoti_env = os.environ.get("ENABLE_AOTI")
    enable_aoti_default = aoti_env is None or aoti_env.strip().lower() in ("1", "true", "yes", "y", "on")
    GPU_MAX_DURATION = 600 if IS_SPACE and enable_aoti_default else 100
else:
    GPU_MAX_DURATION = int(_gpu_duration_env)


def _env_bool(name: str) -> Optional[bool]:
    val = os.environ.get(name)
    if val is None:
        return None
    return val.strip().lower() in ("1", "true", "yes", "y", "on")


_default_keep_model_loaded_env = _env_bool("KEEP_MODEL_LOADED_DEFAULT")
if _default_keep_model_loaded_env is None:
    DEFAULT_KEEP_MODEL_LOADED = not IS_SPACE
else:
    DEFAULT_KEEP_MODEL_LOADED = _default_keep_model_loaded_env


def _gpu_guard(fn):
    if spaces is None:
        return fn
    if GPU_MAX_DURATION > 0:
        try:
            return spaces.GPU(fn, duration=GPU_MAX_DURATION)
        except TypeError:
            return spaces.GPU(fn)
    return spaces.GPU(fn)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

# HF Spaces: disable SSR to avoid Node proxy/port issues.
os.environ.setdefault("GRADIO_SSR_MODE", "0")
# Mitigate CUDA memory fragmentation on small GPUs.
os.environ.setdefault("PYTORCH_ALLOC_CONF", "expandable_segments:True")
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")

if TYPE_CHECKING:
    from heartlib import HeartMuLaGenPipeline
import gradio as gr
import re
try:
    from google import genai  # google-genai package
    from google.genai import types
except Exception:
    genai = None
    types = None
from openai import OpenAI
from transformers import BitsAndBytesConfig

# Global pipelines (managed lazily)

# Default model path (HF Spaces + local override via env)
MODEL_PATH = os.environ.get("MODEL_PATH", "./ckpt")

# LLM API Presets
LLM_PRESETS = {
    "gemini": {
        "name": "Google Gemini",
        "api_type": "gemini",
        "default_model": "gemini-2.0-flash-lite",
        "env_key": "GEMINI_API_KEY",
        "base_url": None,
    },
    "openai": {
        "name": "OpenAI",
        "api_type": "openai",
        "default_model": "gpt-4o-mini",
        "env_key": "OPENAI_API_KEY",
        "base_url": None,
    },
    "deepseek": {
        "name": "DeepSeek",
        "api_type": "openai",
        "default_model": "deepseek-chat",
        "env_key": "DEEPSEEK_API_KEY",
        "base_url": "https://api.deepseek.com",
    },
    "custom": {
        "name": "Custom OpenAI-Compatible",
        "api_type": "openai",
        "default_model": "custom-model",
        "env_key": None,
        "base_url": None,
    }
}

# Default example from assets
EXAMPLE_LYRICS = """[Intro]

[Verse]
The sun creeps in across the floor
I hear the traffic outside the door
The coffee pot begins to hiss
It is another morning just like this

[Prechorus]
The world keeps spinning round and round
Feet are planted on the ground
I find my rhythm in the sound

[Chorus]
Every day the light returns
Every day the fire burns
We keep on walking down this street
Moving to the same steady beat
It is the ordinary magic that we meet

[Verse]
The hours tick deeply into noon
Chasing shadows,chasing the moon
Work is done and the lights go low
Watching the city start to glow

[Bridge]
It is not always easy,not always bright
Sometimes we wrestle with the night
But we make it to the morning light

[Chorus]
Every day the light returns
Every day the fire burns
We keep on walking down this street
Moving to the same steady beat

[Outro]
Just another day
Every single day"""

EXAMPLE_TAGS = "piano,happy"

# Tag categories for selection
TAG_DATA = {
    "Gender": [
        "Male", "Female"
    ],
    "Genre": [
        "Pop", "Folk", "Ballad", "Electronic", "Rock", "Acoustic", "R&B",
        "Indie", "Dance", "Indie Pop", "J-Pop", "Hip-Hop", "Country",
        "Latin", "Alternative", "Christian", "Cantopop", "Gospel", "Soul",
        "Mandopop"
    ],
    "Instrument": [
        "Drums", "Piano", "Guitar", "Strings", "Synthesizer", "Bass",
        "Acoustic Guitar", "Keyboard", "Electronic Drums", "Vocals",
        "Drum Machine", "Electric Guitar", "Percussion", "Beat",
        "Orchestra", "Saxophone", "Accordion", "Voice", "String", "Vocal"
    ],
    "Mood": [
        "Melancholy", "Romantic", "Energetic", "Hopeful", "Dreamy",
        "Relaxed", "Sad", "Calm", "Cheerful", "Reflective", "Emotional",
        "Joyful", "Sentimental", "Uplifting", "Warm", "Peaceful", "Upbeat",
        "Gentle", "Nostalgic", "Epic"
    ],
    "Scene": [
        "Driving", "Road Trip", "Cafe", "Relaxing", "Wedding", "Meditation",
        "Workout", "Walking", "Alone", "Travel", "Reflection", "Rainy Day",
        "Night", "Church", "Coffee Shop", "Gym", "Gaming", "Study",
        "Dating", "Date"
    ],
    "Singer Timbre": [
        "Soft", "Clear", "Warm", "Gentle", "Smooth", "Sweet", "Emotional",
        "Mellow", "Powerful", "Youthful", "Bright", "Rough", "Raspy",
        "Melodic", "Deep", "Soulful", "Strong", "Energetic", "Breathy",
        "Passionate"
    ],
    "Topic": [
        "Love", "Relationship", "Hope", "Longing", "Loss", "Heartbreak",
        "Memory", "Reflection", "Life", "Faith", "Regret", "Freedom",
        "Breakup", "Nature", "Loneliness", "Dreams", "Nostalgia", "Romance",
        "Friendship", "Youth"
    ]
}

DATA_DIR = Path(os.environ.get("HEARTMULA_DATA_DIR", os.path.join(tempfile.gettempdir(), "heartmula_stream")))
DATA_DIR.mkdir(parents=True, exist_ok=True)
print(f"DATA_DIR = {DATA_DIR}")

# Clear ZeroGPU offload cache to avoid disk-full errors.
offload_path = "/data-nvme/zerogpu-offload"
try:
    if os.path.exists(offload_path):
        for name in os.listdir(offload_path):
            os.system(f"rm -rf {offload_path}/{name}")
except Exception as e:
    print(f"WARN: failed to clear ZeroGPU offload cache: {e}")

GRADIO_QUEUE_MAX_SIZE = int(os.environ.get("GRADIO_QUEUE_MAX_SIZE", "24"))
GRADIO_DEFAULT_CONCURRENCY = int(os.environ.get("GRADIO_DEFAULT_CONCURRENCY", "1"))
GPU_CONCURRENCY_LIMIT = int(os.environ.get("GRADIO_GPU_CONCURRENCY", "1"))
DEFAULT_DURATION_SEC = int(os.environ.get("DEFAULT_DURATION_SEC", "60" if IS_SPACE else "180"))
DEFAULT_QUANT_MODE = os.environ.get("DEFAULT_QUANT_MODE", "4bit" if IS_SPACE else "none")
DEFAULT_OFFLOAD_MODE = os.environ.get("DEFAULT_OFFLOAD_MODE", "aggressive" if IS_SPACE else "auto")
DEFAULT_GENERATION_MODE = os.environ.get(
    "DEFAULT_GENERATION_MODE",
    "Accelerated" if IS_SPACE else "Original (No Acceleration)",
)
DEFAULT_SPEED_SUBMODE = os.environ.get("DEFAULT_SPEED_SUBMODE", "Standard")
DEFAULT_PRESET = os.environ.get("DEFAULT_PRESET", "ZeroGPU AOTI FP8" if IS_SPACE else "Balanced")
MODEL_DETOKENIZE_INTERVAL_SEC = float(os.environ.get("MODEL_DETOKENIZE_INTERVAL_SEC", "29.76"))
AUDIO_SAMPLE_RATE = int(os.environ.get("AUDIO_SAMPLE_RATE", "48000"))
FRAME_MS = 80.0
BLOCK_FRAMES = max(1, int(round((MODEL_DETOKENIZE_INTERVAL_SEC * 1000.0) / FRAME_MS)))
BLOCK_SAMPLES = max(1, int(round(MODEL_DETOKENIZE_INTERVAL_SEC * AUDIO_SAMPLE_RATE)))
MIN_BUFFER_BLOCKS = int(os.environ.get("MIN_BUFFER_BLOCKS", "1"))
PREFETCH_BLOCKS = int(os.environ.get("PREFETCH_BLOCKS", "2"))
MAX_QUEUE_BLOCKS = int(os.environ.get("MAX_QUEUE_BLOCKS", "4"))
ALLOW_STREAMING_ZERO_GPU = _env_bool("ALLOW_STREAMING_ZERO_GPU")
STREAMING_ALLOWED = (not IS_SPACE) or bool(ALLOW_STREAMING_ZERO_GPU)

PRESET_CONFIGS = {
    "ZeroGPU Safe": {
        "duration": 60,
        "quant_mode": "4bit",
        "offload_mode": "aggressive",
        "keep_model_loaded": False,
        "temperature": 1.0,
        "topk": 50,
        "cfg_scale": 1.5,
        "generation_mode": "Accelerated",
        "speed_submode": "Standard",
    },
    "ZeroGPU AOTI FP8": {
        "duration": 60,
        "quant_mode": "fp8",
        "offload_mode": "aggressive",
        "keep_model_loaded": False,
        "temperature": 1.0,
        "topk": 50,
        "cfg_scale": 1.5,
        "generation_mode": "Accelerated",
        "speed_submode": "Standard",
    },
    "Balanced": {
        "duration": 120,
        "quant_mode": "4bit" if IS_SPACE else "none",
        "offload_mode": "auto",
        "keep_model_loaded": DEFAULT_KEEP_MODEL_LOADED,
        "temperature": 1.0,
        "topk": 50,
        "cfg_scale": 1.5,
        "generation_mode": "Accelerated",
        "speed_submode": "Standard",
    },
    "Quality": {
        "duration": 180,
        "quant_mode": "none",
        "offload_mode": "auto",
        "keep_model_loaded": DEFAULT_KEEP_MODEL_LOADED,
        "temperature": 0.9,
        "topk": 50,
        "cfg_scale": 2.0,
        "generation_mode": "Original (No Acceleration)",
        "speed_submode": "Standard",
    },
}
if DEFAULT_PRESET not in PRESET_CONFIGS:
    DEFAULT_PRESET = "Balanced"
_default_preset_config = PRESET_CONFIGS[DEFAULT_PRESET]
if "DEFAULT_DURATION_SEC" not in os.environ:
    DEFAULT_DURATION_SEC = _default_preset_config["duration"]
if "DEFAULT_QUANT_MODE" not in os.environ:
    DEFAULT_QUANT_MODE = _default_preset_config["quant_mode"]
if "DEFAULT_OFFLOAD_MODE" not in os.environ:
    DEFAULT_OFFLOAD_MODE = _default_preset_config["offload_mode"]
if "DEFAULT_GENERATION_MODE" not in os.environ:
    DEFAULT_GENERATION_MODE = _default_preset_config["generation_mode"]
if "DEFAULT_SPEED_SUBMODE" not in os.environ:
    DEFAULT_SPEED_SUBMODE = _default_preset_config["speed_submode"]


class ModelManager:
    def __init__(self, model_path: str, use_deepspeed_override: Optional[bool] = None):
        import torch
        from heartlib import HeartMuLaGenPipeline

        self.model_path = model_path
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
        self._gen_pipes: Dict[Tuple[str, str, str], "HeartMuLaGenPipeline"] = {}
        if use_deepspeed_override is None:
            self.use_deepspeed = os.getenv("USE_DEEPSPEED_INFERENCE", "0").lower() in ("1", "true", "yes")
        else:
            self.use_deepspeed = use_deepspeed_override
        self.ds_inference_config = self._make_ds_inference_config()
        self._HeartMuLaGenPipeline = HeartMuLaGenPipeline

    def _make_ds_inference_config(self) -> Dict[str, Any]:
        if not self.use_deepspeed:
            return {}
        mp_size = int(os.getenv("DEEPSPEED_TP_SIZE", os.getenv("WORLD_SIZE", "1")))
        replace_method = os.getenv("DEEPSPEED_REPLACE_METHOD", "auto")
        kernel_inject = os.getenv("DEEPSPEED_KERNEL_INJECT", "1").lower() in ("1", "true", "yes")
        return {
            "mp_size": mp_size,
            "dtype": self.dtype,
            "replace_method": replace_method,
            "replace_with_kernel_inject": kernel_inject,
        }

    def _make_bnb_config(self, quant_mode: str) -> Optional[BitsAndBytesConfig]:
        import torch
        if quant_mode == "none":
            return None
        if self.device.type != "cuda":
            raise gr.Error("Quantization requires CUDA.")
        if quant_mode == "fp8":
            return None
        if quant_mode == "4bit":
            quant_type = "nf4"
            try:
                major, _ = torch.cuda.get_device_capability()
                if major >= 10:
                    quant_type = "fp4"
            except Exception:
                pass
            return BitsAndBytesConfig(
                load_in_4bit=True,
                bnb_4bit_quant_type=quant_type,
                bnb_4bit_compute_dtype=torch.bfloat16,
                bnb_4bit_use_double_quant=True,
            )
        if quant_mode == "8bit":
            return BitsAndBytesConfig(load_in_8bit=True)
        raise gr.Error(f"Unknown quant mode: {quant_mode}")

    def get_gen_pipeline(self, version: str, codec_version: str, quant_mode: str) -> "HeartMuLaGenPipeline":
        key = (version, codec_version, quant_mode)
        if key not in self._gen_pipes:
            bnb_config = self._make_bnb_config(quant_mode)
            self._gen_pipes[key] = self._HeartMuLaGenPipeline.from_pretrained(
                self.model_path,
                device=self.device,
                dtype=self.dtype,
                version=version,
                codec_version=codec_version,
                bnb_config=bnb_config,
                torchao_quantize=quant_mode == "fp8",
                lazy_load=True,
                use_deepspeed=self.use_deepspeed,
                ds_inference_config=self.ds_inference_config,
            )
        return self._gen_pipes[key]


model_managers: Dict[str, ModelManager] = {}


def get_model_manager(use_acceleration: bool) -> ModelManager:
    key = "accelerated" if use_acceleration else "original"
    if key not in model_managers:
        os.makedirs(MODEL_PATH, exist_ok=True)
        download_models_if_needed(MODEL_PATH)
        use_deepspeed_override = None if use_acceleration else False
        model_managers[key] = ModelManager(MODEL_PATH, use_deepspeed_override=use_deepspeed_override)
    return model_managers[key]


def update_tag_string(*args):
    """
    Collects selected tags from all categories and joins them.
    args: list of lists (selections from each CheckboxGroup)
    """
    all_tags = []
    for selection in args:
        if selection:
            if isinstance(selection, list):
                all_tags.extend(selection)
            else:
                all_tags.append(selection)
    # Remove duplicates while preserving order
    seen = set()
    unique_tags = []
    for t in all_tags:
        if t not in seen:
            unique_tags.append(t)
            seen.add(t)
    return ",".join(unique_tags)


def process_lyrics_correct(content):
    """
    Correct lyrics processing logic aligned with training data.
    1. Removes timestamps [xx:xx].
    2. Split lines and strip whitespace from each line.
    3. Remove leading/trailing empty lines.
    4. Collapse multiple newlines (3 or more) into 2.
    """
    # 0. Convert to lowercase
    content = content.lower()

    # 1. Remove timestamps [00:12] or [00:12.34]
    content = re.sub(r'\[[^\]]*\d{1,2}:\d{2}[^\]]*\]', '', content)

    # 2. Split lines and strip whitespace from each line
    lines = [line.strip() for line in content.split('\n')]

    # 3. Remove leading empty lines
    while lines and lines[0] == '':
        lines.pop(0)

    # 4. Remove trailing empty lines
    while lines and lines[-1] == '':
        lines.pop()

    # 5. Join back to string
    content = '\n'.join(lines)

    # 6. Collapse multiple newlines (3 or more) into 2
    content = re.sub(r'\n{3,}', '\n\n', content)

    return content


def save_audio_to_wav(sample_rate: int, audio_np: np.ndarray, output_dir: Path) -> str:
    output_dir.mkdir(parents=True, exist_ok=True)
    unique_id = str(uuid.uuid4())
    wav_path = output_dir / f"{unique_id}.wav"
    audio_int16 = (audio_np * 32767).astype(np.int16)
    with wave.open(str(wav_path), "wb") as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(audio_int16.tobytes())
    return str(wav_path)


def convert_wav_to_mp3(wav_path: str, output_dir: Path) -> str:
    output_dir.mkdir(parents=True, exist_ok=True)
    unique_id = str(uuid.uuid4())
    mp3_path = output_dir / f"{unique_id}.mp3"
    cmd = [
        "ffmpeg",
        "-y",
        "-i",
        wav_path,
        "-codec:a",
        "libmp3lame",
        "-qscale:a",
        "2",
        str(mp3_path),
    ]
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return str(mp3_path)


def check_models_exist(ckpt_dir):
    """Check if all required models exist"""
    required_files = [
        os.path.join(ckpt_dir, "gen_config.json"),
        os.path.join(ckpt_dir, "tokenizer.json"),
        os.path.join(ckpt_dir, "HeartCodec-oss"),
        os.path.join(ckpt_dir, "HeartMuLa-oss-3B"),
    ]

    for path in required_files:
        if not os.path.exists(path):
            return False
    return True


def download_models_if_needed(ckpt_dir):
    """Download models from ModelScope if not present"""
    if check_models_exist(ckpt_dir):
        print("=" * 50)
        print(f"✓ Checkpoints found in {ckpt_dir}")
        print("✓ Skipping download")
        print("=" * 50)
        return

    print("=" * 50)
    print("⬇  Starting model download from ModelScope")
    print("=" * 50)
    print("")

    from modelscope import snapshot_download

    # Download HeartMuLaGen (config and tokenizer)
    print("━" * 50)
    print("📦 [1/3] Downloading HeartMuLaGen config and tokenizer...")
    print("━" * 50)
    snapshot_download('HeartMuLa/HeartMuLaGen', local_dir=ckpt_dir)
    print("✓ HeartMuLaGen download completed")
    print("")

    # Download HeartMuLa-oss-3B
    print("━" * 50)
    print("📦 [2/3] Downloading HeartMuLa-oss-3B model...")
    print("━" * 50)
    snapshot_download('HeartMuLa/HeartMuLa-oss-3B',
                      local_dir=os.path.join(ckpt_dir, 'HeartMuLa-oss-3B'))
    print("✓ HeartMuLa-oss-3B download completed")
    print("")

    # Download HeartCodec-oss
    print("━" * 50)
    print("📦 [3/3] Downloading HeartCodec-oss model...")
    print("━" * 50)
    snapshot_download('HeartMuLa/HeartCodec-oss',
                      local_dir=os.path.join(ckpt_dir, 'HeartCodec-oss'))
    print("✓ HeartCodec-oss download completed")
    print("")

    print("=" * 50)
    print("✓ All models downloaded successfully!")
    print("=" * 50)
    print("")


def load_pipeline(model_path, version, codec_version, quant_mode, use_acceleration: bool):
    """Load HeartMuLa pipeline (lazy)"""
    manager = get_model_manager(use_acceleration)
    print(f"Using model from {model_path} on {manager.device}...")
    return manager.get_gen_pipeline(version, codec_version, quant_mode)


def _validate_generation_inputs(lyrics: str, tags: str) -> None:
    if not lyrics.strip():
        raise gr.Error("Please enter lyrics")
    if not tags.strip():
        raise gr.Error("Please enter tags")


def generate(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    output_format,
    keep_model_loaded,
    offload_mode,
    backend,
    use_acceleration,
):
    """Generate music"""
    import torch
    _validate_generation_inputs(lyrics, tags)

    max_audio_length_ms = int(duration_sec * 1000)

    try:
        if backend == "exllama_v2":
            raise gr.Error("ExLlamaV2 backend is not implemented yet.")

        pipe = load_pipeline(MODEL_PATH, version, codec_version, quant_mode, use_acceleration)
        output_path = os.path.join(DATA_DIR, f"gen_{uuid.uuid4().hex}.wav")

        with torch.inference_mode():
            pipe(
                {
                    "lyrics": lyrics,
                    "tags": tags,
                },
                max_audio_length_ms=max_audio_length_ms,
                save_path=output_path,
                topk=topk,
                temperature=temperature,
                cfg_scale=cfg_scale,
                keep_model_loaded=keep_model_loaded,
                offload_mode=offload_mode,
            )
        try:
            file_size = os.path.getsize(output_path)
            print(f"Generated file: {output_path} ({file_size} bytes)")
        except Exception as e:
            print(f"WARN: failed to stat output file {output_path}: {e}")

        if output_format == "mp3":
            mp3_path = convert_wav_to_mp3(output_path, DATA_DIR)
            return mp3_path

        return output_path

    except Exception as e:
        raise gr.Error(f"Generation error: {str(e)}")
    finally:
        if not keep_model_loaded and torch.cuda.is_available():
            torch.cuda.empty_cache()


@_gpu_guard
def generate_original(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    output_format,
    keep_model_loaded,
    offload_mode,
    backend,
):
    return generate(
        lyrics,
        tags,
        cfg_scale,
        duration_sec,
        temperature,
        topk,
        version,
        codec_version,
        quant_mode,
        output_format,
        keep_model_loaded,
        offload_mode,
        backend,
        False,
    )


@_gpu_guard
def generate_accelerated(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    output_format,
    keep_model_loaded,
    offload_mode,
    backend,
):
    return generate(
        lyrics,
        tags,
        cfg_scale,
        duration_sec,
        temperature,
        topk,
        version,
        codec_version,
        quant_mode,
        output_format,
        keep_model_loaded,
        offload_mode,
        backend,
        True,
    )


def _normalize_stream_chunk(chunk: np.ndarray) -> np.ndarray:
    chunk = np.nan_to_num(chunk, nan=0.0, posinf=0.0, neginf=0.0)
    return np.clip(chunk, -1.0, 1.0)


def generate_music_streaming(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    keep_model_loaded,
    offload_mode,
    backend,
    use_acceleration,
) -> Iterator[Tuple[int, np.ndarray]]:
    _validate_generation_inputs(lyrics, tags)
    if backend == "exllama_v2":
        raise gr.Error("ExLlamaV2 backend is not implemented yet.")
    pipe = load_pipeline(MODEL_PATH, version, codec_version, quant_mode, use_acceleration)
    max_audio_length_ms = int(duration_sec * 1000)
    for chunk in pipe.stream(
        {"lyrics": lyrics, "tags": tags},
        max_audio_length_ms=max_audio_length_ms,
        temperature=temperature,
        topk=topk,
        cfg_scale=cfg_scale,
        chunk_frames=BLOCK_FRAMES,
        keep_model_loaded=keep_model_loaded,
        offload_mode=offload_mode,
    ):
        if chunk.dim() == 2:
            chunk = chunk.squeeze(0)
        chunk_np = chunk.cpu().numpy()
        chunk_np = _normalize_stream_chunk(chunk_np)
        print(f"stream chunk: samples={chunk_np.shape[0]} sr={AUDIO_SAMPLE_RATE}")
        yield AUDIO_SAMPLE_RATE, chunk_np


def stream_generate(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    keep_model_loaded,
    offload_mode,
    backend,
    use_acceleration,
):
    try:
        _validate_generation_inputs(lyrics, tags)
        start_threshold = max(MIN_BUFFER_BLOCKS, PREFETCH_BLOCKS)
        queue = []
        started = False
        print("block stream start:", f"block_sec={MODEL_DETOKENIZE_INTERVAL_SEC}", f"duration_sec={duration_sec}")
        for sr, chunk_np in generate_music_streaming(
            lyrics=lyrics,
            tags=tags,
            cfg_scale=cfg_scale,
            duration_sec=duration_sec,
            temperature=temperature,
            topk=topk,
            version=version,
            codec_version=codec_version,
            quant_mode=quant_mode,
            keep_model_loaded=keep_model_loaded,
            offload_mode=offload_mode,
            backend=backend,
            use_acceleration=use_acceleration,
        ):
            chunk_np = chunk_np.astype("float32", copy=False)
            queue.append(chunk_np)
            if not started and len(queue) < start_threshold and len(queue) < MAX_QUEUE_BLOCKS:
                continue
            if not started:
                started = True
                print(f"block stream start playback: buffered_blocks={len(queue)}")
            while queue:
                block = queue.pop(0)
                print(f"block stream yield: samples={block.shape[0]}")
                yield sr, block
    except Exception as e:
        raise gr.Error(f"Streaming error: {str(e)}")


@_gpu_guard
def stream_generate_accelerated(
    lyrics,
    tags,
    cfg_scale,
    duration_sec,
    temperature,
    topk,
    version,
    codec_version,
    quant_mode,
    keep_model_loaded,
    offload_mode,
    backend,
):
    if IS_SPACE and not STREAMING_ALLOWED:
        raise gr.Error("Streaming is disabled on ZeroGPU. Use Standard mode or set ALLOW_STREAMING_ZERO_GPU=1.")
    return stream_generate(
        lyrics,
        tags,
        cfg_scale,
        duration_sec,
        temperature,
        topk,
        version,
        codec_version,
        quant_mode,
        keep_model_loaded,
        offload_mode,
        backend,
        True,
    )


def generate_lyrics(theme, tags, language, api_choice, api_key_input, custom_base_url, custom_model, progress=gr.Progress()):
    """Generate lyrics using selected LLM API"""

    if not theme.strip():
        raise gr.Error("Please enter a theme")

    progress(0.1, desc="Preparing request...")

    # Get preset configuration
    if api_choice not in LLM_PRESETS:
        raise gr.Error(f"Unknown API choice: {api_choice}")

    preset = LLM_PRESETS[api_choice]

    # Determine API key
    api_key = api_key_input.strip() if api_key_input and api_key_input.strip() else None
    if not api_key and preset["env_key"]:
        api_key = os.environ.get(preset["env_key"])

    if not api_key:
        raise gr.Error(f"No API key provided. Please enter your API key in the field above.")

    # Determine base URL and model
    base_url = custom_base_url.strip() if custom_base_url and custom_base_url.strip() else preset["base_url"]
    model_name = custom_model.strip() if custom_model and custom_model.strip() else preset["default_model"]


    # Language mapping
    language_names = {
        "en": "English",
        "zh": "Chinese",
        "jp": "Japanese",
        "kr": "Korean",
        "sp": "Spanish"
    }
    lang_name = language_names.get(language, "English")

    # Tags processing
    tags_text = tags.strip() if tags.strip() else "pop, emotional"

    # Create prompt
    prompt = f"""You are a professional songwriter. Generate song lyrics based on the following requirements:

**Theme**: {theme}
**Music Style/Tags**: {tags_text}
**Language**: {lang_name}

**Format Requirements** (CRITICAL):
1. Use lowercase for all lyrics text (except structure tags which are in brackets)
2. Include proper song structure tags: [Intro], [Verse], [Prechorus], [Chorus], [Bridge], [Outro]
3. Each structure tag should be on its own line
4. Separate different sections with a blank line (one empty line between sections)
5. NO timestamps like [00:12] - only structure tags allowed
6. Keep lyrics concise and suitable for a 3-4 minute song

**Structure Guidelines**:
- [Intro]: Optional, 1-2 lines if included
- [Verse]: Story-telling part, 4-6 lines, can repeat with different lyrics
- [Prechorus]: Optional, 2-4 lines, builds tension before chorus
- [Chorus]: Main hook, catchy and repetitive, 4-6 lines
- [Bridge]: Optional, provides contrast, 4-6 lines
- [Outro]: Closing, 1-2 lines

**Example Format**:
```
[Intro]

[Verse]
the sun creeps in across the floor
i hear the traffic outside the door
the coffee pot begins to hiss
it is another morning just like this

[Chorus]
every day the light returns
every day the fire burns
we keep on walking down this street
moving to the same steady beat
```

Now generate lyrics in {lang_name} based on the theme "{theme}" with style "{tags_text}".
Output ONLY the lyrics with structure tags, no explanations.
"""

    try:
        if preset["api_type"] == "gemini":
            if genai is None or types is None:
                raise gr.Error("Gemini SDK not available. Install `google-genai` or switch provider.")
            # Gemini API
            progress(0.3, desc=f"Connecting to {preset['name']}...")

            # Set proxy if needed
            try:
                proxy_host = os.environ.get("PROXY_HOST", "127.0.0.1")
                proxy_port = os.environ.get("PROXY_PORT", "7890")
                os.environ['http_proxy'] = f'http://{proxy_host}:{proxy_port}'
                os.environ['https_proxy'] = f'http://{proxy_host}:{proxy_port}'
            except Exception:
                pass  # Proxy is optional

            client = genai.Client(api_key=api_key)

            progress(0.5, desc=f"Generating lyrics with {preset['name']}...")

            response = client.models.generate_content(
                model=model_name,
                contents=[
                    types.Content(
                        role='user',
                        parts=[types.Part(text=prompt)]
                    )
                ],
                config=types.GenerateContentConfig(
                    temperature=0.8,
                    max_output_tokens=2000
                )
            )

            generated_lyrics = response.text.strip()

        elif preset["api_type"] == "openai":
            # OpenAI-compatible API (OpenAI, DeepSeek, Custom)
            progress(0.3, desc=f"Connecting to {preset['name']}...")

            # Create client with optional base_url
            if base_url:
                client = OpenAI(api_key=api_key, base_url=base_url)
            else:
                client = OpenAI(api_key=api_key)

            progress(0.5, desc=f"Generating lyrics with {preset['name']}...")

            response = client.chat.completions.create(
                model=model_name,
                messages=[
                    {"role": "system", "content": "You are a professional songwriter who creates well-structured lyrics."},
                    {"role": "user", "content": prompt}
                ],
                temperature=0.8,
                max_tokens=2000
            )

            generated_lyrics = response.choices[0].message.content.strip()

        else:
            raise gr.Error(f"Unknown API type: {preset['api_type']}")

        progress(0.9, desc="Processing response...")

        # Clean up the response (remove markdown code blocks if present)
        if generated_lyrics.startswith("```"):
            lines = generated_lyrics.split("\n")
            generated_lyrics = "\n".join(lines[1:-1]) if len(lines) > 2 else generated_lyrics

        # Apply our lyrics processing function to ensure format consistency
        generated_lyrics = process_lyrics_correct(generated_lyrics)

        progress(1.0, desc="Done!")

        return generated_lyrics

    except Exception as e:
        raise gr.Error(f"Lyrics generation error: {str(e)}")


def create_ui():
    """Create Gradio UI"""
    speed_submode_value = DEFAULT_SPEED_SUBMODE if STREAMING_ALLOWED else "Standard"
    show_stream_default = (
        STREAMING_ALLOWED
        and DEFAULT_GENERATION_MODE != "Original (No Acceleration)"
        and speed_submode_value == "Streaming"
    )

    with gr.Blocks(title="HeartMuLa Music Generation") as demo:
        gr.Markdown("# HeartMuLa Music Generation")
        gr.Markdown("Generate music from lyrics and style tags")
        gr.Markdown("Tip: start with the **ZeroGPU Safe** preset for reliable generation on small GPUs.")

        with gr.Tabs():
            # Tab 1: Music Generation
            with gr.Tab("Music Generation"):
                with gr.Row():
                    with gr.Column():
                        lyrics = gr.Textbox(
                            label="Lyrics",
                            lines=15,
                            value=EXAMPLE_LYRICS,
                            placeholder="Enter lyrics here..."
                        )

                        # Add format button
                        format_btn = gr.Button("Format Lyrics", size="sm")

                        # Tag Selection
                        gr.Markdown("### Tags")

                        tags = gr.Textbox(
                            label="Selected Tags (comma-separated)",
                            value=EXAMPLE_TAGS,
                            placeholder="e.g., piano,happy,pop",
                            lines=2
                        )

                        # Tag categories in accordion
                        tag_checkboxes = []
                        with gr.Accordion("Tag Categories (Click to Expand)", open=False):
                            with gr.Row():
                                with gr.Column():
                                    t1 = gr.CheckboxGroup(choices=TAG_DATA["Gender"], label="Gender")
                                    tag_checkboxes.append(t1)
                                    t2 = gr.CheckboxGroup(choices=TAG_DATA["Genre"], label="Genre")
                                    tag_checkboxes.append(t2)
                                with gr.Column():
                                    t3 = gr.CheckboxGroup(choices=TAG_DATA["Instrument"], label="Instrument")
                                    tag_checkboxes.append(t3)
                                    t4 = gr.CheckboxGroup(choices=TAG_DATA["Mood"], label="Mood")
                                    tag_checkboxes.append(t4)
                                with gr.Column():
                                    t5 = gr.CheckboxGroup(choices=TAG_DATA["Scene"], label="Scene")
                                    tag_checkboxes.append(t5)
                                    t6 = gr.CheckboxGroup(choices=TAG_DATA["Singer Timbre"], label="Singer Timbre")
                                    tag_checkboxes.append(t6)
                                with gr.Column():
                                    t7 = gr.CheckboxGroup(choices=TAG_DATA["Topic"], label="Topic")
                                    tag_checkboxes.append(t7)

                        gr.Markdown("### Quick Presets")
                        with gr.Row():
                            preset_selector = gr.Dropdown(
                                choices=list(PRESET_CONFIGS.keys()),
                                value=DEFAULT_PRESET,
                                label="Preset"
                            )
                            apply_preset_btn = gr.Button("Apply Preset", size="sm")

                        # Generation parameters
                        with gr.Row():
                            cfg_scale = gr.Slider(0.0, 3.0, value=1.5, step=0.1, label="CFG Scale")
                            duration = gr.Slider(10, 300, value=DEFAULT_DURATION_SEC, step=10, label="Duration (sec)")

                        with gr.Row():
                            temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature")
                            topk = gr.Slider(1, 100, value=50, step=1, label="Top-K")

                        with gr.Accordion("Advanced Settings", open=False):
                            backend = gr.Dropdown(
                                choices=[("HF Pipeline", "hf"), ("ExLlamaV2 (Not Implemented)", "exllama_v2")],
                                value="hf",
                                label="Backend"
                            )
                            version = gr.Dropdown(
                                choices=["3B", "7B", "RL-oss-3B-20260123"],
                                value="3B",
                                label="Model Version"
                            )
                            codec_version = gr.Dropdown(
                                choices=["oss", "oss-20260123"],
                                value="oss",
                                label="Codec Version"
                            )
                            quant_mode = gr.Dropdown(
                                choices=[
                                    ("None", "none"),
                                    ("4-bit (NF4/FP4)", "4bit"),
                                    ("8-bit", "8bit"),
                                    ("FP8 (TorchAO)", "fp8"),
                                ],
                                value=DEFAULT_QUANT_MODE,
                                label="Quantization (ZeroGPU recommended: 4-bit)"
                            )
                            keep_model_loaded = gr.Checkbox(
                                value=DEFAULT_KEEP_MODEL_LOADED,
                                label="Keep Model Loaded"
                            )
                            offload_mode = gr.Dropdown(
                                choices=["auto", "aggressive"],
                                value=DEFAULT_OFFLOAD_MODE,
                                label="Offload Mode"
                            )
                            output_format = gr.Radio(
                                choices=[("WAV", "wav"), ("MP3", "mp3")],
                                value="wav",
                                label="Output Format",
                                visible=not show_stream_default,
                            )
                            gr.Markdown(
                                f"Streaming is block-based: {MODEL_DETOKENIZE_INTERVAL_SEC:.2f}s per block "
                                f"({BLOCK_FRAMES} frames, {BLOCK_SAMPLES} samples)."
                            )

                        gr.Markdown("### 🚀 Generation")

                        generation_mode = gr.Radio(
                            choices=["Original (No Acceleration)", "Accelerated"],
                            value=DEFAULT_GENERATION_MODE,
                            label="Generation Mode",
                        )

                        speed_choices = [("Standard", "Standard")]
                        if STREAMING_ALLOWED:
                            speed_choices.append(("Block Streaming (Preview)", "Streaming"))

                        speed_submode = gr.Radio(
                            choices=speed_choices,
                            value=speed_submode_value,
                            label="Accelerated Options",
                            visible=DEFAULT_GENERATION_MODE != "Original (No Acceleration)",
                        )

                        btn_original = gr.Button(
                            "🎼 Generate Music (Original)",
                            variant="primary",
                            size="lg",
                            visible=DEFAULT_GENERATION_MODE == "Original (No Acceleration)",
                        )
                        btn_accel = gr.Button(
                            "🎼 Generate Music (Accelerated)",
                            variant="primary",
                            size="lg",
                            visible=DEFAULT_GENERATION_MODE != "Original (No Acceleration)"
                            and not show_stream_default,
                        )
                        btn_stream = gr.Button(
                            "🎼 Generate Music (Block Streaming)",
                            variant="primary",
                            size="lg",
                            visible=show_stream_default,
                        )
                        cancel_stream_btn = gr.Button(
                            "Cancel Streaming",
                            variant="secondary",
                            size="lg",
                            visible=show_stream_default,
                        )
                        cancel_state = gr.State()

                    with gr.Column():
                        # Notice section
                        with gr.Accordion("Usage Notice", open=True):
                            gr.Markdown("""
### ZeroGPU Tips
- Default preset is **ZeroGPU AOTI FP8** for fastest H200 path
- Prefer **4-bit quantization** if FP8 or AOTI is unavailable
- Keep durations short (<= 60s) for faster first results
- **Streaming is block-based**, not token streaming: each block is ~29.76s
- Use Streaming for preview; Standard mode outputs a full downloadable file
- Try **FP8 + AOTI** (preset: ZeroGPU AOTI FP8) for the fastest H200 path
- ZeroGPU does not support generator streaming in forked GPU; streaming is for local deploy only

### Lyrics Format Requirements

**Automatic Processing:**
1. All text will be converted to **lowercase**
2. Timestamps (e.g., [00:12]) will be **automatically removed**
3. Leading/trailing whitespace on each line will be **stripped**
4. Leading/trailing empty lines will be **removed**
5. Multiple consecutive empty lines (3+) will be **collapsed to 2**

**Recommended Format:**
- Use standard song structure tags: `[Intro]`, `[Verse]`, `[Chorus]`, `[Bridge]`, `[Outro]`, etc.
- Separate sections with **blank lines**
- Case doesn't matter (will be auto-converted)

**Example:**
```
[Intro]

[Verse]
The sun creeps in across the floor
I hear the traffic outside the door

[Chorus]
Every day the light returns
Every day the fire burns
```

---

### Tags Format
- Use **commas** to separate multiple tags: `piano,happy,pop`
- Tags influence the style and mood of the generated music
- Select from categories below or type directly
                            """)

                        output_audio_file = gr.Audio(
                            label="Generated Music (Full)",
                            type="filepath",
                            interactive=False,
                            visible=not show_stream_default,
                        )
                        stream_audio = gr.Audio(
                            label="Block Streaming Audio (Preview)",
                            streaming=True,
                            autoplay=True,
                            type="numpy",
                            format="wav",
                            interactive=False,
                            visible=show_stream_default,
                        )

                # Event handlers for tag selection
                for cb in tag_checkboxes:
                    cb.change(fn=update_tag_string, inputs=tag_checkboxes, outputs=tags)

                # Button callbacks
                format_btn.click(
                    fn=process_lyrics_correct,
                    inputs=[lyrics],
                    outputs=[lyrics]
                )

                def update_visibility(gen_mode, spd_mode):
                    if gen_mode == "Original (No Acceleration)":
                        return (
                            gr.update(visible=False),  # speed_submode
                            gr.update(visible=True),  # btn_original
                            gr.update(visible=False),  # btn_accel
                            gr.update(visible=False),  # btn_stream
                            gr.update(visible=False),  # cancel_stream_btn
                            gr.update(visible=True),  # output_format
                            gr.update(visible=True),  # output_audio_file
                            gr.update(visible=False),  # stream_audio
                        )
                    show_stream = STREAMING_ALLOWED and spd_mode == "Streaming"
                    return (
                        gr.update(visible=True),  # speed_submode
                        gr.update(visible=False),  # btn_original
                        gr.update(visible=not show_stream),  # btn_accel
                        gr.update(visible=show_stream),  # btn_stream
                        gr.update(visible=show_stream),  # cancel_stream_btn
                        gr.update(visible=not show_stream),  # output_format
                        gr.update(visible=not show_stream),  # output_audio_file
                        gr.update(visible=show_stream),  # stream_audio
                    )

                def apply_preset(preset_name):
                    preset = PRESET_CONFIGS.get(preset_name, PRESET_CONFIGS[DEFAULT_PRESET])
                    return (
                        gr.update(value=preset["cfg_scale"]),
                        gr.update(value=preset["duration"]),
                        gr.update(value=preset["temperature"]),
                        gr.update(value=preset["topk"]),
                        gr.update(value=preset["quant_mode"]),
                        gr.update(value=preset["keep_model_loaded"]),
                        gr.update(value=preset["offload_mode"]),
                        gr.update(value=preset["generation_mode"]),
                        gr.update(value=preset["speed_submode"]),
                    )

                generation_mode.change(
                    fn=update_visibility,
                    inputs=[generation_mode, speed_submode],
                    outputs=[
                        speed_submode,
                        btn_original,
                        btn_accel,
                        btn_stream,
                        cancel_stream_btn,
                        output_format,
                        output_audio_file,
                        stream_audio,
                    ],
                )
                speed_submode.change(
                    fn=update_visibility,
                    inputs=[generation_mode, speed_submode],
                    outputs=[
                        speed_submode,
                        btn_original,
                        btn_accel,
                        btn_stream,
                        cancel_stream_btn,
                        output_format,
                        output_audio_file,
                        stream_audio,
                    ],
                )

                preset_event = apply_preset_btn.click(
                    fn=apply_preset,
                    inputs=[preset_selector],
                    outputs=[
                        cfg_scale,
                        duration,
                        temperature,
                        topk,
                        quant_mode,
                        keep_model_loaded,
                        offload_mode,
                        generation_mode,
                        speed_submode,
                    ],
                )

                preset_event.then(
                    fn=update_visibility,
                    inputs=[generation_mode, speed_submode],
                    outputs=[
                        speed_submode,
                        btn_original,
                        btn_accel,
                        btn_stream,
                        cancel_stream_btn,
                        output_format,
                        output_audio_file,
                        stream_audio,
                    ],
                )

                btn_original.click(
                    fn=generate_original,
                    inputs=[
                        lyrics,
                        tags,
                        cfg_scale,
                        duration,
                        temperature,
                        topk,
                        version,
                        codec_version,
                        quant_mode,
                        output_format,
                        keep_model_loaded,
                        offload_mode,
                        backend,
                    ],
                    outputs=[output_audio_file],
                    concurrency_id="gpu_queue",
                    concurrency_limit=GPU_CONCURRENCY_LIMIT,
                )

                btn_accel.click(
                    fn=generate_accelerated,
                    inputs=[
                        lyrics,
                        tags,
                        cfg_scale,
                        duration,
                        temperature,
                        topk,
                        version,
                        codec_version,
                        quant_mode,
                        output_format,
                        keep_model_loaded,
                        offload_mode,
                        backend,
                    ],
                    outputs=[output_audio_file],
                    concurrency_id="gpu_queue",
                    concurrency_limit=GPU_CONCURRENCY_LIMIT,
                )

                stream_event = btn_stream.click(
                    fn=stream_generate_accelerated,
                    inputs=[
                        lyrics,
                        tags,
                        cfg_scale,
                        duration,
                        temperature,
                        topk,
                        version,
                        codec_version,
                        quant_mode,
                        keep_model_loaded,
                        offload_mode,
                        backend,
                    ],
                    outputs=[stream_audio],
                    concurrency_id="gpu_queue",
                    concurrency_limit=GPU_CONCURRENCY_LIMIT,
                )

                cancel_stream_btn.click(
                    fn=lambda: None,
                    inputs=None,
                    outputs=[cancel_state],
                    cancels=[stream_event],
                )

            # Tab 2: Lyrics Generation
            with gr.Tab("Lyrics Generation"):
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("### Generate Lyrics with AI")

                        api_selector = gr.Radio(
                            choices=[
                                ("Google Gemini", "gemini"),
                                ("OpenAI", "openai"),
                                ("DeepSeek", "deepseek"),
                                ("Custom (OpenAI-compatible)", "custom")
                            ],
                            value="gemini",
                            label="Select LLM Provider"
                        )

                        with gr.Accordion("API Configuration", open=True):
                            api_key_input = gr.Textbox(
                                label="API Key (Required)",
                                type="password",
                                placeholder="Enter your API key or set environment variable",
                                info="Will use environment variable if not provided here"
                            )

                            custom_base_url = gr.Textbox(
                                label="Custom Base URL (Optional)",
                                placeholder="e.g., https://api.your-provider.com/v1",
                                info="Leave empty to use default. For custom providers only.",
                                visible=False
                            )

                            custom_model = gr.Textbox(
                                label="Model Name (Optional)",
                                placeholder="e.g., gpt-4o, deepseek-chat",
                                info="Leave empty to use recommended default",
                                visible=False
                            )

                        def update_custom_fields(choice):
                            """Show/hide custom fields based on API choice"""
                            if choice == "custom":
                                return gr.update(visible=True), gr.update(visible=True)
                            elif choice == "deepseek":
                                return gr.update(visible=False), gr.update(visible=True)
                            else:
                                return gr.update(visible=False), gr.update(visible=False)

                        api_selector.change(
                            fn=update_custom_fields,
                            inputs=[api_selector],
                            outputs=[custom_base_url, custom_model]
                        )

                        theme_input = gr.Textbox(
                            label="Theme",
                            placeholder="e.g., Love lost in the city, Dreams and hope, Rainy day memories...",
                            lines=2
                        )

                        tags_gen = gr.Textbox(
                            label="Music Style/Tags",
                            placeholder="e.g., piano, melancholy, pop",
                            value="pop,emotional"
                        )

                        language_select = gr.Radio(
                            choices=[
                                ("English", "en"),
                                ("中文 (Chinese)", "zh"),
                                ("日本語 (Japanese)", "jp"),
                                ("한국어 (Korean)", "kr"),
                                ("Español (Spanish)", "sp")
                            ],
                            value="en",
                            label="Language"
                        )

                        generate_lyrics_btn = gr.Button(
                            "Generate Lyrics",
                            variant="primary",
                            size="lg"
                        )

                    with gr.Column():
                        with gr.Accordion("How to Use", open=True):
                            gr.Markdown("""
### How to Generate Lyrics

**Theme**: Describe your song's story or emotion
- Examples: "Lost love in Tokyo", "Overcoming obstacles", "Summer road trip"

**Music Style/Tags**: Define mood and genre
- Examples: "piano,melancholy,ballad", "upbeat,electronic,dance"

**Tips**
- Generated lyrics follow standard song structure ([Intro], [Verse], [Chorus], etc.)
- Edit lyrics before using for music generation
- Be specific with themes for better results

                            """)

                        generated_lyrics_output = gr.Textbox(
                            label="Generated Lyrics",
                            lines=20,
                            placeholder="Generated lyrics will appear here...",
                            interactive=False
                        )

                        copy_to_music_gen = gr.Button(
                            "Copy to Music Generation Tab",
                            size="sm"
                        )

                # Lyrics generation button callback
                generate_lyrics_btn.click(
                    fn=generate_lyrics,
                    inputs=[theme_input, tags_gen, language_select, api_selector, api_key_input, custom_base_url, custom_model],
                    outputs=[generated_lyrics_output]
                )

                # Copy lyrics to music generation tab
                def copy_lyrics(lyrics_text):
                    return lyrics_text

                copy_to_music_gen.click(
                    fn=copy_lyrics,
                    inputs=[generated_lyrics_output],
                    outputs=[lyrics]
                )

    return demo


demo = create_ui()
demo.queue(max_size=GRADIO_QUEUE_MAX_SIZE, default_concurrency_limit=GRADIO_DEFAULT_CONCURRENCY)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", "7860"))
    demo.launch(
        server_name="0.0.0.0",
        server_port=port,
        allowed_paths=[str(DATA_DIR), "/tmp"],
        ssr_mode=False,
    )