File size: 146,036 Bytes
6cf4573
 
 
 
 
 
 
 
 
 
b3f7f32
d9fa683
8ca132a
09846c9
02a1f95
c06e566
6cf4573
 
8375700
c06e566
7592f82
6cf4573
40ba3a7
d0e121d
8ca132a
8697d49
8ca132a
 
 
6d556cb
8ca132a
6cf4573
 
 
eaedb53
6cf4573
 
 
d0e121d
7592f82
6cf4573
 
7592f82
6cf4573
 
 
 
7592f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebde550
6cf4573
 
 
 
e7175d4
ac67bf3
e7175d4
 
 
 
ac67bf3
e7175d4
 
 
 
 
 
 
 
 
 
ac67bf3
e7175d4
ac67bf3
e7175d4
 
ac67bf3
e7175d4
7592f82
6cf4573
8375700
 
51979c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8375700
 
 
 
 
 
 
 
 
 
 
 
6cf4573
51979c2
d0e121d
 
 
51979c2
 
d0e121d
6cf4573
 
d0e121d
d9fa683
 
 
 
 
 
6cf4573
 
160db86
 
 
51979c2
09846c9
 
6cf4573
 
160db86
ae7de9b
 
 
8429f30
 
 
 
ae7de9b
 
 
 
 
 
 
7a2a7f5
 
 
 
 
34affeb
 
 
 
 
 
 
ae7de9b
 
 
 
 
 
 
 
 
065a652
8429f30
ae7de9b
 
8429f30
 
ae7de9b
 
 
 
c3068ef
a4226e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51979c2
a4226e1
 
 
 
 
 
 
 
 
 
51979c2
a4226e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00e0606
 
 
 
a4226e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51979c2
 
 
a4226e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3272260
 
 
 
 
 
 
 
 
 
 
 
e071ca4
 
3272260
e071ca4
 
3272260
7827e41
 
 
 
7592f82
6cf4573
 
72afd74
 
 
 
 
51979c2
9f07d3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf7a39
72afd74
 
9f07d3f
 
 
 
 
 
72afd74
 
585a112
 
 
1afc0fb
 
 
 
 
 
585a112
 
 
 
72afd74
585a112
 
 
72afd74
 
585a112
1afc0fb
 
585a112
 
1afc0fb
585a112
72afd74
 
6cf4573
 
 
 
 
 
 
 
 
 
 
7592f82
 
 
 
 
 
 
 
 
 
 
 
 
 
6cf4573
7592f82
 
 
07afd9c
c06e566
 
7592f82
 
 
 
c06e566
 
7592f82
c06e566
 
7592f82
 
 
 
c06e566
 
7592f82
c06e566
 
7592f82
c06e566
7592f82
 
c06e566
 
7592f82
c06e566
 
 
7592f82
 
 
 
 
 
 
 
 
 
 
 
 
 
c06e566
 
 
 
7592f82
c06e566
7592f82
c06e566
 
 
 
 
 
 
 
7592f82
c06e566
7592f82
 
c06e566
 
 
7592f82
c06e566
 
7592f82
 
c06e566
09846c9
 
a4226e1
6cf4573
 
 
8697d49
6cf4573
09846c9
160db86
 
 
07afd9c
e7175d4
c06e566
 
 
07afd9c
 
6cf4573
 
 
 
 
 
 
 
 
 
160db86
6cf4573
 
 
eaedb53
6cf4573
eaedb53
 
0429f8a
6cf4573
0429f8a
6cf4573
 
0429f8a
6cf4573
 
 
 
 
 
 
 
 
 
 
0429f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
6cf4573
 
 
 
0429f8a
6cf4573
0429f8a
 
166fb8e
0429f8a
 
efe424b
fe18eeb
efe424b
fe18eeb
 
 
efe424b
fe18eeb
 
efe424b
 
3272260
 
 
efe424b
3272260
 
efe424b
3272260
 
 
fe18eeb
 
 
 
 
3272260
fe18eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbba693
efe424b
 
51979c2
9f07d3f
 
51979c2
9f07d3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
585a112
9f07d3f
51979c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fa90c
51979c2
93fa90c
 
 
 
 
 
 
51979c2
 
 
 
 
 
93fa90c
51979c2
 
 
 
 
 
 
 
 
 
3272260
 
 
 
93fa90c
3272260
 
 
 
 
 
 
 
 
 
 
 
 
 
9f79da8
3272260
 
9f79da8
3272260
9f07d3f
3272260
 
9f79da8
3272260
 
9f79da8
3272260
 
 
 
 
 
 
93fa90c
3272260
 
9f79da8
3272260
 
 
51979c2
 
 
 
 
 
 
 
 
 
160db86
 
07afd9c
13cc4e6
e7175d4
13cc4e6
 
d9fa683
eaedb53
160db86
211f7b6
160db86
51979c2
25f6ae6
51979c2
a4226e1
 
6cf4573
e7175d4
9f79da8
13cc4e6
 
 
 
9f79da8
13cc4e6
8635f79
a4226e1
8635f79
160db86
8635f79
51979c2
 
8635f79
51979c2
 
 
 
 
 
8635f79
51979c2
8635f79
160db86
13cc4e6
160db86
 
6cf4573
160db86
a4226e1
 
 
6cf4573
13cc4e6
160db86
 
 
5aeadc9
160db86
6cf4573
 
160db86
 
 
 
 
 
 
 
 
51979c2
 
a4226e1
 
 
 
13cc4e6
 
12556c0
 
 
 
 
9f79da8
13cc4e6
 
 
 
 
 
 
9f79da8
13cc4e6
 
 
 
 
51979c2
 
9f79da8
13cc4e6
e7175d4
13cc4e6
 
e152b28
13cc4e6
 
 
e7175d4
13cc4e6
 
3272260
d141e30
 
3272260
 
 
 
 
fe18eeb
3272260
d141e30
 
 
3272260
 
 
9f79da8
3272260
9f79da8
160db86
3272260
 
 
 
 
 
 
 
93fa90c
3272260
 
9f79da8
 
 
 
160db86
 
6cf4573
 
 
 
 
 
 
 
 
 
 
160db86
bdf9957
07afd9c
 
4d5093e
cc23c05
 
 
 
 
 
c06e566
 
07afd9c
 
 
13cc4e6
4d5093e
e3d955b
 
13cc4e6
4d5093e
 
e3d955b
 
 
 
 
 
 
4d5093e
00ee17c
51979c2
13cc4e6
6cf4573
 
d9fa683
679c604
da46dda
679c604
6cf4573
51979c2
6cf4573
a4226e1
6cf4573
e3d955b
 
 
 
 
 
 
 
 
 
 
 
4d5093e
 
 
 
 
 
bdf9957
 
 
13cc4e6
6cf4573
 
d9fa683
6cf4573
13cc4e6
5aeadc9
3272260
bdf9957
 
 
a4226e1
bdf9957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166fb8e
bdf9957
 
51979c2
 
13cc4e6
 
d141e30
 
 
 
13cc4e6
 
 
 
 
 
 
 
 
 
 
 
51979c2
 
13cc4e6
5aeadc9
13cc4e6
 
4c173d1
4d5093e
e3d955b
13cc4e6
 
3272260
 
 
fe18eeb
 
 
 
3272260
160db86
3272260
 
 
 
 
93fa90c
3272260
6cf4573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07afd9c
 
ac67bf3
cc23c05
 
 
 
 
 
c06e566
 
07afd9c
 
 
13cc4e6
4c173d1
e3d955b
 
13cc4e6
4d5093e
 
e3d955b
 
4d5093e
51979c2
a4226e1
6cf4573
 
d9fa683
6cf4573
679c604
4d5093e
d9fa683
6cf4573
51979c2
 
6cf4573
a4226e1
6cf4573
e3d955b
 
 
 
 
 
 
 
 
 
 
4d5093e
 
 
 
 
 
 
 
 
 
6cf4573
13cc4e6
2b2a599
13cc4e6
6cf4573
 
 
 
 
2b2a599
d141e30
 
 
 
13cc4e6
6cf4573
2b2a599
13cc4e6
5aeadc9
2b2a599
 
13cc4e6
2b2a599
d141e30
 
2b2a599
 
 
 
 
 
 
 
 
 
 
 
 
166fb8e
2b2a599
 
51979c2
 
13cc4e6
5aeadc9
d141e30
 
 
 
13cc4e6
 
 
 
 
 
 
 
 
 
 
 
51979c2
 
13cc4e6
 
 
 
 
4d5093e
e3d955b
13cc4e6
 
3272260
 
 
 
 
 
 
 
 
 
 
93fa90c
3272260
 
6cf4573
 
 
02a1f95
 
 
 
 
 
 
 
 
 
3272260
02a1f95
 
 
 
a4226e1
02a1f95
 
 
 
 
93fa90c
02a1f95
 
 
9f07d3f
02a1f95
4aa0f7d
 
d141e30
02a1f95
4aa0f7d
 
 
 
51979c2
02a1f95
4aa0f7d
 
 
 
3272260
02a1f95
a4226e1
 
02a1f95
a4226e1
02a1f95
 
 
a4226e1
bb533eb
 
8ddbfd1
41cd582
 
02a1f95
 
 
 
 
e7175d4
ef4f0ff
 
 
 
 
 
 
 
 
 
 
 
 
c06e566
02a1f95
 
 
13cc4e6
 
e7175d4
13cc4e6
02a1f95
 
 
 
 
51979c2
02a1f95
51979c2
a4226e1
02a1f95
4c173d1
 
 
 
 
 
 
a4226e1
 
 
 
93fa90c
d9fa683
a4226e1
 
51979c2
 
 
02a1f95
a4226e1
02a1f95
 
 
13cc4e6
02a1f95
 
 
 
 
 
13cc4e6
 
 
 
 
 
 
 
4c173d1
13cc4e6
 
 
 
fe18eeb
 
0bc4a35
02a1f95
0bc4a35
02a1f95
 
 
 
 
 
ac67bf3
e7175d4
c06e566
02a1f95
 
 
13cc4e6
 
4c173d1
e7175d4
13cc4e6
02a1f95
 
 
 
 
51979c2
a4226e1
 
02a1f95
51979c2
02a1f95
a4226e1
13cc4e6
02a1f95
d5399ac
 
 
93fa90c
d9fa683
d5399ac
02a1f95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166fb8e
13cc4e6
02a1f95
13cc4e6
 
 
 
 
 
 
 
d5399ac
13cc4e6
 
4c173d1
e7175d4
13cc4e6
fe18eeb
 
 
 
 
13cc4e6
 
 
02a1f95
 
 
 
 
 
 
 
 
e7175d4
c06e566
02a1f95
 
 
13cc4e6
 
 
e7175d4
13cc4e6
02a1f95
 
 
 
 
51979c2
a4226e1
 
02a1f95
51979c2
a4226e1
02a1f95
 
 
d5399ac
 
93fa90c
d9fa683
d5399ac
02a1f95
d5399ac
4c173d1
 
a4226e1
 
4c173d1
a4226e1
 
 
 
 
 
02a1f95
 
 
 
 
 
166fb8e
13cc4e6
 
 
 
 
 
 
 
 
 
 
d5399ac
13cc4e6
 
 
e7175d4
02a1f95
13cc4e6
02a1f95
13cc4e6
02a1f95
 
 
 
 
 
c06e566
 
 
 
 
 
b60f330
 
 
 
 
 
 
 
 
 
 
121a071
 
3272260
 
121a071
 
 
3272260
 
121a071
3272260
 
 
121a071
 
 
 
 
 
 
 
b60f330
 
93fa90c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47fd0ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51979c2
47fd0ad
 
51979c2
47fd0ad
 
 
 
 
 
 
 
51979c2
 
 
47fd0ad
d9fa683
 
 
 
47fd0ad
 
d9fa683
47fd0ad
 
 
 
 
 
 
 
51979c2
 
 
 
 
 
7592f82
 
 
 
 
47fd0ad
 
7592f82
 
 
 
 
00ee17c
7592f82
 
 
 
00ee17c
 
 
 
 
 
 
 
47fd0ad
00ee17c
7592f82
 
 
b60f330
 
2fdad19
 
47fd0ad
121a071
7592f82
121a071
7592f82
47fd0ad
 
 
93fa90c
47fd0ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3272260
7592f82
b60f330
 
 
 
2fdad19
 
47fd0ad
b60f330
47fd0ad
b60f330
7592f82
47fd0ad
e3d955b
4d5093e
00ee17c
e3d955b
 
 
 
 
 
00ee17c
47fd0ad
 
 
 
 
 
 
b60f330
7592f82
b60f330
 
 
 
 
2fdad19
 
47fd0ad
b60f330
47fd0ad
b60f330
7592f82
47fd0ad
e3d955b
4d5093e
e3d955b
4d5093e
 
e3d955b
 
47fd0ad
 
 
 
7592f82
 
b60f330
 
6cf4573
 
 
 
c06e566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3cec42
c06e566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cf4573
02a1f95
 
 
 
 
a4226e1
02a1f95
160db86
 
 
02a1f95
160db86
02a1f95
160db86
211f7b6
 
02a1f95
 
 
 
8375700
 
 
 
 
 
 
51979c2
8375700
 
 
3c63946
 
 
 
 
 
 
8375700
 
3c63946
 
 
 
8375700
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a1f95
bb533eb
41cd582
 
504a99f
4aa0f7d
 
504a99f
 
 
02a1f95
9f79da8
02a1f95
9f79da8
02a1f95
 
a4226e1
 
 
02a1f95
 
51979c2
02a1f95
fcc4220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a1f95
 
fcc4220
 
504a99f
41cd582
504a99f
02a1f95
41d322f
 
 
504a99f
f300658
41d322f
 
 
 
 
 
 
 
 
f300658
41d322f
f300658
41d322f
 
 
 
 
 
fcc4220
41d322f
 
 
 
02a1f95
fcc4220
 
 
 
 
504a99f
 
 
fcc4220
504a99f
fcc4220
 
504a99f
 
 
 
 
4d5093e
 
 
 
 
 
 
 
504a99f
4d5093e
 
 
 
504a99f
0378fbe
504a99f
 
 
 
 
 
fcc4220
504a99f
 
 
fcc4220
 
 
 
 
 
504a99f
fcc4220
 
504a99f
 
 
fcc4220
 
 
 
 
504a99f
 
 
41cd582
52e1535
 
41cd582
 
52e1535
 
977875e
 
41cd582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc4220
 
 
 
52e1535
 
fcc4220
0378fbe
52e1535
 
 
0378fbe
f300658
fcc4220
504a99f
 
 
 
 
 
fcc4220
504a99f
fcc4220
 
 
 
 
 
 
 
 
 
4aa0f7d
 
41d322f
 
fcc4220
41d322f
 
 
 
 
 
 
 
 
 
4aa0f7d
41d322f
 
 
 
 
 
 
 
 
fcc4220
4aa0f7d
a4226e1
 
 
 
 
 
 
 
 
fcc4220
 
 
 
 
 
 
 
 
95fa2df
fcc4220
a4226e1
 
02a1f95
 
fcc4220
 
 
 
51979c2
 
bb533eb
fcc4220
 
bb533eb
 
fcc4220
 
 
 
 
 
 
 
 
4afe095
a4226e1
fcc4220
 
8ddbfd1
 
 
 
 
fcc4220
 
 
 
02a1f95
 
 
 
 
 
bb533eb
 
 
 
 
 
02a1f95
bb533eb
8697d49
34cf138
8697d49
34cf138
e792114
 
02a1f95
 
34cf138
02a1f95
8697d49
bb533eb
8697d49
 
02a1f95
 
 
 
bb533eb
9ef3cf6
 
bb533eb
 
02a1f95
8697d49
9f79da8
02a1f95
 
 
 
 
 
9f79da8
02a1f95
 
bb533eb
 
 
8ddbfd1
41cd582
 
02a1f95
 
 
 
 
bb533eb
8697d49
 
6cf4573
 
 
 
 
 
 
 
 
 
8ca132a
6cf4573
 
 
 
 
 
 
 
 
1cf1eb7
782f393
1cf1eb7
2648727
 
 
1cf1eb7
 
2648727
 
 
 
 
 
1b4e6b8
 
 
 
 
 
 
1cf1eb7
42b7d7f
41d322f
 
 
 
bb533eb
41d322f
 
 
af578ae
 
 
 
 
 
 
 
8b3b191
af578ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b3b191
af578ae
 
 
 
 
 
 
 
 
8b3b191
401978a
83e14c2
bb533eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b60f330
 
 
 
63065b2
 
 
 
 
 
 
b60f330
bb533eb
 
b60f330
 
 
bb533eb
 
 
 
 
 
 
 
 
b60f330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb533eb
b60f330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d916fd2
bb533eb
ff59c3d
 
 
 
 
 
 
 
 
34cf138
 
 
 
0224191
 
 
 
 
af578ae
 
 
 
8b3b191
af578ae
 
 
 
 
03cbad9
0691643
03cbad9
 
8b3b191
 
 
 
 
 
0bc4a35
8b3b191
 
 
 
03cbad9
 
 
 
 
 
 
 
 
8b3b191
af578ae
bb533eb
 
34cf138
 
0219f72
 
 
 
 
 
 
 
 
 
 
 
 
ff59c3d
 
110c8ab
ff59c3d
c71e56e
 
ff59c3d
c71e56e
 
ff59c3d
110c8ab
ff59c3d
 
 
 
 
 
 
0224191
 
 
 
 
 
 
 
 
34cf138
 
 
1ccc3d0
 
0224191
 
 
 
 
34cf138
0219f72
1ccc3d0
 
0224191
 
1ccc3d0
 
34cf138
 
 
1ccc3d0
 
34cf138
 
 
0224191
 
63065b2
0224191
 
 
 
 
 
3272260
0224191
 
 
 
 
 
 
 
63065b2
3272260
 
63065b2
 
 
0224191
 
 
63065b2
3272260
 
0224191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0219f72
34cf138
 
0224191
 
63065b2
34cf138
 
63065b2
 
 
41d322f
 
 
 
 
 
 
 
 
 
b60f330
 
41d322f
 
b60f330
 
 
 
 
41d322f
b60f330
 
 
 
 
 
 
 
1bc8988
 
41d322f
 
 
 
 
 
 
 
 
 
f300658
41d322f
 
 
 
 
 
 
 
bb533eb
41d322f
 
 
 
 
 
 
 
 
 
 
 
 
160db86
1044cda
6cf4573
36c1b45
 
 
 
 
160db86
d0e121d
6cf4573
 
 
 
 
 
 
2648727
ae7de9b
5031a07
86e179e
bb533eb
 
5cf7a39
bb533eb
dec15b9
 
985bfa2
2648727
02a1f95
bb533eb
6cf4573
df3bd13
 
 
 
 
 
00c5735
e0917de
6cf4573
 
e0917de
6cf4573
 
 
 
 
 
 
d0e121d
6cf4573
9f79da8
 
 
 
 
 
 
 
 
 
 
 
6cf4573
9ef3cf6
 
 
6cf4573
e0917de
6cf4573
bb533eb
9ef3cf6
 
 
 
 
53f384c
c06e566
 
 
 
 
 
 
 
02a1f95
6cf4573
 
 
 
 
2648727
ae7de9b
bb533eb
86e179e
5031a07
bb533eb
 
5cf7a39
bb533eb
dec15b9
 
985bfa2
2648727
02a1f95
bb533eb
6cf4573
df3bd13
 
 
 
00c5735
6cf4573
 
 
 
 
160db86
95c5c55
02a1f95
 
6cf4573
9ef3cf6
6cf4573
e0917de
95c5c55
bb533eb
9ef3cf6
 
 
 
 
6cf4573
c06e566
 
 
 
 
 
02a1f95
6cf4573
 
 
 
 
2648727
ae7de9b
bb533eb
 
5031a07
bb533eb
 
 
5cf7a39
bb533eb
dec15b9
 
985bfa2
2648727
02a1f95
bb533eb
6cf4573
df3bd13
 
 
 
00c5735
6cf4573
 
 
 
 
 
95c5c55
02a1f95
 
6cf4573
9ef3cf6
6cf4573
e0917de
95c5c55
bb533eb
9ef3cf6
 
 
 
 
d0e121d
c06e566
 
 
 
 
 
02a1f95
ae7de9b
 
 
 
 
 
 
 
 
e0917de
 
 
 
 
 
b60f330
 
 
 
 
 
 
84cc585
 
 
 
 
 
 
 
b60f330
 
 
 
 
 
 
 
 
 
 
 
 
84cc585
b60f330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cc585
b60f330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cc585
b60f330
 
 
0691643
af578ae
 
401978a
d2864d0
d9929fb
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
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
"""
Generate Audio for Video β€” multi-model Gradio app.

Supported models
----------------
  TARO          – video-conditioned diffusion via CAVP + onset features (16 kHz, 8.192 s window)
  MMAudio       – multimodal flow-matching with CLIP/Synchformer + text prompt (44 kHz, 8 s window)
  HunyuanFoley  – text-guided foley via SigLIP2 + Synchformer + CLAP (48 kHz, up to 15 s)
"""

import html as _html
import math
import os
import sys
import json
import shutil
import tempfile
import random
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path

import torch
import numpy as np
import torchaudio
import ffmpeg
import spaces
import gradio as gr
from huggingface_hub import hf_hub_download, snapshot_download

# ================================================================== #
#                     CHECKPOINT CONFIGURATION                        #
# ================================================================== #

CKPT_REPO_ID = "JackIsNotInTheBox/Generate_Audio_for_Video_Checkpoints"
CACHE_DIR    = "/tmp/model_ckpts"
os.makedirs(CACHE_DIR, exist_ok=True)

# ---- Local directories that must exist before parallel downloads start ----
MMAUDIO_WEIGHTS_DIR  = Path(CACHE_DIR) / "MMAudio" / "weights"
MMAUDIO_EXT_DIR      = Path(CACHE_DIR) / "MMAudio" / "ext_weights"
HUNYUAN_MODEL_DIR    = Path(CACHE_DIR) / "HunyuanFoley"
MMAUDIO_WEIGHTS_DIR.mkdir(parents=True, exist_ok=True)
MMAUDIO_EXT_DIR.mkdir(parents=True, exist_ok=True)
HUNYUAN_MODEL_DIR.mkdir(parents=True, exist_ok=True)

# ------------------------------------------------------------------ #
# Parallel checkpoint + model downloads                               #
# All downloads are I/O-bound (network), so running them in threads   #
# cuts Space cold-start time roughly proportional to the number of   #
# independent groups (previously sequential, now concurrent).         #
# hf_hub_download / snapshot_download are thread-safe.               #
# ------------------------------------------------------------------ #

def _dl_taro():
    """Download TARO .ckpt/.pt files and return their local paths."""
    c = hf_hub_download(repo_id=CKPT_REPO_ID, filename="TARO/cavp_epoch66.ckpt", cache_dir=CACHE_DIR)
    o = hf_hub_download(repo_id=CKPT_REPO_ID, filename="TARO/onset_model.ckpt",  cache_dir=CACHE_DIR)
    t = hf_hub_download(repo_id=CKPT_REPO_ID, filename="TARO/taro_ckpt.pt",      cache_dir=CACHE_DIR)
    print("TARO checkpoints downloaded.")
    return c, o, t

def _dl_mmaudio():
    """Download MMAudio .pth files and return their local paths."""
    m = hf_hub_download(repo_id=CKPT_REPO_ID, filename="MMAudio/mmaudio_large_44k_v2.pth",
                        cache_dir=CACHE_DIR, local_dir=str(MMAUDIO_WEIGHTS_DIR), local_dir_use_symlinks=False)
    v = hf_hub_download(repo_id=CKPT_REPO_ID, filename="MMAudio/v1-44.pth",
                        cache_dir=CACHE_DIR, local_dir=str(MMAUDIO_EXT_DIR), local_dir_use_symlinks=False)
    s = hf_hub_download(repo_id=CKPT_REPO_ID, filename="MMAudio/synchformer_state_dict.pth",
                        cache_dir=CACHE_DIR, local_dir=str(MMAUDIO_EXT_DIR), local_dir_use_symlinks=False)
    print("MMAudio checkpoints downloaded.")
    return m, v, s

def _dl_hunyuan():
    """Download HunyuanVideoFoley .pth files."""
    hf_hub_download(repo_id=CKPT_REPO_ID, filename="HunyuanVideo-Foley/hunyuanvideo_foley.pth",
                    cache_dir=CACHE_DIR, local_dir=str(HUNYUAN_MODEL_DIR), local_dir_use_symlinks=False)
    hf_hub_download(repo_id=CKPT_REPO_ID, filename="HunyuanVideo-Foley/vae_128d_48k.pth",
                    cache_dir=CACHE_DIR, local_dir=str(HUNYUAN_MODEL_DIR), local_dir_use_symlinks=False)
    hf_hub_download(repo_id=CKPT_REPO_ID, filename="HunyuanVideo-Foley/synchformer_state_dict.pth",
                    cache_dir=CACHE_DIR, local_dir=str(HUNYUAN_MODEL_DIR), local_dir_use_symlinks=False)
    print("HunyuanVideoFoley checkpoints downloaded.")

def _dl_clap():
    """Pre-download CLAP so from_pretrained() hits local cache inside the ZeroGPU worker."""
    snapshot_download(repo_id="laion/larger_clap_general")
    print("CLAP model pre-downloaded.")

def _dl_clip():
    """Pre-download MMAudio's CLIP model (~3.95 GB) to avoid GPU-window budget drain."""
    snapshot_download(repo_id="apple/DFN5B-CLIP-ViT-H-14-384")
    print("MMAudio CLIP model pre-downloaded.")

def _dl_audioldm2():
    """Pre-download AudioLDM2 VAE/vocoder used by TARO's from_pretrained() calls."""
    snapshot_download(repo_id="cvssp/audioldm2")
    print("AudioLDM2 pre-downloaded.")

def _dl_bigvgan():
    """Pre-download BigVGAN vocoder (~489 MB) used by MMAudio."""
    snapshot_download(repo_id="nvidia/bigvgan_v2_44khz_128band_512x")
    print("BigVGAN vocoder pre-downloaded.")

print("[startup] Starting parallel checkpoint + model downloads…")
_t_dl_start = time.perf_counter()
with ThreadPoolExecutor(max_workers=7) as _pool:
    _fut_taro     = _pool.submit(_dl_taro)
    _fut_mmaudio  = _pool.submit(_dl_mmaudio)
    _fut_hunyuan  = _pool.submit(_dl_hunyuan)
    _fut_clap     = _pool.submit(_dl_clap)
    _fut_clip     = _pool.submit(_dl_clip)
    _fut_aldm2    = _pool.submit(_dl_audioldm2)
    _fut_bigvgan  = _pool.submit(_dl_bigvgan)
    # Raise any download exceptions immediately
    for _fut in as_completed([_fut_taro, _fut_mmaudio, _fut_hunyuan,
                               _fut_clap, _fut_clip, _fut_aldm2, _fut_bigvgan]):
        _fut.result()

cavp_ckpt_path, onset_ckpt_path, taro_ckpt_path = _fut_taro.result()
mmaudio_model_path, mmaudio_vae_path, mmaudio_synchformer_path = _fut_mmaudio.result()
print(f"[startup] All downloads done in {time.perf_counter() - _t_dl_start:.1f}s")

# ================================================================== #
#                     SHARED CONSTANTS / HELPERS                      #
# ================================================================== #

# CPU β†’ GPU context passing via function-name-keyed global store.
#
# Problem: ZeroGPU runs @spaces.GPU functions on its own worker thread, so
# threading.local() is invisible to the GPU worker.  Passing ctx as a
# function argument exposes it to Gradio's API endpoint, causing
# "Too many arguments" errors.
#
# Solution: store context in a plain global dict keyed by function name.
# A per-key Lock serialises concurrent callers for the same function
# (ZeroGPU is already synchronous β€” the wrapper blocks until the GPU fn
# returns β€” so in practice only one call per GPU fn is in-flight at a time).
# The global dict is readable from any thread.
_GPU_CTX: dict = {}
_GPU_CTX_LOCK  = threading.Lock()

def _ctx_store(fn_name: str, data: dict) -> None:
    """Store *data* under *fn_name* key (overwrites previous)."""
    with _GPU_CTX_LOCK:
        _GPU_CTX[fn_name] = data

def _ctx_load(fn_name: str) -> dict:
    """Pop and return the context dict stored under *fn_name*."""
    with _GPU_CTX_LOCK:
        return _GPU_CTX.pop(fn_name, {})

MAX_SLOTS = 8   # max parallel generation slots shown in UI
MAX_SEGS  = 8   # max segments per slot (same as MAX_SLOTS; video ≀ ~64 s at 8 s/seg)

# Segment overlay palette β€” shared between _build_waveform_html and _build_regen_pending_html
SEG_COLORS = [
    "rgba(100,180,255,{a})", "rgba(255,160,100,{a})",
    "rgba(120,220,140,{a})", "rgba(220,120,220,{a})",
    "rgba(255,220,80,{a})",  "rgba(80,220,220,{a})",
    "rgba(255,100,100,{a})", "rgba(180,255,180,{a})",
]

# ------------------------------------------------------------------ #
# Micro-helpers that eliminate repeated boilerplate across the file   #
# ------------------------------------------------------------------ #

def _ensure_syspath(subdir: str) -> str:
    """Add *subdir* (relative to app.py) to sys.path if not already present.
    Returns the absolute path for convenience."""
    p = os.path.join(os.path.dirname(os.path.abspath(__file__)), subdir)
    if p not in sys.path:
        sys.path.insert(0, p)
    return p


def _get_device_and_dtype() -> tuple:
    """Return (device, weight_dtype) pair used by all GPU functions."""
    device = "cuda" if torch.cuda.is_available() else "cpu"
    return device, torch.bfloat16


def _extract_segment_clip(silent_video: str, seg_start: float, seg_dur: float,
                          output_path: str) -> str:
    """Stream-copy a segment from *silent_video* to *output_path*. Returns *output_path*."""
    ffmpeg.input(silent_video, ss=seg_start, t=seg_dur).output(
        output_path, vcodec="copy", an=None
    ).run(overwrite_output=True, quiet=True)
    return output_path

# Per-slot reentrant locks β€” prevent concurrent regens on the same slot from
# producing a race condition where the second regen reads stale state
# (the shared seg_state textbox hasn't been updated yet by the first regen).
# Locks are keyed by slot_id string (e.g. "taro_0", "mma_2").
_SLOT_LOCKS: dict = {}
_SLOT_LOCKS_MUTEX = threading.Lock()

def _get_slot_lock(slot_id: str) -> threading.Lock:
    with _SLOT_LOCKS_MUTEX:
        if slot_id not in _SLOT_LOCKS:
            _SLOT_LOCKS[slot_id] = threading.Lock()
        return _SLOT_LOCKS[slot_id]

def set_global_seed(seed: int) -> None:
    np.random.seed(seed % (2**32))
    random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)

def get_random_seed() -> int:
    return random.randint(0, 2**32 - 1)

def _resolve_seed(seed_val) -> int:
    """Normalise seed_val to a non-negative int.
    Negative values (UI default 'random') produce a fresh random seed."""
    seed_val = int(seed_val)
    return seed_val if seed_val >= 0 else get_random_seed()

def get_video_duration(video_path: str) -> float:
    """Return video duration in seconds (CPU only)."""
    probe = ffmpeg.probe(video_path)
    return float(probe["format"]["duration"])

def strip_audio_from_video(video_path: str, output_path: str) -> None:
    """Write a silent copy of *video_path* to *output_path* (stream-copy, no re-encode)."""
    ffmpeg.input(video_path).output(output_path, vcodec="copy", an=None).run(
        overwrite_output=True, quiet=True
    )

def _transcode_for_browser(video_path: str) -> str:
    """Re-encode uploaded video to H.264/AAC MP4 so the browser preview widget can play it.

    Returns a NEW path in a fresh /tmp/gradio/ subdirectory. Gradio probes the
    returned path fresh, sees H.264, and serves it directly without its own
    slow fallback converter. The in-place overwrite approach loses the race
    because Gradio probes the original path at upload time before this callback runs.
    Only called on upload β€” not during generation.
    """
    if video_path is None:
        return video_path
    try:
        probe     = ffmpeg.probe(video_path)
        has_audio = any(s["codec_type"] == "audio" for s in probe.get("streams", []))
        # Check if already H.264 β€” skip transcode if so
        video_streams = [s for s in probe.get("streams", []) if s["codec_type"] == "video"]
        if video_streams and video_streams[0].get("codec_name") == "h264":
            print(f"[transcode_for_browser] already H.264, skipping")
            return video_path
        # Write the H.264 output into the SAME directory as the original upload.
        # Gradio's file server only allows paths under dirs it registered β€” the
        # upload dir is already allowed, so a sibling file there will serve fine.
        import os as _os
        upload_dir = _os.path.dirname(video_path)
        stem       = _os.path.splitext(_os.path.basename(video_path))[0]
        out_path   = _os.path.join(upload_dir, stem + "_h264.mp4")
        kwargs = dict(
            vcodec="libx264", preset="fast", crf=18,
            pix_fmt="yuv420p", movflags="+faststart",
        )
        if has_audio:
            kwargs["acodec"]        = "aac"
            kwargs["audio_bitrate"] = "128k"
        else:
            kwargs["an"] = None
        # map 0:v:0 explicitly to skip non-video streams (e.g. data/timecode tracks)
        ffmpeg.input(video_path).output(out_path, map="0:v:0", **kwargs).run(
            overwrite_output=True, quiet=True
        )
        print(f"[transcode_for_browser] transcoded to H.264: {out_path}")
        return out_path
    except Exception as e:
        print(f"[transcode_for_browser] failed, using original: {e}")
        return video_path


# ------------------------------------------------------------------ #
# Temp directory registry β€” tracks dirs for cleanup on new generation #
# ------------------------------------------------------------------ #
_TEMP_DIRS: list = []      # list of tmp_dir paths created by generate_*
_TEMP_DIRS_MAX  = 10       # keep at most this many; older ones get cleaned up

def _register_tmp_dir(tmp_dir: str) -> str:
    """Register a temp dir so it can be cleaned up when newer ones replace it."""
    _TEMP_DIRS.append(tmp_dir)
    while len(_TEMP_DIRS) > _TEMP_DIRS_MAX:
        old = _TEMP_DIRS.pop(0)
        try:
            shutil.rmtree(old, ignore_errors=True)
            print(f"[cleanup] Removed old temp dir: {old}")
        except Exception:
            pass
    return tmp_dir


def _save_seg_wavs(wavs: list[np.ndarray], tmp_dir: str, prefix: str) -> list[str]:
    """Save a list of numpy wav arrays to .npy files, return list of paths.
    This avoids serialising large float arrays into JSON/HTML data-state."""
    paths = []
    for i, w in enumerate(wavs):
        p = os.path.join(tmp_dir, f"{prefix}_seg{i}.npy")
        np.save(p, w)
        paths.append(p)
    return paths


def _load_seg_wavs(paths: list[str]) -> list[np.ndarray]:
    """Load segment wav arrays from .npy file paths."""
    return [np.load(p) for p in paths]


# ------------------------------------------------------------------ #
# Shared model-loading helpers (deduplicate generate / regen code)    #
# ------------------------------------------------------------------ #

def _load_taro_models(device, weight_dtype):
    """Load TARO MMDiT + AudioLDM2 VAE/vocoder. Returns (model_net, vae, vocoder, latents_scale)."""
    from TARO.models   import MMDiT
    from diffusers     import AutoencoderKL
    from transformers  import SpeechT5HifiGan

    model_net = MMDiT(adm_in_channels=120, z_dims=[768], encoder_depth=4).to(device)
    model_net.load_state_dict(torch.load(taro_ckpt_path, map_location=device, weights_only=False)["ema"])
    model_net.eval().to(weight_dtype)
    vae     = AutoencoderKL.from_pretrained("cvssp/audioldm2", subfolder="vae",
                                            local_files_only=True).to(device).eval()
    vocoder = SpeechT5HifiGan.from_pretrained("cvssp/audioldm2", subfolder="vocoder",
                                              local_files_only=True).to(device)
    latents_scale = torch.tensor([0.18215] * 8).view(1, 8, 1, 1).to(device)
    return model_net, vae, vocoder, latents_scale


def _load_taro_feature_extractors(device):
    """Load CAVP + onset extractors. Returns (extract_cavp, onset_model)."""
    from TARO.cavp_util  import Extract_CAVP_Features
    from TARO.onset_util import VideoOnsetNet

    extract_cavp = Extract_CAVP_Features(
        device=device, config_path="TARO/cavp/cavp.yaml", ckpt_path=cavp_ckpt_path,
    )
    raw_sd = torch.load(onset_ckpt_path, map_location=device, weights_only=False)["state_dict"]
    onset_sd = {}
    for k, v in raw_sd.items():
        if "model.net.model" in k:   k = k.replace("model.net.model", "net.model")
        elif "model.fc." in k:       k = k.replace("model.fc", "fc")
        onset_sd[k] = v
    onset_model = VideoOnsetNet(pretrained=False).to(device)
    onset_model.load_state_dict(onset_sd)
    onset_model.eval()
    return extract_cavp, onset_model


def _load_mmaudio_models(device, dtype):
    """Load MMAudio net + feature_utils. Returns (net, feature_utils, model_cfg, seq_cfg)."""
    from mmaudio.eval_utils               import all_model_cfg
    from mmaudio.model.networks            import get_my_mmaudio
    from mmaudio.model.utils.features_utils import FeaturesUtils

    model_cfg = all_model_cfg["large_44k_v2"]
    model_cfg.model_path       = Path(mmaudio_model_path)
    model_cfg.vae_path         = Path(mmaudio_vae_path)
    model_cfg.synchformer_ckpt = Path(mmaudio_synchformer_path)
    model_cfg.bigvgan_16k_path = None
    seq_cfg = model_cfg.seq_cfg

    net = get_my_mmaudio(model_cfg.model_name).to(device, dtype).eval()
    net.load_weights(torch.load(model_cfg.model_path, map_location=device, weights_only=True))
    feature_utils = FeaturesUtils(
        tod_vae_ckpt=str(model_cfg.vae_path),
        synchformer_ckpt=str(model_cfg.synchformer_ckpt),
        enable_conditions=True, mode=model_cfg.mode,
        bigvgan_vocoder_ckpt=None, need_vae_encoder=False,
    ).to(device, dtype).eval()
    return net, feature_utils, model_cfg, seq_cfg


def _load_hunyuan_model(device, model_size):
    """Load HunyuanFoley model dict + config. Returns (model_dict, cfg)."""
    from hunyuanvideo_foley.utils.model_utils import load_model
    model_size = model_size.lower()
    config_map = {
        "xl":  "HunyuanVideo-Foley/configs/hunyuanvideo-foley-xl.yaml",
        "xxl": "HunyuanVideo-Foley/configs/hunyuanvideo-foley-xxl.yaml",
    }
    config_path = config_map.get(model_size, config_map["xxl"])
    hunyuan_weights_dir = str(HUNYUAN_MODEL_DIR / "HunyuanVideo-Foley")
    print(f"[HunyuanFoley] Loading {model_size.upper()} model from {hunyuan_weights_dir}")
    return load_model(hunyuan_weights_dir, config_path, device,
                      enable_offload=False, model_size=model_size)


def mux_video_audio(silent_video: str, audio_path: str, output_path: str,
                     model: str = None) -> None:
    """Mux a silent video with an audio file into *output_path*.

    For HunyuanFoley (*model*="hunyuan") we use its own merge_audio_video which
    handles its specific ffmpeg quirks; all other models use stream-copy muxing.
    """
    if model == "hunyuan":
        _ensure_syspath("HunyuanVideo-Foley")
        from hunyuanvideo_foley.utils.media_utils import merge_audio_video
        merge_audio_video(audio_path, silent_video, output_path)
    else:
        v_in = ffmpeg.input(silent_video)
        a_in = ffmpeg.input(audio_path)
        ffmpeg.output(
            v_in["v:0"],
            a_in["a:0"],
            output_path,
            vcodec="libx264", preset="fast", crf=18,
            pix_fmt="yuv420p",
            acodec="aac", audio_bitrate="128k",
            movflags="+faststart",
        ).run(overwrite_output=True, quiet=True)


# ------------------------------------------------------------------ #
# Shared sliding-window segmentation and crossfade helpers            #
# Used by all three models (TARO, MMAudio, HunyuanFoley).            #
# ------------------------------------------------------------------ #

def _build_segments(total_dur_s: float, window_s: float, crossfade_s: float) -> list[tuple[float, float]]:
    """Return list of (start, end) pairs covering *total_dur_s*.

    Every segment uses the full *window_s* inference window.  Segments are
    equally spaced so every overlap is identical, guaranteeing the crossfade
    setting is honoured at every boundary with no raw bleed.

    Algorithm
    ---------
    1. Clamp crossfade_s so the step stays positive.
    2. Find the minimum n such that n segments of *window_s* cover
       *total_dur_s* with overlap β‰₯ crossfade_s at every boundary:
           n = ceil((total_dur_s - crossfade_s) / (window_s - crossfade_s))
    3. Compute equal spacing: step = (total_dur_s - window_s) / (n - 1)
       so that every gap is identical and the last segment ends exactly at
       total_dur_s.
    4. Every segment is exactly *window_s* wide.  The trailing audio of each
       segment beyond its contact edge is discarded in _stitch_wavs.
    """
    crossfade_s = min(crossfade_s, window_s * 0.5)
    if total_dur_s <= window_s:
        return [(0.0, total_dur_s)]
    step_min = window_s - crossfade_s          # minimum step to honour crossfade
    n = math.ceil((total_dur_s - crossfade_s) / step_min)
    n = max(n, 2)
    # Equal step so first seg starts at 0 and last seg ends at total_dur_s
    step_s = (total_dur_s - window_s) / (n - 1)
    return [(i * step_s, i * step_s + window_s) for i in range(n)]


def _cf_join(a: np.ndarray, b: np.ndarray,
             crossfade_s: float, db_boost: float, sr: int) -> np.ndarray:
    """Equal-power crossfade join.  Works for both mono (T,) and stereo (C, T) arrays.
    Stereo arrays are expected in (channels, samples) layout.

    db_boost is applied to the overlap region as a whole (after blending), so
    it compensates for the -3 dB equal-power dip without doubling amplitude.
    Applying gain to each side independently (the common mistake) causes a
    +3 dB loudness bump at the seam β€” this version avoids that."""
    stereo = a.ndim == 2
    n_a = a.shape[1] if stereo else len(a)
    n_b = b.shape[1] if stereo else len(b)
    cf  = min(int(round(crossfade_s * sr)), n_a, n_b)
    if cf <= 0:
        return np.concatenate([a, b], axis=1 if stereo else 0)
    gain     = 10 ** (db_boost / 20.0)
    t        = np.linspace(0.0, 1.0, cf, dtype=np.float32)
    fade_out = np.cos(t * np.pi / 2)   # 1 β†’ 0
    fade_in  = np.sin(t * np.pi / 2)   # 0 β†’ 1
    if stereo:
        # Blend first, then apply boost to the overlap region as a unit
        overlap = (a[:, -cf:] * fade_out + b[:, :cf] * fade_in) * gain
        return np.concatenate([a[:, :-cf], overlap, b[:, cf:]], axis=1)
    else:
        overlap = (a[-cf:] * fade_out + b[:cf] * fade_in) * gain
        return np.concatenate([a[:-cf], overlap, b[cf:]])


# ================================================================== #
#                              TARO                                   #
# ================================================================== #
# Constants sourced from TARO/infer.py and TARO/models.py:
#   SR=16000, TRUNCATE=131072  β†’  8.192 s window
#   TRUNCATE_FRAME = 4 fps Γ— 131072/16000 = 32 CAVP frames per window
#   TRUNCATE_ONSET = 120 onset frames per window
#   latent shape: (1, 8, 204, 16) β€” fixed by MMDiT architecture
#   latents_scale: [0.18215]*8 β€” AudioLDM2 VAE scale factor
# ================================================================== #

# ================================================================== #
#                  MODEL CONSTANTS & CONFIGURATION REGISTRY           #
# ================================================================== #
# All per-model numeric constants live here β€” MODEL_CONFIGS is the   #
# single source of truth consumed by duration estimation, segmentation,#
# and the UI.  Standalone names kept only where other code references #
# them by name (TARO geometry, TARGET_SR, GPU_DURATION_CAP).         #
# ================================================================== #

# TARO geometry β€” referenced directly in _taro_infer_segment
TARO_SR             = 16000
TARO_TRUNCATE       = 131072
TARO_FPS            = 4
TARO_TRUNCATE_FRAME = int(TARO_FPS * TARO_TRUNCATE / TARO_SR)  # 32
TARO_TRUNCATE_ONSET = 120
TARO_MODEL_DUR      = TARO_TRUNCATE / TARO_SR                  # 8.192 s

GPU_DURATION_CAP = 300   # hard cap per @spaces.GPU call β€” never reserve more than this

MODEL_CONFIGS = {
    "taro": {
        "window_s":       TARO_MODEL_DUR,  # 8.192 s
        "sr":             TARO_SR,          # 16000 (output resampled to TARGET_SR)
        "secs_per_step":  0.025,  # measured 0.023 s/step on H200
        "load_overhead":  15,     # model load + CAVP feature extraction
        "tab_prefix":     "taro",
        "label":          "TARO",
        "regen_fn":       None,   # set after function definitions (avoids forward-ref)
    },
    "mmaudio": {
        "window_s":       8.0,    # MMAudio's fixed generation window
        "sr":             48000,  # resampled from 44100 in post-processing
        "secs_per_step":  0.25,   # measured 0.230 s/step on H200
        "load_overhead":  30,     # 15s warm + 15s model init
        "tab_prefix":     "mma",
        "label":          "MMAudio",
        "regen_fn":       None,
    },
    "hunyuan": {
        "window_s":       15.0,   # HunyuanFoley max video duration
        "sr":             48000,
        "secs_per_step":  0.35,   # measured 0.328 s/step on H200
        "load_overhead":  55,     # ~55s to load the 10 GB XXL weights
        "tab_prefix":     "hf",
        "label":          "HunyuanFoley",
        "regen_fn":       None,
    },
}

# Convenience aliases used only in the TARO inference path
TARO_SECS_PER_STEP  = MODEL_CONFIGS["taro"]["secs_per_step"]
MMAUDIO_WINDOW      = MODEL_CONFIGS["mmaudio"]["window_s"]
MMAUDIO_SECS_PER_STEP = MODEL_CONFIGS["mmaudio"]["secs_per_step"]
HUNYUAN_MAX_DUR     = MODEL_CONFIGS["hunyuan"]["window_s"]
HUNYUAN_SECS_PER_STEP = MODEL_CONFIGS["hunyuan"]["secs_per_step"]


def _clamp_duration(secs: float, label: str) -> int:
    """Clamp a raw GPU-seconds estimate to [60, GPU_DURATION_CAP] and log it."""
    result = min(GPU_DURATION_CAP, max(60, int(secs)))
    print(f"[duration] {label}: {secs:.0f}s raw β†’ {result}s reserved")
    return result


def _estimate_gpu_duration(model_key: str, num_samples: int, num_steps: int,
                           total_dur_s: float = None, crossfade_s: float = 0,
                           video_file: str = None) -> int:
    """Estimate GPU seconds for a full generation call.

    Formula: num_samples Γ— n_segs Γ— num_steps Γ— secs_per_step + load_overhead
    """
    cfg = MODEL_CONFIGS[model_key]
    try:
        if total_dur_s is None:
            total_dur_s = get_video_duration(video_file)
        n_segs = len(_build_segments(total_dur_s, cfg["window_s"], float(crossfade_s)))
    except Exception:
        n_segs = 1
    secs = int(num_samples) * n_segs * int(num_steps) * cfg["secs_per_step"] + cfg["load_overhead"]
    print(f"[duration] {cfg['label']}: {int(num_samples)}samp Γ— {n_segs}seg Γ— "
          f"{int(num_steps)}steps β†’ {secs:.0f}s β†’ capped ", end="")
    return _clamp_duration(secs, cfg["label"])


def _estimate_regen_duration(model_key: str, num_steps: int) -> int:
    """Estimate GPU seconds for a single-segment regen call."""
    cfg  = MODEL_CONFIGS[model_key]
    secs = int(num_steps) * cfg["secs_per_step"] + cfg["load_overhead"]
    print(f"[duration] {cfg['label']} regen: 1 seg Γ— {int(num_steps)} steps β†’ ", end="")
    return _clamp_duration(secs, f"{cfg['label']} regen")

_TARO_CACHE_MAXLEN = 16   # evict oldest entries beyond this limit
_TARO_INFERENCE_CACHE: dict = {}   # keyed by (video_file, seed, cfg, steps, mode, crossfade_s)
_TARO_CACHE_LOCK = threading.Lock()


def _taro_calc_max_samples(total_dur_s: float, num_steps: int, crossfade_s: float) -> int:
    n_segs        = len(_build_segments(total_dur_s, TARO_MODEL_DUR, crossfade_s))
    time_per_seg  = num_steps * TARO_SECS_PER_STEP
    max_s         = int(600.0 / (n_segs * time_per_seg))
    return max(1, min(max_s, MAX_SLOTS))


def _taro_duration(video_file, seed_val, cfg_scale, num_steps, mode,
                   crossfade_s, crossfade_db, num_samples):
    """Pre-GPU callable β€” must match _taro_gpu_infer's input order exactly."""
    return _estimate_gpu_duration("taro", int(num_samples), int(num_steps),
                                  video_file=video_file, crossfade_s=crossfade_s)


def _taro_infer_segment(
    model, vae, vocoder,
    cavp_feats_full, onset_feats_full,
    seg_start_s: float, seg_end_s: float,
    device, weight_dtype,
    cfg_scale: float, num_steps: int, mode: str,
    latents_scale,
    euler_sampler, euler_maruyama_sampler,
) -> np.ndarray:
    """Single-segment TARO inference. Returns wav array trimmed to segment length."""
    # CAVP features (4 fps)
    cavp_start = int(round(seg_start_s * TARO_FPS))
    cavp_slice = cavp_feats_full[cavp_start : cavp_start + TARO_TRUNCATE_FRAME]
    if cavp_slice.shape[0] < TARO_TRUNCATE_FRAME:
        pad = np.zeros(
            (TARO_TRUNCATE_FRAME - cavp_slice.shape[0],) + cavp_slice.shape[1:],
            dtype=cavp_slice.dtype,
        )
        cavp_slice = np.concatenate([cavp_slice, pad], axis=0)
    video_feats = torch.from_numpy(cavp_slice).unsqueeze(0).to(device, weight_dtype)

    # Onset features  (onset_fps = TRUNCATE_ONSET / MODEL_DUR β‰ˆ 14.65 fps)
    onset_fps   = TARO_TRUNCATE_ONSET / TARO_MODEL_DUR
    onset_start = int(round(seg_start_s * onset_fps))
    onset_slice = onset_feats_full[onset_start : onset_start + TARO_TRUNCATE_ONSET]
    if onset_slice.shape[0] < TARO_TRUNCATE_ONSET:
        onset_slice = np.pad(
            onset_slice,
            ((0, TARO_TRUNCATE_ONSET - onset_slice.shape[0]),),
            mode="constant",
        )
    onset_feats_t = torch.from_numpy(onset_slice).unsqueeze(0).to(device, weight_dtype)

    # Latent noise β€” shape matches MMDiT architecture (in_channels=8, 204Γ—16 spatial)
    z = torch.randn(1, model.in_channels, 204, 16, device=device, dtype=weight_dtype)

    sampling_kwargs = dict(
        model=model,
        latents=z,
        y=onset_feats_t,
        context=video_feats,
        num_steps=int(num_steps),
        heun=False,
        cfg_scale=float(cfg_scale),
        guidance_low=0.0,
        guidance_high=0.7,
        path_type="linear",
    )
    with torch.no_grad():
        samples = (euler_maruyama_sampler if mode == "sde" else euler_sampler)(**sampling_kwargs)
        # samplers return (output_tensor, zs) β€” index [0] for the audio latent
        if isinstance(samples, tuple):
            samples = samples[0]

    # Decode: AudioLDM2 VAE β†’ mel β†’ vocoder β†’ waveform
    samples = vae.decode(samples / latents_scale).sample
    wav = vocoder(samples.squeeze().float()).detach().cpu().numpy()
    return wav  # full window β€” _stitch_wavs handles contact-edge trimming


# ================================================================== #
#                     TARO 16 kHz β†’ 48 kHz upsample                   #
# ================================================================== #
# TARO generates at 16 kHz; all other models output at 44.1/48 kHz.
# We upsample via sinc resampling (torchaudio, CPU-only) so the final
# stitched audio is uniformly at 48 kHz across all three models.

TARGET_SR   = 48000   # unified output sample rate for all three models
TARO_SR_OUT = TARGET_SR


def _resample_to_target(wav: np.ndarray, src_sr: int,
                         dst_sr: int = None) -> np.ndarray:
    """Resample *wav* (mono or stereo numpy float32) from *src_sr* to *dst_sr*.

    *dst_sr* defaults to TARGET_SR (48 kHz).  No-op if src_sr == dst_sr.
    Uses torchaudio Kaiser-windowed sinc resampling β€” CPU-only, ZeroGPU-safe.
    """
    if dst_sr is None:
        dst_sr = TARGET_SR
    if src_sr == dst_sr:
        return wav
    stereo = wav.ndim == 2
    t = torch.from_numpy(np.ascontiguousarray(wav.astype(np.float32)))
    if not stereo:
        t = t.unsqueeze(0)          # [1, T]
    t = torchaudio.functional.resample(t, src_sr, dst_sr)
    if not stereo:
        t = t.squeeze(0)            # [T]
    return t.numpy()


def _upsample_taro(wav_16k: np.ndarray) -> np.ndarray:
    """Upsample a mono 16 kHz numpy array to 48 kHz via sinc resampling (CPU).

    torchaudio.functional.resample uses a Kaiser-windowed sinc filter β€”
    mathematically optimal for bandlimited signals, zero CUDA risk.
    Returns a mono float32 numpy array at 48 kHz.
    """
    dur_in = len(wav_16k) / TARO_SR
    print(f"[TARO upsample] {dur_in:.2f}s @ {TARO_SR}Hz β†’ {TARGET_SR}Hz (sinc, CPU) …")
    result = _resample_to_target(wav_16k, TARO_SR)
    print(f"[TARO upsample] done β€” {len(result)/TARGET_SR:.2f}s @ {TARGET_SR}Hz "
          f"(expected {dur_in * 3:.2f}s, ratio 3Γ—)")
    return result


def _stitch_wavs(wavs: list[np.ndarray], crossfade_s: float, db_boost: float,
                 total_dur_s: float, sr: int,
                 segments: list[tuple[float, float]] = None) -> np.ndarray:
    """Crossfade-join a list of wav arrays and trim to *total_dur_s*.
    Works for both mono (T,) and stereo (C, T) arrays.

    When *segments* is provided (list of (start, end) video-time pairs),
    each wav is trimmed to its contact-edge window before joining:

      contact_edge[i→i+1] = midpoint of overlap = (seg[i].end + seg[i+1].start) / 2
      half_cf              = crossfade_s / 2

      seg i  keep: [contact_edge[i-1→i] - half_cf,  contact_edge[i→i+1] + half_cf]
             expressed as sample offsets into the generated audio for that segment.

    This guarantees every crossfade zone is exactly crossfade_s wide with no
    raw bleed regardless of how much the inference windows overlap.
    """
    def _trim(wav, start_s, end_s, seg_start_s):
        """Trim wav to [start_s, end_s] expressed in absolute video time,
        where the wav starts at seg_start_s in video time."""
        s = max(0, int(round((start_s - seg_start_s) * sr)))
        e = int(round((end_s   - seg_start_s) * sr))
        e = min(e, wav.shape[1] if wav.ndim == 2 else len(wav))
        return wav[:, s:e] if wav.ndim == 2 else wav[s:e]

    if segments is None or len(segments) == 1:
        out = wavs[0]
        for nw in wavs[1:]:
            out = _cf_join(out, nw, crossfade_s, db_boost, sr)
        n = int(round(total_dur_s * sr))
        return out[:, :n] if out.ndim == 2 else out[:n]

    half_cf = crossfade_s / 2.0

    # Compute contact edges between consecutive segments
    contact_edges = [
        (segments[i][1] + segments[i + 1][0]) / 2.0
        for i in range(len(segments) - 1)
    ]

    # Trim each segment to its keep window
    trimmed = []
    for i, (wav, (seg_start, seg_end)) in enumerate(zip(wavs, segments)):
        keep_start = (contact_edges[i - 1] - half_cf) if i > 0                 else seg_start
        keep_end   = (contact_edges[i]     + half_cf) if i < len(segments) - 1 else total_dur_s
        trimmed.append(_trim(wav, keep_start, keep_end, seg_start))

    # Crossfade-join the trimmed segments
    out = trimmed[0]
    for nw in trimmed[1:]:
        out = _cf_join(out, nw, crossfade_s, db_boost, sr)

    n = int(round(total_dur_s * sr))
    return out[:, :n] if out.ndim == 2 else out[:n]


def _save_wav(path: str, wav: np.ndarray, sr: int) -> None:
    """Save a numpy wav array (mono or stereo) to *path* via torchaudio."""
    t = torch.from_numpy(np.ascontiguousarray(wav))
    if t.ndim == 1:
        t = t.unsqueeze(0)
    torchaudio.save(path, t, sr)


def _log_inference_timing(label: str, elapsed: float, n_segs: int,
                          num_steps: int, constant: float) -> None:
    """Print a standardised inference-timing summary line."""
    total_steps = n_segs * num_steps
    secs_per_step = elapsed / total_steps if total_steps > 0 else 0
    print(f"[{label}] Inference done: {n_segs} seg(s) Γ— {num_steps} steps in "
          f"{elapsed:.1f}s wall β†’ {secs_per_step:.3f}s/step "
          f"(current constant={constant})")


def _build_seg_meta(*, segments, wav_paths, audio_path, video_path,
                    silent_video, sr, model, crossfade_s, crossfade_db,
                    total_dur_s, source_video=None, **extras) -> dict:
    """Build the seg_meta dict shared by all three generate_* functions.
    Model-specific keys are passed via **extras.

    *source_video* is the original Gradio-managed upload path
    (/tmp/gradio/...).  It lives for the entire session and is used as a
    fallback when *silent_video* (which lives in a managed tmp dir) has been
    evicted or the Space has restarted since generation.
    """
    meta = {
        "segments":     segments,
        "wav_paths":    wav_paths,
        "audio_path":   audio_path,
        "video_path":   video_path,
        "silent_video": silent_video,
        "source_video": source_video or video_path,
        "sr":           sr,
        "model":        model,
        "crossfade_s":  crossfade_s,
        "crossfade_db": crossfade_db,
        "total_dur_s":  total_dur_s,
    }
    meta.update(extras)
    return meta


def _post_process_samples(results: list, *, model: str, tmp_dir: str,
                           silent_video: str, segments: list,
                           crossfade_s: float, crossfade_db: float,
                           total_dur_s: float, sr: int,
                           source_video: str = None,
                           extra_meta_fn=None) -> list:
    """Shared CPU post-processing for all three generate_* wrappers.

    Each entry in *results* is a tuple whose first element is a list of
    per-segment wav arrays.  The remaining elements are model-specific
    (e.g. TARO returns features, HunyuanFoley returns text_feats).

    *extra_meta_fn(sample_idx, result_tuple, tmp_dir) -> dict* is an optional
    callback that returns model-specific extra keys to merge into seg_meta
    (e.g. cavp_path, onset_path, text_feats_path).

    Returns a list of (video_path, audio_path, seg_meta) tuples.
    """
    outputs = []
    print(f"[_post_process_samples] model={model} num_results={len(results)} tmp_dir={tmp_dir!r}")
    for sample_idx, result in enumerate(results):
        seg_wavs = result[0]
        print(f"[_post_process_samples] sample {sample_idx}: seg_wavs count={len(seg_wavs) if seg_wavs else None}")

        full_wav   = _stitch_wavs(seg_wavs, crossfade_s, crossfade_db, total_dur_s, sr, segments)
        audio_path = os.path.join(tmp_dir, f"{model}_{sample_idx}.wav")
        _save_wav(audio_path, full_wav, sr)
        print(f"[_post_process_samples] sample {sample_idx}: saved audio={audio_path!r} exists={os.path.exists(audio_path)}")
        video_path = os.path.join(tmp_dir, f"{model}_{sample_idx}.mp4")
        mux_video_audio(silent_video, audio_path, video_path, model=model)
        print(f"[_post_process_samples] sample {sample_idx}: muxed video={video_path!r} exists={os.path.exists(video_path)}")
        wav_paths  = _save_seg_wavs(seg_wavs, tmp_dir, f"{model}_{sample_idx}")

        extras = extra_meta_fn(sample_idx, result, tmp_dir) if extra_meta_fn else {}
        seg_meta = _build_seg_meta(
            segments=segments, wav_paths=wav_paths, audio_path=audio_path,
            video_path=video_path, silent_video=silent_video, sr=sr,
            model=model, crossfade_s=crossfade_s, crossfade_db=crossfade_db,
            total_dur_s=total_dur_s, source_video=source_video, **extras,
        )
        outputs.append((video_path, audio_path, seg_meta))
    print(f"[_post_process_samples] returning {len(outputs)} output(s)")
    return outputs


def _cpu_preprocess(video_file: str, model_dur: float,
                    crossfade_s: float) -> tuple:
    """Shared CPU pre-processing for all generate_* wrappers.
    Returns (tmp_dir, silent_video, total_dur_s, segments)."""
    tmp_dir      = _register_tmp_dir(tempfile.mkdtemp())
    silent_video = os.path.join(tmp_dir, "silent_input.mp4")
    strip_audio_from_video(video_file, silent_video)
    total_dur_s  = get_video_duration(video_file)
    segments     = _build_segments(total_dur_s, model_dur, crossfade_s)
    return tmp_dir, silent_video, total_dur_s, segments


@spaces.GPU(duration=_taro_duration)
def _taro_gpu_infer(video_file, seed_val, cfg_scale, num_steps, mode,
                    crossfade_s, crossfade_db, num_samples):
    """GPU-only TARO inference β€” model loading + feature extraction + diffusion.
    Returns list of (wavs_list, onset_feats) per sample."""
    seed_val     = _resolve_seed(seed_val)
    crossfade_s  = float(crossfade_s)
    num_samples  = int(num_samples)

    torch.set_grad_enabled(False)
    device, weight_dtype = _get_device_and_dtype()

    _ensure_syspath("TARO")
    from TARO.onset_util import extract_onset
    from TARO.samplers   import euler_sampler, euler_maruyama_sampler

    ctx        = _ctx_load("taro_gpu_infer")
    print(f"[_taro_gpu_infer] ctx keys={list(ctx.keys())}")
    tmp_dir    = ctx["tmp_dir"]
    silent_video = ctx["silent_video"]
    segments   = ctx["segments"]
    total_dur_s = ctx["total_dur_s"]
    print(f"[_taro_gpu_infer] tmp_dir={tmp_dir!r} silent_video={silent_video!r} segments={segments} total_dur_s={total_dur_s}")

    print(f"[_taro_gpu_infer] calling _load_taro_feature_extractors")
    extract_cavp, onset_model = _load_taro_feature_extractors(device)
    print(f"[_taro_gpu_infer] extractors loaded, calling extract_cavp")
    cavp_feats  = extract_cavp(silent_video, tmp_path=tmp_dir)
    print(f"[_taro_gpu_infer] cavp done, calling extract_onset")
    # Onset features depend only on the video β€” extract once for all samples
    onset_feats = extract_onset(silent_video, onset_model, tmp_path=tmp_dir, device=device)
    print(f"[_taro_gpu_infer] onset done, freeing extractors")

    # Free feature extractors before loading the heavier inference models
    del extract_cavp, onset_model
    if torch.cuda.is_available():
        torch.cuda.empty_cache()

    print(f"[_taro_gpu_infer] calling _load_taro_models")
    model, vae, vocoder, latents_scale = _load_taro_models(device, weight_dtype)
    print(f"[_taro_gpu_infer] models loaded")

    results = []   # list of (wavs, onset_feats) per sample
    for sample_idx in range(num_samples):
        sample_seed = seed_val + sample_idx
        cache_key   = (video_file, sample_seed, float(cfg_scale), int(num_steps), mode, crossfade_s)

        with _TARO_CACHE_LOCK:
            cached = _TARO_INFERENCE_CACHE.get(cache_key)
        if cached is not None:
            print(f"[TARO] Sample {sample_idx+1}: cache hit.")
            results.append((cached["wavs"], cavp_feats, None))
        else:
            set_global_seed(sample_seed)
            wavs = []
            _t_infer_start = time.perf_counter()
            for seg_start_s, seg_end_s in segments:
                print(f"[TARO] Sample {sample_idx+1} | {seg_start_s:.2f}s – {seg_end_s:.2f}s")
                wav = _taro_infer_segment(
                    model, vae, vocoder,
                    cavp_feats, onset_feats,
                    seg_start_s, seg_end_s,
                    device, weight_dtype,
                    cfg_scale, num_steps, mode,
                    latents_scale,
                    euler_sampler, euler_maruyama_sampler,
                )
                wavs.append(wav)
            _log_inference_timing("TARO", time.perf_counter() - _t_infer_start,
                                  len(segments), int(num_steps), TARO_SECS_PER_STEP)
            with _TARO_CACHE_LOCK:
                _TARO_INFERENCE_CACHE[cache_key] = {"wavs": wavs}
                while len(_TARO_INFERENCE_CACHE) > _TARO_CACHE_MAXLEN:
                    _TARO_INFERENCE_CACHE.pop(next(iter(_TARO_INFERENCE_CACHE)))
            results.append((wavs, cavp_feats, onset_feats))

        # Free GPU memory between samples so VRAM fragmentation doesn't
        # degrade diffusion quality on samples 2, 3, 4, etc.
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    print(f"[_taro_gpu_infer] returning {len(results)} results")
    return results


def generate_taro(video_file, seed_val, cfg_scale, num_steps, mode,
                  crossfade_s, crossfade_db, num_samples):
    """TARO: video-conditioned diffusion, 16 kHz, 8.192 s sliding window.
    CPU pre/post-processing wraps the GPU-only inference to minimize ZeroGPU cost."""
    print(f"[generate_taro] START video_file={video_file!r} num_samples={num_samples}")
    crossfade_s  = float(crossfade_s)
    crossfade_db = float(crossfade_db)
    num_samples  = int(num_samples)

    # ── CPU pre-processing (no GPU needed) ──
    tmp_dir, silent_video, total_dur_s, segments = _cpu_preprocess(
        video_file, TARO_MODEL_DUR, crossfade_s)
    print(f"[generate_taro] preprocess done: total_dur_s={total_dur_s:.2f} segments={segments} tmp_dir={tmp_dir}")

    _ctx_store("taro_gpu_infer", {
        "tmp_dir": tmp_dir, "silent_video": silent_video,
        "segments": segments, "total_dur_s": total_dur_s,
    })

    # ── GPU inference only ──
    results = _taro_gpu_infer(video_file, seed_val, cfg_scale, num_steps, mode,
                              crossfade_s, crossfade_db, num_samples)

    # ── CPU post-processing (no GPU needed) ──
    # Upsample 16kHz β†’ 48kHz and normalise result tuples to (seg_wavs, ...)
    cavp_path  = os.path.join(tmp_dir, "taro_cavp.npy")
    onset_path = os.path.join(tmp_dir, "taro_onset.npy")
    _feats_saved = False

    def _upsample_and_save_feats(result):
        nonlocal _feats_saved
        wavs, cavp_feats, onset_feats = result
        wavs = [_upsample_taro(w) for w in wavs]
        if not _feats_saved:
            np.save(cavp_path, cavp_feats)
            if onset_feats is not None:
                np.save(onset_path, onset_feats)
            _feats_saved = True
        return (wavs, cavp_feats, onset_feats)

    print(f"[generate_taro] gpu_infer returned {len(results)} result(s)")
    results = [_upsample_and_save_feats(r) for r in results]
    print(f"[generate_taro] upsample done, cavp_path={cavp_path} onset_path={onset_path}")

    def _taro_extras(sample_idx, result, td):
        return {"cavp_path": cavp_path, "onset_path": onset_path}

    outputs = _post_process_samples(
        results, model="taro", tmp_dir=tmp_dir,
        silent_video=silent_video, segments=segments,
        crossfade_s=crossfade_s, crossfade_db=crossfade_db,
        total_dur_s=total_dur_s, sr=TARO_SR_OUT,
        source_video=video_file,
        extra_meta_fn=_taro_extras,
    )
    print(f"[generate_taro] post_process done: {len(outputs)} output(s)")
    padded = _pad_outputs(outputs)
    print(f"[generate_taro] padded outputs: {[(type(x).__name__, x is not None) for x in padded[:6]]}")
    return padded


# ================================================================== #
#                            MMAudio                                  #
# ================================================================== #
# Constants sourced from MMAudio/mmaudio/model/sequence_config.py:
#   CONFIG_44K: duration=8.0 s, sampling_rate=44100
#   CLIP encoder: 8 fps, 384Γ—384 px
#   Synchformer: 25 fps, 224Γ—224 px
#   Default variant: large_44k_v2
# MMAudio uses flow-matching (FlowMatching with euler inference).
# generate() handles all feature extraction + decoding internally.
# ================================================================== #



def _mmaudio_duration(video_file, prompt, negative_prompt, seed_val,
                      cfg_strength, num_steps, crossfade_s, crossfade_db, num_samples,
                      silent_video=None, segments_json=None,
                      clip_start_s=0.0, clip_dur_s=None, **_kwargs):
    """Pre-GPU callable β€” must match _mmaudio_gpu_infer's input signature exactly.
    silent_video, segments_json, clip_start_s, clip_dur_s are extra positional args
    that xregen passes; they must appear here so ZeroGPU doesn't raise TypeError
    when forwarding all args to this duration fn before the GPU fn runs."""
    return _estimate_gpu_duration("mmaudio", int(num_samples), int(num_steps),
                                  video_file=video_file, crossfade_s=crossfade_s)


@spaces.GPU(duration=_mmaudio_duration)
def _mmaudio_gpu_infer(video_file, prompt, negative_prompt, seed_val,
                       cfg_strength, num_steps, crossfade_s, crossfade_db, num_samples,
                       silent_video, segments_json,
                       clip_start_s=0.0, clip_dur_s=None):
    """GPU-only MMAudio inference β€” model loading + flow-matching generation.
    Returns list of (seg_audios, sr) per sample.

    All video paths and segment data are passed explicitly as positional args
    to survive ZeroGPU process isolation (kwargs are silently dropped).

    When *clip_dur_s* is set, *silent_video* is the full source and a clip
    [clip_start_s, clip_start_s+clip_dur_s] is extracted first inside the
    GPU window (ffmpeg is CPU-safe here).  This avoids passing pre-extracted
    tmp files that don't exist in the GPU worker's process.
    """
    print(f"[_mmaudio_gpu_infer] START video={video_file!r} silent={silent_video!r} clip_start={clip_start_s} clip_dur={clip_dur_s} num_samples={num_samples}")
    _ensure_syspath("MMAudio")
    from mmaudio.eval_utils        import generate, load_video
    from mmaudio.model.flow_matching   import FlowMatching

    seed_val     = _resolve_seed(seed_val)
    num_samples  = int(num_samples)
    num_steps    = int(num_steps)
    crossfade_s  = float(crossfade_s)

    device, dtype = _get_device_and_dtype()

    net, feature_utils, model_cfg, seq_cfg = _load_mmaudio_models(device, dtype)

    # If a clip window is specified, extract it now (inside the GPU fn, so the
    # file exists in this worker's /tmp).
    tmp_dir = _register_tmp_dir(tempfile.mkdtemp())
    if clip_dur_s is not None:
        clip_dur_s = float(clip_dur_s)
        clip_path  = _extract_segment_clip(
            silent_video, float(clip_start_s), clip_dur_s,
            os.path.join(tmp_dir, "mma_xregen_clip.mp4"),
        )
        silent_video = clip_path

    # Extract per-segment clips from silent_video (now the correct clip source).
    segments = json.loads(segments_json)
    seg_clip_paths = [
        _extract_segment_clip(silent_video, s, e - s,
                              os.path.join(tmp_dir, f"mma_seg_{i}.mp4"))
        for i, (s, e) in enumerate(segments)
    ]

    sr = seq_cfg.sampling_rate   # 44100

    results = []
    for sample_idx in range(num_samples):
        rng = torch.Generator(device=device)
        rng.manual_seed(seed_val + sample_idx)

        seg_audios = []
        _t_mma_start = time.perf_counter()
        fm = FlowMatching(min_sigma=0, inference_mode="euler", num_steps=num_steps)

        for seg_i, (seg_start, seg_end) in enumerate(segments):
            seg_dur = seg_end - seg_start
            seg_path = seg_clip_paths[seg_i]

            video_info  = load_video(seg_path, seg_dur)
            clip_frames = video_info.clip_frames.unsqueeze(0)
            sync_frames = video_info.sync_frames.unsqueeze(0)
            actual_dur  = video_info.duration_sec

            seq_cfg.duration = actual_dur
            net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)

            print(f"[MMAudio] Sample {sample_idx+1} | seg {seg_i+1}/{len(segments)} "
                  f"{seg_start:.1f}–{seg_end:.1f}s | dur={actual_dur:.2f}s | prompt='{prompt}'")

            with torch.no_grad():
                audios = generate(
                    clip_frames,
                    sync_frames,
                    [prompt],
                    negative_text=[negative_prompt] if negative_prompt else None,
                    feature_utils=feature_utils,
                    net=net,
                    fm=fm,
                    rng=rng,
                    cfg_strength=float(cfg_strength),
                )
            wav = audios.float().cpu()[0].numpy()  # (C, T) β€” full window
            seg_audios.append(wav)

        _log_inference_timing("MMAudio", time.perf_counter() - _t_mma_start,
                              len(segments), int(num_steps), MMAUDIO_SECS_PER_STEP)
        results.append((seg_audios, sr))

        # Free GPU memory between samples to prevent VRAM fragmentation
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    return results


def generate_mmaudio(video_file, prompt, negative_prompt, seed_val,
                     cfg_strength, num_steps, crossfade_s, crossfade_db, num_samples):
    """MMAudio: flow-matching video-to-audio, 44.1 kHz, 8 s sliding window.
    CPU pre/post-processing wraps the GPU-only inference to minimize ZeroGPU cost."""
    num_samples  = int(num_samples)
    crossfade_s  = float(crossfade_s)
    crossfade_db = float(crossfade_db)

    # ── CPU pre-processing ──
    tmp_dir, silent_video, total_dur_s, segments = _cpu_preprocess(
        video_file, MMAUDIO_WINDOW, crossfade_s)
    print(f"[MMAudio] Video={total_dur_s:.2f}s | {len(segments)} segment(s) Γ— ≀8 s")

    # ── GPU inference only ──
    results = _mmaudio_gpu_infer(video_file, prompt, negative_prompt, seed_val,
                                 cfg_strength, num_steps, crossfade_s, crossfade_db,
                                 num_samples,
                                 silent_video, json.dumps(segments))

    # ── CPU post-processing ──
    # Resample 44100 β†’ 48000 and normalise tuples to (seg_wavs, ...)
    resampled = []
    for seg_audios, sr in results:
        if sr != TARGET_SR:
            print(f"[MMAudio upsample] resampling {sr}Hz β†’ {TARGET_SR}Hz (sinc, CPU) …")
            seg_audios = [_resample_to_target(w, sr) for w in seg_audios]
            print(f"[MMAudio upsample] done β€” {len(seg_audios)} seg(s) @ {TARGET_SR}Hz")
        resampled.append((seg_audios,))

    outputs = _post_process_samples(
        resampled, model="mmaudio", tmp_dir=tmp_dir,
        silent_video=silent_video, segments=segments,
        crossfade_s=crossfade_s, crossfade_db=crossfade_db,
        total_dur_s=total_dur_s, sr=TARGET_SR,
        source_video=video_file,
    )
    return _pad_outputs(outputs)


# ================================================================== #
#                        HunyuanVideoFoley                            #
# ================================================================== #
# Constants sourced from HunyuanVideo-Foley/hunyuanvideo_foley/constants.py
# and configs/hunyuanvideo-foley-xxl.yaml:
#   sample_rate = 48000 Hz (from DAC VAE)
#   audio_frame_rate = 50 (latent fps, xxl config)
#   max video duration = 15 s
#   SigLIP2 fps = 8, Synchformer fps = 25
#   CLAP text encoder: laion/larger_clap_general (auto-downloaded from HF Hub)
#   Default guidance_scale=4.5, num_inference_steps=50
# ================================================================== #



def _hunyuan_duration(video_file, prompt, negative_prompt, seed_val,
                      guidance_scale, num_steps, model_size, crossfade_s, crossfade_db,
                      num_samples, silent_video=None, segments_json=None, total_dur_s=None,
                      clip_start_s=0.0, clip_dur_s=None, **_kwargs):
    """Pre-GPU callable β€” must match _hunyuan_gpu_infer's input signature exactly.
    silent_video, segments_json, total_dur_s, clip_start_s, clip_dur_s are extra
    positional args that xregen passes; they must appear here so ZeroGPU doesn't
    raise TypeError when forwarding all args to this duration fn."""
    return _estimate_gpu_duration("hunyuan", int(num_samples), int(num_steps),
                                  video_file=video_file, crossfade_s=crossfade_s)


@spaces.GPU(duration=_hunyuan_duration)
def _hunyuan_gpu_infer(video_file, prompt, negative_prompt, seed_val,
                       guidance_scale, num_steps, model_size, crossfade_s, crossfade_db,
                       num_samples, silent_video, segments_json, total_dur_s,
                       clip_start_s=0.0, clip_dur_s=None):
    """GPU-only HunyuanFoley inference β€” model loading + feature extraction + denoising.
    Returns list of (seg_wavs, sr, text_feats) per sample.

    All paths passed explicitly as positional args to survive ZeroGPU isolation.
    When *clip_dur_s* is set, the clip is extracted inside the GPU window.
    """
    _ensure_syspath("HunyuanVideo-Foley")
    from hunyuanvideo_foley.utils.model_utils  import denoise_process
    from hunyuanvideo_foley.utils.feature_utils import feature_process

    seed_val     = _resolve_seed(seed_val)
    num_samples  = int(num_samples)
    crossfade_s  = float(crossfade_s)
    total_dur_s  = float(total_dur_s)
    set_global_seed(seed_val)

    device, _    = _get_device_and_dtype()
    model_size   = model_size.lower()

    model_dict, cfg = _load_hunyuan_model(device, model_size)

    # Extract xregen clip inside GPU fn if needed (tmp files from caller invisible here).
    tmp_dir = _register_tmp_dir(tempfile.mkdtemp())
    if clip_dur_s is not None:
        clip_dur_s = float(clip_dur_s)
        clip_path  = _extract_segment_clip(
            silent_video, float(clip_start_s), clip_dur_s,
            os.path.join(tmp_dir, "hny_xregen_clip.mp4"),
        )
        silent_video = clip_path
        total_dur_s  = clip_dur_s

    segments = json.loads(segments_json)
    dummy_seg_path = _extract_segment_clip(
        silent_video, 0, min(total_dur_s, HUNYUAN_MAX_DUR),
        os.path.join(tmp_dir, "_seg_dummy.mp4"),
    )
    seg_clip_paths = [
        _extract_segment_clip(silent_video, s, e - s,
                              os.path.join(tmp_dir, f"hny_seg_{i}.mp4"))
        for i, (s, e) in enumerate(segments)
    ]

    # Text feature extraction (GPU β€” runs once for all segments)
    _, text_feats, _ = feature_process(
        dummy_seg_path,
        prompt if prompt else "",
        model_dict,
        cfg,
        neg_prompt=negative_prompt if negative_prompt else None,
    )

    # Import visual-only feature extractor to avoid redundant text extraction
    # per segment (text_feats already computed once above for the whole batch).
    from hunyuanvideo_foley.utils.feature_utils import encode_video_features

    results = []
    for sample_idx in range(num_samples):
        seg_wavs = []
        sr = 48000
        _t_hny_start = time.perf_counter()
        for seg_i, (seg_start, seg_end) in enumerate(segments):
            seg_dur = seg_end - seg_start
            seg_path = seg_clip_paths[seg_i]

            # Extract only visual features β€” reuse text_feats from above
            visual_feats, seg_audio_len = encode_video_features(seg_path, model_dict)
            print(f"[HunyuanFoley] Sample {sample_idx+1} | seg {seg_i+1}/{len(segments)} "
                  f"{seg_start:.1f}–{seg_end:.1f}s β†’ {seg_audio_len:.2f}s audio")

            audio_batch, sr = denoise_process(
                visual_feats,
                text_feats,
                seg_audio_len,
                model_dict,
                cfg,
                guidance_scale=float(guidance_scale),
                num_inference_steps=int(num_steps),
                batch_size=1,
            )
            wav = audio_batch[0].float().cpu().numpy()  # full window
            seg_wavs.append(wav)

        _log_inference_timing("HunyuanFoley", time.perf_counter() - _t_hny_start,
                              len(segments), int(num_steps), HUNYUAN_SECS_PER_STEP)
        results.append((seg_wavs, sr, text_feats))

        # Free GPU memory between samples to prevent VRAM fragmentation
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    return results


def generate_hunyuan(video_file, prompt, negative_prompt, seed_val,
                     guidance_scale, num_steps, model_size, crossfade_s, crossfade_db, num_samples):
    """HunyuanVideoFoley: text-guided foley, 48 kHz, up to 15 s.
    CPU pre/post-processing wraps the GPU-only inference to minimize ZeroGPU cost."""
    num_samples  = int(num_samples)
    crossfade_s  = float(crossfade_s)
    crossfade_db = float(crossfade_db)

    # ── CPU pre-processing (no GPU needed) ──
    tmp_dir, silent_video, total_dur_s, segments = _cpu_preprocess(
        video_file, HUNYUAN_MAX_DUR, crossfade_s)
    print(f"[HunyuanFoley] Video={total_dur_s:.2f}s | {len(segments)} segment(s) Γ— ≀15 s")

    # ── GPU inference only ──
    results = _hunyuan_gpu_infer(video_file, prompt, negative_prompt, seed_val,
                                 guidance_scale, num_steps, model_size,
                                 crossfade_s, crossfade_db, num_samples,
                                 silent_video, json.dumps(segments), total_dur_s)

    # ── CPU post-processing (no GPU needed) ──
    def _hunyuan_extras(sample_idx, result, td):
        _, _sr, text_feats = result
        path = os.path.join(td, f"hunyuan_{sample_idx}_text_feats.pt")
        torch.save(text_feats, path)
        return {"text_feats_path": path}

    outputs = _post_process_samples(
        results, model="hunyuan", tmp_dir=tmp_dir,
        silent_video=silent_video, segments=segments,
        crossfade_s=crossfade_s, crossfade_db=crossfade_db,
        total_dur_s=total_dur_s, sr=48000,
        source_video=video_file,
        extra_meta_fn=_hunyuan_extras,
    )
    return _pad_outputs(outputs)


# ================================================================== #
#                  SEGMENT REGENERATION HELPERS                       #
# ================================================================== #
# Each regen function:
#   1. Runs inference for ONE segment (random seed, current settings)
#   2. Splices the new wav into the stored wavs list
#   3. Re-stitches the full track, re-saves .wav and re-muxes .mp4
#   4. Returns (new_video_path, new_audio_path, updated_seg_meta, new_waveform_html)
# ================================================================== #


def _splice_and_save(new_wav, seg_idx, meta, slot_id):
    """Replace wavs[seg_idx] with new_wav, re-stitch, re-save, re-mux.
    Returns (video_path, audio_path, updated_meta, waveform_html).
    """
    wavs         = _load_seg_wavs(meta["wav_paths"])
    wavs[seg_idx]= new_wav
    crossfade_s  = float(meta["crossfade_s"])
    crossfade_db = float(meta["crossfade_db"])
    sr           = int(meta["sr"])
    total_dur_s  = float(meta["total_dur_s"])
    silent_video = _resolve_silent_video(meta)
    segments     = meta["segments"]
    model        = meta["model"]

    full_wav = _stitch_wavs(wavs, crossfade_s, crossfade_db, total_dur_s, sr, segments)

    # Save new audio β€” use a new timestamped filename so Gradio / the browser
    # treats it as a genuinely different file and reloads the video player.
    _ts        = int(time.time() * 1000)
    tmp_dir    = os.path.dirname(meta["audio_path"])
    _base      = os.path.splitext(os.path.basename(meta["audio_path"]))[0]
    # Strip any previous timestamp suffix before adding a new one
    _base_clean = _base.rsplit("_regen_", 1)[0]
    audio_path = os.path.join(tmp_dir, f"{_base_clean}_regen_{_ts}.wav")
    _save_wav(audio_path, full_wav, sr)

    # Re-mux into a new video file so the browser is forced to reload it
    _vid_base   = os.path.splitext(os.path.basename(meta["video_path"]))[0]
    _vid_base_clean = _vid_base.rsplit("_regen_", 1)[0]
    video_path  = os.path.join(tmp_dir, f"{_vid_base_clean}_regen_{_ts}.mp4")
    mux_video_audio(silent_video, audio_path, video_path, model=model)

    # Save updated segment wavs to .npy files
    updated_wav_paths = _save_seg_wavs(wavs, tmp_dir, os.path.splitext(_base_clean)[0])
    updated_meta = dict(meta)
    updated_meta["wav_paths"]  = updated_wav_paths
    updated_meta["audio_path"] = audio_path
    updated_meta["video_path"] = video_path

    state_json_new = json.dumps(updated_meta)

    waveform_html = _build_waveform_html(audio_path, segments, slot_id, "",
                                         state_json=state_json_new,
                                         video_path=video_path,
                                         crossfade_s=crossfade_s)
    return video_path, audio_path, updated_meta, waveform_html


def _taro_regen_duration(video_file, seg_idx, seg_meta_json,
                         seed_val, cfg_scale, num_steps, mode,
                         crossfade_s, crossfade_db, slot_id=None):
    # If cached CAVP/onset features exist, skip ~10s feature-extractor overhead
    try:
        meta = json.loads(seg_meta_json)
        cavp_ok  = os.path.exists(meta.get("cavp_path", ""))
        onset_ok = os.path.exists(meta.get("onset_path", ""))
        if cavp_ok and onset_ok:
            cfg  = MODEL_CONFIGS["taro"]
            secs = int(num_steps) * cfg["secs_per_step"] + 5  # 5s model-load only
            result = min(GPU_DURATION_CAP, max(30, int(secs)))
            print(f"[duration] TARO regen (cache hit): 1 seg Γ— {int(num_steps)} steps β†’ {secs:.0f}s β†’ capped {result}s")
            return result
    except Exception:
        pass
    return _estimate_regen_duration("taro", int(num_steps))


@spaces.GPU(duration=_taro_regen_duration)
def _regen_taro_gpu(video_file, seg_idx, seg_meta_json,
                    seed_val, cfg_scale, num_steps, mode,
                    crossfade_s, crossfade_db, slot_id=None):
    """GPU-only TARO regen β€” returns new_wav for a single segment."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)
    seg_start_s, seg_end_s = meta["segments"][seg_idx]

    torch.set_grad_enabled(False)
    device, weight_dtype = _get_device_and_dtype()

    _ensure_syspath("TARO")
    from TARO.samplers import euler_sampler, euler_maruyama_sampler

    # Load cached CAVP/onset features from .npy files (CPU I/O, fast, outside GPU budget)
    cavp_path  = meta.get("cavp_path", "")
    onset_path = meta.get("onset_path", "")
    if cavp_path and os.path.exists(cavp_path) and onset_path and os.path.exists(onset_path):
        print("[TARO regen] Loading cached CAVP + onset features from disk")
        cavp_feats  = np.load(cavp_path)
        onset_feats = np.load(onset_path)
    else:
        print("[TARO regen] Cache miss β€” re-extracting CAVP + onset features")
        from TARO.onset_util import extract_onset
        extract_cavp, onset_model = _load_taro_feature_extractors(device)
        silent_video = _resolve_silent_video(meta)
        tmp_dir      = _register_tmp_dir(tempfile.mkdtemp())
        cavp_feats   = extract_cavp(silent_video, tmp_path=tmp_dir)
        onset_feats  = extract_onset(silent_video, onset_model, tmp_path=tmp_dir, device=device)
        del extract_cavp, onset_model
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    model_net, vae, vocoder, latents_scale = _load_taro_models(device, weight_dtype)

    set_global_seed(random.randint(0, 2**32 - 1))

    return _taro_infer_segment(
        model_net, vae, vocoder, cavp_feats, onset_feats,
        seg_start_s, seg_end_s, device, weight_dtype,
        float(cfg_scale), int(num_steps), mode, latents_scale,
        euler_sampler, euler_maruyama_sampler,
    )


def regen_taro_segment(video_file, seg_idx, seg_meta_json,
                       seed_val, cfg_scale, num_steps, mode,
                       crossfade_s, crossfade_db, slot_id):
    """Regenerate one TARO segment. GPU inference + CPU splice/save."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)

    # GPU: inference β€” CAVP/onset features loaded from disk paths in seg_meta_json
    new_wav = _regen_taro_gpu(video_file, seg_idx, seg_meta_json,
                              seed_val, cfg_scale, num_steps, mode,
                              crossfade_s, crossfade_db, slot_id)

    # Upsample 16kHz β†’ 48kHz (sinc, CPU)
    new_wav = _upsample_taro(new_wav)
    # CPU: splice, stitch, mux, save
    video_path, audio_path, updated_meta, waveform_html = _splice_and_save(
        new_wav, seg_idx, meta, slot_id
    )
    return video_path, audio_path, json.dumps(updated_meta), waveform_html


def _mmaudio_regen_duration(video_file, seg_idx, seg_meta_json,
                             prompt, negative_prompt, seed_val,
                             cfg_strength, num_steps, crossfade_s, crossfade_db,
                             slot_id=None):
    return _estimate_regen_duration("mmaudio", int(num_steps))


@spaces.GPU(duration=_mmaudio_regen_duration)
def _regen_mmaudio_gpu(video_file, seg_idx, seg_meta_json,
                       prompt, negative_prompt, seed_val,
                       cfg_strength, num_steps, crossfade_s, crossfade_db,
                       slot_id=None):
    """GPU-only MMAudio regen β€” returns (new_wav, sr) for a single segment."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)
    seg_start, seg_end = meta["segments"][seg_idx]
    seg_dur = seg_end - seg_start

    _ensure_syspath("MMAudio")
    from mmaudio.eval_utils          import generate, load_video
    from mmaudio.model.flow_matching import FlowMatching

    device, dtype = _get_device_and_dtype()

    net, feature_utils, model_cfg, seq_cfg = _load_mmaudio_models(device, dtype)
    sr = seq_cfg.sampling_rate

    # Extract segment clip inside the GPU function β€” ffmpeg is CPU-only and safe here.
    # This avoids any cross-process context passing that fails under ZeroGPU isolation.
    seg_path = _extract_segment_clip(
        _resolve_silent_video(meta), seg_start, seg_dur,
        os.path.join(_register_tmp_dir(tempfile.mkdtemp()), "regen_seg.mp4"),
    )

    rng = torch.Generator(device=device)
    rng.manual_seed(random.randint(0, 2**32 - 1))

    fm          = FlowMatching(min_sigma=0, inference_mode="euler", num_steps=int(num_steps))
    video_info  = load_video(seg_path, seg_dur)
    clip_frames = video_info.clip_frames.unsqueeze(0)
    sync_frames = video_info.sync_frames.unsqueeze(0)
    actual_dur  = video_info.duration_sec
    seq_cfg.duration = actual_dur
    net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)

    with torch.no_grad():
        audios = generate(
            clip_frames, sync_frames, [prompt],
            negative_text=[negative_prompt] if negative_prompt else None,
            feature_utils=feature_utils, net=net, fm=fm, rng=rng,
            cfg_strength=float(cfg_strength),
        )
    new_wav = audios.float().cpu()[0].numpy()  # full window β€” _stitch_wavs trims
    return new_wav, sr


def regen_mmaudio_segment(video_file, seg_idx, seg_meta_json,
                          prompt, negative_prompt, seed_val,
                          cfg_strength, num_steps, crossfade_s, crossfade_db, slot_id):
    """Regenerate one MMAudio segment. GPU inference + CPU splice/save."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)

    # GPU: inference (segment clip extraction happens inside the GPU function)
    new_wav, sr = _regen_mmaudio_gpu(video_file, seg_idx, seg_meta_json,
                                     prompt, negative_prompt, seed_val,
                                     cfg_strength, num_steps, crossfade_s, crossfade_db,
                                     slot_id)

    # Resample to 48kHz if needed (MMAudio outputs at 44100 Hz)
    if sr != TARGET_SR:
        print(f"[MMAudio regen upsample] {sr}Hz β†’ {TARGET_SR}Hz (sinc, CPU) …")
        new_wav = _resample_to_target(new_wav, sr)
        sr = TARGET_SR
    meta["sr"] = sr

    # CPU: splice, stitch, mux, save
    video_path, audio_path, updated_meta, waveform_html = _splice_and_save(
        new_wav, seg_idx, meta, slot_id
    )
    return video_path, audio_path, json.dumps(updated_meta), waveform_html


def _hunyuan_regen_duration(video_file, seg_idx, seg_meta_json,
                             prompt, negative_prompt, seed_val,
                             guidance_scale, num_steps, model_size,
                             crossfade_s, crossfade_db, slot_id=None):
    return _estimate_regen_duration("hunyuan", int(num_steps))


@spaces.GPU(duration=_hunyuan_regen_duration)
def _regen_hunyuan_gpu(video_file, seg_idx, seg_meta_json,
                       prompt, negative_prompt, seed_val,
                       guidance_scale, num_steps, model_size,
                       crossfade_s, crossfade_db, slot_id=None):
    """GPU-only HunyuanFoley regen β€” returns (new_wav, sr) for a single segment."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)
    seg_start, seg_end = meta["segments"][seg_idx]
    seg_dur = seg_end - seg_start

    _ensure_syspath("HunyuanVideo-Foley")
    from hunyuanvideo_foley.utils.model_utils  import denoise_process
    from hunyuanvideo_foley.utils.feature_utils import feature_process

    device, _   = _get_device_and_dtype()
    model_dict, cfg = _load_hunyuan_model(device, model_size)

    set_global_seed(random.randint(0, 2**32 - 1))

    # Extract segment clip inside the GPU function β€” ffmpeg is CPU-only and safe here.
    seg_path = _extract_segment_clip(
        _resolve_silent_video(meta), seg_start, seg_dur,
        os.path.join(_register_tmp_dir(tempfile.mkdtemp()), "regen_seg.mp4"),
    )

    text_feats_path = meta.get("text_feats_path", "")
    if text_feats_path and os.path.exists(text_feats_path):
        print("[HunyuanFoley regen] Loading cached text features from disk")
        from hunyuanvideo_foley.utils.feature_utils import encode_video_features
        visual_feats, seg_audio_len = encode_video_features(seg_path, model_dict)
        text_feats = torch.load(text_feats_path, map_location=device, weights_only=False)
    else:
        print("[HunyuanFoley regen] Cache miss β€” extracting text + visual features")
        visual_feats, text_feats, seg_audio_len = feature_process(
            seg_path, prompt if prompt else "", model_dict, cfg,
            neg_prompt=negative_prompt if negative_prompt else None,
        )
    audio_batch, sr = denoise_process(
        visual_feats, text_feats, seg_audio_len, model_dict, cfg,
        guidance_scale=float(guidance_scale),
        num_inference_steps=int(num_steps),
        batch_size=1,
    )
    new_wav = audio_batch[0].float().cpu().numpy()  # full window β€” _stitch_wavs trims
    return new_wav, sr


def regen_hunyuan_segment(video_file, seg_idx, seg_meta_json,
                          prompt, negative_prompt, seed_val,
                          guidance_scale, num_steps, model_size,
                          crossfade_s, crossfade_db, slot_id):
    """Regenerate one HunyuanFoley segment. GPU inference + CPU splice/save."""
    meta    = json.loads(seg_meta_json)
    seg_idx = int(seg_idx)

    # GPU: inference (segment clip extraction happens inside the GPU function)
    new_wav, sr = _regen_hunyuan_gpu(video_file, seg_idx, seg_meta_json,
                                     prompt, negative_prompt, seed_val,
                                     guidance_scale, num_steps, model_size,
                                     crossfade_s, crossfade_db, slot_id)

    meta["sr"] = sr

    # CPU: splice, stitch, mux, save
    video_path, audio_path, updated_meta, waveform_html = _splice_and_save(
        new_wav, seg_idx, meta, slot_id
    )
    return video_path, audio_path, json.dumps(updated_meta), waveform_html


# Wire up regen_fn references now that the functions are defined
MODEL_CONFIGS["taro"]["regen_fn"]    = regen_taro_segment
MODEL_CONFIGS["mmaudio"]["regen_fn"] = regen_mmaudio_segment
MODEL_CONFIGS["hunyuan"]["regen_fn"] = regen_hunyuan_segment


# ================================================================== #
#                  CROSS-MODEL REGEN WRAPPERS                        #
# ================================================================== #
# Three shared endpoints β€” one per model β€” that can be called from   #
# *any* slot tab.  slot_id is passed as plain string data so the     #
# result is applied back to the correct slot by the JS listener.     #
# The new segment is resampled to match the slot's existing SR before #
# being handed to _splice_and_save, so TARO (16 kHz) / MMAudio      #
# (44.1 kHz) / Hunyuan (48 kHz) outputs can all be mixed freely.    #
# ================================================================== #

def _resample_to_slot_sr(wav: np.ndarray, src_sr: int, dst_sr: int,
                         slot_wav_ref: np.ndarray = None) -> np.ndarray:
    """Resample *wav* from src_sr to dst_sr, then match channel layout to
    *slot_wav_ref* (the first existing segment in the slot).

    TARO is mono (T,), MMAudio/Hunyuan are stereo (C, T).  Mixing them
    without normalisation causes a shape mismatch in _cf_join.  Rules:
      - stereo β†’ mono : average channels
      - mono   β†’ stereo: duplicate the single channel
    """
    wav = _resample_to_target(wav, src_sr, dst_sr)

    # Match channel layout to the slot's existing segments
    if slot_wav_ref is not None:
        slot_stereo = slot_wav_ref.ndim == 2
        wav_stereo  = wav.ndim == 2
        if slot_stereo and not wav_stereo:
            wav = np.stack([wav, wav], axis=0)   # mono β†’ stereo (C, T)
        elif not slot_stereo and wav_stereo:
            wav = wav.mean(axis=0)               # stereo β†’ mono  (T,)
    return wav


def _resolve_silent_video(meta: dict) -> str:
    """Return a valid silent (audio-stripped) video path for *meta*.

    Prefers meta["silent_video"] if it still exists on disk.  Falls back to
    re-stripping meta["source_video"] (the original Gradio upload path, which
    persists for the full session lifetime) into a fresh tmp file.
    This prevents xregen failures caused by tmp-dir eviction or Space restarts
    between initial generation and the regen call.
    """
    sv = meta.get("silent_video", "")
    if sv and os.path.exists(sv):
        return sv
    source = meta.get("source_video") or meta.get("video_path", "")
    if not source or not os.path.exists(source):
        raise FileNotFoundError(
            f"Cannot locate source video for regen β€” "
            f"silent_video={sv!r}, source_video={source!r}"
        )
    out = os.path.join(_register_tmp_dir(tempfile.mkdtemp()), "silent_input.mp4")
    print(f"[regen] silent_video missing, re-stripping from source_video: {source}")
    strip_audio_from_video(source, out)
    return out


def _xregen_clip_window(meta: dict, seg_idx: int, target_window_s: float) -> tuple:
    """Compute the video clip window for a cross-model regen.

    Centers *target_window_s* on the original segment's midpoint, clamped to
    [0, total_dur_s].  Returns (clip_start, clip_end, clip_dur).

    If the video is shorter than *target_window_s*, the full video is used
    (suboptimal but never breaks).  If the segment span exceeds
    *target_window_s*, the caller should run _build_segments on the span and
    generate multiple sub-segments β€” but the clip window is still returned as
    the full segment span so the caller can decide.
    """
    total_dur_s = float(meta["total_dur_s"])
    seg_start, seg_end = meta["segments"][seg_idx]
    seg_mid   = (seg_start + seg_end) / 2.0
    half_win  = target_window_s / 2.0

    clip_start = max(0.0, seg_mid - half_win)
    clip_end   = min(total_dur_s, seg_mid + half_win)
    # If clamped at one end, extend the other to preserve full window if possible
    if clip_start == 0.0:
        clip_end = min(total_dur_s, target_window_s)
    elif clip_end == total_dur_s:
        clip_start = max(0.0, total_dur_s - target_window_s)
    clip_dur = clip_end - clip_start
    return clip_start, clip_end, clip_dur


def _xregen_splice(new_wav_raw: np.ndarray, src_sr: int,
                   meta: dict, seg_idx: int, slot_id: str,
                   clip_start_s: float = None) -> tuple:
    """Shared epilogue for all xregen_* functions: resample β†’ splice β†’ save.
    Returns (video_path, waveform_html).

    *clip_start_s* is the absolute video time where new_wav_raw starts.
    When the clip was centered on the segment midpoint (not at seg_start),
    we need to shift the wav so _stitch_wavs can trim it correctly relative
    to the original segment's start.  We do this by prepending silence so
    the wav's time origin aligns with the original segment's start.
    """
    slot_sr   = int(meta["sr"])
    slot_wavs = _load_seg_wavs(meta["wav_paths"])
    new_wav   = _resample_to_slot_sr(new_wav_raw, src_sr, slot_sr, slot_wavs[0])

    # Align new_wav so sample index 0 corresponds to seg_start in video time.
    # _stitch_wavs trims using seg_start as the time origin, so if the clip
    # started AFTER seg_start (clip_start_s > seg_start), we prepend silence
    # equal to (clip_start_s - seg_start) to shift the audio back to seg_start.
    if clip_start_s is not None:
        seg_start = meta["segments"][seg_idx][0]
        offset_s  = seg_start - clip_start_s   # negative when clip starts after seg_start
        if offset_s < 0:
            pad_samples = int(round(abs(offset_s) * slot_sr))
            silence = np.zeros(
                (new_wav.shape[0], pad_samples) if new_wav.ndim == 2 else pad_samples,
                dtype=new_wav.dtype,
            )
            new_wav = np.concatenate([silence, new_wav], axis=1 if new_wav.ndim == 2 else 0)

    video_path, audio_path, updated_meta, waveform_html = _splice_and_save(
        new_wav, seg_idx, meta, slot_id
    )
    return video_path, waveform_html


def _xregen_dispatch(state_json: str, seg_idx: int, slot_id: str, infer_fn):
    """Shared generator skeleton for all xregen_* wrappers.

    Yields pending HTML immediately, then calls *infer_fn()* β€” a zero-argument
    callable that runs model-specific CPU prep + GPU inference and returns
    (wav_array, src_sr, clip_start_s).  For TARO, *infer_fn* should return
    the wav already upsampled to 48 kHz; pass TARO_SR_OUT as src_sr.

    Yields:
        First:  (gr.update(), gr.update(value=pending_html))  β€” shown while GPU runs
        Second: (gr.update(value=video_path), gr.update(value=waveform_html))
    """
    import traceback as _tb
    meta         = json.loads(state_json)
    pending_html = _build_regen_pending_html(meta["segments"], seg_idx, slot_id, "")
    yield gr.update(), gr.update(value=pending_html)

    print(f"[_xregen_dispatch] slot={slot_id} seg={seg_idx} calling infer_fn={infer_fn}")
    try:
        new_wav_raw, src_sr, clip_start_s = infer_fn()
        print(f"[_xregen_dispatch] infer_fn returned wav shape={getattr(new_wav_raw,'shape',None)} sr={src_sr} clip_start={clip_start_s}")
    except Exception as _e:
        print(f"[_xregen_dispatch] EXCEPTION in infer_fn: {_e}")
        _tb.print_exc()
        raise
    video_path, waveform_html = _xregen_splice(new_wav_raw, src_sr, meta, seg_idx, slot_id, clip_start_s)
    print(f"[_xregen_dispatch] splice done video_path={video_path!r}")
    yield gr.update(value=video_path), gr.update(value=waveform_html)


def xregen_taro(seg_idx, state_json, slot_id,
                seed_val, cfg_scale, num_steps, mode,
                crossfade_s, crossfade_db,
                request: gr.Request = None):
    """Cross-model regen: run TARO on its optimal window, splice into *slot_id*."""
    seg_idx = int(seg_idx)
    meta    = json.loads(state_json)

    def _run():
        clip_start, clip_end, clip_dur = _xregen_clip_window(meta, seg_idx, TARO_MODEL_DUR)
        tmp_dir    = _register_tmp_dir(tempfile.mkdtemp())
        clip_path  = _extract_segment_clip(
            _resolve_silent_video(meta), clip_start, clip_dur,
            os.path.join(tmp_dir, "xregen_taro_clip.mp4"),
        )
        # Build a minimal fake-video meta so generate_taro can run on clip_path
        sub_segs   = _build_segments(clip_dur, TARO_MODEL_DUR, float(crossfade_s))
        sub_meta_json = json.dumps({
            "segments": sub_segs, "silent_video": clip_path,
            "total_dur_s": clip_dur,
        })
        # Run full TARO generation pipeline on the clip
        _ctx_store("taro_gpu_infer", {
            "tmp_dir": tmp_dir, "silent_video": clip_path,
            "segments": sub_segs, "total_dur_s": clip_dur,
        })
        results = _taro_gpu_infer(clip_path, seed_val, cfg_scale, num_steps, mode,
                                  crossfade_s, crossfade_db, 1)
        wavs, _, _ = results[0]
        wavs = [_upsample_taro(w) for w in wavs]
        wav  = _stitch_wavs(wavs, float(crossfade_s), float(crossfade_db),
                            clip_dur, TARO_SR_OUT, sub_segs)
        return wav, TARO_SR_OUT, clip_start

    yield from _xregen_dispatch(state_json, seg_idx, slot_id, _run)


def xregen_mmaudio(seg_idx, state_json, slot_id,
                   prompt, negative_prompt, seed_val,
                   cfg_strength, num_steps, crossfade_s, crossfade_db,
                   request: gr.Request = None):
    """Cross-model regen: run MMAudio on its optimal window, splice into *slot_id*."""
    seg_idx = int(seg_idx)
    meta    = json.loads(state_json)

    def _run():
        clip_start, clip_end, clip_dur = _xregen_clip_window(meta, seg_idx, MMAUDIO_WINDOW)
        source_video = _resolve_silent_video(meta)
        sub_segs = _build_segments(clip_dur, MMAUDIO_WINDOW, float(crossfade_s))
        print(f"[xregen_mmaudio._run] clip_start={clip_start} clip_dur={clip_dur} source_video={source_video!r} sub_segs={sub_segs}")
        # Pass clip_start_s/clip_dur_s so the GPU fn extracts the clip internally β€”
        # pre-extracted tmp files are invisible to the ZeroGPU worker process.
        results = _mmaudio_gpu_infer(source_video, prompt, negative_prompt, seed_val,
                                     cfg_strength, num_steps, crossfade_s, crossfade_db, 1,
                                     source_video, json.dumps(sub_segs),
                                     clip_start, clip_dur)
        print(f"[xregen_mmaudio._run] gpu_infer returned {len(results)} results")
        seg_wavs, sr = results[0]
        wav = _stitch_wavs(seg_wavs, float(crossfade_s), float(crossfade_db),
                           clip_dur, sr, sub_segs)
        if sr != TARGET_SR:
            wav = _resample_to_target(wav, sr)
            sr  = TARGET_SR
        return wav, sr, clip_start

    yield from _xregen_dispatch(state_json, seg_idx, slot_id, _run)


def xregen_hunyuan(seg_idx, state_json, slot_id,
                   prompt, negative_prompt, seed_val,
                   guidance_scale, num_steps, model_size,
                   crossfade_s, crossfade_db,
                   request: gr.Request = None):
    """Cross-model regen: run HunyuanFoley on its optimal window, splice into *slot_id*."""
    seg_idx = int(seg_idx)
    meta    = json.loads(state_json)

    def _run():
        clip_start, clip_end, clip_dur = _xregen_clip_window(meta, seg_idx, HUNYUAN_MAX_DUR)
        source_video = _resolve_silent_video(meta)
        sub_segs = _build_segments(clip_dur, HUNYUAN_MAX_DUR, float(crossfade_s))
        results  = _hunyuan_gpu_infer(source_video, prompt, negative_prompt, seed_val,
                                      guidance_scale, num_steps, model_size,
                                      crossfade_s, crossfade_db, 1,
                                      source_video, json.dumps(sub_segs), clip_dur,
                                      clip_start, clip_dur)
        seg_wavs, sr, _ = results[0]
        wav = _stitch_wavs(seg_wavs, float(crossfade_s), float(crossfade_db),
                           clip_dur, sr, sub_segs)
        return wav, sr, clip_start

    yield from _xregen_dispatch(state_json, seg_idx, slot_id, _run)


# ================================================================== #
#                        SHARED UI HELPERS                            #
# ================================================================== #

def _register_regen_handlers(tab_prefix, model_key, regen_seg_tb, regen_state_tb,
                              input_components, slot_vids, slot_waves):
    """Register per-slot regen button handlers for a model tab.

    This replaces the three nearly-identical for-loops that previously existed
    for TARO, MMAudio, and HunyuanFoley tabs.

    Args:
        tab_prefix:       e.g. "taro", "mma", "hf"
        model_key:        e.g. "taro", "mmaudio", "hunyuan"
        regen_seg_tb:     gr.Textbox for seg_idx (render=False)
        regen_state_tb:   gr.Textbox for state_json (render=False)
        input_components: list of Gradio input components (video, seed, etc.)
                          β€” order must match regen_fn signature after (seg_idx, state_json, video)
        slot_vids:        list of gr.Video components per slot
        slot_waves:       list of gr.HTML components per slot
    Returns:
        list of hidden gr.Buttons (one per slot)
    """
    cfg      = MODEL_CONFIGS[model_key]
    regen_fn = cfg["regen_fn"]
    label    = cfg["label"]
    btns     = []
    for _i in range(MAX_SLOTS):
        _slot_id = f"{tab_prefix}_{_i}"
        _btn = gr.Button(render=False, elem_id=f"regen_btn_{_slot_id}")
        btns.append(_btn)
        print(f"[startup] registering regen handler for slot {_slot_id}")

        def _make_regen(_si, _sid, _model_key, _label, _regen_fn):
            def _do(seg_idx, state_json, *args):
                print(f"[regen {_label}] slot={_sid} seg_idx={seg_idx} "
                      f"state_json_len={len(state_json) if state_json else 0}")
                if not state_json:
                    print(f"[regen {_label}] early-exit: state_json empty")
                    yield gr.update(), gr.update()
                    return
                lock = _get_slot_lock(_sid)
                with lock:
                    state        = json.loads(state_json)
                    pending_html = _build_regen_pending_html(
                        state["segments"], int(seg_idx), _sid, ""
                    )
                    yield gr.update(), gr.update(value=pending_html)
                    print(f"[regen {_label}] slot={_sid} seg_idx={seg_idx} β€” calling regen")
                    try:
                        # args[0] = video, args[1:] = model-specific params
                        vid, aud, new_meta_json, html = _regen_fn(
                            args[0], int(seg_idx), state_json, *args[1:], _sid,
                        )
                        print(f"[regen {_label}] slot={_sid} seg_idx={seg_idx} β€” done, vid={vid!r}")
                    except Exception as _e:
                        print(f"[regen {_label}] slot={_sid} seg_idx={seg_idx} β€” ERROR: {_e}")
                        raise
                    yield gr.update(value=vid), gr.update(value=html)
            return _do

        _btn.click(
            fn=_make_regen(_i, _slot_id, model_key, label, regen_fn),
            inputs=[regen_seg_tb, regen_state_tb] + input_components,
            outputs=[slot_vids[_i], slot_waves[_i]],
            api_name=f"regen_{tab_prefix}_{_i}",
        )
    return btns


def _pad_outputs(outputs: list) -> list:
    """Flatten (video, audio, seg_meta) triples and pad to MAX_SLOTS * 3 with None.

    Each entry in *outputs* must be a (video_path, audio_path, seg_meta) tuple where
    seg_meta = {"segments": [...], "audio_path": str, "video_path": str,
                "sr": int, "model": str, "crossfade_s": float,
                "crossfade_db": float, "wav_paths": list[str]}
    """
    result = []
    for i in range(MAX_SLOTS):
        if i < len(outputs):
            result.extend(outputs[i])          # 3 items: video, audio, meta
        else:
            result.extend([None, None, None])
    return result


# ------------------------------------------------------------------ #
# WaveSurfer waveform + segment marker HTML builder                   #
# ------------------------------------------------------------------ #

def _build_regen_pending_html(segments: list, regen_seg_idx: int, slot_id: str,
                              hidden_input_id: str) -> str:
    """Return a waveform placeholder shown while a segment is being regenerated.

    Renders a dark bar with the active segment highlighted in amber + a spinner.
    """
    segs_json = json.dumps(segments)
    seg_colors = [c.format(a="0.25") for c in SEG_COLORS]
    active_color = "rgba(255,180,0,0.55)"
    duration = segments[-1][1] if segments else 1.0

    # Compute contact edges: midpoint of overlap between consecutive segments.
    # Matches the contact-edge formula used in _build_waveform_html's canvas JS
    # so segment boundaries stay visually identical during pending regen.
    contact_edges = []
    for i in range(len(segments) - 1):
        contact_edges.append((segments[i][1] + segments[i + 1][0]) / 2)

    seg_divs = ""
    for i, seg in enumerate(segments):
        left_t  = 0.0              if i == 0                    else contact_edges[i - 1]
        right_t = duration         if i == len(segments) - 1    else contact_edges[i]
        left_pct  = left_t  / duration * 100
        width_pct = (right_t - left_t) / duration * 100
        color     = active_color if i == regen_seg_idx else seg_colors[i % len(seg_colors)]
        extra     = "border:2px solid #ffb300;animation:wf_pulse 0.8s ease-in-out infinite alternate;" if i == regen_seg_idx else ""
        seg_divs += (
            f'<div style="position:absolute;top:0;left:{left_pct:.2f}%;'
            f'width:{width_pct:.2f}%;height:100%;background:{color};{extra}">'
            f'<span style="color:rgba(255,255,255,0.7);font-size:10px;padding:2px 3px;">Seg {i+1}</span>'
            f'</div>'
        )

    spinner = (
        '<div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);'
        'display:flex;align-items:center;gap:6px;">'
        '<div style="width:14px;height:14px;border:2px solid #ffb300;'
        'border-top-color:transparent;border-radius:50%;'
        'animation:wf_spin 0.7s linear infinite;"></div>'
        f'<span style="color:#ffb300;font-size:12px;white-space:nowrap;">'
        f'Regenerating Seg {regen_seg_idx+1}…</span>'
        '</div>'
    )

    return f"""
<style>
@keyframes wf_pulse {{from{{opacity:0.5}}to{{opacity:1}}}}
@keyframes wf_spin  {{to{{transform:rotate(360deg)}}}}
</style>
<div style="background:#1a1a1a;border-radius:8px;padding:10px;margin-top:6px;">
  <div style="position:relative;width:100%;height:80px;background:#1e1e2e;border-radius:4px;overflow:hidden;">
    {seg_divs}
    {spinner}
  </div>
  <div style="color:#888;font-size:11px;margin-top:6px;">Regenerating β€” please wait…</div>
</div>
"""


def _build_waveform_html(audio_path: str, segments: list, slot_id: str,
                         hidden_input_id: str, state_json: str = "",
                         fn_index: int = -1, video_path: str = "",
                         crossfade_s: float = 0.0) -> str:
    """Return a self-contained HTML block with a Canvas waveform (display only),
    segment boundary markers, and a download link.

    Uses Web Audio API + Canvas β€” no external libraries.
    The waveform is SILENT. The playhead tracks the Gradio <video> element
    in the same slot via its timeupdate event.
    """
    print(f"[_build_waveform_html] audio_path={audio_path!r} exists={os.path.exists(audio_path) if audio_path else False} slot={slot_id}")
    if not audio_path or not os.path.exists(audio_path):
        print(f"[_build_waveform_html] returning placeholder β€” audio missing")
        return "<p style='color:#888;font-size:12px'>No audio yet.</p>"

    # Serve audio via Gradio's file API instead of base64-encoding the entire
    # WAV inline. For a 25s stereo 44.1kHz track this saves ~5 MB per slot.
    audio_url = f"/gradio_api/file={audio_path}"

    segs_json = json.dumps(segments)
    seg_colors = [c.format(a="0.35") for c in SEG_COLORS]

    # NOTE: Gradio updates gr.HTML via innerHTML which does NOT execute <script> tags.
    # Solution: put the entire waveform (canvas + JS) inside an <iframe srcdoc="...">.
    # iframes always execute their scripts. The iframe posts messages to the parent for
    # segment-click events; the parent listens and fires the Gradio regen trigger.
    # For playhead sync, the iframe polls window.parent for a <video> element.

    iframe_inner = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
  * {{ margin:0; padding:0; box-sizing:border-box; }}
  body {{ background:#1a1a1a; overflow:hidden; }}
  #wrap {{ position:relative; width:100%; height:80px; }}
  canvas {{ display:block; }}
  #cv  {{ position:absolute; top:0; left:0; width:100%; height:100%; }}
  #cvp {{ position:absolute; top:0; left:0; width:100%; height:100%; pointer-events:none; }}
</style>
</head>
<body>
<div id="wrap">
  <canvas id="cv"></canvas>
  <canvas id="cvp"></canvas>
</div>
<script>
(function() {{
  const SLOT_ID  = '{slot_id}';
  const segments = {segs_json};
  const segColors = {json.dumps(seg_colors)};
  const crossfadeSec = {crossfade_s};
  let   audioDuration = 0;

  // ── Popup via postMessage to parent global listener ─────────────────
  // The parent page (Gradio) has a global window.addEventListener('message',...)
  // set up via gr.Blocks(js=...) that handles popup show/hide and regen trigger.
  function showPopup(idx, mx, my) {{
    console.log('[wf showPopup] slot='+SLOT_ID+' idx='+idx+' posting to parent');
    // Convert iframe-local coords to parent page coords
    try {{
      const fr = window.frameElement ? window.frameElement.getBoundingClientRect() : {{left:0,top:0}};
      window.parent.postMessage({{
        type:'wf_popup', action:'show',
        slot_id: SLOT_ID, seg_idx: idx,
        t0: segments[idx][0], t1: segments[idx][1],
        x: mx + fr.left, y: my + fr.top
      }}, '*');
      console.log('[wf showPopup] postMessage sent OK');
    }} catch(e) {{
      console.log('[wf showPopup] postMessage fallback, err='+e.message);
      window.parent.postMessage({{
        type:'wf_popup', action:'show',
        slot_id: SLOT_ID, seg_idx: idx,
        t0: segments[idx][0], t1: segments[idx][1],
        x: mx, y: my
      }}, '*');
    }}
  }}
  function hidePopup() {{
    window.parent.postMessage({{type:'wf_popup', action:'hide'}}, '*');
  }}

  // ── Canvas waveform ────────────────────────────────────────────────
  const cv  = document.getElementById('cv');
  const cvp = document.getElementById('cvp');
  const wrap= document.getElementById('wrap');

  function drawWaveform(channelData, duration) {{
    audioDuration = duration;
    const dpr = window.devicePixelRatio || 1;
    const W   = wrap.getBoundingClientRect().width  || window.innerWidth || 600;
    const H   = 80;
    cv.width  = W * dpr;  cv.height = H * dpr;
    const ctx = cv.getContext('2d');
    ctx.scale(dpr, dpr);

    ctx.fillStyle = '#1e1e2e';
    ctx.fillRect(0, 0, W, H);

    // Compute contact edges: midpoint of overlap between consecutive segments.
    // Each segment is colored from its left contact edge to its right contact edge.
    // First segment starts at 0; last segment ends at duration.
    const contactEdges = [];
    for (let i = 0; i < segments.length - 1; i++) {{
      contactEdges.push((segments[i][1] + segments[i+1][0]) / 2);
    }}

    segments.forEach(function(seg, idx) {{
      const x1   = idx === 0 ? 0 : (contactEdges[idx-1] / duration) * W;
      const xEnd = idx === segments.length - 1
        ? W
        : (contactEdges[idx] / duration) * W;
      ctx.fillStyle = segColors[idx % segColors.length];
      ctx.fillRect(x1, 0, xEnd - x1, H);
      ctx.fillStyle = 'rgba(255,255,255,0.6)';
      ctx.font = '10px sans-serif';
      ctx.fillText('Seg '+(idx+1), x1+3, 12);
    }});

    const samples = channelData.length;
    const barW=2, gap=1, step=barW+gap;
    const numBars = Math.floor(W / step);
    const blockSz = Math.floor(samples / numBars);
    ctx.fillStyle = '#4a9eff';
    for (let i=0; i<numBars; i++) {{
      let max=0;
      const s=i*blockSz;
      for (let j=0; j<blockSz; j++) {{
        const v=Math.abs(channelData[s+j]||0);
        if (v>max) max=v;
      }}
      const barH=Math.max(1, max*H);
      ctx.fillRect(i*step, (H-barH)/2, barW, barH);
    }}

    segments.forEach(function(seg) {{
      [seg[0],seg[1]].forEach(function(t) {{
        const x=(t/duration)*W;
        ctx.strokeStyle='rgba(255,255,255,0.4)';
        ctx.lineWidth=1;
        ctx.beginPath(); ctx.moveTo(x,0); ctx.lineTo(x,H); ctx.stroke();
      }});
    }});

    // ── Crossfade overlap indicators ──
    // The crossfade is applied Β±crossfadeSec/2 around each contact edge.
    // contactEdges[i] = midpoint of overlap between seg i and seg i+1.
    if (crossfadeSec > 0 && segments.length > 1) {{
      for (let i = 0; i < segments.length - 1; i++) {{
        // Hatch spans crossfadeSec centred on the contact edge
        const edgeT        = contactEdges[i];
        const overlapStart = edgeT - crossfadeSec / 2;
        const overlapEnd   = edgeT + crossfadeSec / 2;
        const xL = (overlapStart / duration) * W;
        const xR = (overlapEnd   / duration) * W;
        // Diagonal hatch pattern over the overlap zone
        ctx.save();
        ctx.beginPath();
        ctx.rect(xL, 0, xR - xL, H);
        ctx.clip();
        ctx.strokeStyle = 'rgba(255,255,255,0.35)';
        ctx.lineWidth = 1;
        const spacing = 6;
        for (let lx = xL - H; lx < xR + H; lx += spacing) {{
          ctx.beginPath();
          ctx.moveTo(lx, H);
          ctx.lineTo(lx + H, 0);
          ctx.stroke();
        }}
        ctx.restore();
      }}
    }}

    cv.onclick = function(e) {{
      const r=cv.getBoundingClientRect();
      const xRel=(e.clientX-r.left)/r.width;
      const tClick=xRel*duration;
      // Pick the segment by contact-edge boundaries β€” matches the visual coloring.
      // Seg 0 owns [0, contactEdges[0]), seg N-1 owns [contactEdges[N-2], duration).
      let hit=-1;
      segments.forEach(function(seg,idx){{
        const visStart = idx === 0 ? 0 : contactEdges[idx-1];
        const visEnd   = idx === segments.length - 1 ? duration : contactEdges[idx];
        if (tClick >= visStart && tClick < visEnd) hit = idx;
      }});
      console.log('[wf click] tClick='+tClick.toFixed(2)+' hit='+hit+' audioDuration='+audioDuration+' segments='+JSON.stringify(segments));
      if (hit>=0) showPopup(hit, e.clientX, e.clientY);
      else hidePopup();
    }};
  }}

  function drawPlayhead(progress) {{
    const dpr = window.devicePixelRatio || 1;
    const W   = wrap.getBoundingClientRect().width || window.innerWidth || 600;
    const H   = 80;
    if (cvp.width !== W*dpr) {{ cvp.width=W*dpr; cvp.height=H*dpr; }}
    const ctx = cvp.getContext('2d');
    ctx.clearRect(0,0,W*dpr,H*dpr);
    ctx.save();
    ctx.scale(dpr,dpr);
    const x=progress*W;
    ctx.strokeStyle='#fff';
    ctx.lineWidth=2;
    ctx.beginPath(); ctx.moveTo(x,0); ctx.lineTo(x,H); ctx.stroke();
    ctx.restore();
  }}

  // Poll parent for video time β€” find the video in the same wf_container slot
  function findSlotVideo() {{
    try {{
      const par = window.parent.document;
      // Walk up from our iframe to find wf_container_{slot_id}, then find its sibling video
      const container = par.getElementById('wf_container_{slot_id}');
      if (!container) return par.querySelector('video');
      // The video is inside the same gr.Group β€” walk up to find it
      let node = container.parentElement;
      while (node && node !== par.body) {{
        const v = node.querySelector('video');
        if (v) return v;
        node = node.parentElement;
      }}
      return null;
    }} catch(e) {{ return null; }}
  }}

  setInterval(function() {{
    const vid = findSlotVideo();
    if (vid && vid.duration && isFinite(vid.duration) && audioDuration > 0) {{
      drawPlayhead(vid.currentTime / vid.duration);
    }}
  }}, 50);

  // ── Fetch + decode audio from Gradio file API ──────────────────────
  const audioUrl = '{audio_url}';
  fetch(audioUrl)
    .then(function(r) {{ return r.arrayBuffer(); }})
    .then(function(arrayBuf) {{
      const AudioCtx = window.AudioContext || window.webkitAudioContext;
      if (!AudioCtx) return;
      const tmpCtx = new AudioCtx({{sampleRate:44100}});
      tmpCtx.decodeAudioData(arrayBuf,
        function(ab) {{
          try {{ tmpCtx.close(); }} catch(e) {{}}
          function tryDraw() {{
            const W = wrap.getBoundingClientRect().width || window.innerWidth;
            if (W > 0) {{ drawWaveform(ab.getChannelData(0), ab.duration); }}
            else {{ setTimeout(tryDraw, 100); }}
          }}
          tryDraw();
        }},
        function(err) {{}}
      );
    }})
    .catch(function(e) {{}});
}})();
</script>
</body>
</html>"""

    # Escape for HTML attribute (srcdoc uses HTML entities)
    srcdoc         = _html.escape(iframe_inner, quote=True)
    state_escaped  = _html.escape(state_json or "", quote=True)

    return f"""
<div id="wf_container_{slot_id}"
     data-fn-index="{fn_index}"
     data-state="{state_escaped}"
     style="background:#1a1a1a;border-radius:8px;padding:10px;margin-top:6px;position:relative;">
  <div style="position:relative;width:100%;height:80px;">
    <iframe id="wf_iframe_{slot_id}"
            srcdoc="{srcdoc}"
            sandbox="allow-scripts allow-same-origin"
            style="width:100%;height:80px;border:none;border-radius:4px;display:block;"
            scrolling="no"></iframe>
  </div>
  <div style="display:flex;align-items:center;gap:8px;margin-top:6px;">
    <span id="wf_statusbar_{slot_id}" style="color:#888;font-size:11px;">Click a segment to regenerate &nbsp;|&nbsp; Playhead syncs to video</span>
    <a href="{audio_url}" download="audio_{slot_id}.wav"
       style="margin-left:auto;background:#333;color:#eee;border:1px solid #555;
              border-radius:4px;padding:3px 10px;font-size:12px;text-decoration:none;">
      &#8595; Audio</a>{f'''
    <a href="/gradio_api/file={video_path}" download="video_{slot_id}.mp4"
       style="background:#333;color:#eee;border:1px solid #555;
              border-radius:4px;padding:3px 10px;font-size:12px;text-decoration:none;">
      &#8595; Video</a>''' if video_path else ''}
  </div>
  <div id="wf_seglabel_{slot_id}"
       style="color:#aaa;font-size:11px;margin-top:4px;min-height:16px;"></div>
</div>
"""


def _make_output_slots(tab_prefix: str) -> tuple:
    """Build MAX_SLOTS output groups for one tab.

    Each slot has: video and waveform HTML.
    Regen is triggered via direct Gradio queue API calls from JS (no hidden
    trigger textboxes needed β€” DOM event dispatch is unreliable in Gradio 5
    Svelte components).  State JSON is embedded in the waveform HTML's
    data-state attribute and passed directly in the queue API payload.
    Returns (grps, vids, waveforms).
    """
    grps, vids, waveforms = [], [], []
    for i in range(MAX_SLOTS):
        slot_id = f"{tab_prefix}_{i}"
        with gr.Group(visible=(i == 0)) as g:
            vids.append(gr.Video(label=f"Generation {i+1} β€” Video",
                                 elem_id=f"slot_vid_{slot_id}",
                                 show_download_button=False))
            waveforms.append(gr.HTML(
                value="<p style='color:#888;font-size:12px'>Generate audio to see waveform.</p>",
                elem_id=f"slot_wave_{slot_id}",
            ))
        grps.append(g)
    return grps, vids, waveforms


def _unpack_outputs(flat: list, n: int, tab_prefix: str) -> list:
    """Turn a flat _pad_outputs list into Gradio update lists.

    flat has MAX_SLOTS * 3 items: [vid0, aud0, meta0, vid1, aud1, meta1, ...]
    Returns updates for vids + waveforms only (NOT grps).
    Group visibility is handled separately via .then() to avoid Gradio 5 SSR
    'Too many arguments' caused by mixing gr.Group updates with other outputs.
    State JSON is embedded in the waveform HTML data-state attribute so JS
    can read it when calling the Gradio queue API for regen.
    """
    n = int(n)
    print(f"[_unpack_outputs] tab={tab_prefix} n={n} flat_len={len(flat)}")
    vid_updates  = []
    wave_updates = []
    for i in range(MAX_SLOTS):
        vid_path  = flat[i * 3]
        aud_path  = flat[i * 3 + 1]
        meta      = flat[i * 3 + 2]
        print(f"[_unpack_outputs] slot {i}: vid={vid_path is not None} aud={aud_path!r} meta={meta is not None}")
        vid_updates.append(gr.update(value=vid_path))
        if aud_path and meta:
            slot_id      = f"{tab_prefix}_{i}"
            state_json   = json.dumps(meta)
            html = _build_waveform_html(aud_path, meta["segments"], slot_id,
                                        "", state_json=state_json,
                                        video_path=meta.get("video_path", ""),
                                        crossfade_s=float(meta.get("crossfade_s", 0)))
            wave_updates.append(gr.update(value=html))
        else:
            wave_updates.append(gr.update(
                value="<p style='color:#888;font-size:12px'>Generate audio to see waveform.</p>"
            ))
    return vid_updates + wave_updates


def _on_video_upload_taro(video_file, num_steps, crossfade_s):
    if video_file is None:
        return gr.update(maximum=MAX_SLOTS, value=1)
    try:
        D     = get_video_duration(video_file)
        max_s = _taro_calc_max_samples(D, int(num_steps), float(crossfade_s))
    except Exception:
        max_s = MAX_SLOTS
    return gr.update(maximum=max_s, value=min(1, max_s))


def _update_slot_visibility(n):
    n = int(n)
    return [gr.update(visible=(i < n)) for i in range(MAX_SLOTS)]


# ================================================================== #
#                       GRADIO UI                                     #
# ================================================================== #

_SLOT_CSS = """
/* Responsive video: fills column width, height auto from aspect ratio */
.gradio-video video {
    width: 100%;
    height: auto;
    max-height: 60vh;
    object-fit: contain;
}
/* Force two-column layout to stay equal-width */
.gradio-container .gradio-row > .gradio-column {
    flex: 1 1 0 !important;
    min-width: 0 !important;
    max-width: 50% !important;
}
/* Hide the built-in download button on output video slots β€” downloads are
   handled by the waveform panel links which always reflect the latest regen. */
[id^="slot_vid_"] .download-icon,
[id^="slot_vid_"] button[aria-label="Download"],
[id^="slot_vid_"] a[download] {
    display: none !important;
}
"""

_GLOBAL_JS = """
() => {
  // Global postMessage handler for waveform iframe events.
  // Runs once on page load (Gradio js= parameter).
  // Handles: popup open/close relay, regen trigger via Gradio queue API.
  if (window._wf_global_listener) return;  // already registered
  window._wf_global_listener = true;

  // ── ZeroGPU quota attribution ──
  // HF Spaces run inside an iframe on huggingface.co. Gradio's own JS client
  // gets ZeroGPU auth headers (x-zerogpu-token, x-zerogpu-uuid) by sending a
  // postMessage("zerogpu-headers") to the parent frame. The parent responds
  // with a Map of headers that must be included on queue/join calls.
  // We replicate this exact mechanism so our raw regen fetch() calls are
  // attributed to the logged-in user's Pro quota.
  function _fetchZerogpuHeaders() {
    return new Promise(function(resolve) {
      // Check if we're in an HF iframe with zerogpu support
      if (typeof window === 'undefined' || window.parent === window || !window.supports_zerogpu_headers) {
        console.log('[zerogpu] not in HF iframe or no zerogpu support');
        resolve({});
        return;
      }
      // Determine origin β€” same logic as Gradio's client
      var hostname = window.location.hostname;
      var hfhubdev = 'dev.spaces.huggingface.tech';
      var origin = hostname.includes('.dev.')
        ? 'https://moon-' + hostname.split('.')[1] + '.' + hfhubdev
        : 'https://huggingface.co';
      // Use MessageChannel just like Gradio's post_message helper
      var channel = new MessageChannel();
      var done = false;
      channel.port1.onmessage = function(ev) {
        channel.port1.close();
        done = true;
        var headers = ev.data;
        if (headers && typeof headers === 'object') {
          // Convert Map to plain object if needed
          var obj = {};
          if (typeof headers.forEach === 'function') {
            headers.forEach(function(v, k) { obj[k] = v; });
          } else {
            obj = headers;
          }
          console.log('[zerogpu] got headers from parent:', Object.keys(obj).join(', '));
          resolve(obj);
        } else {
          resolve({});
        }
      };
      window.parent.postMessage('zerogpu-headers', origin, [channel.port2]);
      // Timeout: don't block regen if parent doesn't respond
      setTimeout(function() { if (!done) { done = true; channel.port1.close(); resolve({}); } }, 3000);
    });
  }

  // Cache: api_name -> fn_index, built once from gradio_config.dependencies
  let _fnIndexCache = null;
  function getFnIndex(apiName) {
    if (!_fnIndexCache) {
      _fnIndexCache = {};
      const deps = window.gradio_config && window.gradio_config.dependencies;
      if (deps) deps.forEach(function(d, i) {
        if (d.api_name) _fnIndexCache[d.api_name] = i;
      });
    }
    return _fnIndexCache[apiName];
  }

  // Read a component's current DOM value by elem_id.
  // For Number/Slider: reads the <input type="number"> or <input type="range">.
  // For Textbox/Radio: reads the <textarea> or checked <input type="radio">.
  // Returns null if not found.
  function readComponentValue(elemId) {
    const el = document.getElementById(elemId);
    if (!el) return null;
    const numInput = el.querySelector('input[type="number"]');
    if (numInput) return parseFloat(numInput.value);
    const rangeInput = el.querySelector('input[type="range"]');
    if (rangeInput) return parseFloat(rangeInput.value);
    const radio = el.querySelector('input[type="radio"]:checked');
    if (radio) return radio.value;
    const ta = el.querySelector('textarea');
    if (ta) return ta.value;
    const txt = el.querySelector('input[type="text"], input:not([type])');
    if (txt) return txt.value;
    return null;
  }

  // Fire regen for a given slot and segment by posting directly to the
  // Gradio queue API β€” bypasses Svelte binding entirely.
  // targetModel: 'taro' | 'mma' | 'hf'  (which model to use for inference)
  // If targetModel matches the slot's own prefix, uses the per-slot regen_* endpoint.
  // Otherwise uses the shared xregen_* cross-model endpoint.
  function fireRegen(slot_id, seg_idx, targetModel) {
    // Block if a regen is already in-flight for this slot
    if (_regenInFlight[slot_id]) {
      console.log('[fireRegen] blocked β€” regen already in-flight for', slot_id);
      return;
    }
    _regenInFlight[slot_id] = true;

    const prefix  = slot_id.split('_')[0];   // owning tab: 'taro'|'mma'|'hf'
    const slotNum = parseInt(slot_id.split('_')[1], 10);

    // Decide which endpoint to call
    const crossModel = (targetModel !== prefix);
    let apiName, data;

    // Read state_json from the waveform container data-state attribute
    const container = document.getElementById('wf_container_' + slot_id);
    const stateJson  = container ? (container.getAttribute('data-state') || '') : '';
    if (!stateJson) {
      console.warn('[fireRegen] no state_json for slot', slot_id);
      return;
    }

    if (!crossModel) {
      // ── Same-model regen: per-slot endpoint, video passed as null ──
      apiName = 'regen_' + prefix + '_' + slotNum;
      if (prefix === 'taro') {
        data = [seg_idx, stateJson, null,
          readComponentValue('taro_seed'), readComponentValue('taro_cfg'),
          readComponentValue('taro_steps'), readComponentValue('taro_mode'),
          readComponentValue('taro_cf_dur'), readComponentValue('taro_cf_db')];
      } else if (prefix === 'mma') {
        data = [seg_idx, stateJson, null,
          readComponentValue('mma_prompt'), readComponentValue('mma_neg'),
          readComponentValue('mma_seed'), readComponentValue('mma_cfg'),
          readComponentValue('mma_steps'),
          readComponentValue('mma_cf_dur'), readComponentValue('mma_cf_db')];
      } else {
        data = [seg_idx, stateJson, null,
          readComponentValue('hf_prompt'), readComponentValue('hf_neg'),
          readComponentValue('hf_seed'), readComponentValue('hf_guidance'),
          readComponentValue('hf_steps'), readComponentValue('hf_size'),
          readComponentValue('hf_cf_dur'), readComponentValue('hf_cf_db')];
      }
    } else {
      // ── Cross-model regen: shared xregen_* endpoint ──
      // slot_id is passed so the server knows which slot's state to splice into.
      // UI params are read from the target model's tab inputs.
      if (targetModel === 'taro') {
        apiName = 'xregen_taro';
        data = [seg_idx, stateJson, slot_id,
          readComponentValue('taro_seed'), readComponentValue('taro_cfg'),
          readComponentValue('taro_steps'), readComponentValue('taro_mode'),
          readComponentValue('taro_cf_dur'), readComponentValue('taro_cf_db')];
      } else if (targetModel === 'mma') {
        apiName = 'xregen_mmaudio';
        data = [seg_idx, stateJson, slot_id,
          readComponentValue('mma_prompt'), readComponentValue('mma_neg'),
          readComponentValue('mma_seed'), readComponentValue('mma_cfg'),
          readComponentValue('mma_steps'),
          readComponentValue('mma_cf_dur'), readComponentValue('mma_cf_db')];
      } else {
        apiName = 'xregen_hunyuan';
        data = [seg_idx, stateJson, slot_id,
          readComponentValue('hf_prompt'), readComponentValue('hf_neg'),
          readComponentValue('hf_seed'), readComponentValue('hf_guidance'),
          readComponentValue('hf_steps'), readComponentValue('hf_size'),
          readComponentValue('hf_cf_dur'), readComponentValue('hf_cf_db')];
      }
    }

    console.log('[fireRegen] calling api', apiName, 'seg', seg_idx);

    // Snapshot current waveform HTML + video src before mutating anything,
    // so we can restore on error (e.g. quota exceeded).
    var _preRegenWaveHtml = null;
    var _preRegenVideoSrc = null;
    var waveElSnap = document.getElementById('slot_wave_' + slot_id);
    if (waveElSnap) _preRegenWaveHtml = waveElSnap.innerHTML;
    var vidElSnap = document.getElementById('slot_vid_' + slot_id);
    if (vidElSnap) { var vSnap = vidElSnap.querySelector('video'); if (vSnap) _preRegenVideoSrc = vSnap.getAttribute('src'); }

    // Show spinner immediately
    const lbl = document.getElementById('wf_seglabel_' + slot_id);
    if (lbl) lbl.textContent = 'Regenerating Seg ' + (seg_idx + 1) + '...';

    const fnIndex = getFnIndex(apiName);
    if (fnIndex === undefined) {
      console.warn('[fireRegen] fn_index not found for api_name:', apiName);
      return;
    }
    // Get ZeroGPU auth headers from the HF parent frame (same mechanism
    // Gradio's own JS client uses), then fire the regen queue/join call.
    // Falls back to user-supplied HF token if zerogpu headers aren't available.
    _fetchZerogpuHeaders().then(function(zerogpuHeaders) {
      var regenHeaders = {'Content-Type': 'application/json'};
      var hasZerogpu = zerogpuHeaders && Object.keys(zerogpuHeaders).length > 0;
      if (hasZerogpu) {
        // Merge zerogpu headers (x-zerogpu-token, x-zerogpu-uuid)
        for (var k in zerogpuHeaders) { regenHeaders[k] = zerogpuHeaders[k]; }
        console.log('[fireRegen] using zerogpu headers from parent frame');
      } else {
        console.warn('[fireRegen] no zerogpu headers available β€” may use anonymous quota');
      }
      fetch('/gradio_api/queue/join', {
        method: 'POST',
        credentials: 'include',
        headers: regenHeaders,
        body: JSON.stringify({
          data: data,
          fn_index: fnIndex,
          api_name: '/' + apiName,
          session_hash: window.__gradio_session_hash__,
          event_data: null,
          trigger_id: null
        })
      }).then(function(r) { return r.json(); }).then(function(j) {
        if (!j.event_id) { console.error('[fireRegen] no event_id:', j); return; }
        console.log('[fireRegen] queued, event_id:', j.event_id);
        _listenAndApply(j.event_id, slot_id, seg_idx, _preRegenWaveHtml, _preRegenVideoSrc);
      }).catch(function(e) {
        console.error('[fireRegen] fetch error:', e);
        if (lbl) lbl.textContent = 'Error β€” see console';
        var sb = document.getElementById('wf_statusbar_' + slot_id);
        if (sb) { sb.style.color = '#e05252'; sb.textContent = '\u26a0 Request failed: ' + e.message; }
      });
    });
  }

  // Subscribe to Gradio SSE stream for an event and apply outputs to DOM.
  // For regen handlers, output[0] = video update, output[1] = waveform HTML update.
  function _applyVideoSrc(slot_id, newSrc) {
    var vidEl = document.getElementById('slot_vid_' + slot_id);
    if (!vidEl) return false;
    var video = vidEl.querySelector('video');
    if (!video) return false;
    if (video.getAttribute('src') === newSrc) return true; // already correct
    video.setAttribute('src', newSrc);
    video.src = newSrc;
    video.load();
    console.log('[_applyVideoSrc] applied src to', 'slot_vid_' + slot_id, 'src:', newSrc.slice(-40));
    return true;
  }

  // Toast notification β€” styled like ZeroGPU quota warnings.
  function _showRegenToast(message, isError) {
    var t = document.createElement('div');
    t.style.cssText = 'position:fixed;bottom:24px;left:50%;transform:translateX(-50%);' +
      'z-index:2147483647;padding:12px 20px;border-radius:8px;font-family:sans-serif;' +
      'font-size:13px;max-width:520px;text-align:center;box-shadow:0 4px 20px rgba(0,0,0,.6);' +
      'background:' + (isError ? '#7a1c1c' : '#1c4a1c') + ';color:#fff;' +
      'border:1px solid ' + (isError ? '#c0392b' : '#27ae60') + ';' +
      'pointer-events:none;';
    t.textContent = message;
    document.body.appendChild(t);
    setTimeout(function() {
      t.style.transition = 'opacity 0.5s';
      t.style.opacity = '0';
      setTimeout(function() { t.parentNode && t.parentNode.removeChild(t); }, 600);
    }, isError ? 8000 : 3000);
  }

  function _listenAndApply(eventId, slot_id, seg_idx, preRegenWaveHtml, preRegenVideoSrc) {
    var _pendingVideoSrc = null;
    const es = new EventSource('/gradio_api/queue/data?session_hash=' + window.__gradio_session_hash__);
    es.onmessage = function(e) {
      var msg;
      try { msg = JSON.parse(e.data); } catch(_) { return; }
      if (msg.event_id !== eventId) return;
      if (msg.msg === 'process_generating' || msg.msg === 'process_completed') {
        var out = msg.output;
        if (out && out.data) {
          var vidUpdate  = out.data[0];
          var waveUpdate = out.data[1];
          var newSrc = null;
          if (vidUpdate) {
            if (vidUpdate.value && vidUpdate.value.video && vidUpdate.value.video.url) newSrc = vidUpdate.value.video.url;
            else if (vidUpdate.video && vidUpdate.video.url) newSrc = vidUpdate.video.url;
            else if (vidUpdate.value && vidUpdate.value.url) newSrc = vidUpdate.value.url;
            else if (typeof vidUpdate.value === 'string') newSrc = vidUpdate.value;
            else if (vidUpdate.url) newSrc = vidUpdate.url;
          }
          if (newSrc) _pendingVideoSrc = newSrc;
          var waveHtml = null;
          if (waveUpdate) {
            if (typeof waveUpdate === 'string') waveHtml = waveUpdate;
            else if (waveUpdate.value && typeof waveUpdate.value === 'string') waveHtml = waveUpdate.value;
          }
          if (waveHtml) {
            var waveEl = document.getElementById('slot_wave_' + slot_id);
            if (waveEl) {
              var inner = waveEl.querySelector('.prose') || waveEl.querySelector('div');
              if (inner) inner.innerHTML = waveHtml;
              else waveEl.innerHTML = waveHtml;
            }
          }
        }
        if (msg.msg === 'process_completed') {
          es.close();
          _regenInFlight[slot_id] = false;
          var errMsg = msg.output && msg.output.error;
          var hadError = !!errMsg;
          console.log('[fireRegen] completed for', slot_id, 'error:', hadError, errMsg || '');
          var lbl = document.getElementById('wf_seglabel_' + slot_id);
          if (hadError) {
            var toastMsg = typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg);
            // Restore previous waveform HTML and video src
            if (preRegenWaveHtml !== null) {
              var waveEl2 = document.getElementById('slot_wave_' + slot_id);
              if (waveEl2) waveEl2.innerHTML = preRegenWaveHtml;
            }
            if (preRegenVideoSrc !== null) {
              var vidElR = document.getElementById('slot_vid_' + slot_id);
              if (vidElR) { var vR = vidElR.querySelector('video'); if (vR) { vR.setAttribute('src', preRegenVideoSrc); vR.src = preRegenVideoSrc; vR.load(); } }
            }
            // Update the statusbar (query after restore so we get the freshly-restored element)
            var isAbort   = toastMsg.toLowerCase().indexOf('aborted') !== -1;
            var isTimeout = toastMsg.toLowerCase().indexOf('timeout') !== -1;
            var failMsg = isAbort || isTimeout
              ? '\u26a0 GPU cold-start β€” segment unchanged, try again'
              : '\u26a0 Regen failed β€” segment unchanged';
            var statusBar = document.getElementById('wf_statusbar_' + slot_id);
            if (statusBar) {
              statusBar.style.color = '#e05252';
              statusBar.textContent = failMsg;
              setTimeout(function() { statusBar.style.color = '#888'; statusBar.textContent = 'Click a segment to regenerate \u00a0|\u00a0 Playhead syncs to video'; }, 8000);
            }
          } else {
            if (lbl) lbl.textContent = 'Done';
            var src = _pendingVideoSrc;
            if (src) {
              _applyVideoSrc(slot_id, src);
              setTimeout(function() { _applyVideoSrc(slot_id, src); }, 50);
              setTimeout(function() { _applyVideoSrc(slot_id, src); }, 300);
              setTimeout(function() { _applyVideoSrc(slot_id, src); }, 800);
              var vidEl = document.getElementById('slot_vid_' + slot_id);
              if (vidEl) {
                var obs = new MutationObserver(function() { _applyVideoSrc(slot_id, src); });
                obs.observe(vidEl, {subtree: true, attributes: true, attributeFilter: ['src'], childList: true});
                setTimeout(function() { obs.disconnect(); }, 2000);
              }
            }
          }
        }
      }
      if (msg.msg === 'close_stream') { es.close(); }
    };
    es.onerror = function() { es.close(); _regenInFlight[slot_id] = false; };
  }

  // Track in-flight regen per slot β€” prevents queuing multiple jobs from rapid clicks
  var _regenInFlight = {};

  // Shared popup element created once and reused across all slots
  let _popup = null;
  let _pendingSlot = null, _pendingIdx = null;

  function ensurePopup() {
    if (_popup) return _popup;
    _popup = document.createElement('div');
    _popup.style.cssText = 'display:none;position:fixed;z-index:99999;' +
      'background:#2a2a2a;border:1px solid #555;border-radius:6px;' +
      'padding:8px 12px;box-shadow:0 4px 16px rgba(0,0,0,.5);font-family:sans-serif;';
    var btnStyle = 'color:#fff;border:none;border-radius:4px;padding:5px 10px;' +
                   'font-size:11px;cursor:pointer;flex:1;';
    _popup.innerHTML =
      '<div id="_wf_popup_lbl" style="color:#ccc;font-size:11px;margin-bottom:6px;white-space:nowrap;"></div>' +
      '<div style="display:flex;gap:5px;">' +
        '<button id="_wf_popup_taro" style="background:#1d6fa5;' + btnStyle + '">&#10227; TARO</button>' +
        '<button id="_wf_popup_mma"  style="background:#2d7a4a;' + btnStyle + '">&#10227; MMAudio</button>' +
        '<button id="_wf_popup_hf"   style="background:#7a3d8c;' + btnStyle + '">&#10227; Hunyuan</button>' +
      '</div>';
    document.body.appendChild(_popup);
    ['taro','mma','hf'].forEach(function(model) {
      document.getElementById('_wf_popup_' + model).onclick = function(e) {
        e.stopPropagation();
        var slot = _pendingSlot, idx = _pendingIdx;
        hidePopup();
        if (slot !== null && idx !== null) fireRegen(slot, idx, model);
      };
    });
    // Use bubble phase (false) so stopPropagation() on the button click prevents this from firing
    document.addEventListener('click', function() { hidePopup(); }, false);
    return _popup;
  }

  function hidePopup() {
    if (_popup) _popup.style.display = 'none';
    _pendingSlot = null; _pendingIdx = null;
  }

  window.addEventListener('message', function(e) {
    const d = e.data;
    console.log('[global msg] received type=' + (d && d.type) + ' action=' + (d && d.action));
    if (!d || d.type !== 'wf_popup') return;
    const p = ensurePopup();
    if (d.action === 'hide') { hidePopup(); return; }
    // action === 'show'
    _pendingSlot = d.slot_id;
    _pendingIdx  = d.seg_idx;
    const lbl = document.getElementById('_wf_popup_lbl');
    if (lbl) lbl.textContent = 'Seg ' + (d.seg_idx + 1) +
      '  (' + d.t0.toFixed(2) + 's \u2013 ' + d.t1.toFixed(2) + 's)';
    p.style.display = 'block';
    p.style.left = (d.x + 10) + 'px';
    p.style.top  = (d.y + 10) + 'px';
    requestAnimationFrame(function() {
      const r = p.getBoundingClientRect();
      if (r.right  > window.innerWidth  - 8) p.style.left = (window.innerWidth  - r.width  - 8) + 'px';
      if (r.bottom > window.innerHeight - 8) p.style.top  = (window.innerHeight - r.height - 8) + 'px';
    });
  });
}
"""

with gr.Blocks(title="Generate Audio for Video", css=_SLOT_CSS, js=_GLOBAL_JS) as demo:
    gr.Markdown(
        "# Generate Audio for Video\n"
        "Choose a model and upload a video to generate synchronized audio.\n\n"
        "| Model | Best for | Avoid for |\n"
        "|-------|----------|-----------|\n"
        "| **TARO** | Natural, physics-driven impacts β€” footsteps, collisions, water, wind, crackling fire. Excels when the sound is tightly coupled to visible motion without needing a text description. | Dialogue, music, or complex layered soundscapes where semantic context matters. |\n"
        "| **MMAudio** | Mixed scenes where you want both visual grounding *and* semantic control via a text prompt β€” e.g. a busy street scene where you want to emphasize the rain rather than the traffic. Great for ambient textures and nuanced sound design. | Pure impact/foley shots where TARO's motion-coupling would be sharper, or cinematic music beds. |\n"
        "| **HunyuanFoley** | Cinematic foley requiring high fidelity and explicit creative direction β€” dramatic SFX, layered environmental design, or any scene where you have a clear written description of the desired sound palette. | Quick one-shot clips where you don't want to write a prompt, or raw impact sounds where timing precision matters more than richness. |"
    )

    with gr.Tabs():

        # ---------------------------------------------------------- #
        # Tab 1 β€” TARO                                                #
        # ---------------------------------------------------------- #
        with gr.Tab("TARO"):
            with gr.Row():
                with gr.Column(scale=1):
                    taro_video   = gr.Video(label="Input Video")
                    taro_seed    = gr.Number(label="Seed (-1 = random)", value=-1, precision=0, elem_id="taro_seed")
                    taro_cfg     = gr.Slider(label="CFG Scale", minimum=1, maximum=15, value=8.0, step=0.5, elem_id="taro_cfg")
                    taro_steps   = gr.Slider(label="Sampling Steps", minimum=10, maximum=50, value=25, step=1, elem_id="taro_steps")
                    taro_mode    = gr.Radio(label="Sampling Mode", choices=["sde", "ode"], value="sde", elem_id="taro_mode")
                    taro_cf_dur  = gr.Slider(label="Crossfade Duration (s)", minimum=0, maximum=4, value=2, step=0.1, elem_id="taro_cf_dur")
                    taro_cf_db   = gr.Textbox(label="Crossfade Boost (dB)", value="3", elem_id="taro_cf_db")
                    taro_samples    = gr.Slider(label="Generations", minimum=1, maximum=MAX_SLOTS, value=1, step=1)
                    taro_btn        = gr.Button("Generate", variant="primary")

                with gr.Column(scale=1):
                    (taro_slot_grps, taro_slot_vids,
                     taro_slot_waves) = _make_output_slots("taro")

            # Hidden regen plumbing β€” render=False so no DOM element is created,
            # avoiding Gradio's "Too many arguments" Svelte validation error.
            # JS passes values directly via queue/join data array at the correct
            # positional index (these show up as inputs to the fn but have no DOM).
            taro_regen_seg   = gr.Textbox(value="0", render=False)
            taro_regen_state = gr.Textbox(value="", render=False)

            for trigger in [taro_video, taro_steps, taro_cf_dur]:
                trigger.change(
                    fn=_on_video_upload_taro,
                    inputs=[taro_video, taro_steps, taro_cf_dur],
                    outputs=[taro_samples],
                )
            taro_samples.change(
                fn=_update_slot_visibility,
                inputs=[taro_samples],
                outputs=taro_slot_grps,
            )

            def _run_taro(video, seed, cfg, steps, mode, cf_dur, cf_db, n):
                print(f"[_run_taro] called video={video!r} n={n}")
                try:
                    flat = generate_taro(video, seed, cfg, steps, mode, cf_dur, cf_db, n)
                    print(f"[_run_taro] generate_taro returned flat_len={len(flat) if flat else None}")
                    result = _unpack_outputs(flat, n, "taro")
                    print(f"[_run_taro] _unpack_outputs returned {len(result)} updates")
                    return result
                except Exception as e:
                    import traceback
                    print(f"[_run_taro] EXCEPTION: {e}")
                    traceback.print_exc()
                    raise

            # Split group visibility into a separate .then() to avoid Gradio 5 SSR
            # "Too many arguments" caused by including gr.Group in mixed output lists.
            (taro_btn.click(
                fn=_run_taro,
                inputs=[taro_video, taro_seed, taro_cfg, taro_steps, taro_mode,
                        taro_cf_dur, taro_cf_db, taro_samples],
                outputs=taro_slot_vids + taro_slot_waves,
            ).then(
                fn=_update_slot_visibility,
                inputs=[taro_samples],
                outputs=taro_slot_grps,
            ))

            # Per-slot regen handlers β€” JS calls /gradio_api/queue/join with
            # fn_index (by api_name) + data=[seg_idx, state_json, video, ...params].
            taro_regen_btns = _register_regen_handlers(
                "taro", "taro", taro_regen_seg, taro_regen_state,
                [taro_video, taro_seed, taro_cfg, taro_steps,
                 taro_mode, taro_cf_dur, taro_cf_db],
                taro_slot_vids, taro_slot_waves,
            )

        # ---------------------------------------------------------- #
        # Tab 2 β€” MMAudio                                             #
        # ---------------------------------------------------------- #
        with gr.Tab("MMAudio"):
            with gr.Row():
                with gr.Column(scale=1):
                    mma_video    = gr.Video(label="Input Video")
                    mma_prompt   = gr.Textbox(label="Prompt", placeholder="e.g. footsteps on gravel", elem_id="mma_prompt")
                    mma_neg      = gr.Textbox(label="Negative Prompt", value="music", placeholder="music, speech", elem_id="mma_neg")
                    mma_seed     = gr.Number(label="Seed (-1 = random)", value=-1, precision=0, elem_id="mma_seed")
                    mma_cfg      = gr.Slider(label="CFG Strength", minimum=1, maximum=10, value=4.5, step=0.5, elem_id="mma_cfg")
                    mma_steps    = gr.Slider(label="Steps", minimum=10, maximum=50, value=25, step=1, elem_id="mma_steps")
                    mma_cf_dur   = gr.Slider(label="Crossfade Duration (s)", minimum=0, maximum=4, value=2, step=0.1, elem_id="mma_cf_dur")
                    mma_cf_db    = gr.Textbox(label="Crossfade Boost (dB)", value="3", elem_id="mma_cf_db")
                    mma_samples     = gr.Slider(label="Generations", minimum=1, maximum=MAX_SLOTS, value=1, step=1)
                    mma_btn         = gr.Button("Generate", variant="primary")

                with gr.Column(scale=1):
                    (mma_slot_grps, mma_slot_vids,
                     mma_slot_waves) = _make_output_slots("mma")

            # Hidden regen plumbing β€” render=False so no DOM element is created,
            # avoiding Gradio's "Too many arguments" Svelte validation error.
            mma_regen_seg   = gr.Textbox(value="0", render=False)
            mma_regen_state = gr.Textbox(value="", render=False)

            mma_samples.change(
                fn=_update_slot_visibility,
                inputs=[mma_samples],
                outputs=mma_slot_grps,
            )

            def _run_mmaudio(video, prompt, neg, seed, cfg, steps, cf_dur, cf_db, n):
                flat = generate_mmaudio(video, prompt, neg, seed, cfg, steps, cf_dur, cf_db, n)
                return _unpack_outputs(flat, n, "mma")

            (mma_btn.click(
                fn=_run_mmaudio,
                inputs=[mma_video, mma_prompt, mma_neg, mma_seed,
                        mma_cfg, mma_steps, mma_cf_dur, mma_cf_db, mma_samples],
                outputs=mma_slot_vids + mma_slot_waves,
            ).then(
                fn=_update_slot_visibility,
                inputs=[mma_samples],
                outputs=mma_slot_grps,
            ))

            mma_regen_btns = _register_regen_handlers(
                "mma", "mmaudio", mma_regen_seg, mma_regen_state,
                [mma_video, mma_prompt, mma_neg, mma_seed,
                 mma_cfg, mma_steps, mma_cf_dur, mma_cf_db],
                mma_slot_vids, mma_slot_waves,
            )

        # ---------------------------------------------------------- #
        # Tab 3 β€” HunyuanVideoFoley                                   #
        # ---------------------------------------------------------- #
        with gr.Tab("HunyuanFoley"):
            with gr.Row():
                with gr.Column(scale=1):
                    hf_video    = gr.Video(label="Input Video")
                    hf_prompt   = gr.Textbox(label="Prompt", placeholder="e.g. rain hitting a metal roof", elem_id="hf_prompt")
                    hf_neg      = gr.Textbox(label="Negative Prompt", value="noisy, harsh", elem_id="hf_neg")
                    hf_seed     = gr.Number(label="Seed (-1 = random)", value=-1, precision=0, elem_id="hf_seed")
                    hf_guidance = gr.Slider(label="Guidance Scale", minimum=1, maximum=10, value=4.5, step=0.5, elem_id="hf_guidance")
                    hf_steps    = gr.Slider(label="Steps", minimum=10, maximum=100, value=50, step=5, elem_id="hf_steps")
                    hf_size     = gr.Radio(label="Model Size", choices=["xl", "xxl"], value="xxl", elem_id="hf_size")
                    hf_cf_dur   = gr.Slider(label="Crossfade Duration (s)", minimum=0, maximum=4, value=2, step=0.1, elem_id="hf_cf_dur")
                    hf_cf_db    = gr.Textbox(label="Crossfade Boost (dB)", value="3", elem_id="hf_cf_db")
                    hf_samples      = gr.Slider(label="Generations", minimum=1, maximum=MAX_SLOTS, value=1, step=1)
                    hf_btn          = gr.Button("Generate", variant="primary")

                with gr.Column(scale=1):
                    (hf_slot_grps, hf_slot_vids,
                     hf_slot_waves) = _make_output_slots("hf")

            # Hidden regen plumbing β€” render=False so no DOM element is created,
            # avoiding Gradio's "Too many arguments" Svelte validation error.
            hf_regen_seg    = gr.Textbox(value="0", render=False)
            hf_regen_state  = gr.Textbox(value="", render=False)

            hf_samples.change(
                fn=_update_slot_visibility,
                inputs=[hf_samples],
                outputs=hf_slot_grps,
            )

            def _run_hunyuan(video, prompt, neg, seed, guidance, steps, size, cf_dur, cf_db, n):
                flat = generate_hunyuan(video, prompt, neg, seed, guidance, steps, size, cf_dur, cf_db, n)
                return _unpack_outputs(flat, n, "hf")

            (hf_btn.click(
                fn=_run_hunyuan,
                inputs=[hf_video, hf_prompt, hf_neg, hf_seed,
                        hf_guidance, hf_steps, hf_size, hf_cf_dur, hf_cf_db, hf_samples],
                outputs=hf_slot_vids + hf_slot_waves,
            ).then(
                fn=_update_slot_visibility,
                inputs=[hf_samples],
                outputs=hf_slot_grps,
            ))

            hf_regen_btns = _register_regen_handlers(
                "hf", "hunyuan", hf_regen_seg, hf_regen_state,
                [hf_video, hf_prompt, hf_neg, hf_seed,
                 hf_guidance, hf_steps, hf_size, hf_cf_dur, hf_cf_db],
                hf_slot_vids, hf_slot_waves,
            )

    # ---- Browser-safe transcode on upload ----
    # Gradio serves the original uploaded file to the browser preview widget,
    # so H.265 sources show as blank. We re-encode to H.264 on upload and feed
    # the result back so the preview plays. mux_video_audio already re-encodes
    # to H.264 during generation, so no double-conversion conflict.
    taro_video.upload(fn=_transcode_for_browser, inputs=[taro_video], outputs=[taro_video])
    mma_video.upload(fn=_transcode_for_browser,  inputs=[mma_video],  outputs=[mma_video])
    hf_video.upload(fn=_transcode_for_browser,   inputs=[hf_video],   outputs=[hf_video])

    # ---- Cross-tab video sync ----
    _sync = lambda v: (gr.update(value=v), gr.update(value=v))
    taro_video.change(fn=_sync, inputs=[taro_video], outputs=[mma_video, hf_video])
    mma_video.change(fn=_sync,  inputs=[mma_video],  outputs=[taro_video, hf_video])
    hf_video.change(fn=_sync,   inputs=[hf_video],   outputs=[taro_video, mma_video])

    # ---- Cross-model regen endpoints ----
    # render=False inputs/outputs: no DOM elements created, no SSR validation impact.
    # JS calls these via /gradio_api/queue/join using the api_name and applies
    # the returned video+waveform directly to the target slot's DOM elements.
    _xr_seg       = gr.Textbox(value="0",  render=False)
    _xr_state     = gr.Textbox(value="",   render=False)
    _xr_slot_id   = gr.Textbox(value="",   render=False)
    # Dummy outputs for xregen events: must be real rendered components so Gradio
    # can look them up in session state during postprocess_data.  The JS listener
    # (_listenAndApply) applies the returned video/HTML directly to the correct
    # slot's DOM elements and ignores Gradio's own output routing, so these
    # slot-0 components simply act as sinks β€” their displayed value is overwritten
    # by the real JS update immediately after.
    _xr_dummy_vid  = taro_slot_vids[0]
    _xr_dummy_wave = taro_slot_waves[0]

    # TARO cross-model regen inputs: seg_idx, state_json, slot_id, seed, cfg, steps, mode, cf_dur, cf_db
    _xr_taro_seed  = gr.Textbox(value="-1",  render=False)
    _xr_taro_cfg   = gr.Textbox(value="7.5", render=False)
    _xr_taro_steps = gr.Textbox(value="25",  render=False)
    _xr_taro_mode  = gr.Textbox(value="sde", render=False)
    _xr_taro_cfd   = gr.Textbox(value="2",   render=False)
    _xr_taro_cfdb  = gr.Textbox(value="3",   render=False)
    gr.Button(render=False).click(
        fn=xregen_taro,
        inputs=[_xr_seg, _xr_state, _xr_slot_id,
                _xr_taro_seed, _xr_taro_cfg, _xr_taro_steps,
                _xr_taro_mode, _xr_taro_cfd, _xr_taro_cfdb],
        outputs=[_xr_dummy_vid, _xr_dummy_wave],
        api_name="xregen_taro",
    )

    # MMAudio cross-model regen inputs: seg_idx, state_json, slot_id, prompt, neg, seed, cfg, steps, cf_dur, cf_db
    _xr_mma_prompt = gr.Textbox(value="",    render=False)
    _xr_mma_neg    = gr.Textbox(value="",    render=False)
    _xr_mma_seed   = gr.Textbox(value="-1",  render=False)
    _xr_mma_cfg    = gr.Textbox(value="4.5", render=False)
    _xr_mma_steps  = gr.Textbox(value="25",  render=False)
    _xr_mma_cfd    = gr.Textbox(value="2",   render=False)
    _xr_mma_cfdb   = gr.Textbox(value="3",   render=False)
    gr.Button(render=False).click(
        fn=xregen_mmaudio,
        inputs=[_xr_seg, _xr_state, _xr_slot_id,
                _xr_mma_prompt, _xr_mma_neg, _xr_mma_seed,
                _xr_mma_cfg, _xr_mma_steps, _xr_mma_cfd, _xr_mma_cfdb],
        outputs=[_xr_dummy_vid, _xr_dummy_wave],
        api_name="xregen_mmaudio",
    )

    # HunyuanFoley cross-model regen inputs: seg_idx, state_json, slot_id, prompt, neg, seed, guidance, steps, size, cf_dur, cf_db
    _xr_hf_prompt = gr.Textbox(value="",    render=False)
    _xr_hf_neg    = gr.Textbox(value="",    render=False)
    _xr_hf_seed   = gr.Textbox(value="-1",  render=False)
    _xr_hf_guide  = gr.Textbox(value="4.5", render=False)
    _xr_hf_steps  = gr.Textbox(value="50",  render=False)
    _xr_hf_size   = gr.Textbox(value="xxl", render=False)
    _xr_hf_cfd    = gr.Textbox(value="2",   render=False)
    _xr_hf_cfdb   = gr.Textbox(value="3",   render=False)
    gr.Button(render=False).click(
        fn=xregen_hunyuan,
        inputs=[_xr_seg, _xr_state, _xr_slot_id,
                _xr_hf_prompt, _xr_hf_neg, _xr_hf_seed,
                _xr_hf_guide, _xr_hf_steps, _xr_hf_size,
                _xr_hf_cfd, _xr_hf_cfdb],
        outputs=[_xr_dummy_vid, _xr_dummy_wave],
        api_name="xregen_hunyuan",
    )

    # NOTE: ZeroGPU quota attribution is handled via postMessage("zerogpu-headers")
    # to the HF parent frame β€” the same mechanism Gradio's own JS client uses.
    # This replaced the old x-ip-token relay approach which was unreliable.

print("[startup] app.py fully loaded β€” regen handlers registered, SSR disabled")
demo.queue(max_size=10).launch(ssr_mode=False, height=900, allowed_paths=["/tmp"])