File size: 170,287 Bytes
90ce156
 
 
 
 
 
dc79d25
90ce156
35616fd
90ce156
 
 
db10d55
c3f6ca2
90ce156
62c372b
90ce156
 
 
 
 
 
 
 
afd380b
90ce156
96c5ff4
 
1585384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
c3f6ca2
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46a9d20
2a9046b
90ce156
 
 
 
 
 
 
 
 
 
2f79947
90ce156
 
52300bf
b7a0fa0
90ce156
 
b7a0fa0
 
90ce156
cf90d70
 
11e219a
90ce156
 
 
afd380b
90ce156
b7a0fa0
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
8a696da
90ce156
 
 
 
 
 
 
 
c3f6ca2
90ce156
 
62f553a
 
8dfb377
 
62f553a
 
 
 
 
 
 
 
 
 
 
 
8dfb377
62f553a
 
2f79947
afd380b
 
 
 
 
 
2f79947
afd380b
62f553a
8dfb377
afd380b
 
 
 
 
2f79947
afd380b
 
 
 
 
 
 
2f79947
62f553a
2f79947
 
 
 
 
 
 
 
 
b7a0fa0
2f79947
 
 
 
8dfb377
2f79947
 
 
 
 
8dfb377
 
 
afd380b
2f79947
 
 
 
8dfb377
2f79947
8dfb377
62f553a
2f79947
 
b7a0fa0
8dfb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
2f79947
afd380b
8dfb377
62f553a
2f79947
 
 
8dfb377
 
2f79947
 
 
8dfb377
2f79947
8dfb377
 
2f79947
8dfb377
 
2f79947
dc79d25
2f79947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dfb377
 
2f79947
 
 
 
 
 
 
8dfb377
2f79947
 
 
 
 
8dfb377
 
 
 
2f79947
62f553a
2f79947
62f553a
 
8dfb377
2f79947
 
8dfb377
2f79947
8dfb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
 
 
 
 
 
 
 
 
 
 
 
 
 
8dfb377
 
 
 
 
2f79947
8dfb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
 
8dfb377
afd380b
 
2f79947
 
db10d55
 
 
 
 
 
 
2f79947
 
 
 
 
 
 
 
 
 
 
52300bf
 
 
 
 
 
 
 
 
 
 
8dfb377
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
afd380b
 
8dfb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
2f79947
 
 
 
 
8dfb377
2f79947
 
 
 
8dfb377
 
 
 
 
 
2f79947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dfb377
 
 
2f79947
 
 
 
 
 
 
 
 
 
8dfb377
 
 
2f79947
 
 
 
 
 
 
 
 
 
 
62f553a
2f79947
 
 
 
 
 
 
 
 
 
62f553a
 
2f79947
 
 
 
afd380b
2f79947
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
 
8dfb377
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f533f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ada711
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0ee9e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf90d70
 
 
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
afd380b
90ce156
 
 
 
 
 
 
 
 
 
2f79947
90ce156
 
2f79947
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c5ff4
b7a0fa0
90ce156
 
96c5ff4
90ce156
 
96c5ff4
 
90ce156
 
 
 
 
afd380b
90ce156
 
 
 
 
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624aeae
 
 
90ce156
 
624aeae
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f6ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e79d110
 
 
c3f6ca2
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
e79d110
90ce156
 
 
 
 
 
 
 
 
 
 
e79d110
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
624aeae
90ce156
 
 
 
 
 
624aeae
c3f6ca2
90ce156
 
 
 
 
e79d110
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
b7a0fa0
 
 
90ce156
 
b7a0fa0
 
 
dc79d25
b7a0fa0
 
 
 
 
 
 
90ce156
dc79d25
b7a0fa0
 
 
 
 
dc79d25
90ce156
 
 
 
 
 
 
b7a0fa0
 
 
 
 
 
 
 
 
90ce156
 
b7a0fa0
 
 
 
90ce156
 
 
 
dc79d25
90ce156
 
b7a0fa0
90ce156
b7a0fa0
 
 
90ce156
 
b7a0fa0
90ce156
 
 
b7a0fa0
90ce156
 
 
 
842f4a7
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
 
 
 
90ce156
 
b7a0fa0
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
b7a0fa0
 
90ce156
b7a0fa0
 
 
 
90ce156
 
db10d55
b7a0fa0
 
 
 
 
 
 
 
 
 
e18b210
b7a0fa0
 
 
 
 
 
 
 
db10d55
b7a0fa0
 
 
 
 
 
 
 
 
 
e18b210
b7a0fa0
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
e18b210
 
 
 
 
 
dc79d25
 
 
e18b210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
90ce156
 
 
 
 
 
 
 
 
2f79947
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
 
 
 
 
afd380b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
 
afd380b
 
 
 
 
 
 
 
 
 
 
2f79947
 
afd380b
 
 
2f79947
afd380b
 
 
 
 
 
 
 
 
 
 
 
 
 
2f79947
afd380b
 
 
 
 
 
f1aeada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
f1aeada
db10d55
f1aeada
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1aeada
 
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
3586de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
 
3586de6
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
3586de6
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3586de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
 
e18b210
 
 
db10d55
 
 
 
 
 
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
 
 
 
dc79d25
e18b210
 
 
dc79d25
 
db10d55
 
 
 
e18b210
 
 
 
 
 
 
 
 
dc79d25
 
e18b210
 
 
 
 
 
 
 
 
dc79d25
 
 
e18b210
dc79d25
 
 
 
 
e18b210
 
 
 
 
 
db10d55
 
 
 
 
dc79d25
db10d55
 
 
 
 
 
 
 
 
 
dc79d25
 
3586de6
 
 
 
db10d55
 
 
 
dc79d25
 
 
 
 
 
 
db10d55
dc79d25
 
db10d55
dc79d25
3586de6
 
 
 
 
 
dc79d25
 
 
 
db10d55
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
3586de6
 
 
 
db10d55
 
 
 
 
dc79d25
db10d55
 
 
 
3586de6
 
 
db10d55
 
 
 
 
dc79d25
db10d55
3586de6
 
db10d55
 
 
 
 
3586de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
 
 
 
dc79d25
db10d55
 
 
 
dc79d25
52300bf
 
db10d55
 
 
 
 
dc79d25
db10d55
 
 
 
dc79d25
 
db10d55
dc79d25
 
 
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
db10d55
 
 
 
dc79d25
 
db10d55
 
 
 
 
dc79d25
db10d55
 
 
 
 
dc79d25
db10d55
 
dc79d25
db10d55
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
db10d55
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
90ce156
 
 
 
db10d55
 
 
 
 
 
 
e18b210
db10d55
 
90ce156
 
 
 
db10d55
90ce156
 
db10d55
90ce156
 
db10d55
 
 
 
 
 
 
 
e18b210
 
 
db10d55
afd380b
 
db10d55
afd380b
 
db10d55
90ce156
 
 
 
db10d55
90ce156
 
db10d55
90ce156
 
 
db10d55
 
 
 
 
 
 
 
e18b210
 
 
db10d55
afd380b
 
db10d55
afd380b
 
db10d55
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b25029
90ce156
 
5b25029
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c65177f
90ce156
c65177f
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11e219a
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a696da
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
90ce156
afd380b
90ce156
afd380b
90ce156
afd380b
90ce156
afd380b
90ce156
afd380b
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c5ff4
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf90d70
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
dc79d25
90ce156
 
 
 
 
 
 
 
 
 
 
 
afd380b
dc79d25
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c5ff4
 
 
 
90ce156
96c5ff4
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
90ce156
 
 
 
 
 
 
fddaf5e
 
 
 
 
 
 
 
62c372b
 
 
46a9d20
62c372b
46a9d20
62c372b
 
46a9d20
62c372b
 
 
 
46a9d20
62c372b
 
 
 
 
 
 
 
 
46a9d20
 
 
62c372b
 
46a9d20
62c372b
 
79b62dc
fddaf5e
79b62dc
b7a0fa0
 
 
 
79b62dc
fddaf5e
79b62dc
 
 
 
 
 
fddaf5e
b7a0fa0
96c5ff4
 
 
 
8a696da
 
 
 
 
96c5ff4
 
fddaf5e
96c5ff4
 
 
8a696da
 
96c5ff4
 
 
 
 
8a696da
96c5ff4
 
8a696da
 
 
 
96c5ff4
8a696da
 
 
 
 
 
 
 
96c5ff4
8a696da
 
 
b7a0fa0
 
 
35616fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c5ff4
35616fd
 
 
 
96c5ff4
35616fd
 
96c5ff4
35616fd
 
96c5ff4
35616fd
 
96c5ff4
35616fd
 
96c5ff4
 
 
 
 
35616fd
 
 
96c5ff4
 
 
 
 
 
35616fd
 
 
 
 
 
 
96c5ff4
 
 
35616fd
 
 
96c5ff4
35616fd
 
b7a0fa0
 
2847bf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf90d70
35616fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2847bf5
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e18b210
 
 
 
 
96c5ff4
afd380b
96c5ff4
afd380b
96c5ff4
afd380b
96c5ff4
afd380b
96c5ff4
afd380b
96c5ff4
db10d55
96c5ff4
db10d55
 
dc79d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
 
 
 
 
 
dc79d25
db10d55
 
 
 
 
 
 
96c5ff4
 
 
 
 
 
 
2f79947
96c5ff4
 
 
 
 
 
 
db10d55
 
 
 
 
 
dc79d25
db10d55
 
96c5ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
90ce156
96c5ff4
8a696da
 
 
 
90ce156
dc79d25
 
8a696da
90ce156
 
 
 
1585384
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62f553a
90ce156
 
62f553a
90ce156
 
62f553a
90ce156
 
62f553a
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
90ce156
afd380b
b7a0fa0
90ce156
b7a0fa0
90ce156
 
 
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
842f4a7
 
cf90d70
b7a0fa0
90ce156
 
 
 
 
 
 
b7a0fa0
90ce156
 
 
 
 
 
b7a0fa0
 
842f4a7
 
cf90d70
842f4a7
eff87e2
90ce156
b7a0fa0
842f4a7
90ce156
 
cf90d70
eff87e2
90ce156
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
db10d55
90ce156
 
 
 
 
 
 
 
2f79947
afd380b
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
afd380b
 
96c5ff4
 
 
 
 
 
 
 
afd380b
 
90ce156
 
 
 
afd380b
90ce156
afd380b
 
 
96c5ff4
 
 
afd380b
96c5ff4
afd380b
 
90ce156
 
 
 
29f533f
eff87e2
29f533f
afd380b
90ce156
afd380b
cf90d70
 
 
 
afd380b
 
 
 
 
e18b210
afd380b
 
 
b7a0fa0
 
90ce156
 
afd380b
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
6ada711
90ce156
 
db10d55
 
90ce156
52300bf
f0ee9e7
52300bf
 
90ce156
b7a0fa0
2f79947
b7a0fa0
 
2f79947
 
db10d55
afd380b
2f79947
 
 
afd380b
 
2f79947
 
afd380b
2f79947
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
2f79947
 
b7a0fa0
2f79947
afd380b
2f79947
afd380b
b7a0fa0
2f79947
 
 
 
 
 
 
db10d55
b7a0fa0
 
afd380b
 
 
 
 
 
90ce156
afd380b
 
 
 
2f79947
afd380b
 
 
 
 
 
 
 
2f79947
afd380b
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db10d55
90ce156
 
 
 
 
b7a0fa0
90ce156
 
 
 
29f533f
 
 
 
b7a0fa0
 
 
 
 
 
 
 
db10d55
b7a0fa0
db10d55
90ce156
e18b210
90ce156
 
 
 
db10d55
b7a0fa0
 
 
 
 
 
db10d55
b7a0fa0
e18b210
b7a0fa0
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd380b
b7a0fa0
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc79d25
 
 
 
 
 
90ce156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a0fa0
 
 
 
 
 
 
 
fddaf5e
 
 
 
 
 
b7a0fa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90ce156
 
 
 
dc79d25
b7a0fa0
 
2f79947
b7a0fa0
 
dc79d25
 
 
 
90ce156
 
dc79d25
 
90ce156
 
 
 
afd380b
e18b210
90ce156
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
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
from __future__ import annotations

import argparse
import base64
import concurrent.futures
import gc
import hashlib
import html
import math
import json
import os
import random
import re
import shutil
import subprocess
import sys
import threading
import time
import traceback
from collections import deque
from copy import deepcopy
from datetime import datetime
from pathlib import Path
from typing import Optional
from urllib.parse import quote

os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True,max_split_size_mb:128")

try:
    import spaces
except ImportError:  # pragma: no cover - keeps local CPU runs working
    class _SpacesShim:
        @staticmethod
        def GPU(*args, **kwargs):
            if args and callable(args[0]) and not kwargs:
                return args[0]

            def decorator(fn):
                return fn

            return decorator

    spaces = _SpacesShim()

import gradio as gr
import torch
from huggingface_hub import snapshot_download
from safetensors import safe_open
from safetensors.torch import load_file, save_file
from transformers import set_seed
from transformers.models.qwen2_5_vl.configuration_qwen2_5_vl import Qwen2_5_VLVisionConfig

from common.utils.logging import get_logger
from common.utils.misc import AutoEncoderParams, tuple_mul
from config.config_factory import DataArguments, InferenceArguments, ModelArguments
from data.data_utils import add_special_tokens
from data.dataset_base import DataConfig, simple_custom_collate
from data.datasets_custom import ValidationDataset
from inference_lance import (
    PROMPT_JSON_FILENAME,
    apply_inference_defaults,
    clean_memory,
    init_from_model_path_if_needed,
    save_prompt_results,
    validate_on_fixed_batch,
)
from modeling.lance import Lance, LanceConfig, Qwen2ForCausalLM
from modeling.qwen2 import Qwen2Tokenizer
from modeling.qwen2.modeling_qwen2 import Qwen2Config
from modeling.vae.wan.model import WanVideoVAE
from modeling.vit.qwen2_5_vl_vit import Qwen2_5_VisionTransformerPretrainedModel


REPO_ROOT = Path(__file__).resolve().parent
GRADIO_TMP_ROOT = Path(os.getenv("LANCE_GRADIO_TMP_ROOT", "/tmp/lance_gradio")).expanduser()
TMP_INPUT_DIR = GRADIO_TMP_ROOT / "inputs"
RESULTS_ROOT = GRADIO_TMP_ROOT / "results"
GLOBAL_RECORDS_FILE = GRADIO_TMP_ROOT / "generation_records.jsonl"
RUN_RECORD_FILENAME = "generation_record.json"

LOCAL_MODEL_BASE_DIR = Path("downloads")
SPACE_MODEL_BASE_DIR = Path("/data/lance_models")
DEFAULT_MODEL_REPO_ID = "bytedance-research/Lance"
DEFAULT_FLASH_ATTN_VERSION = "2.8.3"
DEFAULT_FLASH_ATTN_WHEEL_URL = "https://huggingface.co/strangertoolshf/flash_attention_2_wheelhouse/resolve/main/wheelhouse-flash_attn-2.8.3/linux_x86_64/torch2.8/cu12/abiTRUE/cp310/flash_attn-2.8.3+cu12torch2.8cxx11abiTRUE-cp310-cp310-linux_x86_64.whl"
DEFAULT_MODEL_VARIANT = "video"
MODEL_VARIANT_VIDEO = "video"
MODEL_VARIANT_IMAGE = "image"
MODEL_VARIANT_TO_DIR = {
    MODEL_VARIANT_VIDEO: "Lance_3B_Video",
    MODEL_VARIANT_IMAGE: "Lance_3B",
}
DEFAULT_MODEL_PATH = LOCAL_MODEL_BASE_DIR / MODEL_VARIANT_TO_DIR[MODEL_VARIANT_VIDEO]
DEFAULT_VIT_TYPE = "qwen_2_5_vl_original"
DEFAULT_TASK = "t2v"
DEFAULT_TIMESTEPS = 30
DEFAULT_TIMESTEP_SHIFT = 3.5
DEFAULT_CFG_TEXT_SCALE = 4.0
DEFAULT_RESOLUTION = "video_480p"
DEFAULT_VIDEO_EDIT_RESOLUTION = "video_480p"
DEFAULT_IMAGE_RESOLUTION = "image_768x768"
DEFAULT_BASIC_SEED = 42
DEFAULT_HEIGHT = 352
DEFAULT_WIDTH = 640
DEFAULT_IMAGE_SIZE = 768
DEFAULT_VIDEO_DURATION_SECONDS = 5
MAX_VIDEO_DURATION_SECONDS = 10
MAX_VIDEO_NUM_FRAMES = 12 * MAX_VIDEO_DURATION_SECONDS + 1
DEFAULT_NUM_FRAMES = 12 * DEFAULT_VIDEO_DURATION_SECONDS + 1
DEFAULT_VIDEO_ASPECT_RATIO = "16:9"
DEFAULT_IMAGE_ASPECT_RATIO = "1:1"
ASPECT_RATIO_CHOICES = ["21:9", "16:9", "3:2", "4:3", "1:1", "3:4", "2:3", "9:16"]

VIDEO_360P_ASPECT_RATIO_TO_SIZE = {
    "21:9": (672, 288),
    "16:9": (640, 352),
    "3:2": (528, 352),
    "4:3": (560, 416),
    "1:1": (480, 480),
    "3:4": (416, 560),
    "2:3": (352, 528),
    "9:16": (352, 640),
}

VIDEO_480P_ASPECT_RATIO_TO_SIZE = {
    "21:9": (976, 416),
    "16:9": (848, 480),
    "3:2": (784, 528),
    "4:3": (736, 560),
    "1:1": (640, 640),
    "3:4": (560, 736),
    "2:3": (528, 784),
    "9:16": (480, 848),
}

VIDEO_RESOLUTION_TO_SIZE_MAP = {
    "video_360p": VIDEO_360P_ASPECT_RATIO_TO_SIZE,
    "video_480p": VIDEO_480P_ASPECT_RATIO_TO_SIZE,
}

IMAGE_ASPECT_RATIO_TO_SIZE = {
    "21:9": (1168, 496),
    "16:9": (1024, 576),
    "3:2": (944, 624),
    "4:3": (880, 672),
    "1:1": (768, 768),
    "3:4": (672, 880),
    "2:3": (624, 944),
    "9:16": (576, 1024),
}
DEFAULT_GPUS = "0"
DEFAULT_QUEUE_SIZE = 32
DEFAULT_CONCURRENCY_LIMIT = 1
USE_KVCACHE = True
TEXT_TEMPLATE = True
RECORD_WRITE_LOCK = threading.Lock()

LANCE_HOMEPAGE_URL = "https://lance-project.github.io/"
LANCE_PAPER_URL = "http://arxiv.org/abs/2605.18678"
LANCE_HUGGING_FACE_URL = "https://huggingface.co/bytedance-research/Lance"
LANCE_GITHUB_URL = "https://github.com/bytedance/Lance"
LANCE_LOGO_PATH = REPO_ROOT / "assets" / "logo" / "lance-logo.png"

APP_CSS = """
:root {
  color-scheme: light;
  --lance-accent: #fb923c;
  --lance-accent-hover: #f97316;
  --lance-surface: #ffffff;
  --lance-surface-muted: #f8fafc;
  --lance-border: rgba(148, 163, 184, .36);
  --lance-text: #111827;
  --lance-text-muted: #475569;
  --lance-shadow: 0 8px 24px rgba(15, 23, 42, .08);
  --body-background-fill: var(--lance-surface);
  --background-fill-primary: var(--lance-surface);
  --block-background-fill: var(--lance-surface);
  --input-background-fill: var(--lance-surface);
  --button-primary-background-fill: var(--lance-accent);
  --button-primary-background-fill-hover: var(--lance-accent-hover);
  --button-primary-text-color: #0f172a;
}
body, .gradio-container, .contain { background: var(--lance-surface) !important; color: var(--lance-text) !important; }
.gradio-container, .contain { max-width: 1180px !important; margin: 0 auto !important; }
.lance-hero { text-align: center; padding: 8px 12px 4px; }
.lance-logo { width: min(150px, 34vw); height: auto; display: block; margin: 0 auto 4px; }
.lance-title { margin: 0 auto 5px; font-size: clamp(22px, 2.4vw, 32px); line-height: 1.08; font-weight: 800; }
.lance-badges { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin: 4px auto 0; }
.lance-badges a { line-height: 0; }
.lance-badges img { height: 20px; width: auto; display: block; }
.lance-status, .lance-run-status { max-width: 1120px; margin: 8px auto !important; }
.lance-run-status p { margin: 0 !important; }
.lance-run-status-pill { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px; border: 1px solid var(--lance-border); background: var(--lance-surface); color: var(--lance-text-muted); font-size: 14px; font-weight: 700; box-shadow: var(--lance-shadow); }
.lance-run-status-chip { width: 8px; height: 8px; border-radius: 999px; background: var(--lance-accent); box-shadow: 0 0 0 4px rgba(251,146,60,.18); }
.lance-run-status-dots i { display: inline-block; width: 4px; height: 4px; margin-left: 3px; border-radius: 999px; background: currentColor; opacity: .45; animation: lance-dot-pulse 1.1s infinite ease-in-out; }
.lance-run-status-dots i:nth-child(2) { animation-delay: .15s; }
.lance-run-status-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes lance-dot-pulse { 40% { transform: translateY(-1px); opacity: 1; } }

.lance-main-row { display: grid !important; grid-template-columns: minmax(0, 1.16fr) minmax(0, 0.84fr) !important; gap: 18px !important; align-items: start !important; }
.lance-main-column { min-width: 0 !important; width: 100% !important; }
.lance-panel, .lance-control-field, .example-panel { border: 0 !important; box-shadow: none !important; background: transparent !important; padding: 0 !important; }
.lance-panel > .form, .lance-control-field > .form, .lance-label-html, .lance-label-html > div, .lance-label-html .wrap { border: 0 !important; background: transparent !important; box-shadow: none !important; padding: 0 !important; margin: 0 !important; min-height: 0 !important; }
.lance-section-label, .lance-generation-label { margin: 0 0 10px !important; font-weight: 800 !important; color: var(--body-text-color) !important; }
.lance-section-label { font-size: 18px !important; }
.lance-generation-label { font-size: 14px !important; }
.lance-label-icon { display: none !important; }
.lance-output-label { display: inline-flex !important; align-items: center !important; gap: 8px !important; }
.lance-output-label .lance-label-icon { display: inline-flex !important; align-items: center !important; justify-content: center !important; width: 20px !important; height: 20px !important; color: var(--lance-accent) !important; }
.lance-output-label .lance-label-icon svg { width: 18px !important; height: 18px !important; display: block !important; }

.lance-taskbar-wrap { max-width: 1120px; margin: 0 auto 12px !important; }
.task-selector {
  overflow-x: auto !important;
  padding: 4px 0 12px !important;
  scrollbar-width: thin;
  display: flex !important;
  justify-content: center !important;
}
.task-selector > .wrap, .task-selector .wrap {
  width: max-content !important;
  max-width: min(100%, 1080px) !important;
  margin: 0 auto !important;
  padding: 4px !important;
  display: flex !important;
  justify-content: center !important;
  flex-wrap: nowrap !important;
  gap: 10px !important;
  border-radius: 999px !important;
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
}
.task-selector label {
  min-width: max-content !important;
  min-height: 38px !important;
  padding: 9px 18px !important;
  border: 0 !important;
  border-radius: 999px !important;
  background: #f1f5f9 !important;
  color: var(--lance-text-muted) !important;
  justify-content: center !important;
  white-space: nowrap !important;
}
.task-selector label:has(input:checked) { background: var(--lance-accent) !important; color: #0f172a !important; box-shadow: 0 6px 16px rgba(251,146,60,.22) !important; }
.task-selector input:checked + span { color: #0f172a !important; font-weight: 800 !important; }

.lance-taskbar-wrap,
.lance-taskbar-wrap > div,
.lance-taskbar-wrap > .form,
.lance-taskbar-wrap .block,
.task-selector,
.task-selector > div,
.task-selector > .form,
.task-selector .form,
.task-selector .wrap {
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
}
.task-selector > .wrap,
.task-selector .wrap {
  padding: 0 !important;
}
.task-selector label {
  background: #f8fafc !important;
  border: 1px solid rgba(148,163,184,.25) !important;
  box-shadow: 0 3px 10px rgba(15,23,42,.04) !important;
}
.task-selector label:has(input:checked) {
  background: var(--lance-accent) !important;
  border-color: transparent !important;
  color: #0f172a !important;
  box-shadow: 0 8px 18px rgba(249,115,22,.24) !important;
}
.task-selector input:checked + span { color: #0f172a !important; }

.lance-task-prompt-panel { max-width: 1040px; margin: 0 auto 10px !important; }
.main-prompt-control, .main-prompt-control > div, .main-prompt-control .wrap { border: 0 !important; background: transparent !important; box-shadow: none !important; }
.main-prompt-control textarea { min-height: 160px !important; padding: 18px !important; border: 1px solid var(--lance-border) !important; border-radius: 16px !important; background: var(--lance-surface) !important; color: var(--lance-text) !important; font-size: 15px !important; line-height: 1.45 !important; box-shadow: var(--lance-shadow) !important; }
.main-prompt-control textarea::placeholder { color: #94a3b8 !important; }
.prompt-options {
  position: relative !important;
  z-index: 2 !important;
  margin: 8px 0 16px !important;
  padding: 0 !important;
}
.prompt-options > .form {
  display: grid !important;
  grid-template-columns: repeat(4, max-content) !important;
  align-items: center !important;
  justify-content: start !important;
  justify-items: start !important;
  gap: 6px !important;
  width: max-content !important;
  max-width: 100% !important;
}

.prompt-chip,
.prompt-chip > .form,
.prompt-chip > div,
.prompt-chip .block,
.prompt-chip .form,
.prompt-chip .container,
.prompt-chip .wrap {
  width: 100% !important;
  min-width: 0 !important;
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
  padding: 0 !important;
  margin: 0 !important;
}
.prompt-chip {
  display: block !important;
  min-width: 0 !important;
  width: auto !important;
  flex: 0 0 auto !important;
}
.prompt-chip .wrap,
.prompt-chip .container,
.prompt-chip > .form,
.prompt-chip .form {
  display: inline-flex !important;
  align-items: center !important;
  width: auto !important;
}
.prompt-chip button,
.prompt-chip [role="button"],
.prompt-chip select,
.prompt-chip input {
  width: auto !important;
  min-width: 58px !important;
  min-height: 32px !important;
  height: 32px !important;
  border-radius: 999px !important;
  border: 1px solid var(--lance-border) !important;
  outline: 0 !important;
  background: var(--lance-surface-muted) !important;
  color: var(--lance-text) !important;
  font-size: 10px !important;
  font-weight: 800 !important;
  box-shadow: none !important;
  padding: 0 8px !important;
}
.frame-interpolation-row button,
.frame-interpolation-row [role="button"],
.frame-interpolation-row select,
.frame-interpolation-row input { min-width: 82px !important; }
.video-resolution-row button,
.video-resolution-row [role="button"],
.video-resolution-row select,
.video-resolution-row input { min-width: 58px !important; }
.aspect-ratio-row button,
.aspect-ratio-row [role="button"],
.aspect-ratio-row select,
.aspect-ratio-row input { min-width: 48px !important; }
.video-duration-row button,
.video-duration-row [role="button"],
.video-duration-row select,
.video-duration-row input { min-width: 44px !important; }
.output-resolution-row button,
.output-resolution-row [role="button"],
.output-resolution-row select,
.output-resolution-row input { min-width: 70px !important; }
.prompt-chip button,
.prompt-chip [role="button"] { white-space: nowrap !important; }
.prompt-chip .icon-wrap,
.prompt-chip .select-arrow,
.prompt-chip .label-wrap,
.prompt-chip .block-title,
.prompt-chip .block-info,
.prompt-chip label {
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
}
@media (max-width: 1200px) {
  .lance-main-row { grid-template-columns: minmax(0, 1.24fr) minmax(0, 0.76fr) !important; }
  .prompt-options > .form {
    grid-template-columns: repeat(4, max-content) !important;
    justify-content: start !important;
    gap: 4px !important;
  }
  .prompt-chip button, .prompt-chip [role="button"], .prompt-chip select, .prompt-chip input {
    font-size: 9.5px !important;
    min-width: 50px !important;
    padding: 0 6px !important;
  }
  .frame-interpolation-row button,
  .frame-interpolation-row [role="button"],
  .frame-interpolation-row select,
  .frame-interpolation-row input { min-width: 76px !important; }
  .aspect-ratio-row button,
  .aspect-ratio-row [role="button"],
  .aspect-ratio-row select,
  .aspect-ratio-row input { min-width: 42px !important; }
  .video-duration-row button,
  .video-duration-row [role="button"],
  .video-duration-row select,
  .video-duration-row input { min-width: 40px !important; }
}

.prompt-options {
  margin: 8px 0 16px !important;
  padding: 0 !important;
}
.prompt-options > .form {
  display: inline-flex !important;
  flex-wrap: nowrap !important;
  justify-content: flex-start !important;
  justify-items: start !important;
  align-items: center !important;
  gap: 6px !important;
  width: auto !important;
  max-width: 100% !important;
}
.prompt-chip,
.prompt-chip > .form,
.prompt-chip > div,
.prompt-chip .block,
.prompt-chip .form,
.prompt-chip .container,
.prompt-chip .wrap {
  width: auto !important;
  min-width: 0 !important;
  max-width: none !important;
}
.prompt-chip button,
.prompt-chip [role="button"],
.prompt-chip select,
.prompt-chip input {
  width: auto !important;
  min-width: 0 !important;
  height: 30px !important;
  min-height: 30px !important;
  font-size: 9.5px !important;
  padding: 0 8px !important;
  border-radius: 999px !important;
}
.frame-interpolation-row button,
.frame-interpolation-row [role="button"],
.frame-interpolation-row select,
.frame-interpolation-row input { min-width: 74px !important; max-width: 82px !important; }
.video-resolution-row button,
.video-resolution-row [role="button"],
.video-resolution-row select,
.video-resolution-row input { min-width: 50px !important; max-width: 58px !important; }
.aspect-ratio-row button,
.aspect-ratio-row [role="button"],
.aspect-ratio-row select,
.aspect-ratio-row input { min-width: 44px !important; max-width: 52px !important; }
.video-duration-row button,
.video-duration-row [role="button"],
.video-duration-row select,
.video-duration-row input { min-width: 38px !important; max-width: 46px !important; }
.output-resolution-row button,
.output-resolution-row [role="button"],
.output-resolution-row select,
.output-resolution-row input { min-width: 64px !important; max-width: 80px !important; }
@media (max-width: 1200px) {
  .prompt-options > .form {
    display: inline-flex !important;
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    gap: 4px !important;
    width: auto !important;
  }
  .prompt-chip button,
  .prompt-chip [role="button"],
  .prompt-chip select,
  .prompt-chip input {
    font-size: 9px !important;
    padding: 0 6px !important;
    height: 29px !important;
    min-height: 29px !important;
  }
}

.lance-display-frame, .lance-display-frame > div, .lance-display-frame textarea, .output-media-control { width: 100% !important; }
.lance-output-panel { background: transparent !important; }
.lance-output-panel .lance-display-frame > div,
.lance-output-panel .lance-display-frame .wrap,
.lance-output-panel .output-media-control,
.lance-output-panel .output-media-control > div {
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  padding: 0 !important;
}
.lance-output-panel .output-media-control video,
.lance-output-panel .output-media-control img,
.lance-output-panel .lance-display-frame textarea {
  border-radius: 18px !important;
  border: 1px solid rgba(116, 126, 140, .34) !important;
  background: linear-gradient(180deg, rgba(250,251,253,.94), rgba(244,246,249,.9)) !important;
  box-shadow: 0 10px 28px rgba(15,23,42,.10), inset 0 0 0 1px rgba(255,255,255,.75) !important;
}
.lance-output-panel .lance-display-frame textarea { color: #101828 !important; }
.output-media-control video, .output-media-control img { border-radius: 18px !important; }
.lance-run-button { max-width: 1040px !important; margin: 10px auto 16px !important; border-radius: 12px !important; font-size: 18px !important; font-weight: 800 !important; }
.lance-quota-note {
  max-width: 1040px !important;
  margin: -8px auto 16px !important;
  text-align: center !important;
  color: var(--lance-text-muted) !important;
  font-size: 13px !important;
  line-height: 1.45 !important;
}
.lance-quota-note p {
  margin: 0 !important;
}
button.lance-run-button, .lance-run-button button { width: 100% !important; border: 0 !important; border-radius: 12px !important; background: var(--lance-accent) !important; color: #0f172a !important; font-size: 18px !important; font-weight: 800 !important; box-shadow: 0 10px 24px rgba(249,115,22,.22) !important; }
button.lance-run-button:hover, .lance-run-button button:hover { background: var(--lance-accent-hover) !important; color: #0f172a !important; }

button.lance-run-button, .lance-run-button button {
  background: var(--lance-accent) !important;
  color: #0f172a !important;
  box-shadow: 0 10px 24px rgba(249,115,22,.22) !important;
}
button.lance-run-button:hover, .lance-run-button button:hover {
  background: var(--lance-accent-hover) !important;
  color: #0f172a !important;
}

.lance-advanced-accordion { max-width: 1040px; margin: 8px auto 0 !important; }
.lance-advanced-accordion .label-wrap, .lance-advanced-accordion summary { font-weight: 800 !important; }

.lance-recommended-section { max-width: 1040px; margin: 20px auto 0 !important; }
.lance-recommended-section .lance-section-label { text-align: left !important; font-size: 20px !important; margin-bottom: 12px !important; }
.prompt-example-full-table {
  max-height: 420px !important;
  overflow: auto !important;
  border: 1px solid rgba(148,163,184,.24) !important;
  border-radius: 18px !important;
  background: linear-gradient(180deg, #ffffff, #f8fafc) !important;
  box-shadow: 0 12px 28px rgba(15,23,42,.07) !important;
  padding: 12px !important;
}
.prompt-example-full-table > .form { gap: 10px !important; }
.prompt-examples .prompt-example-row-button,
.prompt-examples .prompt-example-row-button button {
  width: 100% !important;
  height: auto !important;
  min-height: 52px !important;
  max-height: 150px !important;
  padding: 12px 14px !important;
  border: 1px solid rgba(148,163,184,.22) !important;
  border-radius: 14px !important;
  background: #fff !important;
  color: var(--lance-text) !important;
  text-align: left !important;
  justify-content: flex-start !important;
  align-items: flex-start !important;
  white-space: normal !important;
  overflow-y: auto !important;
  box-shadow: 0 6px 16px rgba(15,23,42,.045) !important;
  transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease !important;
}
.prompt-examples .prompt-example-row-button:hover,
.prompt-examples .prompt-example-row-button button:hover {
  transform: translateY(-1px) !important;
  border-color: rgba(251,146,60,.48) !important;
  box-shadow: 0 10px 22px rgba(15,23,42,.075) !important;
}
.prompt-examples .prompt-example-row-button span,
.prompt-examples .prompt-example-row-button p,
.prompt-examples .prompt-example-row-button div {
  white-space: pre-wrap !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
  line-height: 1.38 !important;
  color: var(--lance-text) !important;
}

.prompt-example-multimodal-row,
.prompt-example-multimodal-row > .form {
  width: 100% !important;
  min-width: 0 !important;
  margin: 0 !important;
  gap: 12px !important;
  align-items: stretch !important;
}
.prompt-example-multimodal-row > .form {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) 230px !important;
  padding: 8px !important;
  border: 1px solid rgba(148,163,184,.20) !important;
  border-radius: 16px !important;
  background: #fff !important;
  box-shadow: 0 6px 16px rgba(15,23,42,.045) !important;
}
.prompt-example-prompt-cell,
.prompt-example-prompt-cell > .form,
.prompt-example-media-cell,
.prompt-example-media-cell > .form {
  min-width: 0 !important;
  width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
}
.prompt-example-multimodal-row .prompt-example-row-button,
.prompt-example-multimodal-row .prompt-example-row-button button {
  height: 100% !important;
  min-height: 132px !important;
  max-height: 132px !important;
  border: 0 !important;
  box-shadow: none !important;
  background: #f8fafc !important;
}
.prompt-example-media-html,
.prompt-example-media-html > div,
.prompt-example-media-html .wrap {
  width: 100% !important;
  height: 132px !important;
  min-height: 132px !important;
  max-height: 132px !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 1px solid rgba(148,163,184,.22) !important;
  border-radius: 14px !important;
  background: #fff !important;
  box-shadow: none !important;
  overflow: hidden !important;
}
.prompt-example-media-html video,
.prompt-example-media-html img,
.example-preview-video,
.example-preview-image {
  width: 100% !important;
  height: 132px !important;
  border-radius: 12px !important;
  display: block !important;
  background: var(--lance-surface-muted) !important;
  object-fit: contain !important;
  object-position: center center !important;
}
.reference-media-fallback {
  width: 100% !important;
  height: 132px !important;
  border-radius: 12px !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  background: var(--lance-surface-muted) !important;
  color: var(--lance-text-muted) !important;
  font-size: 12px !important;
  font-weight: 700 !important;
  text-align: center !important;
}
@media (max-width: 760px) {
  .prompt-example-multimodal-row > .form { grid-template-columns: minmax(0, 1fr) 140px !important; }
  .prompt-example-multimodal-row .prompt-example-row-button,
  .prompt-example-multimodal-row .prompt-example-row-button button,
  .prompt-example-media-html,
  .prompt-example-media-html > div,
  .prompt-example-media-html .wrap,
  .prompt-example-media-html video,
  .prompt-example-media-html img,
  .example-preview-video,
  .example-preview-image {
    height: 108px !important;
    min-height: 108px !important;
    max-height: 108px !important;
  }
}

@media (max-width: 900px) { .lance-main-row { grid-template-columns: minmax(0, 1fr) !important; } .prompt-options { margin-top: 8px !important; } }

.prompt-example-full-table {
  max-height: none !important;
  overflow: visible !important;
  padding: 18px !important;
}
.prompt-example-full-table > .form {
  gap: 18px !important;
}
.prompt-examples .prompt-example-row-button,
.prompt-examples .prompt-example-row-button button {
  min-height: 168px !important;
  height: auto !important;
  max-height: none !important;
  padding: 22px 24px !important;
  line-height: 1.62 !important;
  overflow: hidden !important;
  display: flex !important;
  align-items: flex-start !important;
}
.prompt-examples .prompt-example-row-button span,
.prompt-examples .prompt-example-row-button p,
.prompt-examples .prompt-example-row-button div {
  line-height: 1.62 !important;
  overflow: hidden !important;
}
.prompt-example-multimodal-row .prompt-example-row-button,
.prompt-example-multimodal-row .prompt-example-row-button button,
.prompt-example-media-html,
.prompt-example-media-html > div,
.prompt-example-media-html .wrap,
.prompt-example-media-html video,
.prompt-example-media-html img,
.example-preview-video,
.example-preview-image,
.reference-media-fallback {
  min-height: 160px !important;
  height: 160px !important;
  max-height: 160px !important;
}

.prompt-example-full-table {
  max-height: 560px !important;
}
.prompt-examples .prompt-example-row-button,
.prompt-examples .prompt-example-row-button button {
  min-height: 96px !important;
  max-height: none !important;
  padding: 18px 20px !important;
  overflow-y: visible !important;
}
.prompt-examples .prompt-example-row-button span,
.prompt-examples .prompt-example-row-button p,
.prompt-examples .prompt-example-row-button div {
  line-height: 1.55 !important;
}

.task-selector label:has(input:checked) {
  box-shadow: 0 4px 10px rgba(249,115,22,.12) !important;
}

.prompt-options {
  margin: 5px 0 14px !important;
}
.prompt-options > .form {
  gap: 7px !important;
}
.prompt-chip button,
.prompt-chip [role="button"],
.prompt-chip select,
.prompt-chip input {
  height: 31px !important;
  min-height: 31px !important;
  font-size: 10.5px !important;
  padding: 0 9px !important;
}
.frame-interpolation-row button,
.frame-interpolation-row [role="button"],
.frame-interpolation-row select,
.frame-interpolation-row input { min-width: 78px !important; max-width: 88px !important; }
.video-resolution-row button,
.video-resolution-row [role="button"],
.video-resolution-row select,
.video-resolution-row input { min-width: 54px !important; max-width: 62px !important; }
.aspect-ratio-row button,
.aspect-ratio-row [role="button"],
.aspect-ratio-row select,
.aspect-ratio-row input { min-width: 48px !important; max-width: 56px !important; }
.video-duration-row button,
.video-duration-row [role="button"],
.video-duration-row select,
.video-duration-row input { min-width: 42px !important; max-width: 50px !important; }
.output-resolution-row button,
.output-resolution-row [role="button"],
.output-resolution-row select,
.output-resolution-row input { min-width: 68px !important; max-width: 86px !important; }

.lance-recommended-section { margin-top: 24px !important; }
.prompt-example-full-table {
  max-height: 480px !important;
  padding: 16px !important;
}
.prompt-example-full-table > .form {
  gap: 12px !important;
}
.prompt-examples .prompt-example-row-button,
.prompt-examples .prompt-example-row-button button {
  min-height: 66px !important;
  padding: 16px 18px !important;
  line-height: 1.48 !important;
}
.prompt-examples .prompt-example-row-button span,
.prompt-examples .prompt-example-row-button p,
.prompt-examples .prompt-example-row-button div {
  line-height: 1.48 !important;
}
.prompt-example-multimodal-row,
.prompt-example-multimodal-row > .form {
  gap: 14px !important;
}
.prompt-example-multimodal-row > .form {
  padding: 12px !important;
}
.prompt-example-multimodal-row .prompt-example-row-button,
.prompt-example-multimodal-row .prompt-example-row-button button,
.prompt-example-media-html,
.prompt-example-media-html > div,
.prompt-example-media-html .wrap,
.prompt-example-media-html video,
.prompt-example-media-html img,
.example-preview-video,
.example-preview-image,
.reference-media-fallback {
  min-height: 148px !important;
  height: 148px !important;
  max-height: 148px !important;
}

@media (max-width: 1200px) {
  .prompt-options { margin-top: 5px !important; }
  .prompt-chip button,
  .prompt-chip [role="button"],
  .prompt-chip select,
  .prompt-chip input {
    font-size: 10px !important;
    height: 30px !important;
    min-height: 30px !important;
    padding: 0 7px !important;
  }
}

.prompt-example-full-table,
.prompt-example-full-table > .form,
.prompt-examples,
.prompt-examples > .form {
  max-height: none !important;
  height: auto !important;
  overflow: visible !important;
}

.prompt-example-full-table {
  padding: 16px !important;
}

.prompt-example-full-table > .form {
  gap: 14px !important;
}

.prompt-examples .prompt-example-row-button,
.prompt-examples .prompt-example-row-button button {
  min-height: 96px !important;
  height: auto !important;
  max-height: none !important;
  padding: 18px 22px !important;
  overflow: visible !important;
  white-space: normal !important;
  display: block !important;
  text-align: left !important;
}

.prompt-examples .prompt-example-row-button span,
.prompt-examples .prompt-example-row-button p,
.prompt-examples .prompt-example-row-button div {
  max-height: none !important;
  height: auto !important;
  overflow: visible !important;
  white-space: normal !important;
  overflow-wrap: anywhere !important;
  word-break: normal !important;
  line-height: 1.5 !important;
  text-overflow: unset !important;
  -webkit-line-clamp: unset !important;
  line-clamp: unset !important;
}

.prompt-example-multimodal-row,
.prompt-example-multimodal-row > .form {
  max-height: none !important;
  overflow: visible !important;
  gap: 12px !important;
}

.prompt-example-multimodal-row > .form {
  padding: 12px !important;
}

.prompt-example-multimodal-row .prompt-example-row-button,
.prompt-example-multimodal-row .prompt-example-row-button button,
.prompt-example-media-html,
.prompt-example-media-html > div,
.prompt-example-media-html .wrap,
.prompt-example-media-html video,
.prompt-example-media-html img,
.example-preview-video,
.example-preview-image,
.reference-media-fallback {
  min-height: 148px !important;
  height: 148px !important;
  max-height: 148px !important;
}

.lance-output-panel .output-media-control {
  min-height: 220px !important;
  border: 1px solid rgba(116,126,140,.34) !important;
  border-radius: 18px !important;
  background: linear-gradient(180deg, rgba(250,251,253,.94), rgba(244,246,249,.9)) !important;
  box-shadow: 0 10px 28px rgba(15,23,42,.10), inset 0 0 0 1px rgba(255,255,255,.75) !important;
  overflow: hidden !important;
}

.lance-output-panel .output-media-control > div,
.lance-output-panel .output-media-control .wrap {
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
}

.lance-output-panel .output-media-control video,
.lance-output-panel .output-media-control img {
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  border-radius: 18px !important;
  width: 100% !important;
  height: 100% !important;
  object-fit: contain !important;
}

.frame-interpolation-row button,
.frame-interpolation-row [role="button"],
.frame-interpolation-row select,
.frame-interpolation-row input {
  min-width: 138px !important;
  max-width: 158px !important;
  width: auto !important;
  font-size: 10.5px !important;
  padding-left: 12px !important;
  padding-right: 12px !important;
}

@media (max-width: 1200px) {
  .frame-interpolation-row button,
  .frame-interpolation-row [role="button"],
  .frame-interpolation-row select,
  .frame-interpolation-row input {
    min-width: 126px !important;
    max-width: 146px !important;
    font-size: 10px !important;
    padding-left: 10px !important;
    padding-right: 10px !important;
  }
}

.lance-output-panel .output-text-control {
  min-height: 220px !important;
  border: 1px solid rgba(116,126,140,.34) !important;
  border-radius: 18px !important;
  background: linear-gradient(180deg, rgba(250,251,253,.94), rgba(244,246,249,.9)) !important;
  box-shadow: 0 10px 28px rgba(15,23,42,.10), inset 0 0 0 1px rgba(255,255,255,.75) !important;
  overflow: hidden !important;
  padding: 0 !important;
}

.lance-output-panel .output-text-control > div,
.lance-output-panel .output-text-control .wrap,
.lance-output-panel .output-text-control .container {
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  padding: 0 !important;
}

.lance-output-panel .output-text-control textarea {
  min-height: 220px !important;
  border: 0 !important;
  border-radius: 18px !important;
  background: transparent !important;
  box-shadow: none !important;
  color: #101828 !important;
  padding: 18px !important;
  resize: none !important;
}

.prompt-options > .form {
  display: inline-flex !important;
  flex-wrap: nowrap !important;
  justify-content: flex-start !important;
  align-items: center !important;
  gap: 8px !important;
  width: auto !important;
  max-width: 100% !important;
}

.prompt-chip button,
.prompt-chip [role="button"],
.prompt-chip select,
.prompt-chip input {
  height: 36px !important;
  min-height: 36px !important;
  font-size: 12px !important;
  font-weight: 800 !important;
  padding-left: 12px !important;
  padding-right: 12px !important;
}

.frame-interpolation-row button,
.frame-interpolation-row [role="button"],
.frame-interpolation-row select,
.frame-interpolation-row input {
  min-width: 166px !important;
  max-width: 184px !important;
}

.video-resolution-row button,
.video-resolution-row [role="button"],
.video-resolution-row select,
.video-resolution-row input {
  min-width: 74px !important;
  max-width: 84px !important;
}

.aspect-ratio-row button,
.aspect-ratio-row [role="button"],
.aspect-ratio-row select,
.aspect-ratio-row input {
  min-width: 72px !important;
  max-width: 82px !important;
}

.video-duration-row button,
.video-duration-row [role="button"],
.video-duration-row select,
.video-duration-row input {
  min-width: 62px !important;
  max-width: 72px !important;
}

.output-resolution-row button,
.output-resolution-row [role="button"],
.output-resolution-row select,
.output-resolution-row input {
  min-width: 92px !important;
  max-width: 114px !important;
}

@media (max-width: 1200px) {
  .prompt-options > .form {
    gap: 6px !important;
  }
  .prompt-chip button,
  .prompt-chip [role="button"],
  .prompt-chip select,
  .prompt-chip input {
    height: 34px !important;
    min-height: 34px !important;
    font-size: 11px !important;
    padding-left: 9px !important;
    padding-right: 9px !important;
  }
  .frame-interpolation-row button,
  .frame-interpolation-row [role="button"],
  .frame-interpolation-row select,
  .frame-interpolation-row input {
    min-width: 148px !important;
    max-width: 166px !important;
  }
  .video-resolution-row button,
  .video-resolution-row [role="button"],
  .video-resolution-row select,
  .video-resolution-row input {
    min-width: 66px !important;
    max-width: 76px !important;
  }
  .aspect-ratio-row button,
  .aspect-ratio-row [role="button"],
  .aspect-ratio-row select,
  .aspect-ratio-row input {
    min-width: 64px !important;
    max-width: 74px !important;
  }
  .video-duration-row button,
  .video-duration-row [role="button"],
  .video-duration-row select,
  .video-duration-row input {
    min-width: 56px !important;
    max-width: 66px !important;
  }
}

.lance-run-button {
  margin-bottom: 6px !important;
}

.lance-quota-note,
.lance-quota-note > div,
.lance-quota-note .wrap,
.lance-quota-note .prose {
  min-height: 0 !important;
  padding-top: 0 !important;
  padding-bottom: 0 !important;
}

.lance-quota-note {
  max-width: 1040px !important;
  margin: 0 auto 8px !important;
  text-align: center !important;
  color: var(--lance-text-muted) !important;
  font-size: 12px !important;
  line-height: 1.1 !important;
}

.lance-quota-note p {
  margin: 0 !important;
  padding: 0 !important;
  line-height: 1.1 !important;
}

.frame-interpolation-row,
.frame-interpolation-disabled {
  display: none !important;
  visibility: hidden !important;
  width: 0 !important;
  max-width: 0 !important;
  height: 0 !important;
  max-height: 0 !important;
  min-height: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
}

"""

APP_JS = None

TASK_T2V = "t2v"
TASK_T2I = "t2i"
TASK_V2T = "v2t"
TASK_X2T = "x2t"
TASK_X2T_VIDEO = "x2t_video"
TASK_X2T_IMAGE = "x2t_image"
TASK_IMAGE_EDIT = "image_edit"
TASK_VIDEO_EDIT = "video_edit"
TASK_LABEL_VIDEO_GENERATION = "Video Generation"
TASK_LABEL_VIDEO_EDIT = "Video Edit"
TASK_LABEL_VIDEO_UNDERSTANDING = "Video Understanding"
TASK_LABEL_IMAGE_GENERATION = "Image Generation"
TASK_LABEL_IMAGE_EDIT = "Image Edit"
TASK_LABEL_IMAGE_UNDERSTANDING = "Image Understanding"
TASK_CHOICES = [
    TASK_LABEL_VIDEO_GENERATION,
    TASK_LABEL_VIDEO_EDIT,
    TASK_LABEL_VIDEO_UNDERSTANDING,
    TASK_LABEL_IMAGE_GENERATION,
    TASK_LABEL_IMAGE_EDIT,
    TASK_LABEL_IMAGE_UNDERSTANDING,
]
TASK_LABEL_TO_INTERNAL = {
    TASK_LABEL_VIDEO_GENERATION: TASK_T2V,
    TASK_LABEL_VIDEO_EDIT: TASK_VIDEO_EDIT,
    TASK_LABEL_VIDEO_UNDERSTANDING: TASK_X2T_VIDEO,
    TASK_LABEL_IMAGE_GENERATION: TASK_T2I,
    TASK_LABEL_IMAGE_EDIT: TASK_IMAGE_EDIT,
    TASK_LABEL_IMAGE_UNDERSTANDING: TASK_X2T_IMAGE,
    TASK_T2V: TASK_T2V,
    TASK_VIDEO_EDIT: TASK_VIDEO_EDIT,
    TASK_V2T: TASK_X2T_VIDEO,
    TASK_X2T: TASK_X2T_VIDEO,
    TASK_X2T_VIDEO: TASK_X2T_VIDEO,
    TASK_T2I: TASK_T2I,
    TASK_IMAGE_EDIT: TASK_IMAGE_EDIT,
    TASK_X2T_IMAGE: TASK_X2T_IMAGE,
}
GENERATION_TASKS = {TASK_T2V, TASK_T2I, TASK_IMAGE_EDIT, TASK_VIDEO_EDIT}
UNDERSTANDING_TASKS = {TASK_X2T_VIDEO, TASK_X2T_IMAGE}
IMAGE_TASKS = {TASK_T2I, TASK_IMAGE_EDIT, TASK_X2T_IMAGE}
VIDEO_TASKS = {TASK_T2V, TASK_VIDEO_EDIT, TASK_X2T_VIDEO}
EDIT_TASKS = {TASK_IMAGE_EDIT, TASK_VIDEO_EDIT}
VIDEO_RESOLUTION_CHOICES = [DEFAULT_RESOLUTION]
VIDEO_EDIT_RESOLUTION_CHOICES = [DEFAULT_VIDEO_EDIT_RESOLUTION]
IMAGE_RESOLUTION_CHOICES = [DEFAULT_IMAGE_RESOLUTION]
RESOLUTION_CHOICES = VIDEO_RESOLUTION_CHOICES + IMAGE_RESOLUTION_CHOICES
VIDEO_RESOLUTION_DISPLAY_CHOICES = [("360p", "video_360p"), ("480p", "video_480p")]
V2T_QA_SYSTEM_PROMPT = "View the video  attentively and provide a suitable answer to the posed question."
I2T_QA_SYSTEM_PROMPT = "View the image attentively and provide a suitable answer to the posed question."


def get_aspect_ratio_choices_for_task(task: str) -> list[tuple[str, str]]:
    """Get Aspect Ratio choices with default/recommended marker for the given task."""
    internal_task = normalize_task(task)
    default_ratio = DEFAULT_IMAGE_ASPECT_RATIO if internal_task in IMAGE_TASKS else DEFAULT_VIDEO_ASPECT_RATIO
    return [
        (f"{ratio}" if ratio == default_ratio else ratio, ratio)
        for ratio in ASPECT_RATIO_CHOICES
    ]


def get_video_duration_choices() -> list[tuple[str, int]]:
    return [(f"{seconds}s", seconds) for seconds in range(1, 11)]

def env_flag(name: str, default: bool) -> bool:
    value = os.getenv(name)
    if value is None:
        return default
    return value.strip().lower() in {"1", "true", "yes", "on"}


def running_on_space() -> bool:
    return bool(os.getenv("SPACE_ID") or os.getenv("SPACE_HOST"))


def display_path(path: Path) -> str:
    path_text = path.as_posix()
    if path.is_absolute():
        try:
            path_text = path.relative_to(Path.cwd()).as_posix()
        except ValueError:
            return path_text
    if path_text == "." or path_text.startswith("./"):
        return path_text
    return f"./{path_text}"


def get_model_base_dir() -> Path:
    configured = os.getenv("LANCE_MODEL_BASE_DIR")
    if configured:
        configured_path = Path(configured).expanduser()
        if _path_can_be_created_or_written(configured_path):
            return configured_path
    if LOCAL_MODEL_BASE_DIR.exists():
        return LOCAL_MODEL_BASE_DIR
    if running_on_space() and SPACE_MODEL_BASE_DIR.exists() and os.access(SPACE_MODEL_BASE_DIR, os.W_OK):
        return SPACE_MODEL_BASE_DIR
    return LOCAL_MODEL_BASE_DIR


def _path_can_be_created_or_written(path: Path) -> bool:
    if path.exists():
        return path.is_dir() and os.access(path, os.W_OK)
    probe = path.parent
    while not probe.exists() and probe != probe.parent:
        probe = probe.parent
    return probe.exists() and os.access(probe, os.W_OK)


def normalize_model_variant(model_variant: Optional[str] = None) -> str:
    variant = (model_variant or os.getenv("LANCE_MODEL_VARIANT", DEFAULT_MODEL_VARIANT)).strip().lower()
    if variant in {"image", "t2i", "i2t"}:
        return MODEL_VARIANT_IMAGE
    return MODEL_VARIANT_VIDEO


def get_model_path(model_variant: Optional[str] = None) -> Path:
    variant = normalize_model_variant(model_variant)
    variant_env_name = "LANCE_IMAGE_MODEL_PATH" if variant == MODEL_VARIANT_IMAGE else "LANCE_VIDEO_MODEL_PATH"
    variant_configured = os.getenv(variant_env_name)
    if variant_configured:
        return Path(variant_configured).expanduser()

    configured = os.getenv("LANCE_MODEL_PATH")
    if configured:
        return Path(configured).expanduser()

    model_dir_name = MODEL_VARIANT_TO_DIR[variant]
    return get_model_base_dir() / model_dir_name


def get_required_model_asset_paths(model_base_dir: Path, model_path: Path) -> list[Path]:
    return [
        model_path / "llm_config.json",
        model_path / "model.safetensors",
        model_base_dir / "Qwen2.5-VL-ViT" / "vit.safetensors",
        model_base_dir / "Wan2.2_VAE.pth",
    ]


def get_model_download_allow_patterns(model_variant: Optional[str] = None) -> list[str]:
    variant = normalize_model_variant(model_variant)
    model_dir_name = MODEL_VARIANT_TO_DIR[variant]
    return [
        f"{model_dir_name}/**",
        "Qwen2.5-VL-ViT/**",
        "Wan2.2_VAE.pth",
        "generation_config.json",
        "llm_config.json",
        "tokenizer.json",
        "tokenizer_config.json",
        "vocab.json",
        "merges.txt",
        "config.json",
    ]


def _get_safetensors_first_tensor_dtype(path: Path) -> Optional[torch.dtype]:
    if not path.exists():
        return None
    with safe_open(str(path), framework="pt", device="cpu") as f:
        keys = list(f.keys())
        if not keys:
            return None
        return f.get_tensor(keys[0]).dtype


def convert_model_weights_to_bf16_inplace(model_path: Path) -> bool:
    weight_path = model_path / "model.safetensors"
    if not weight_path.exists():
        return False

    first_dtype = _get_safetensors_first_tensor_dtype(weight_path)
    if first_dtype is None or first_dtype == torch.bfloat16:
        return False

    if first_dtype != torch.float32:
        print(
            f"[startup] Skipping bf16 conversion for {weight_path} because the first tensor dtype is {first_dtype}.",
            flush=True,
        )
        return False

    temp_path = weight_path.with_suffix(".bf16.safetensors.tmp")
    print(f"[startup] Converting {weight_path} to bf16 to reduce disk usage.", flush=True)
    with safe_open(str(weight_path), framework="pt", device="cpu") as f:
        metadata = f.metadata()
        tensor_names = list(f.keys())
        tensors = {}
        for name in tensor_names:
            tensor = f.get_tensor(name)
            tensors[name] = tensor.to(torch.bfloat16) if tensor.dtype == torch.float32 else tensor
        save_file(tensors, str(temp_path), metadata=metadata)

    os.replace(temp_path, weight_path)
    print(f"[startup] Replaced original fp32 weights with bf16 weights at {weight_path}.", flush=True)
    return True


def compact_downloaded_model_weights(model_base_dir: Path, variants: Optional[list[str]] = None) -> None:
    model_dir_names = variants or [MODEL_VARIANT_TO_DIR[MODEL_VARIANT_IMAGE], MODEL_VARIANT_TO_DIR[MODEL_VARIANT_VIDEO]]
    for model_dir_name in model_dir_names:
        model_path = model_base_dir / model_dir_name
        try:
            convert_model_weights_to_bf16_inplace(model_path)
        except Exception as exc:
            print(f"[startup] bf16 compaction skipped for {display_path(model_path)}: {exc}", flush=True)


def ensure_model_assets(model_variant: Optional[str] = None) -> Path:
    model_base_dir = get_model_base_dir()
    os.environ["LANCE_MODEL_BASE_DIR"] = display_path(model_base_dir)
    model_path = get_model_path(model_variant)

    required_paths = get_required_model_asset_paths(model_base_dir, model_path)
    if all(path.exists() for path in required_paths):
        compact_downloaded_model_weights(model_base_dir, [MODEL_VARIANT_TO_DIR[normalize_model_variant(model_variant)]])
        return model_path

    downloads_model_base_dir = Path("downloads")
    if model_base_dir == Path(".") and downloads_model_base_dir.exists():
        downloads_model_path = downloads_model_base_dir / MODEL_VARIANT_TO_DIR[normalize_model_variant(model_variant)]
        downloads_required_paths = get_required_model_asset_paths(downloads_model_base_dir, downloads_model_path)
        if all(path.exists() for path in downloads_required_paths):
            model_base_dir = downloads_model_base_dir
            model_path = downloads_model_path
            required_paths = downloads_required_paths
            os.environ["LANCE_MODEL_BASE_DIR"] = display_path(model_base_dir)
            compact_downloaded_model_weights(model_base_dir, [MODEL_VARIANT_TO_DIR[normalize_model_variant(model_variant)]])
            return model_path

    auto_download = env_flag("LANCE_AUTO_DOWNLOAD", running_on_space())
    if not auto_download:
        missing = "\n".join(f"- {display_path(path)}" for path in required_paths if not path.exists())
        raise FileNotFoundError(
            "Lance model assets are missing. Set LANCE_MODEL_BASE_DIR or enable "
            f"LANCE_AUTO_DOWNLOAD=1.\nMissing files:\n{missing}"
        )

    model_base_dir.mkdir(parents=True, exist_ok=True)
    repo_id = os.getenv("LANCE_MODEL_REPO_ID", DEFAULT_MODEL_REPO_ID)
    print(f"[startup] Downloading Lance model assets from {repo_id} to {display_path(model_base_dir)}", flush=True)
    hub_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
    snapshot_path = Path(
        snapshot_download(
            repo_id=repo_id,
            local_dir=str(model_base_dir),
            local_dir_use_symlinks=False,
            resume_download=True,
            token=hub_token,
            allow_patterns=get_model_download_allow_patterns(model_variant),
        )
    )
    if snapshot_path != model_base_dir and not model_path.exists():
        os.environ["LANCE_MODEL_BASE_DIR"] = display_path(snapshot_path)
        model_path = get_model_path(model_variant)
    compact_downloaded_model_weights(model_base_dir, [MODEL_VARIANT_TO_DIR[normalize_model_variant(model_variant)]])
    return model_path


def ensure_dirs() -> None:
    TMP_INPUT_DIR.mkdir(parents=True, exist_ok=True)
    RESULTS_ROOT.mkdir(parents=True, exist_ok=True)


def save_generation_record(record: dict, save_dir: Path) -> None:
    ensure_dirs()
    run_record_path = save_dir / RUN_RECORD_FILENAME
    with run_record_path.open("w", encoding="utf-8") as f:
        json.dump(record, f, ensure_ascii=False, indent=2)

    with RECORD_WRITE_LOCK:
        with GLOBAL_RECORDS_FILE.open("a", encoding="utf-8") as f:
            f.write(json.dumps(record, ensure_ascii=False) + "\n")


def normalize_seed(seed: int) -> int:
    return random.randint(0, 2**31 - 1) if seed == -1 else seed


def video_seconds_to_num_frames(seconds: int) -> int:
    seconds = max(1, min(10, int(seconds)))
    return 12 * seconds + 1


def normalize_task(task: str) -> str:
    task_key = (task or TASK_LABEL_VIDEO_GENERATION).strip()
    task = TASK_LABEL_TO_INTERNAL.get(task_key, TASK_LABEL_TO_INTERNAL.get(task_key.lower(), ""))
    if task not in GENERATION_TASKS | UNDERSTANDING_TASKS:
        raise ValueError(f"Unsupported task type: {task}")
    return task


def normalize_resolution_choice_value(resolution: str, task: str) -> str:
    resolution_text = str(resolution or "").strip()
    for choice in get_resolution_choices_for_task(task):
        if isinstance(choice, tuple):
            label, value = choice
            if resolution_text in {str(label), str(value)}:
                return str(value)
        elif resolution_text == str(choice):
            return str(choice)
    return resolution_text


def get_resolution_choice_values_for_task(task: str) -> list[str]:
    return [choice[1] if isinstance(choice, tuple) else choice for choice in get_resolution_choices_for_task(task)]


def get_resolution_choices_for_task(task: str) -> list[str | tuple[str, str]]:
    internal_task = normalize_task(task)
    if internal_task in IMAGE_TASKS:
        return IMAGE_RESOLUTION_CHOICES
    if internal_task == TASK_T2V:
        return VIDEO_RESOLUTION_DISPLAY_CHOICES
    return VIDEO_EDIT_RESOLUTION_CHOICES if internal_task in VIDEO_TASKS else VIDEO_RESOLUTION_CHOICES


def get_default_resolution_for_task(task: str) -> str:
    internal_task = normalize_task(task)
    if internal_task in IMAGE_TASKS:
        return DEFAULT_IMAGE_RESOLUTION
    if internal_task == TASK_T2V:
        return DEFAULT_RESOLUTION
    return DEFAULT_VIDEO_EDIT_RESOLUTION if internal_task in VIDEO_TASKS else DEFAULT_RESOLUTION


def normalize_resolution_for_backend(resolution: str, task: str) -> str:
    internal_task = normalize_task(task)
    normalized_resolution = normalize_resolution_choice_value(resolution, internal_task)
    return normalized_resolution if normalized_resolution in get_resolution_choice_values_for_task(internal_task) else get_default_resolution_for_task(internal_task)


def get_default_aspect_ratio(task: str) -> str:
    internal_task = normalize_task(task)
    return DEFAULT_IMAGE_ASPECT_RATIO if internal_task in IMAGE_TASKS else DEFAULT_VIDEO_ASPECT_RATIO


def normalize_video_resolution(resolution: Optional[str], task: Optional[str] = None) -> str:
    if task is None:
        return resolution if resolution in VIDEO_RESOLUTION_CHOICES else DEFAULT_RESOLUTION
    normalized_resolution = normalize_resolution_choice_value(resolution, task)
    choices = get_resolution_choice_values_for_task(task)
    return normalized_resolution if normalized_resolution in choices else get_default_resolution_for_task(task)


def get_size_for_aspect_ratio(task: str, aspect_ratio: str, video_resolution: Optional[str] = None) -> tuple[int, int]:
    internal_task = normalize_task(task)
    aspect_ratio = aspect_ratio if aspect_ratio in ASPECT_RATIO_CHOICES else get_default_aspect_ratio(internal_task)
    if internal_task in IMAGE_TASKS:
        size_map = IMAGE_ASPECT_RATIO_TO_SIZE
    else:
        size_map = VIDEO_RESOLUTION_TO_SIZE_MAP[normalize_video_resolution(video_resolution, internal_task)]
    return size_map[aspect_ratio]


def format_size_markdown(task: str, width: int, height: int) -> str:
    return "" if normalize_task(task) in UNDERSTANDING_TASKS else f"{width} x {height}"


def get_size_map_for_task(task: str, video_resolution: Optional[str] = None) -> dict[str, tuple[int, int]]:
    internal_task = normalize_task(task)
    if internal_task in IMAGE_TASKS:
        return IMAGE_ASPECT_RATIO_TO_SIZE
    return VIDEO_RESOLUTION_TO_SIZE_MAP[normalize_video_resolution(video_resolution, internal_task)]


def get_output_resolution_choices_for_task(task: str, video_resolution: Optional[str] = None) -> list[tuple[str, str]]:
    """Get Output Resolution choices with a one-to-one mapping to aspect ratios."""
    internal_task = normalize_task(task)
    default_ratio = get_default_aspect_ratio(internal_task)
    size_map = get_size_map_for_task(internal_task, video_resolution)
    choices = []
    for ratio in ASPECT_RATIO_CHOICES:
        width, height = size_map[ratio]
        resolution_text = format_size_markdown(internal_task, width, height)
        label = f"{resolution_text}" if ratio == default_ratio else resolution_text
        choices.append((label, resolution_text))
    return choices




def build_lance_label_html(text: str, *extra_classes: str) -> str:
    class_names = " ".join(["lance-section-label", *extra_classes]).strip()
    return f'<div class="{class_names}">{html.escape(text)}</div>'


def build_lance_icon_label_html(text: str, icon: str, *extra_classes: str) -> str:
    icon_map = {
        "video": """
            <span class="lance-label-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="3.5" y="6" width="11" height="12" rx="2.2"></rect>
                    <path d="M15 10.2 20.5 7v10L15 13.8z" fill="currentColor" stroke="none"></path>
                </svg>
            </span>
        """,
        "image": """
            <span class="lance-label-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="3.5" y="5.5" width="17" height="13" rx="2.2"></rect>
                    <circle cx="9" cy="10" r="1.5" fill="currentColor" stroke="none"></circle>
                    <path d="M5.5 16.5 10 12l2.7 2.7 2.1-2.1 3.7 3.9"></path>
                </svg>
            </span>
        """,
        "text": """
            <span class="lance-label-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="3.5" y="5.5" width="17" height="13" rx="2.2"></rect>
                    <path d="M7 9h10"></path>
                    <path d="M7 12h7.5"></path>
                    <path d="M7 15h5.5"></path>
                </svg>
            </span>
        """,
        "logs": """
            <span class="lance-label-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="3.5" y="5.5" width="17" height="13" rx="2.2"></rect>
                    <path d="M7 10.2 10 12l-3 1.8"></path>
                    <path d="M12.5 15h4"></path>
                </svg>
            </span>
        """,
    }
    icon_html = icon_map.get(icon, "")
    class_names = " ".join(["lance-section-label", "lance-icon-label", *extra_classes]).strip()
    return f'<div class="{class_names}">{icon_html}<span>{html.escape(text)}</span></div>'


def update_size_from_aspect_ratio(task: str, aspect_ratio: str, video_resolution: Optional[str] = None):
    width, height = get_size_for_aspect_ratio(task, aspect_ratio, video_resolution)
    return height, width, gr.update(
        choices=get_output_resolution_choices_for_task(task, video_resolution),
        value=format_size_markdown(task, width, height),
    )


def update_output_resolution_from_video_profile(task: str, aspect_ratio: str, video_resolution: str):
    width, height = get_size_for_aspect_ratio(task, aspect_ratio, video_resolution)
    return (
        gr.update(
            choices=get_output_resolution_choices_for_task(task, video_resolution),
            value=format_size_markdown(task, width, height),
        ),
        height,
        width,
    )


def reset_generation_defaults_for_task(task: str):
    internal_task = normalize_task(task)
    aspect_ratio = get_default_aspect_ratio(internal_task)
    resolution = get_default_resolution_for_task(internal_task)
    width, height = get_size_for_aspect_ratio(internal_task, aspect_ratio, resolution)
    num_frames = DEFAULT_VIDEO_DURATION_SECONDS
    return aspect_ratio, height, width, num_frames, resolution, gr.update(
        choices=get_output_resolution_choices_for_task(internal_task, resolution),
        value=format_size_markdown(internal_task, width, height),
    )


def make_prompt_example_click_handler(prompt_text: str, cache_key: str = ""):
    """Create a click handler for custom text-to-visual prompt-example rows.

    gr.Dataset and gr.Examples render long text through compact preview cells, so
    long prompts/instructions/questions can be truncated before CSS gets a chance
    to wrap them. The custom rows below use normal buttons for display and keep
    the full prompt string in this closure for click-to-fill behavior.
    """

    def _handler(task: str):
        defaults = reset_generation_defaults_for_task(task)
        return (prompt_text, pack_recommended_cache_carrier(cache_key, task), *defaults)

    return _handler


def make_media_prompt_example_click_handler(
    prompt_text: str,
    input_video_path: Optional[str] = None,
    input_image_path: Optional[str] = None,
    cache_key: str = "",
):
    """Create a click handler for edit/understanding example rows.

    The row button renders the complete prompt/instruction/question, while the
    closure also carries the matching media path so one click still fills every
    required input component.
    """

    def _handler(task: str):
        defaults = reset_generation_defaults_for_task(task)
        return (prompt_text, input_video_path, input_image_path, pack_recommended_cache_carrier(cache_key, task), *defaults)

    return _handler


def get_understanding_system_prompt_choices(task: str) -> list[str]:
    internal_task = normalize_task(task)
    if internal_task == TASK_X2T_IMAGE:
        return [I2T_QA_SYSTEM_PROMPT]
    return [V2T_QA_SYSTEM_PROMPT]


def normalize_understanding_system_prompt(task: str, system_prompt: Optional[str]) -> str:
    return get_understanding_system_prompt_choices(task)[0]


RECOMMENDED_CACHE_CARRIER_PREFIX = "__LANCE_RECOMMENDED_CASE_KEY__="


def pack_recommended_cache_carrier(cache_key: str, task: str) -> str:
    """Carry a recommended case key through the existing hidden system_prompt input.

    This keeps Generate at the original Gradio inputs while carrying only the
    example identity. Actual cache hits are validated later with a full request
    signature so user-edited parameters never reuse the wrong output.
    """
    internal_task = normalize_task(task)
    base_prompt = normalize_understanding_system_prompt(internal_task, None) if internal_task in UNDERSTANDING_TASKS else ""
    if not cache_key:
        return base_prompt
    return f"{RECOMMENDED_CACHE_CARRIER_PREFIX}{cache_key}\n{base_prompt}"


def unpack_recommended_cache_carrier(system_prompt: Optional[str]) -> tuple[str, Optional[str]]:
    text = str(system_prompt or "")
    if not text.startswith(RECOMMENDED_CACHE_CARRIER_PREFIX):
        return "", system_prompt
    payload = text[len(RECOMMENDED_CACHE_CARRIER_PREFIX):]
    cache_key, _, base_prompt = payload.partition("\n")
    return cache_key.strip(), (base_prompt if base_prompt else None)


def create_request_json(
    task: str,
    prompt: str,
    input_video: Optional[str],
    input_image: Optional[str],
    system_prompt: Optional[str] = None,
) -> Path:
    ensure_dirs()
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
    prompt_file = TMP_INPUT_DIR / f"{task}_{timestamp}.json"

    if task == TASK_T2V:
        payload = {"000000.mp4": prompt}
    elif task == TASK_T2I:
        payload = {"000000.png": prompt}
    elif task == TASK_VIDEO_EDIT:
        if not input_video:
            raise ValueError("The video edit task requires an input video.")
        payload = {
            "000000": {
                "interleave_array": [prompt, input_video, input_video],
                "element_dtype_array": ["text", "video", "video"],
                "istarget_in_interleave": [0, 0, 1],
            }
        }
    elif task == TASK_IMAGE_EDIT:
        if not input_image:
            raise ValueError("The image edit task requires an input image.")
        payload = {
            "000000": {
                "interleave_array": [prompt, input_image, input_image],
                "element_dtype_array": ["text", "image", "image"],
                "istarget_in_interleave": [0, 0, 1],
            }
        }
    elif task == TASK_X2T_VIDEO:
        if not input_video:
            raise ValueError("The video understanding task requires an input video.")
        system_prompt = normalize_understanding_system_prompt(task, system_prompt)
        payload = {
            "000000": {
                "interleave_array": [input_video, [system_prompt, prompt, ""]],
                "element_dtype_array": ["video", "text"],
                "istarget_in_interleave": [0, 1],
            }
        }
    elif task == TASK_X2T_IMAGE:
        if not input_image:
            raise ValueError("The image understanding task requires an input image.")
        system_prompt = normalize_understanding_system_prompt(task, system_prompt)
        payload = {
            "000000": {
                "interleave_array": [input_image, [system_prompt, prompt, ""]],
                "element_dtype_array": ["image", "text"],
                "istarget_in_interleave": [0, 1],
            }
        }
    else:
        raise ValueError(f"Unsupported task type: {task}")

    with prompt_file.open("w", encoding="utf-8") as f:
        json.dump(payload, f, ensure_ascii=False, indent=2)
    return prompt_file


def resolve_example_path(path: str) -> str:
    candidate = Path(path)
    if candidate.is_absolute():
        return str(candidate)
    repo_candidate = (REPO_ROOT / candidate)
    if repo_candidate.exists():
        return str(repo_candidate.resolve())
    if candidate.exists():
        return str(candidate.resolve())
    return path


def resolve_browser_video_example_path(path: str) -> str:
    candidate = Path(path)
    compatible_candidate = candidate.with_name(f"{candidate.stem}_h264{candidate.suffix}")
    repo_compatible_candidate = REPO_ROOT / compatible_candidate
    if not compatible_candidate.is_absolute() and repo_compatible_candidate.exists():
        return str(repo_compatible_candidate.resolve())
    if compatible_candidate.is_absolute() and compatible_candidate.exists():
        return str(compatible_candidate.resolve())
    repo_candidate = REPO_ROOT / candidate
    if not candidate.is_absolute() and repo_candidate.exists():
        return str(repo_candidate.resolve())
    if candidate.is_absolute() and candidate.exists():
        return str(candidate.resolve())
    return resolve_example_path(path)


def resolve_video_example_paths(path: str) -> tuple[str, str]:
    """Return (browser_preview_path, model_input_path) for a reference video."""
    return resolve_browser_video_example_path(path), resolve_example_path(path)


def _resolve_existing_media_path(media_path: Optional[str]) -> Optional[Path]:
    if not media_path:
        return None
    candidate = Path(str(media_path))
    candidates = [candidate] if candidate.is_absolute() else [REPO_ROOT / candidate, candidate]
    for item in candidates:
        try:
            resolved = item.expanduser().resolve()
        except Exception:
            continue
        if resolved.exists():
            return resolved
    return None


def build_gradio_media_url(media_path: Optional[str]) -> str:
    """Build a Gradio file-serving URL for local recommended-case media."""
    existing = _resolve_existing_media_path(media_path)
    source = str(existing if existing else media_path or "")
    if not source:
        return ""
    try:
        from gradio.route_utils import API_PREFIX
    except Exception:
        API_PREFIX = ""
    return f"{API_PREFIX or ''}/file={quote(source, safe='/:')}"


def build_example_media_html(media_path: Optional[str], media_type: str, fallback_media_path: Optional[str] = None) -> str:
    """Build a lightweight complete-fit media preview for recommended cases."""
    if media_type == "video":
        sources = []
        for candidate in (media_path, fallback_media_path):
            url = build_gradio_media_url(candidate)
            if url and url not in sources:
                sources.append(url)
        if not sources:
            return '<div class="reference-media-fallback">Video file not found</div>'
        source_tags = "".join(
            f'<source src="{html.escape(url, quote=True)}" type="video/mp4">'
            for url in sources
        )
        return (
            '<video class="example-preview-video" controls muted preload="metadata" playsinline>'
            + source_tags
            + 'Your browser cannot play this reference video.</video>'
        )

    url = build_gradio_media_url(media_path)
    if not url:
        return '<div class="reference-media-fallback">Image file not found</div>'
    alt_text = html.escape(Path(str(media_path)).name or "example image", quote=True)
    return f'<img class="example-preview-image" src="{html.escape(url, quote=True)}" alt="{alt_text}" loading="lazy" />'


# Recommended-case cache under the app.py directory. Runtime generated caches are
# written here by default, so each case can be committed with the repository.
LOCAL_RECOMMENDED_OUTPUT_CACHE_DIR = Path(
    os.getenv("LANCE_LOCAL_RECOMMENDED_OUTPUT_CACHE_DIR", str(REPO_ROOT / "lance_gradio" / "recommended_outputs"))
).expanduser()

# Space/runtime cache root. This is kept as a read/query fallback so the app can
# still hit caches that were previously saved on the running Space instance.
SPACE_RECOMMENDED_OUTPUT_CACHE_DIR = Path(
    os.getenv("LANCE_SPACE_RECOMMENDED_OUTPUT_CACHE_DIR", str(GRADIO_TMP_ROOT / "recommended_outputs"))
).expanduser()

# Writable cache target used by store_recommended_cached_result(). By default this
# is app.py's directory / lance_gradio / recommended_outputs. Set
# LANCE_RECOMMENDED_OUTPUT_CACHE_DIR to override it explicitly.
RECOMMENDED_OUTPUT_CACHE_DIR = Path(
    os.getenv("LANCE_RECOMMENDED_OUTPUT_CACHE_DIR", str(LOCAL_RECOMMENDED_OUTPUT_CACHE_DIR))
).expanduser()
ASSET_RECOMMENDED_OUTPUT_CACHE_DIR = LOCAL_RECOMMENDED_OUTPUT_CACHE_DIR
RECOMMENDED_CASE_CACHE: dict[str, dict] = {}


def _sanitize_cache_token(value: object) -> str:
    text = str(value or "").strip()
    text = re.sub(r"[^A-Za-z0-9._-]+", "-", text)
    return text.strip("-") or "default"


def _recommended_output_type(task: str) -> str:
    internal_task = normalize_task(task)
    if internal_task in {TASK_T2V, TASK_VIDEO_EDIT}:
        return "video"
    if internal_task in {TASK_T2I, TASK_IMAGE_EDIT}:
        return "image"
    return "text"


def _recommended_output_suffixes(output_type: str) -> tuple[str, ...]:
    if output_type == "video":
        return (".mp4", ".webm", ".mov")
    if output_type == "image":
        return (".png", ".jpg", ".jpeg", ".webp")
    return (".txt", ".json")


def _default_recommended_output_name(task: str, example_id: str) -> str:
    output_type = _recommended_output_type(task)
    candidate = Path(str(example_id)).name or _sanitize_cache_token(example_id)
    suffix = Path(candidate).suffix.lower()
    if suffix in _recommended_output_suffixes(output_type):
        return candidate
    return f"{Path(candidate).stem or _sanitize_cache_token(example_id)}{_recommended_output_suffixes(output_type)[0]}"


def _cache_roots() -> list[Path]:
    """Query the new local cache first, then the Space/runtime saved cache."""
    roots = [RECOMMENDED_OUTPUT_CACHE_DIR, SPACE_RECOMMENDED_OUTPUT_CACHE_DIR]
    unique_roots: list[Path] = []
    seen = set()
    for root in roots:
        try:
            key = str(root.expanduser().resolve())
        except Exception:
            key = str(root)
        if key not in seen:
            seen.add(key)
            unique_roots.append(root)
    return unique_roots


def _infer_aspect_ratio_from_size(task: str, width: int, height: int, resolution: Optional[str]) -> str:
    internal_task = normalize_task(task)
    try:
        size_map = get_size_map_for_task(internal_task, resolution)
        requested = (int(width), int(height))
        for ratio, size in size_map.items():
            if tuple(size) == requested:
                return ratio
    except Exception:
        pass
    return get_default_aspect_ratio(internal_task)


def _canonical_float_for_cache(value: object) -> str:
    try:
        number = float(value)
    except Exception:
        return str(value or "")
    # Keep numeric values stable across Gradio/Python representations while still
    # being parameter-sensitive (for example, 3.5 and 3.500 resolve together).
    return f"{number:.10g}"


def _cache_media_content_hash_enabled() -> bool:
    # On Spaces, Gradio may copy example videos to a temporary file before the
    # backend receives them. Path/mtime based identities then differ from local
    # runs even when the media bytes are the same. A content hash makes example
    # media identities stable across repo paths and Gradio temp paths.
    return env_flag("LANCE_CACHE_MEDIA_CONTENT_HASH", True)


def _cache_media_hash_max_bytes() -> int:
    try:
        return int(os.getenv("LANCE_CACHE_MEDIA_HASH_MAX_BYTES", str(512 * 1024 * 1024)))
    except Exception:
        return 512 * 1024 * 1024


def _media_content_identity_for_cache(path: Path) -> str:
    if not _cache_media_content_hash_enabled():
        return ""
    try:
        stat = path.stat()
        max_bytes = _cache_media_hash_max_bytes()
        if max_bytes > 0 and stat.st_size > max_bytes:
            return ""
        digest = hashlib.sha256()
        with path.open("rb") as f:
            for chunk in iter(lambda: f.read(1024 * 1024), b""):
                digest.update(chunk)
        return f"sha256:{digest.hexdigest()}:{stat.st_size}"
    except Exception:
        return ""


def _canonical_media_identity_for_cache(media_path: Optional[str]) -> str:
    """Return a stable identity for media inputs used by recommended-case cache.

    Example files may be passed either as repo-relative paths from JSON, resolved
    absolute paths, or Space/Gradio temp-file paths. Content hashing is attempted
    first so the same example video can match across local and Space even if
    Gradio rewrites the path. If hashing is disabled or too expensive, this
    falls back to repo-relative identity and then path/stat identity.
    """
    if not media_path:
        return ""

    text = str(media_path)
    candidate = Path(text).expanduser()
    candidates = [candidate] if candidate.is_absolute() else [REPO_ROOT / candidate, candidate]
    for item in candidates:
        try:
            resolved = item.resolve()
        except Exception:
            continue
        if not resolved.exists():
            continue

        content_identity = _media_content_identity_for_cache(resolved)
        if content_identity:
            return content_identity

        try:
            rel = resolved.relative_to(REPO_ROOT.resolve()).as_posix()
            return f"repo:{rel}"
        except Exception:
            pass
        try:
            stat = resolved.stat()
            return f"file:{resolved.as_posix()}:{stat.st_size}:{int(stat.st_mtime_ns)}"
        except Exception:
            return f"file:{resolved.as_posix()}"

    return f"path:{text}"


def _stable_json_for_cache(payload: dict) -> str:
    return json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"))


def _recommended_request_signature_hash(request_signature: Optional[dict]) -> str:
    if not request_signature:
        return ""
    return hashlib.sha256(_stable_json_for_cache(request_signature).encode("utf-8")).hexdigest()[:20]


def _recommended_request_cacheable(request_signature: Optional[dict]) -> bool:
    if not request_signature:
        return False
    # A seed of -1 intentionally means random. The actual seed is sampled inside
    # the generation path, so using a pre-existing cache would be misleading.
    return int(request_signature.get("seed", 0)) != -1


def _recommended_signatures_equal(left: Optional[dict], right: Optional[dict]) -> bool:
    if not left or not right:
        return False
    return _stable_json_for_cache(left) == _stable_json_for_cache(right)


def _recommended_cache_media_alias_enabled() -> bool:
    # Gradio Spaces may copy or transcode example media before the backend sees
    # it. For recommended cases, allow legacy cache files to match when all
    # non-media parameters are identical and only the media identity differs.
    return env_flag("LANCE_RECOMMENDED_CACHE_ALLOW_MEDIA_ALIAS", True)


def _recommended_signatures_equal_ignoring_media(left: Optional[dict], right: Optional[dict]) -> bool:
    if not left or not right:
        return False
    left_copy = dict(left)
    right_copy = dict(right)
    for key in ("input_video", "input_image"):
        left_copy.pop(key, None)
        right_copy.pop(key, None)
    return _stable_json_for_cache(left_copy) == _stable_json_for_cache(right_copy)


def build_recommended_request_signature(
    task: str,
    prompt: Optional[str],
    system_prompt: Optional[str],
    input_video: Optional[str],
    input_image: Optional[str],
    height: int,
    width: int,
    num_frames_ui: int,
    seed: int,
    resolution: Optional[str],
    validation_num_timesteps: int,
    validation_timestep_shift: float,
    cfg_text_scale: float,
    enable_frame_interpolation: bool,
) -> dict:
    """Build a complete cache signature for all user-controllable run params."""
    internal_task = normalize_task(task)
    normalized_resolution = normalize_resolution_for_backend(str(resolution), internal_task)
    normalized_height = int(height)
    normalized_width = int(width)
    normalized_num_frames_ui = int(num_frames_ui)
    aspect_ratio = _infer_aspect_ratio_from_size(
        internal_task,
        normalized_width,
        normalized_height,
        normalized_resolution,
    )
    normalized_system_prompt = (
        normalize_understanding_system_prompt(internal_task, system_prompt)
        if internal_task in UNDERSTANDING_TASKS
        else str(system_prompt or "")
    )

    return {
        "signature_version": 2,
        "task": internal_task,
        "prompt": str(prompt or "").strip(),
        "system_prompt": normalized_system_prompt,
        "input_video": _canonical_media_identity_for_cache(input_video),
        "input_image": _canonical_media_identity_for_cache(input_image),
        "resolution": normalized_resolution,
        "aspect_ratio": aspect_ratio,
        "height": normalized_height,
        "width": normalized_width,
        "num_frames_ui": normalized_num_frames_ui,
        "num_frames_backend": video_seconds_to_num_frames(normalized_num_frames_ui)
        if internal_task == TASK_T2V
        else normalized_num_frames_ui,
        "seed": int(seed),
        "validation_num_timesteps": int(validation_num_timesteps),
        "validation_timestep_shift": _canonical_float_for_cache(validation_timestep_shift),
        "cfg_text_scale": _canonical_float_for_cache(cfg_text_scale),
        "enable_frame_interpolation": bool(enable_frame_interpolation),
    }


def _recommended_variant_tokens(
    task: str,
    resolution: Optional[str],
    aspect_ratio: Optional[str],
    duration_seconds: Optional[int] = None,
) -> list[str]:
    internal_task = normalize_task(task)
    normalized_resolution = normalize_resolution_for_backend(
        str(resolution or get_default_resolution_for_task(internal_task)),
        internal_task,
    )
    normalized_aspect = aspect_ratio if aspect_ratio in ASPECT_RATIO_CHOICES else get_default_aspect_ratio(internal_task)
    tokens = [
        _sanitize_cache_token(normalized_resolution),
        _sanitize_cache_token(normalized_aspect),
    ]
    # Only Video Generation exposes a user duration selector. Video Editing and
    # Understanding use the input media duration, so the UI duration should not
    # split their cache.
    if internal_task == TASK_T2V:
        seconds = int(duration_seconds if duration_seconds is not None else DEFAULT_VIDEO_DURATION_SECONDS)
        tokens.append(f"{max(1, min(10, seconds))}s")
    return tokens


def _recommended_output_name_for_variant(
    task: str,
    output_name: str,
    resolution: Optional[str],
    aspect_ratio: Optional[str],
    duration_seconds: Optional[int] = None,
) -> str:
    path_obj = Path(str(output_name))
    stem = path_obj.stem or _sanitize_cache_token(output_name)
    suffix = path_obj.suffix or _recommended_output_suffixes(_recommended_output_type(task))[0]
    tokens = "__".join(_recommended_variant_tokens(task, resolution, aspect_ratio, duration_seconds))
    return f"{stem}__{tokens}{suffix}" if tokens else f"{stem}{suffix}"


def _recommended_output_name_for_signature(
    task: str,
    output_name: str,
    request_signature: dict,
) -> str:
    path_obj = Path(str(output_name))
    stem = path_obj.stem or _sanitize_cache_token(output_name)
    suffix = path_obj.suffix or _recommended_output_suffixes(_recommended_output_type(task))[0]
    signature_hash = _recommended_request_signature_hash(request_signature)
    return f"{stem}__sig-{signature_hash}{suffix}"


def register_recommended_case_cache(
    task: str,
    example_id: str,
    output_name: Optional[str] = None,
    aspect_ratio: Optional[str] = None,
    resolution: Optional[str] = None,
    duration_seconds: Optional[int] = None,
    prompt_text: Optional[str] = None,
    input_video_path: Optional[str] = None,
    input_image_path: Optional[str] = None,
) -> str:
    internal_task = normalize_task(task)
    normalized_resolution = normalize_resolution_for_backend(
        str(resolution or get_default_resolution_for_task(internal_task)),
        internal_task,
    )
    normalized_aspect = aspect_ratio if aspect_ratio in ASPECT_RATIO_CHOICES else get_default_aspect_ratio(internal_task)
    default_width, default_height = get_size_for_aspect_ratio(internal_task, normalized_aspect, normalized_resolution)
    default_duration = int(duration_seconds if duration_seconds is not None else DEFAULT_VIDEO_DURATION_SECONDS)
    default_request_signature = build_recommended_request_signature(
        task=internal_task,
        prompt=prompt_text,
        system_prompt=normalize_understanding_system_prompt(internal_task, None) if internal_task in UNDERSTANDING_TASKS else "",
        input_video=input_video_path,
        input_image=input_image_path,
        height=default_height,
        width=default_width,
        num_frames_ui=default_duration,
        seed=DEFAULT_BASIC_SEED,
        resolution=normalized_resolution,
        validation_num_timesteps=DEFAULT_TIMESTEPS,
        validation_timestep_shift=DEFAULT_TIMESTEP_SHIFT,
        cfg_text_scale=DEFAULT_CFG_TEXT_SCALE,
        enable_frame_interpolation=False,
    )
    cache_key = f"{internal_task}:{_sanitize_cache_token(example_id)}"
    RECOMMENDED_CASE_CACHE[cache_key] = {
        "key": cache_key,
        "task": internal_task,
        "example_id": str(example_id),
        "output_name": output_name or _default_recommended_output_name(internal_task, str(example_id)),
        "output_type": _recommended_output_type(internal_task),
        "resolution": normalized_resolution,
        "aspect_ratio": normalized_aspect,
        "duration_seconds": default_duration,
        "prompt_text": str(prompt_text or ""),
        "input_video_path": str(input_video_path or ""),
        "input_image_path": str(input_image_path or ""),
        "default_request_signature": default_request_signature,
        "default_request_signature_hash": _recommended_request_signature_hash(default_request_signature),
    }
    return cache_key


def infer_recommended_case_key_from_request(
    task: str,
    prompt: str,
    input_video: Optional[str] = None,
    input_image: Optional[str] = None,
) -> str:
    """Best-effort fallback for sessions that do not carry the hidden cache key."""
    internal_task = normalize_task(task)
    prompt_text = str(prompt or "").strip()
    input_video_id = _canonical_media_identity_for_cache(input_video)
    input_image_id = _canonical_media_identity_for_cache(input_image)

    for cache_key, meta in RECOMMENDED_CASE_CACHE.items():
        if meta.get("task") != internal_task:
            continue
        if str(meta.get("prompt_text") or "").strip() != prompt_text:
            continue

        meta_video = str(meta.get("input_video_path") or "")
        meta_image = str(meta.get("input_image_path") or "")
        meta_video_id = _canonical_media_identity_for_cache(meta_video)
        meta_image_id = _canonical_media_identity_for_cache(meta_image)
        if meta_video_id and input_video_id and meta_video_id != input_video_id:
            continue
        if meta_image_id and input_image_id and meta_image_id != input_image_id:
            continue
        if meta_video_id and not input_video_id:
            continue
        if meta_image_id and not input_image_id:
            continue
        return cache_key

    return ""


def _recommended_cache_candidates(
    meta: dict,
    resolution: Optional[str] = None,
    aspect_ratio: Optional[str] = None,
    duration_seconds: Optional[int] = None,
    request_signature: Optional[dict] = None,
):
    task = str(meta["task"])
    output_name = str(meta.get("output_name") or _default_recommended_output_name(task, meta.get("example_id", meta["key"])))
    output_type = str(meta.get("output_type") or _recommended_output_type(task))
    requested_resolution = normalize_resolution_for_backend(str(resolution or meta.get("resolution") or ""), task)
    requested_aspect = aspect_ratio if aspect_ratio in ASPECT_RATIO_CHOICES else str(meta.get("aspect_ratio") or get_default_aspect_ratio(task))
    requested_duration = int(duration_seconds if duration_seconds is not None else meta.get("duration_seconds", DEFAULT_VIDEO_DURATION_SECONDS))
    default_resolution = str(meta.get("resolution") or "")
    default_aspect = str(meta.get("aspect_ratio") or get_default_aspect_ratio(task))
    default_duration = int(meta.get("duration_seconds") or DEFAULT_VIDEO_DURATION_SECONDS)
    default_signature = meta.get("default_request_signature")
    is_default_signature = _recommended_signatures_equal(request_signature, default_signature)
    is_media_alias_signature = (
        _recommended_cache_media_alias_enabled()
        and _recommended_signatures_equal_ignoring_media(request_signature, default_signature)
    )

    stem = Path(output_name).stem or _sanitize_cache_token(meta.get("example_id", meta.get("key", "case")))
    names = set()

    # New strict cache filenames: every user-controllable parameter is part of
    # request_signature, so a changed seed/steps/CFG/media/size/etc. cannot hit
    # an output generated under different settings.
    if request_signature and _recommended_request_cacheable(request_signature):
        signature_hash = _recommended_request_signature_hash(request_signature)
        signature_name = _recommended_output_name_for_signature(task, output_name, request_signature)
        names.add(signature_name)
        for suffix in _recommended_output_suffixes(output_type):
            names.add(f"{stem}__sig-{signature_hash}{suffix}")
            names.add(f"{_sanitize_cache_token(meta['key'])}__sig-{signature_hash}{suffix}")

    # Legacy recommended assets were named only by resolution/aspect/duration, or
    # sometimes just by case id. They are safe for the exact default request
    # signature registered for that recommended case. On Spaces, Gradio can
    # rewrite recommended example videos to temp/transcoded files; in that case
    # input_video changes while the user-visible recommended case is still the
    # same. Allow legacy candidates when every non-media parameter still matches.
    allow_legacy_candidates = request_signature is None or is_default_signature or is_media_alias_signature
    if allow_legacy_candidates:
        names.add(_recommended_output_name_for_variant(task, output_name, requested_resolution, requested_aspect, requested_duration))

        tokens = "__".join(_recommended_variant_tokens(task, requested_resolution, requested_aspect, requested_duration))
        for suffix in _recommended_output_suffixes(output_type):
            names.add(f"{stem}__{tokens}{suffix}")
            names.add(f"{_sanitize_cache_token(meta['key'])}__{tokens}{suffix}")

        # Backward compatibility with the older width/height/duration filename format:
        # stem__video_360p__640x352__3u.mp4
        try:
            width, height = get_size_for_aspect_ratio(task, requested_aspect, requested_resolution)
            old_tokens = f"{_sanitize_cache_token(requested_resolution)}__{int(width)}x{int(height)}"
            if normalize_task(task) == TASK_T2V:
                old_tokens = f"{old_tokens}__{requested_duration}u"
            for suffix in _recommended_output_suffixes(output_type):
                names.add(f"{stem}__{old_tokens}{suffix}")
                names.add(f"{_sanitize_cache_token(meta['key'])}__{old_tokens}{suffix}")
        except Exception:
            pass

        # Legacy generic filename is only allowed for the case's default visible spec.
        if (
            requested_resolution == default_resolution
            and requested_aspect == default_aspect
            and (normalize_task(task) != TASK_T2V or requested_duration == default_duration)
        ):
            names.add(output_name)
            for suffix in _recommended_output_suffixes(output_type):
                names.add(f"{stem}{suffix}")
                names.add(f"{_sanitize_cache_token(meta['key'])}{suffix}")

    for root in _cache_roots():
        for folder in (root / str(task), root):
            for name in names:
                yield folder / name

def _recommended_cache_debug_enabled() -> bool:
    return env_flag("LANCE_DEBUG_RECOMMENDED_CACHE", False)


def find_recommended_cached_output(
    cache_key: str,
    resolution: Optional[str] = None,
    aspect_ratio: Optional[str] = None,
    duration_seconds: Optional[int] = None,
    request_signature: Optional[dict] = None,
) -> Optional[Path]:
    meta = RECOMMENDED_CASE_CACHE.get(cache_key or "")
    if not meta:
        return None

    debug = _recommended_cache_debug_enabled()
    tried: list[str] = []
    for candidate in _recommended_cache_candidates(
        meta,
        resolution=resolution,
        aspect_ratio=aspect_ratio,
        duration_seconds=duration_seconds,
        request_signature=request_signature,
    ):
        if debug and len(tried) < 24:
            tried.append(str(candidate))
        try:
            if candidate.exists() and candidate.is_file():
                return candidate.resolve()
        except Exception:
            continue

    if debug:
        default_signature = meta.get("default_request_signature")
        print(
            "[recommended-cache] Miss "
            + json.dumps(
                {
                    "cache_key": cache_key,
                    "request_sig": _recommended_request_signature_hash(request_signature),
                    "default_sig": _recommended_request_signature_hash(default_signature),
                    "is_default_signature": _recommended_signatures_equal(request_signature, default_signature),
                    "is_media_alias_signature": _recommended_signatures_equal_ignoring_media(request_signature, default_signature),
                    "media_alias_enabled": _recommended_cache_media_alias_enabled(),
                    "roots": [str(root) for root in _cache_roots()],
                    "sample_candidates": tried,
                    "request_input_video": (request_signature or {}).get("input_video"),
                    "default_input_video": (default_signature or {}).get("input_video"),
                    "request_input_image": (request_signature or {}).get("input_image"),
                    "default_input_image": (default_signature or {}).get("input_image"),
                    "request_system_prompt": (request_signature or {}).get("system_prompt"),
                    "default_system_prompt": (default_signature or {}).get("system_prompt"),
                },
                ensure_ascii=False,
            ),
            flush=True,
        )
    return None


def get_recommended_cached_result(
    cache_key: str,
    task: str,
    resolution: Optional[str],
    aspect_ratio: Optional[str],
    duration_seconds: Optional[int] = None,
    request_signature: Optional[dict] = None,
):
    meta = RECOMMENDED_CASE_CACHE.get(cache_key or "")
    if not meta:
        return None
    if not _recommended_request_cacheable(request_signature):
        return None

    cached_path = find_recommended_cached_output(
        cache_key,
        resolution=resolution,
        aspect_ratio=aspect_ratio,
        duration_seconds=duration_seconds,
        request_signature=request_signature,
    )
    if cached_path is None:
        return None

    signature_hash = _recommended_request_signature_hash(request_signature)
    print(f"[recommended-cache] Hit {cache_key} sig={signature_hash}: {cached_path}", flush=True)
    # Keep cache hits silent in the UI. The output is returned directly without
    # exposing cache paths or cache-matching details to end users. Matching is
    # sensitive to the full request signature: prompt, media, size, seed, steps,
    # shift, CFG scale, duration, resolution, and interpolation flag.
    status = ""
    output_type = str(meta.get("output_type") or _recommended_output_type(task))
    if output_type == "video":
        return str(cached_path), None, "", status
    if output_type == "image":
        return None, str(cached_path), "", status
    try:
        return None, None, cached_path.read_text(encoding="utf-8"), status
    except Exception:
        return None, None, str(cached_path), status

def store_recommended_cached_result(
    cache_key: str,
    result,
    resolution: Optional[str],
    aspect_ratio: Optional[str],
    duration_seconds: Optional[int] = None,
    request_signature: Optional[dict] = None,
) -> None:
    meta = RECOMMENDED_CASE_CACHE.get(cache_key or "")
    if not meta:
        return
    if not _recommended_request_cacheable(request_signature):
        return
    if find_recommended_cached_output(
        cache_key,
        resolution=resolution,
        aspect_ratio=aspect_ratio,
        duration_seconds=duration_seconds,
        request_signature=request_signature,
    ) is not None:
        return

    try:
        output_video, output_image, output_text, _status = result
        target_name = _recommended_output_name_for_signature(
            meta["task"],
            str(meta["output_name"]),
            request_signature,
        )
        target = RECOMMENDED_OUTPUT_CACHE_DIR / str(meta["task"]) / target_name
        target.parent.mkdir(parents=True, exist_ok=True)

        if meta["output_type"] == "video" and output_video and Path(str(output_video)).exists():
            shutil.copy2(str(output_video), str(target))
        elif meta["output_type"] == "image" and output_image and Path(str(output_image)).exists():
            shutil.copy2(str(output_image), str(target))
        elif meta["output_type"] == "text" and output_text:
            target.write_text(str(output_text), encoding="utf-8")
        else:
            return

        print(
            f"[recommended-cache] Stored {cache_key} sig={_recommended_request_signature_hash(request_signature)} "
            f"at {target} (resolution={resolution}, aspect_ratio={aspect_ratio}, duration={duration_seconds})",
            flush=True,
        )
    except Exception as exc:
        print(f"[recommended-cache] Could not store {cache_key}: {exc}", flush=True)

def load_json_examples(relative_path: str) -> dict:
    path = REPO_ROOT / relative_path
    with path.open("r", encoding="utf-8") as f:
        return json.load(f)


T2V_EXAMPLE_SUMMARIES = {
    "000000.mp4": "Red panda surfing on a bright seaside wave.",
    "000002.mp4": "Panda cub skateboarding in a creative loft.",
    "000004.mp4": "Young woman shaping clay in a sunlit pottery workshop.",
    "000005.mp4": "Panda boxing a robot in a luxurious palace ring.",
    "000008.mp4": "Fantasy pastel horse stepping through a glowing cloud valley.",
}


def make_generation_examples(
    task_label: str,
    relative_path: str,
    limit: int,
    image_task: bool,
    selected_keys: Optional[list[str]] = None,
    summaries: Optional[dict[str, str]] = None,
) -> list[list]:
    internal_task = normalize_task(task_label)
    data = load_json_examples(relative_path)
    items = [(key, data[key]) for key in selected_keys if key in data] if selected_keys else list(data.items())[:limit]
    examples = []
    for output_name, prompt in items:
        cache_key = register_recommended_case_cache(
            task=internal_task,
            example_id=output_name,
            output_name=output_name,
            aspect_ratio=get_default_aspect_ratio(internal_task),
            resolution=get_default_resolution_for_task(internal_task),
            duration_seconds=DEFAULT_VIDEO_DURATION_SECONDS,
            prompt_text=prompt,
        )
        examples.append([prompt, cache_key])
    return examples


def make_edit_examples(task_label: str, relative_path: str, limit: int, media_type: str) -> list[list]:
    internal_task = normalize_task(task_label)
    data = load_json_examples(relative_path)
    examples = []
    for idx, sample in enumerate(list(data.values())[:limit]):
        interleave = sample["interleave_array"]
        prompt = interleave[0]
        example_id = f"{Path(relative_path).stem}_{idx:06d}"
        cache_key = register_recommended_case_cache(
            task=internal_task,
            example_id=example_id,
            output_name=_default_recommended_output_name(internal_task, example_id),
            aspect_ratio=get_default_aspect_ratio(internal_task),
            resolution=get_default_resolution_for_task(internal_task),
            duration_seconds=DEFAULT_VIDEO_DURATION_SECONDS,
            prompt_text=prompt,
            input_video_path=interleave[1] if media_type == "video" else None,
            input_image_path=interleave[1] if media_type == "image" else None,
        )
        if media_type == "video":
            preview_video_path, input_video_path = resolve_video_example_paths(interleave[1])
            examples.append([prompt, preview_video_path, input_video_path, None, None, cache_key])
        else:
            image_path = resolve_example_path(interleave[1])
            examples.append([prompt, None, None, image_path, image_path, cache_key])
    return examples


def make_understanding_examples(task_label: str, relative_path: str, limit: int, media_type: str) -> list[list]:
    internal_task = normalize_task(task_label)
    data = load_json_examples(relative_path)
    examples = []
    for idx, sample in enumerate(list(data.values())[:limit]):
        interleave = sample["interleave_array"]
        text_payload = interleave[1]
        question = text_payload[1] if isinstance(text_payload, list) and len(text_payload) > 1 else ""
        example_id = f"{Path(relative_path).stem}_{idx:06d}"
        cache_key = register_recommended_case_cache(
            task=internal_task,
            example_id=example_id,
            output_name=_default_recommended_output_name(internal_task, example_id),
            aspect_ratio=get_default_aspect_ratio(internal_task),
            resolution=get_default_resolution_for_task(internal_task),
            duration_seconds=DEFAULT_VIDEO_DURATION_SECONDS,
            prompt_text=question,
            input_video_path=interleave[0] if media_type == "video" else None,
            input_image_path=interleave[0] if media_type == "image" else None,
        )
        if media_type == "video":
            preview_video_path, input_video_path = resolve_video_example_paths(interleave[0])
            examples.append([question, preview_video_path, input_video_path, None, None, cache_key])
        else:
            image_path = resolve_example_path(interleave[0])
            examples.append([question, None, None, image_path, image_path, cache_key])
    return examples


def make_understanding_system_prompt_map(relative_path: str, task: str) -> dict[str, str]:
    data = load_json_examples(relative_path)
    system_prompts = {}
    for sample in data.values():
        interleave = sample["interleave_array"]
        text_payload = interleave[1]
        if not isinstance(text_payload, list) or len(text_payload) < 2:
            continue
        system_prompts[text_payload[1]] = normalize_understanding_system_prompt(task, text_payload[0])
    return system_prompts


VIDEO_GENERATION_EXAMPLES = make_generation_examples(
    TASK_LABEL_VIDEO_GENERATION,
    "config/examples/t2v_example.json",
    limit=7,
    image_task=False,
    #selected_keys=["000000.mp4", "000002.mp4", "000005.mp4", "000004.mp4", "000008.mp4"],
    selected_keys=["000004.mp4", "000002.mp4", "000000.mp4", "000005.mp4", "000008.mp4", "000007.mp4", "000001.mp4"],
    summaries=T2V_EXAMPLE_SUMMARIES,
)
VIDEO_EDIT_EXAMPLES = make_edit_examples(
    TASK_LABEL_VIDEO_EDIT,
    "config/examples/video_edit_example.json",
    limit=3,
    media_type="video",
)
VIDEO_UNDERSTANDING_EXAMPLES = make_understanding_examples(
    TASK_LABEL_VIDEO_UNDERSTANDING,
    "config/examples/x2t_video_example.json",
    limit=3,
    media_type="video",
)
VIDEO_UNDERSTANDING_SYSTEM_PROMPTS = make_understanding_system_prompt_map(
    "config/examples/x2t_video_example.json",
    TASK_X2T_VIDEO,
)
IMAGE_GENERATION_EXAMPLES = make_generation_examples(
    TASK_LABEL_IMAGE_GENERATION,
    "config/examples/t2i_example.json",
    limit=9,
    image_task=True,
    selected_keys=["000000.png", "000003.png", "000002.png", "000005.png", "000006.png", "000007.png", "000008.png", "000009.png", "000010.png"],
)
IMAGE_EDIT_EXAMPLES = make_edit_examples(
    TASK_LABEL_IMAGE_EDIT,
    "config/examples/image_edit_example.json",
    limit=5,
    media_type="image",
)
IMAGE_UNDERSTANDING_EXAMPLES = make_understanding_examples(
    TASK_LABEL_IMAGE_UNDERSTANDING,
    "config/examples/x2t_image_example.json",
    limit=3,
    media_type="image",
)
IMAGE_UNDERSTANDING_SYSTEM_PROMPTS = make_understanding_system_prompt_map(
    "config/examples/x2t_image_example.json",
    TASK_X2T_IMAGE,
)


def build_save_dir(task: str) -> Path:
    ensure_dirs()
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    return RESULTS_ROOT / f"{task}_{timestamp}_{int(time.time() * 1000) % 1000:03d}"


def find_generated_video(save_dir: Path) -> Optional[Path]:
    videos = sorted(save_dir.glob("*.mp4"), key=lambda p: p.stat().st_mtime, reverse=True)
    return videos[0] if videos else None


def find_generated_image(save_dir: Path) -> Optional[Path]:
    images = sorted(save_dir.glob("*.png"), key=lambda p: p.stat().st_mtime, reverse=True)
    return images[0] if images else None


def extract_text_result(save_dir: Path) -> str:
    prompt_result_path = save_dir / PROMPT_JSON_FILENAME
    if not prompt_result_path.exists():
        return ""
    with prompt_result_path.open("r", encoding="utf-8") as f:
        data = json.load(f)
    if not data:
        return ""
    first_value = next(iter(data.values()))
    return first_value if isinstance(first_value, str) else json.dumps(first_value, ensure_ascii=False)


class LanceT2VV2TPipeline:
    def __init__(self, device_id: int, model_variant: str = MODEL_VARIANT_VIDEO) -> None:
        self._init_lock = threading.Lock()
        self._generate_lock = threading.Lock()
        self.initialized = False
        self.device = device_id
        self.model_variant = normalize_model_variant(model_variant)
        self.logger = get_logger(f"lance_{self.model_variant}_gpu{device_id}")

        self.model: Optional[Lance] = None
        self.vae_model: Optional[WanVideoVAE] = None
        self.vae_config: Optional[AutoEncoderParams] = None
        self.tokenizer: Optional[Qwen2Tokenizer] = None
        self.new_token_ids: Optional[dict] = None
        self.image_token_id: Optional[int] = None
        self.base_model_args: Optional[ModelArguments] = None
        self.base_data_args: Optional[DataArguments] = None
        self.base_inference_args: Optional[InferenceArguments] = None

    def _log_stage(self, stage_name: str, start_time: float, extra: str = "") -> None:
        elapsed = time.perf_counter() - start_time
        suffix = f" | {extra}" if extra else ""
        print(f"[startup][gpu:{self.device}] {stage_name} done in {elapsed:.2f}s{suffix}", flush=True)

    def _build_base_model_args(self) -> ModelArguments:
        model_path = str(get_model_path(self.model_variant))
        return ModelArguments(
            model_path=model_path,
            vit_type=DEFAULT_VIT_TYPE,
            llm_qk_norm=True,
            llm_qk_norm_und=True,
            llm_qk_norm_gen=True,
            tie_word_embeddings=False,
            max_num_frames=MAX_VIDEO_NUM_FRAMES,
            max_latent_size=64,
            latent_patch_size=[1, 1, 1],
        )

    def _build_base_inference_args(self) -> InferenceArguments:
        return InferenceArguments(
            validation_num_timesteps=DEFAULT_TIMESTEPS,
            validation_timestep_shift=DEFAULT_TIMESTEP_SHIFT,
            copy_init_moe=True,
            visual_und=True,
            visual_gen=True,
            vae_model_type="wan",
            apply_qwen_2_5_vl_pos_emb=True,
            apply_chat_template=False,
            cfg_type=0,
            validation_data_seed=42,
            video_height=DEFAULT_HEIGHT,
            video_width=DEFAULT_WIDTH,
            num_frames=DEFAULT_NUM_FRAMES,
            task=DEFAULT_TASK,
            save_path_gen=str(RESULTS_ROOT),
            resolution=DEFAULT_RESOLUTION,
            text_template=TEXT_TEMPLATE,
            use_KVcache=USE_KVCACHE,
        )

    def initialize(self) -> None:
        with self._init_lock:
            if self.initialized:
                return

            ensure_dirs()
            resolved_model_path = ensure_model_assets(self.model_variant)
            print(
                f"[startup][gpu:{self.device}][{self.model_variant}] Using Lance model path: {resolved_model_path}",
                flush=True,
            )
            if not torch.cuda.is_available():
                raise RuntimeError("CUDA is unavailable. Lance T2V/V2T Gradio requires a GPU environment.")
            if self.device >= torch.cuda.device_count():
                raise RuntimeError(
                    f"GPU {self.device} is unavailable. Detected {torch.cuda.device_count()} GPU(s)."
                )
            torch.cuda.set_device(self.device)

            model_args = self._build_base_model_args()
            data_args = DataArguments()
            inference_args = self._build_base_inference_args()
            apply_inference_defaults(model_args, data_args, inference_args)
            inference_args.validation_noise_seed = inference_args.validation_data_seed

            self.base_model_args = model_args
            self.base_data_args = data_args
            self.base_inference_args = inference_args

            set_seed(inference_args.global_seed)

            stage_start = time.perf_counter()
            print(
                f"[startup][gpu:{self.device}] Loading LLM config: {Path(model_args.model_path) / 'llm_config.json'}",
                flush=True,
            )
            llm_config: Qwen2Config = Qwen2Config.from_json_file(str(Path(model_args.model_path) / "llm_config.json"))
            self._log_stage("LLM config load", stage_start)

            llm_config.layer_module = model_args.layer_module
            llm_config.qk_norm = model_args.llm_qk_norm
            llm_config.qk_norm_und = model_args.llm_qk_norm_und
            llm_config.qk_norm_gen = model_args.llm_qk_norm_gen
            llm_config.tie_word_embeddings = model_args.tie_word_embeddings
            llm_config.freeze_und = inference_args.freeze_und
            llm_config.apply_qwen_2_5_vl_pos_emb = inference_args.apply_qwen_2_5_vl_pos_emb

            stage_start = time.perf_counter()
            print(f"[startup][gpu:{self.device}] Initializing LLM weights: {model_args.model_path}", flush=True)
            language_model: Qwen2ForCausalLM = Qwen2ForCausalLM(llm_config)
            self._log_stage("LLM weight init", stage_start)

            vit_model = None
            vit_config = None
            if inference_args.visual_und:
                if model_args.vit_type not in ("qwen2_5_vl", "qwen_2_5_vl_original"):
                    raise ValueError(f"Unsupported vit_type: {model_args.vit_type}")
                stage_start = time.perf_counter()
                print(f"[startup][gpu:{self.device}] Loading VIT config: {model_args.vit_path}", flush=True)
                vit_config = Qwen2_5_VLVisionConfig.from_pretrained(model_args.vit_path)
                self._log_stage("VIT config load", stage_start)

                stage_start = time.perf_counter()
                print(
                    f"[startup][gpu:{self.device}] Loading VIT weights: {Path(model_args.vit_path) / 'vit.safetensors'}",
                    flush=True,
                )
                vit_model = Qwen2_5_VisionTransformerPretrainedModel(vit_config)
                vit_weights = load_file(str(Path(model_args.vit_path) / "vit.safetensors"))
                vit_model.load_state_dict(vit_weights, strict=True)
                self._log_stage("VIT weight load", stage_start)
                clean_memory(vit_weights)

            if inference_args.visual_gen:
                stage_start = time.perf_counter()
                print(f"[startup][gpu:{self.device}] Initializing VAE", flush=True)
                vae_model = WanVideoVAE(device=torch.device("cuda", self.device))
                vae_config = deepcopy(vae_model.vae_config)
                self._log_stage("VAE init", stage_start)
            else:
                vae_model = None
                vae_config = None

            config = LanceConfig(
                visual_gen=inference_args.visual_gen,
                visual_und=inference_args.visual_und,
                llm_config=llm_config,
                vit_config=vit_config if inference_args.visual_und else None,
                vae_config=vae_config if inference_args.visual_gen else None,
                latent_patch_size=model_args.latent_patch_size,
                max_num_frames=model_args.max_num_frames,
                max_latent_size=model_args.max_latent_size,
                vit_max_num_patch_per_side=model_args.vit_max_num_patch_per_side,
                connector_act=model_args.connector_act,
                interpolate_pos=model_args.interpolate_pos,
                timestep_shift=inference_args.timestep_shift,
            )
            model: Lance = Lance(
                language_model=language_model,
                vit_model=vit_model if inference_args.visual_und else None,
                vit_type=model_args.vit_type,
                config=config,
                training_args=inference_args,
            )

            stage_start = time.perf_counter()
            print(f"[startup][gpu:{self.device}] Casting Lance model to bf16 on CPU", flush=True)
            model = model.to(dtype=torch.bfloat16)
            self._log_stage("Lance model bf16 cast", stage_start)

            stage_start = time.perf_counter()
            print(f"[startup][gpu:{self.device}] Loading tokenizer: {model_args.model_path}", flush=True)
            tokenizer: Qwen2Tokenizer = Qwen2Tokenizer.from_pretrained(model_args.model_path)
            tokenizer, new_token_ids, num_new_tokens = add_special_tokens(tokenizer)
            self._log_stage("tokenizer load and special token init", stage_start, extra=f"num_new_tokens={num_new_tokens}")

            if inference_args.copy_init_moe:
                language_model.init_moe()

            init_from_model_path_if_needed(model, model_args)

            if num_new_tokens > 0:
                model.language_model.resize_token_embeddings(len(tokenizer))
                model.config.llm_config.vocab_size = len(tokenizer)
                model.language_model.config.vocab_size = len(tokenizer)

            if model_args.vit_type.lower() == "qwen2_5_vl":
                from common.model.hacks import hack_qwen2_5_vl_config

                language_model = hack_qwen2_5_vl_config(language_model)

            image_token_id = language_model.config.video_token_id
            new_token_ids.update({"image_token_id": image_token_id})
            model.update_tokenizer(tokenizer=tokenizer)

            if model_args.tie_word_embeddings:
                model.language_model.untie_lm_head()
                model.language_model.copy_new_token_rows_to_lm_head(num_new_tokens)
                model_args.tie_word_embeddings = False
                llm_config.tie_word_embeddings = False
            else:
                assert (
                    model.language_model.get_input_embeddings().weight.data.data_ptr()
                    != model.language_model.get_output_embeddings().weight.data.data_ptr()
                ), "tie_word_embeddings conflict"

            stage_start = time.perf_counter()
            print(f"[startup][gpu:{self.device}] Moving Lance model to GPU {self.device}", flush=True)
            model = model.to(device=self.device)
            self._log_stage("Lance model move to GPU", stage_start)
            model.eval()
            if vae_model is not None and hasattr(vae_model, "eval"):
                vae_model.eval()

            self.model = model
            self.vae_model = vae_model
            self.vae_config = vae_config
            self.tokenizer = tokenizer
            self.new_token_ids = new_token_ids
            self.image_token_id = image_token_id
            self.initialized = True
            print(
                f"[startup][gpu:{self.device}][{self.model_variant}] Lance multimodal Gradio model loaded and ready for reuse.",
                flush=True,
            )

    def unload(self) -> None:
        with self._init_lock:
            if self.model is not None:
                self.model.cpu()
            if self.vae_model is not None and hasattr(self.vae_model, "vae"):
                vae_inner = self.vae_model.vae
                if hasattr(vae_inner, "model"):
                    vae_inner.model.cpu()

            self.model = None
            self.vae_model = None
            self.vae_config = None
            self.tokenizer = None
            self.new_token_ids = None
            self.image_token_id = None
            self.base_model_args = None
            self.base_data_args = None
            self.base_inference_args = None
            self.initialized = False
            gc.collect()
            if torch.cuda.is_available():
                with torch.cuda.device(self.device):
                    torch.cuda.empty_cache()
                    torch.cuda.ipc_collect()

    def _build_request_batch(
        self,
        prompt_file: Path,
        model_args: ModelArguments,
        data_args: DataArguments,
        inference_args: InferenceArguments,
    ):
        assert self.tokenizer is not None
        assert self.new_token_ids is not None
        assert self.vae_config is not None

        dataset_config = DataConfig.from_yaml(str(prompt_file))
        if inference_args.visual_und:
            dataset_config.vit_patch_size = model_args.vit_patch_size
            dataset_config.vit_patch_size_temporal = model_args.vit_patch_size_temporal
            dataset_config.vit_max_num_patch_per_side = model_args.vit_max_num_patch_per_side
        if inference_args.visual_gen:
            vae_downsample = tuple_mul(
                tuple(model_args.latent_patch_size),
                (
                    self.vae_config.downsample_temporal,
                    self.vae_config.downsample_spatial,
                    self.vae_config.downsample_spatial,
                ),
            )
            dataset_config.latent_patch_size = model_args.latent_patch_size
            dataset_config.vae_downsample = vae_downsample
            dataset_config.max_latent_size = model_args.max_latent_size
            dataset_config.max_num_frames = model_args.max_num_frames

        dataset_config.text_cond_dropout_prob = model_args.text_cond_dropout_prob
        dataset_config.vae_cond_dropout_prob = model_args.vae_cond_dropout_prob
        dataset_config.vit_cond_dropout_prob = model_args.vit_cond_dropout_prob

        dataset_config.num_frames = inference_args.num_frames
        dataset_config.H = inference_args.video_height
        dataset_config.W = inference_args.video_width
        dataset_config.task = inference_args.task
        dataset_config.resolution = inference_args.resolution
        dataset_config.text_template = inference_args.text_template

        val_dataset = ValidationDataset(
            jsonl_path=str(prompt_file),
            tokenizer=self.tokenizer,
            data_args=data_args,
            model_args=model_args,
            training_args=inference_args,
            new_token_ids=self.new_token_ids,
            dataset_config=dataset_config,
            local_rank=0,
            world_size=1,
        )
        return simple_custom_collate([val_dataset[0]])

    def generate(
        self,
        task: str,
        prompt: str,
        system_prompt: Optional[str],
        input_video: Optional[str],
        input_image: Optional[str],
        height: int,
        width: int,
        num_frames: int,
        seed: int,
        resolution: str,
        validation_num_timesteps: int,
        validation_timestep_shift: float,
        cfg_text_scale: float,
        enable_frame_interpolation: bool,
    ):
        self.initialize()
        internal_task = normalize_task(task)
        prompt = (prompt or "").strip()
        input_video = str(input_video).strip() if input_video else ""
        input_image = str(input_image).strip() if input_image else ""

        if internal_task in GENERATION_TASKS and not prompt:
            return None, None, "", "Please enter a prompt."
        if internal_task in UNDERSTANDING_TASKS and not prompt:
            return None, None, "", "Please enter a question."
        if internal_task in {TASK_VIDEO_EDIT, TASK_X2T_VIDEO} and not input_video:
            return None, None, "", "Please upload an input video."
        if internal_task in {TASK_IMAGE_EDIT, TASK_X2T_IMAGE} and not input_image:
            return None, None, "", "Please upload an input image."
        if height <= 0 or width <= 0:
            return None, None, "", "Height and width must be greater than 0."
        if num_frames <= 0:
            return None, None, "", "The number of frames must be greater than 0."

        assert self.model is not None
        assert self.tokenizer is not None
        assert self.new_token_ids is not None
        assert self.image_token_id is not None
        assert self.base_model_args is not None
        assert self.base_data_args is not None
        assert self.base_inference_args is not None
        active_model_path = self.base_model_args.model_path

        with self._generate_lock:
            torch.cuda.set_device(self.device)
            actual_seed = normalize_seed(int(seed))
            prompt_file = create_request_json(
                task=internal_task,
                prompt=prompt,
                input_video=input_video,
                input_image=input_image,
                system_prompt=system_prompt,
            )
            save_dir = build_save_dir(internal_task)
            save_dir.mkdir(parents=True, exist_ok=True)
            request_started_at = datetime.now().isoformat(timespec="seconds")

            request_model_args = deepcopy(self.base_model_args)
            request_model_args.cfg_text_scale = float(cfg_text_scale)

            request_data_args = deepcopy(self.base_data_args)
            request_data_args.val_dataset_config_file = str(prompt_file)

            request_inference_args = deepcopy(self.base_inference_args)
            request_inference_args.validation_num_timesteps = int(validation_num_timesteps)
            request_inference_args.validation_timestep_shift = float(validation_timestep_shift)
            request_inference_args.validation_data_seed = actual_seed
            request_inference_args.validation_noise_seed = actual_seed
            request_inference_args.video_height = int(height)
            request_inference_args.video_width = int(width)
            request_inference_args.num_frames = int(num_frames)
            display_resolution = str(resolution)
            backend_resolution = normalize_resolution_for_backend(display_resolution, internal_task)
            request_inference_args.resolution = backend_resolution
            request_inference_args.save_path_gen = str(save_dir)
            request_inference_args.task = internal_task
            request_inference_args.text_template = TEXT_TEMPLATE
            request_inference_args.prompt_data_dict = {}

            try:
                print(
                    "[lance_gradio_t2v_v2t] Start generation "
                    f"| task={internal_task} | gpu={self.device} | seed={actual_seed} | "
                    f"size={height}x{width} | frames={num_frames} | resolution={display_resolution}",
                    flush=True,
                )
                val_data_cpu = self._build_request_batch(
                    prompt_file=prompt_file,
                    model_args=request_model_args,
                    data_args=request_data_args,
                    inference_args=request_inference_args,
                )
                # Keep the allocator from fragmenting before the heavy forward pass.
                clean_memory()
                generate_start = time.perf_counter()
                validate_on_fixed_batch(
                    fsdp_model=self.model,
                    vae_model=self.vae_model,
                    tokenizer=self.tokenizer,
                    val_data_cpu=val_data_cpu,
                    training_args=request_inference_args,
                    model_args=request_model_args,
                    inference_args=request_inference_args,
                    new_token_ids=self.new_token_ids,
                    image_token_id=self.image_token_id,
                    device=self.device,
                    save_source_video=False,
                    save_path_gen=request_inference_args.save_path_gen,
                    save_path_gt="",
                )
                elapsed = time.perf_counter() - generate_start
                save_prompt_results(request_inference_args.prompt_data_dict, request_inference_args.save_path_gen, self.logger)
                clean_memory()

                video_path = find_generated_video(save_dir) if internal_task in {TASK_T2V, TASK_VIDEO_EDIT} else None
                original_video_path = video_path
                frame_interpolation_enabled = False
                image_path = find_generated_image(save_dir) if internal_task in {TASK_T2I, TASK_IMAGE_EDIT} else None
                text_result = extract_text_result(save_dir) if internal_task in UNDERSTANDING_TASKS else ""
                record = {
                    "request_started_at": request_started_at,
                    "request_finished_at": datetime.now().isoformat(timespec="seconds"),
                    "status": "success",
                    "task": internal_task,
                    "model_variant": self.model_variant,
                    "model_path": active_model_path,
                    "gpu": self.device,
                    "prompt": prompt,
                    "system_prompt": normalize_understanding_system_prompt(internal_task, system_prompt)
                    if internal_task in UNDERSTANDING_TASKS
                    else "",
                    "input_video": input_video,
                    "input_image": input_image,
                    "seed": actual_seed,
                    "height": int(height),
                    "width": int(width),
                    "num_frames": int(num_frames),
                    "resolution": display_resolution,
                    "backend_resolution": backend_resolution,
                    "validation_num_timesteps": int(validation_num_timesteps),
                    "validation_timestep_shift": float(validation_timestep_shift),
                    "cfg_text_scale": float(cfg_text_scale),
                    "frame_interpolation": frame_interpolation_enabled,
                    "elapsed_seconds": round(elapsed, 3),
                    "prompt_file": str(prompt_file),
                    "output_dir": str(save_dir),
                    "original_video_path": str(original_video_path) if original_video_path is not None else "",
                    "video_path": str(video_path) if video_path is not None else "",
                    "image_path": str(image_path) if image_path is not None else "",
                    "text_result": text_result,
                    "rife_error": "",
                }
                if internal_task in {TASK_T2V, TASK_VIDEO_EDIT} and video_path is None:
                    record["status"] = "completed_without_video"
                if internal_task in {TASK_T2I, TASK_IMAGE_EDIT} and image_path is None:
                    record["status"] = "completed_without_image"
                if internal_task in UNDERSTANDING_TASKS and not text_result:
                    record["status"] = "completed_without_text"
                save_generation_record(record, save_dir)

                if internal_task in {TASK_T2V, TASK_VIDEO_EDIT}:
                    if video_path is None:
                        status = (
                            "Inference completed, but no output video was found.\n\n"
                            f"- Task: `{internal_task}`\n"
                            f"- Model: `{self.model_variant}`\n"
                            f"- Model path: `{active_model_path}`\n"
                            f"- GPU: `{self.device}`\n"
                            f"- Actual seed: `{actual_seed}`\n"
                            f"- Output directory: `{save_dir}`"
                        )
                        return None, None, "", status
                    return str(video_path), None, "", ""

                if internal_task in {TASK_T2I, TASK_IMAGE_EDIT}:
                    if image_path is None:
                        status = (
                            "Inference completed, but no output image was found.\n\n"
                            f"- Task: `{internal_task}`\n"
                            f"- Model: `{self.model_variant}`\n"
                            f"- Model path: `{active_model_path}`\n"
                            f"- GPU: `{self.device}`\n"
                            f"- Actual seed: `{actual_seed}`\n"
                            f"- Output directory: `{save_dir}`"
                        )
                        return None, None, "", status
                    return None, str(image_path), "", ""

                return None, None, text_result, ""
            except Exception:
                error_trace = traceback.format_exc()
                print(error_trace, flush=True)
                record = {
                    "request_started_at": request_started_at,
                    "request_finished_at": datetime.now().isoformat(timespec="seconds"),
                    "status": "failed",
                    "task": internal_task,
                    "model_variant": self.model_variant,
                    "model_path": active_model_path,
                    "gpu": self.device,
                    "prompt": prompt,
                    "input_video": input_video,
                    "input_image": input_image,
                    "seed": actual_seed,
                    "height": int(height),
                    "width": int(width),
                    "num_frames": int(num_frames),
                    "resolution": display_resolution,
                    "backend_resolution": backend_resolution,
                    "validation_num_timesteps": int(validation_num_timesteps),
                    "validation_timestep_shift": float(validation_timestep_shift),
                    "cfg_text_scale": float(cfg_text_scale),
                    "prompt_file": str(prompt_file),
                    "output_dir": str(save_dir),
                    "video_path": "",
                    "image_path": "",
                    "text_result": "",
                    "error": error_trace,
                }
                save_generation_record(record, save_dir)
                status = (
                    "Inference failed.\n\n"
                    f"- Task: `{internal_task}`\n"
                    f"- Model: `{self.model_variant}`\n"
                    f"- Model path: `{active_model_path}`\n"
                    f"- GPU: `{self.device}`\n"
                    f"- Actual seed: `{actual_seed}`\n"
                    f"- Resolution: `{display_resolution}`\n"
                    f"- Output directory: `{save_dir}`"
                )
                return None, None, "", status


class PipelinePool:
    def __init__(self, gpu_ids: list[int], model_variant: str = MODEL_VARIANT_VIDEO) -> None:
        if not gpu_ids:
            raise ValueError("At least one GPU must be configured.")
        self.gpu_ids = gpu_ids
        self.model_variant = normalize_model_variant(model_variant)
        self.pipelines = [
            LanceT2VV2TPipeline(device_id=gpu_id, model_variant=self.model_variant)
            for gpu_id in gpu_ids
        ]
        self._available = deque(self.pipelines)
        self._condition = threading.Condition()

    @property
    def size(self) -> int:
        return len(self.pipelines)

    @property
    def gpu_summary(self) -> str:
        return ",".join(str(gpu_id) for gpu_id in self.gpu_ids)

    @property
    def is_initialized(self) -> bool:
        return all(pipeline.initialized for pipeline in self.pipelines)

    def initialize_all(self) -> None:
        if self.is_initialized:
            return
        print(f"[startup][{self.model_variant}] Preparing parallel GPU preload: {self.gpu_ids}", flush=True)
        exceptions: list[Exception] = []
        with concurrent.futures.ThreadPoolExecutor(max_workers=self.size) as executor:
            futures = {
                executor.submit(pipeline.initialize): pipeline.device for pipeline in self.pipelines
            }
            for future in concurrent.futures.as_completed(futures):
                gpu_id = futures[future]
                try:
                    future.result()
                except Exception as exc:
                    print(f"[startup][gpu:{gpu_id}][{self.model_variant}] Preload failed: {exc}", flush=True)
                    exceptions.append(exc)
        if exceptions:
            raise RuntimeError(
                f"{self.model_variant} preload failed on {len(exceptions)} GPU(s). Please check the terminal logs."
            ) from exceptions[0]
        print(
            f"[startup][{self.model_variant}] GPU preload finished. Ready to handle {self.size} concurrent request(s).",
            flush=True,
        )

    def acquire(self) -> LanceT2VV2TPipeline:
        with self._condition:
            while not self._available:
                self._condition.wait()
            return self._available.popleft()

    def release(self, pipeline: LanceT2VV2TPipeline) -> None:
        with self._condition:
            self._available.append(pipeline)
            self._condition.notify()

    def unload_all(self) -> None:
        print(f"[runtime][{self.model_variant}] Unloading model pool from GPU(s): {self.gpu_ids}", flush=True)
        with self._condition:
            while len(self._available) != len(self.pipelines):
                self._condition.wait()

        for pipeline in self.pipelines:
            pipeline.unload()

        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
        print(f"[runtime][{self.model_variant}] Model pool unloaded.", flush=True)

    def generate(
        self,
        task: str,
        prompt: str,
        system_prompt: Optional[str],
        input_video: Optional[str],
        input_image: Optional[str],
        height: int,
        width: int,
        num_frames: int,
        seed: int,
        resolution: str,
        validation_num_timesteps: int,
        validation_timestep_shift: float,
        cfg_text_scale: float,
        enable_frame_interpolation: bool,
    ):
        pipeline = self.acquire()
        try:
            return pipeline.generate(
                task=task,
                prompt=prompt,
                system_prompt=system_prompt,
                input_video=input_video,
                input_image=input_image,
                height=height,
                width=width,
                num_frames=num_frames,
                seed=seed,
                resolution=resolution,
                validation_num_timesteps=validation_num_timesteps,
                validation_timestep_shift=validation_timestep_shift,
                cfg_text_scale=cfg_text_scale,
                enable_frame_interpolation=enable_frame_interpolation,
            )
        finally:
            self.release(pipeline)


ACTIVE_PIPELINE_POOL: Optional[PipelinePool] = None
ACTIVE_POOL_LOCK = threading.Lock()
QUEUE_MAX_SIZE = DEFAULT_QUEUE_SIZE
QUEUE_CONCURRENCY_LIMIT = DEFAULT_CONCURRENCY_LIMIT


def get_task_model_variant(task: str) -> str:
    internal_task = normalize_task(task)
    return MODEL_VARIANT_IMAGE if internal_task in IMAGE_TASKS else MODEL_VARIANT_VIDEO


def get_env_int(name: str, default: int) -> int:
    """Read an integer environment variable, falling back safely on invalid values."""
    try:
        return int(os.getenv(name, str(default)))
    except (TypeError, ValueError):
        return default


def ensure_flash_attn_installed() -> None:
    try:
        from importlib.metadata import PackageNotFoundError, version as package_version
        current_version = package_version("flash_attn")
        if current_version == DEFAULT_FLASH_ATTN_VERSION:
            print(f"[startup] flash-attn {current_version} already installed.", flush=True)
            return
        print(
            f"[startup] flash-attn {current_version} detected; reinstalling {DEFAULT_FLASH_ATTN_VERSION} from wheel.",
            flush=True,
        )
    except Exception:
        print(
            f"[startup] flash-attn not available; installing {DEFAULT_FLASH_ATTN_VERSION} from wheel.",
            flush=True,
        )

    command = [
        sys.executable,
        "-m",
        "pip",
        "install",
        "--no-cache-dir",
        "--no-deps",
        "--force-reinstall",
        DEFAULT_FLASH_ATTN_WHEEL_URL,
    ]
    subprocess.check_call(command)
    print(f"[startup] flash-attn {DEFAULT_FLASH_ATTN_VERSION} installed from wheel.", flush=True)


def get_zerogpu_duration_cap() -> int:
    """Fixed duration requested from ZeroGPU for each run.

    The duration value is a ZeroGPU reservation/timeout hint. Shorter values can
    improve queue priority and reduce wasted quota, but the value must still cover
    model warm-up plus inference. Override per deployment when needed:
        LANCE_ZEROGPU_MAX_DURATION_SECONDS=300
    """
    return max(1, get_env_int("LANCE_ZEROGPU_MAX_DURATION_SECONDS", 300))


def clamp_zerogpu_duration(seconds: int) -> int:
    return max(1, min(int(seconds), get_zerogpu_duration_cap()))


ZERO_GPU_RUN_TASK_DURATION_SECONDS = get_zerogpu_duration_cap()


def is_pipeline_pool_ready_for_variant(model_variant: str) -> bool:
    normalized_variant = normalize_model_variant(model_variant)
    with ACTIVE_POOL_LOCK:
        return bool(
            ACTIVE_PIPELINE_POOL is not None
            and ACTIVE_PIPELINE_POOL.model_variant == normalized_variant
            and ACTIVE_PIPELINE_POOL.is_initialized
        )


def is_pipeline_pool_ready_for_task(task: str) -> bool:
    return is_pipeline_pool_ready_for_variant(get_task_model_variant(task))


def get_pipeline_pool(task: str) -> PipelinePool:
    global ACTIVE_PIPELINE_POOL
    if not torch.cuda.is_available():
        raise RuntimeError(
            "Lance inference requires a GPU. The Gradio UI can start on CPU, but generation is disabled "
            "until GPU hardware is attached."
        )
    model_variant = get_task_model_variant(task)
    gpu_ids = parse_gpu_ids(os.getenv("LANCE_GPUS", DEFAULT_GPUS))
    with ACTIVE_POOL_LOCK:
        if ACTIVE_PIPELINE_POOL is not None and ACTIVE_PIPELINE_POOL.model_variant == model_variant:
            if not ACTIVE_PIPELINE_POOL.is_initialized:
                ACTIVE_PIPELINE_POOL.initialize_all()
            return ACTIVE_PIPELINE_POOL

        if ACTIVE_PIPELINE_POOL is not None:
            previous_variant = ACTIVE_PIPELINE_POOL.model_variant
            print(
                f"[runtime] Switching Lance model from {previous_variant} to {model_variant}.",
                flush=True,
            )
            ACTIVE_PIPELINE_POOL.unload_all()
            ACTIVE_PIPELINE_POOL = None

        ACTIVE_PIPELINE_POOL = PipelinePool(gpu_ids, model_variant=model_variant)
        ACTIVE_PIPELINE_POOL.initialize_all()
        return ACTIVE_PIPELINE_POOL


def finalize_zerogpu_duration(estimated_seconds: float, task: str) -> int:
    """Clamp a heuristic duration to the deployment cap with a small safety margin."""
    task_key = normalize_task(task)
    raw_seconds = float(estimated_seconds)
    if raw_seconds <= 0:
        raw_seconds = _estimate_zerogpu_duration_seconds(
            task_key,
            prompt="",
            system_prompt=None,
            input_video=None,
            input_image=None,
            height=0,
            width=0,
            num_frames=0,
            seed=0,
            resolution="",
            validation_num_timesteps=0,
            validation_timestep_shift=0.0,
            cfg_text_scale=0.0,
            enable_frame_interpolation=False,
        )
    return clamp_zerogpu_duration(math.ceil(raw_seconds * 1.15) + 5)


def _estimate_zerogpu_duration_seconds(
    task: str,
    prompt: str,
    system_prompt: Optional[str],
    input_video: Optional[str],
    input_image: Optional[str],
    height: int,
    width: int,
    num_frames: int,
    seed: int,
    resolution: str,
    validation_num_timesteps: int,
    validation_timestep_shift: float,
    cfg_text_scale: float,
    enable_frame_interpolation: bool,
) -> int:
    internal_task = normalize_task(task)
    prompt_length = len((prompt or "").strip())
    has_video_input = bool((input_video or "").strip())
    has_image_input = bool((input_image or "").strip())
    pool_ready = is_pipeline_pool_ready_for_task(internal_task)
    is_video_task = internal_task in {TASK_T2V, TASK_VIDEO_EDIT, TASK_X2T_VIDEO}
    is_image_task = internal_task in {TASK_T2I, TASK_IMAGE_EDIT, TASK_X2T_IMAGE}

    if internal_task == TASK_T2I:
        return 90 if pool_ready else 150

    if internal_task == TASK_IMAGE_EDIT:
        return 100 if pool_ready else 150

    if internal_task == TASK_X2T_IMAGE:
        return 90 if pool_ready else 150

    if internal_task == TASK_X2T_VIDEO:
        return 120 if pool_ready else 200

    if internal_task == TASK_VIDEO_EDIT:
        base = 170 if pool_ready else 300
        base += min(30 if pool_ready else 48, max(0, num_frames - 37) // 3)
        base += 24 if enable_frame_interpolation else 0
        base += 16 if has_video_input else 0
        base += 10 if resolution == "video_480p" else 0
        return base

    if internal_task == TASK_T2V:
        if pool_ready:
            base = 130 if resolution == "video_360p" else 150
            base += min(36, max(0, num_frames - 37) // 3)
            base += 18 if enable_frame_interpolation else 0
            base += min(12, prompt_length // 320)
            return base
        base = 224 if resolution == "video_360p" else 264
        base += min(56, max(0, num_frames - 37) // 2)
        base += 28 if enable_frame_interpolation else 0
        base += min(20, prompt_length // 260)
        return base

    if is_video_task:
        base = 150 if pool_ready else 240
        base += min(28 if pool_ready else 40, max(0, num_frames - 37) // 3)
        base += 18 if enable_frame_interpolation else 0
        return base

    if is_image_task:
        return 100 if pool_ready else 120

    return 160


def get_run_task_gpu_duration(
    task: str,
    prompt: str,
    system_prompt: Optional[str],
    input_video: Optional[str],
    input_image: Optional[str],
    height: int,
    width: int,
    num_frames: int,
    seed: int,
    resolution: str,
    validation_num_timesteps: int,
    validation_timestep_shift: float,
    cfg_text_scale: float,
    enable_frame_interpolation: bool,
) -> int:
    enable_frame_interpolation = False
    estimated_seconds = _estimate_zerogpu_duration_seconds(
        task=task,
        prompt=prompt,
        system_prompt=system_prompt,
        input_video=input_video,
        input_image=input_image,
        height=height,
        width=width,
        num_frames=num_frames,
        seed=seed,
        resolution=resolution,
        validation_num_timesteps=validation_num_timesteps,
        validation_timestep_shift=validation_timestep_shift,
        cfg_text_scale=cfg_text_scale,
        enable_frame_interpolation=enable_frame_interpolation,
    )
    return finalize_zerogpu_duration(estimated_seconds, task)


def run_task(
    task: str,
    prompt: str,
    system_prompt: Optional[str],
    input_video: Optional[str],
    input_image: Optional[str],
    height: int,
    width: int,
    num_frames: int,
    seed: int,
    resolution: str,
    validation_num_timesteps: int,
    validation_timestep_shift: float,
    cfg_text_scale: float,
    enable_frame_interpolation: bool,
):
    internal_task = normalize_task(task)
    recommended_case_key, clean_system_prompt = unpack_recommended_cache_carrier(system_prompt)
    system_prompt = clean_system_prompt
    if not recommended_case_key:
        recommended_case_key = infer_recommended_case_key_from_request(internal_task, prompt, input_video, input_image)

    if internal_task in UNDERSTANDING_TASKS and not prompt:
        return None, None, "", "Please enter a question."
    if internal_task in {TASK_VIDEO_EDIT, TASK_X2T_VIDEO} and not input_video:
        return None, None, "", "Please upload an input video."
    if internal_task in {TASK_IMAGE_EDIT, TASK_X2T_IMAGE} and not input_image:
        return None, None, "", "Please upload an input image."
    if height <= 0 or width <= 0:
        return None, None, "", "Height and width must be greater than 0."
    if num_frames <= 0:
        return None, None, "", "The number of frames must be greater than 0."

    num_frames_ui = int(num_frames)
    normalized_resolution = normalize_resolution_for_backend(str(resolution), internal_task)
    aspect_ratio = _infer_aspect_ratio_from_size(internal_task, int(width), int(height), normalized_resolution)

    # Ignore any stale interpolation value from old browser sessions before
    # building the cache signature, because interpolation is disabled in this UI.
    enable_frame_interpolation = False

    request_signature = build_recommended_request_signature(
        task=internal_task,
        prompt=prompt,
        system_prompt=system_prompt,
        input_video=input_video,
        input_image=input_image,
        height=int(height),
        width=int(width),
        num_frames_ui=num_frames_ui,
        seed=int(seed),
        resolution=normalized_resolution,
        validation_num_timesteps=int(validation_num_timesteps),
        validation_timestep_shift=float(validation_timestep_shift),
        cfg_text_scale=float(cfg_text_scale),
        enable_frame_interpolation=enable_frame_interpolation,
    )

    cached_result = get_recommended_cached_result(
        recommended_case_key,
        internal_task,
        resolution=normalized_resolution,
        aspect_ratio=aspect_ratio,
        duration_seconds=num_frames_ui,
        request_signature=request_signature,
    )
    if cached_result is not None:
        return cached_result

    if internal_task == TASK_T2V:
        num_frames = video_seconds_to_num_frames(num_frames_ui)
    result = run_task_gpu(
        task=task,
        prompt=prompt,
        system_prompt=system_prompt,
        input_video=input_video,
        input_image=input_image,
        height=height,
        width=width,
        num_frames=num_frames,
        seed=seed,
        resolution=normalized_resolution,
        validation_num_timesteps=validation_num_timesteps,
        validation_timestep_shift=validation_timestep_shift,
        cfg_text_scale=cfg_text_scale,
        enable_frame_interpolation=enable_frame_interpolation,
    )
    store_recommended_cached_result(
        recommended_case_key,
        result,
        resolution=normalized_resolution,
        aspect_ratio=aspect_ratio,
        duration_seconds=num_frames_ui,
        request_signature=request_signature,
    )
    return result


@spaces.GPU(size="large", duration=get_run_task_gpu_duration)
def run_task_gpu(
    task: str,
    prompt: str,
    system_prompt: Optional[str],
    input_video: Optional[str],
    input_image: Optional[str],
    height: int,
    width: int,
    num_frames: int,
    seed: int,
    resolution: str,
    validation_num_timesteps: int,
    validation_timestep_shift: float,
    cfg_text_scale: float,
    enable_frame_interpolation: bool,
):
    pipeline_pool = get_pipeline_pool(task)
    return pipeline_pool.generate(
        task=task,
        prompt=prompt,
        system_prompt=system_prompt,
        input_video=input_video,
        input_image=input_image,
        height=height,
        width=width,
        num_frames=num_frames,
        seed=seed,
        resolution=resolution,
        validation_num_timesteps=validation_num_timesteps,
        validation_timestep_shift=validation_timestep_shift,
        cfg_text_scale=cfg_text_scale,
        enable_frame_interpolation=enable_frame_interpolation,
    )


def build_status_markdown() -> str:
    gpu_text = "unknown"
    pipeline_slots = 0
    active_variant = "none"
    with ACTIVE_POOL_LOCK:
        if ACTIVE_PIPELINE_POOL is not None:
            active_variant = ACTIVE_PIPELINE_POOL.model_variant
            gpu_text = ACTIVE_PIPELINE_POOL.gpu_summary
            pipeline_slots = ACTIVE_PIPELINE_POOL.size
    return (
        f"**Status**  GPU: `{gpu_text}`  |  Queue concurrency: `{QUEUE_CONCURRENCY_LIMIT}`  |  "
        f"Pipeline slots: `{pipeline_slots}`  |  Queue limit: `{QUEUE_MAX_SIZE}`  |  "
        f"Active model: `{active_variant}`"
    )


def build_running_status_markdown() -> str:
    return "Running..."


def get_logo_data_uri() -> str:
    if not LANCE_LOGO_PATH.exists():
        return ""
    encoded_logo = base64.b64encode(LANCE_LOGO_PATH.read_bytes()).decode("ascii")
    return f"data:image/webp;base64,{encoded_logo}"


def build_header_html() -> str:
    logo_data_uri = get_logo_data_uri()
    logo_html = (
        f'<img class="lance-logo" src="{logo_data_uri}" alt="Lance logo">'
        if logo_data_uri
        else ""
    )
    return f"""
    <div class="lance-hero">
        {logo_html}
        <h1 class="lance-title">Lance: Unified Multimodal Modeling by Multi-Task Synergy</h1>
        <div class="lance-badges">
            <a href="{LANCE_HOMEPAGE_URL}" target="_blank" rel="noopener noreferrer">
                <img alt="Homepage" src="https://img.shields.io/badge/Homepage-Lance-2563eb?style=flat&labelColor=475569">
            </a>
            <a href="{LANCE_PAPER_URL}" target="_blank" rel="noopener noreferrer">
                <img alt="Paper" src="https://img.shields.io/badge/Paper-arXiv-2563eb?style=flat&labelColor=475569&logo=arxiv">
            </a>
            <a href="{LANCE_HUGGING_FACE_URL}" target="_blank" rel="noopener noreferrer">
                <img alt="Hugging Face" src="https://img.shields.io/badge/Model-HuggingFace-2563eb?style=flat&labelColor=475569&logo=huggingface">
            </a>
            <a href="{LANCE_GITHUB_URL}" target="_blank" rel="noopener noreferrer">
                <img alt="GitHub" src="https://img.shields.io/badge/Code-GitHub-2563eb?style=flat&labelColor=475569&logo=github">
            </a>
        </div>
    </div>
    """


def update_task_ui(task: str):
    internal_task = normalize_task(task)
    is_image_task = internal_task in IMAGE_TASKS
    is_video_task = internal_task in VIDEO_TASKS
    is_edit_task = internal_task in EDIT_TASKS
    is_understanding_task = internal_task in UNDERSTANDING_TASKS
    is_generation_task = internal_task in GENERATION_TASKS
    is_text_to_visual_task = internal_task in {TASK_T2V, TASK_T2I}
    show_media_input = is_edit_task or is_understanding_task
    resolution_choices = get_resolution_choices_for_task(internal_task)
    resolution_value = get_default_resolution_for_task(internal_task)
    aspect_ratio_value = DEFAULT_IMAGE_ASPECT_RATIO if is_image_task else DEFAULT_VIDEO_ASPECT_RATIO
    width_value, height_value = get_size_for_aspect_ratio(internal_task, aspect_ratio_value, resolution_value)
    size_markdown = format_size_markdown(internal_task, width_value, height_value)
    system_prompt_choices = get_understanding_system_prompt_choices(internal_task)

    if is_text_to_visual_task:
        text_label = "Prompt"
        text_placeholder = "Describe what you want to generate..."
    elif is_edit_task:
        text_label = "Instruction"
        text_placeholder = "Describe the edit you want..."
    else:
        text_label = "Question"
        text_placeholder = "Ask a question about the input..."

    if internal_task in {TASK_T2V, TASK_VIDEO_EDIT}:
        output_label = "Output Video"
    elif internal_task in {TASK_T2I, TASK_IMAGE_EDIT}:
        output_label = "Output Image"
    else:
        output_label = "Output Text"

    output_icon = "video" if output_label == "Output Video" else "image" if output_label == "Output Image" else "text"
    show_generation_settings = is_generation_task or is_edit_task
    show_aspect_ratio = is_text_to_visual_task
    show_input_video = internal_task in {TASK_VIDEO_EDIT, TASK_X2T_VIDEO}
    show_input_image = internal_task in {TASK_IMAGE_EDIT, TASK_X2T_IMAGE}
    show_frame_interpolation_settings = False
    show_video_resolution_settings = internal_task == TASK_T2V

    return (
        gr.update(value=build_lance_label_html(text_label, "lance-prompt-label")),
        gr.update(
            label=text_label,
            placeholder=text_placeholder,
            visible=True,
            value="",
        ),
        gr.update(
            choices=system_prompt_choices,
            value=system_prompt_choices[0],
            visible=False,
        ),
        # Switching task pages should always start from a clean input state.
        # Clear both visual input boxes even if one of them stays visible across tasks.
        gr.update(label="Input Video", visible=show_input_video, value=None),
        gr.update(label="Input Image", visible=show_input_image, value=None),
        gr.update(visible=False),
        gr.update(visible=show_aspect_ratio),
        gr.update(visible=False),
        gr.update(visible=internal_task == TASK_T2V),
        gr.update(visible=show_video_resolution_settings),
        gr.update(choices=get_aspect_ratio_choices_for_task(internal_task), value=aspect_ratio_value, visible=show_aspect_ratio),
        gr.update(value=height_value),
        gr.update(value=width_value),
        gr.update(visible=False, value=False),
        gr.update(choices=get_output_resolution_choices_for_task(internal_task, resolution_value), value=size_markdown, visible=False),
        gr.update(visible=internal_task == TASK_T2V, value=DEFAULT_VIDEO_DURATION_SECONDS),
        gr.update(choices=resolution_choices, value=resolution_value, visible=show_video_resolution_settings),
        gr.update(value=build_lance_icon_label_html(output_label, output_icon, "lance-output-label")),
        gr.update(visible=internal_task in {TASK_T2V, TASK_VIDEO_EDIT}),
        gr.update(visible=internal_task in {TASK_T2I, TASK_IMAGE_EDIT}),
        gr.update(visible=is_understanding_task, value=""),
        gr.update(visible=internal_task == TASK_T2V),
        gr.update(visible=internal_task == TASK_VIDEO_EDIT),
        gr.update(visible=internal_task == TASK_X2T_VIDEO),
        gr.update(visible=internal_task == TASK_T2I),
        gr.update(visible=internal_task == TASK_IMAGE_EDIT),
        gr.update(visible=internal_task == TASK_X2T_IMAGE),
        "",
    )


def build_demo() -> gr.Blocks:
    with gr.Blocks(title="Lance", css=APP_CSS, js=APP_JS) as demo:
        gr.HTML(build_header_html())
        gr.Markdown(build_status_markdown(), elem_classes=["lance-status"], visible=False)

        with gr.Column(elem_classes=["lance-taskbar-wrap"]):
            task = gr.Radio(
                label="Task",
                show_label=False,
                choices=TASK_CHOICES,
                value=TASK_LABEL_VIDEO_GENERATION,
                elem_classes=["task-selector"],
            )

        with gr.Row(elem_classes=["lance-main-row"]):
            with gr.Column(scale=1, elem_classes=["lance-main-column", "lance-input-column"]):
                with gr.Column(elem_classes=["lance-panel", "lance-task-prompt-panel"]):
                    prompt_label = gr.HTML(build_lance_label_html("Prompt", "lance-prompt-label"), elem_classes=["lance-label-html"])
                    prompt = gr.Textbox(
                        label="Prompt",
                        show_label=False,
                        lines=6,
                        placeholder="Describe the video you want to generate...",
                        elem_classes=["main-prompt-control"],
                    )
                    with gr.Row(elem_classes=["prompt-options"]):
                        with gr.Group(elem_classes=["prompt-chip", "video-resolution-row"]) as video_resolution_row:
                            resolution = gr.Dropdown(
                                label="Video Resolution",
                                show_label=False,
                                choices=VIDEO_RESOLUTION_DISPLAY_CHOICES,
                                value=DEFAULT_RESOLUTION,
                                allow_custom_value=True,
                                elem_classes=["generation-control"],
                            )
                        with gr.Group(elem_classes=["prompt-chip", "aspect-ratio-row"]) as aspect_ratio_row:
                            aspect_ratio = gr.Dropdown(
                                label="Aspect Ratio",
                                show_label=False,
                                choices=get_aspect_ratio_choices_for_task(TASK_T2V),
                                value=DEFAULT_VIDEO_ASPECT_RATIO,
                                elem_classes=["generation-control"],
                            )
                        with gr.Group(elem_classes=["prompt-chip", "video-duration-row"]) as video_duration_row:
                            num_frames = gr.Dropdown(
                                label="Video Duration",
                                show_label=False,
                                choices=get_video_duration_choices(),
                                value=DEFAULT_VIDEO_DURATION_SECONDS,
                                elem_classes=["generation-control"],
                            )
                        with gr.Group(visible=False, elem_classes=["prompt-chip", "output-resolution-row"]) as output_resolution_row:
                            real_size = gr.Dropdown(
                                label="Output Resolution",
                                show_label=False,
                                choices=get_output_resolution_choices_for_task(TASK_T2V),
                                value=format_size_markdown(TASK_T2V, DEFAULT_WIDTH, DEFAULT_HEIGHT),
                                interactive=False,
                                visible=False,
                                allow_custom_value=True,
                                elem_classes=["generation-control"],
                            )

                # Hidden compatibility components for old callbacks; frame interpolation is disabled.
                with gr.Group(visible=False, elem_classes=["frame-interpolation-row", "frame-interpolation-disabled"]) as frame_interpolation_row:
                    enable_frame_interpolation = gr.Checkbox(value=False, visible=False)

                system_prompt = gr.Dropdown(
                    label="System Prompt",
                    choices=get_understanding_system_prompt_choices(TASK_X2T_VIDEO),
                    value=V2T_QA_SYSTEM_PROMPT,
                    visible=False,
                    allow_custom_value=True,
                )
                input_video = gr.Video(label="Input Video", visible=False, elem_classes=["lance-display-frame"])
                input_image = gr.Image(label="Input Image", type="filepath", visible=False, elem_classes=["lance-display-frame"])
                height = gr.Number(value=DEFAULT_HEIGHT, precision=0, visible=False)
                width = gr.Number(value=DEFAULT_WIDTH, precision=0, visible=False)

                with gr.Accordion("Advanced Parameters", open=False, elem_classes=["lance-advanced-accordion"]):
                    seed = gr.Number(label="Seed (-1 for random seed)", value=DEFAULT_BASIC_SEED, precision=0)
                    validation_num_timesteps = gr.Slider(
                        minimum=1,
                        maximum=50,
                        step=1,
                        value=DEFAULT_TIMESTEPS,
                        label="Validation Num Timesteps",
                    )
                    with gr.Row():
                        validation_timestep_shift = gr.Number(label="Validation Timestep Shift", value=DEFAULT_TIMESTEP_SHIFT)
                        cfg_text_scale = gr.Number(label="CFG Text Scale", value=DEFAULT_CFG_TEXT_SCALE)

            with gr.Column(scale=1, elem_classes=["lance-main-column", "lance-output-column"]):
                with gr.Column(elem_classes=["lance-panel", "lance-output-panel"]):
                    output_label = gr.HTML(
                        build_lance_icon_label_html("Output Video", "video", "lance-output-label"),
                        elem_classes=["lance-label-html"],
                    )
                    output_video = gr.Video(label="Output Video", show_label=False, elem_classes=["lance-display-frame", "output-media-control"])
                    output_image = gr.Image(label="Output Image", show_label=False, type="filepath", visible=False, elem_classes=["lance-display-frame", "output-media-control"])
                    output_text = gr.Textbox(label="Output Text", show_label=False, lines=3, visible=False, elem_classes=["lance-display-frame", "output-text-control"])
                status = gr.Markdown("", elem_classes=["lance-run-status"])

        recommended_case_key = gr.State("")

        run_button = gr.Button("🚀 Generate", variant="primary", elem_classes=["lance-run-button"])
        gr.Markdown(
            "**Note**: Video-related features may consume more GPU quota and take longer. Cached recommended cases and image tasks are lighter.",
            elem_classes=["lance-quota-note"],
        )

        def build_prompt_example_table(examples: list[list], media_type: Optional[str] = None):
            """Recommended example list with complete-fit reference media previews."""
            example_buttons = []
            with gr.Column(elem_classes=["prompt-example-full-table"]):
                for row in examples:
                    example_prompt = str(row[0]) if row else ""
                    example_cache_key = str(row[-1]) if row and str(row[-1]) in RECOMMENDED_CASE_CACHE else ""

                    preview_video_path = input_video_path = None
                    preview_image_path = input_image_path = None
                    if media_type == "video":
                        preview_video_path = str(row[1]) if len(row) > 1 and row[1] else None
                        input_video_path = str(row[2]) if len(row) > 2 and row[2] else preview_video_path
                    elif media_type == "image":
                        preview_image_path = str(row[3]) if len(row) > 3 and row[3] else (str(row[2]) if len(row) > 2 and row[2] else None)
                        input_image_path = str(row[4]) if len(row) > 4 and row[4] else preview_image_path

                    button_label = example_prompt if len(example_prompt) <= 360 else f"{example_prompt[:357]}..."

                    if media_type in {"video", "image"}:
                        with gr.Row(elem_classes=["prompt-example-multimodal-row"]):
                            with gr.Column(elem_classes=["prompt-example-prompt-cell"]):
                                example_button = gr.Button(
                                    button_label,
                                    variant="secondary",
                                    elem_classes=["prompt-example-row-button"],
                                )
                            with gr.Column(elem_classes=["prompt-example-media-cell"]):
                                if media_type == "video":
                                    gr.HTML(
                                        build_example_media_html(preview_video_path, "video", fallback_media_path=input_video_path),
                                        elem_classes=["prompt-example-media-html"],
                                    )
                                else:
                                    gr.HTML(
                                        build_example_media_html(preview_image_path, "image"),
                                        elem_classes=["prompt-example-media-html"],
                                    )
                    else:
                        example_button = gr.Button(
                            button_label,
                            variant="secondary",
                            elem_classes=["prompt-example-row-button"],
                        )

                    example_buttons.append((example_button, example_prompt, input_video_path, input_image_path, example_cache_key))
            return example_buttons

        def examples_section(title: str, examples: list[list], media_type: Optional[str] = None, visible: bool = False):
            with gr.Column(visible=visible, elem_classes=["lance-recommended-section"]) as group:
                gr.HTML(build_lance_label_html(title, "lance-section-label"), elem_classes=["lance-label-html"])
                with gr.Group(elem_classes=["example-panel", "prompt-examples"]):
                    buttons = build_prompt_example_table(examples, media_type=media_type)
            return group, buttons

        video_generation_examples_group, video_generation_example_buttons = examples_section(
            "Video generation recommended cases", VIDEO_GENERATION_EXAMPLES, visible=True
        )
        video_edit_examples_group, video_edit_example_buttons = examples_section(
            "Video edit recommended cases", VIDEO_EDIT_EXAMPLES, media_type="video"
        )
        video_understanding_examples_group, video_understanding_example_buttons = examples_section(
            "Video understanding recommended cases", VIDEO_UNDERSTANDING_EXAMPLES, media_type="video"
        )
        image_generation_examples_group, image_generation_example_buttons = examples_section(
            "Image generation recommended cases", IMAGE_GENERATION_EXAMPLES
        )
        image_edit_examples_group, image_edit_example_buttons = examples_section(
            "Image edit recommended cases", IMAGE_EDIT_EXAMPLES, media_type="image"
        )
        image_understanding_examples_group, image_understanding_example_buttons = examples_section(
            "Image understanding recommended cases", IMAGE_UNDERSTANDING_EXAMPLES, media_type="image"
        )

        task.change(
            fn=update_task_ui,
            inputs=[task],
            outputs=[
                prompt_label,
                prompt,
                system_prompt,
                input_video,
                input_image,
                frame_interpolation_row,
                aspect_ratio_row,
                output_resolution_row,
                video_duration_row,
                video_resolution_row,
                aspect_ratio,
                height,
                width,
                enable_frame_interpolation,
                real_size,
                num_frames,
                resolution,
                output_label,
                output_video,
                output_image,
                output_text,
                video_generation_examples_group,
                video_edit_examples_group,
                video_understanding_examples_group,
                image_generation_examples_group,
                image_edit_examples_group,
                image_understanding_examples_group,
                recommended_case_key,
            ],
        )

        aspect_ratio.change(
            fn=update_size_from_aspect_ratio,
            inputs=[task, aspect_ratio, resolution],
            outputs=[height, width, real_size],
            queue=False,
            show_api=False,
        )
        # real_size is hidden and derived from task/resolution/aspect_ratio.
        # Do not attach a .change handler here: dynamic Dropdown choices can briefly
        # contain 360p values while the selected value is 480p (or vice versa),
        # which makes Gradio reject the stale value during preprocessing.
        resolution.change(
            fn=update_output_resolution_from_video_profile,
            inputs=[task, aspect_ratio, resolution],
            outputs=[real_size, height, width],
            queue=False,
            show_api=False,
        )

        for example_button, example_prompt, _, _, example_cache_key in video_generation_example_buttons + image_generation_example_buttons:
            example_button.click(
                fn=make_prompt_example_click_handler(example_prompt, example_cache_key),
                inputs=[task],
                outputs=[prompt, system_prompt, aspect_ratio, height, width, num_frames, resolution, real_size],
                queue=False,
                show_api=False,
            )

        for example_button, example_prompt, example_video, example_image, example_cache_key in (
            video_edit_example_buttons
            + video_understanding_example_buttons
            + image_edit_example_buttons
            + image_understanding_example_buttons
        ):
            example_button.click(
                fn=make_media_prompt_example_click_handler(example_prompt, example_video, example_image, example_cache_key),
                inputs=[task],
                outputs=[prompt, input_video, input_image, system_prompt, aspect_ratio, height, width, num_frames, resolution, real_size],
                queue=False,
                show_api=False,
            )

        run_button.click(
            fn=build_running_status_markdown,
            inputs=[],
            outputs=[status],
            queue=False,
            show_api=False,
        ).then(
            fn=run_task,
            inputs=[
                task,
                prompt,
                system_prompt,
                input_video,
                input_image,
                height,
                width,
                num_frames,
                seed,
                resolution,
                validation_num_timesteps,
                validation_timestep_shift,
                cfg_text_scale,
                enable_frame_interpolation,
            ],
            outputs=[output_video, output_image, output_text, status],
            show_progress="minimal",
        )

    return demo


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Lance multimodal Gradio")
    parser.add_argument("--server-name", default=os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"))
    parser.add_argument("--server-port", type=int, default=int(os.getenv("GRADIO_SERVER_PORT", "7860")))
    parser.add_argument("--share", action="store_true", default=env_flag("GRADIO_SHARE", False))
    parser.add_argument(
        "--gpus",
        default=os.getenv("LANCE_GPUS", DEFAULT_GPUS),
        help="Comma-separated GPU list, for example: 0,1,2,3,4,5,6",
    )
    parser.add_argument(
        "--queue-size",
        type=int,
        default=int(os.getenv("LANCE_QUEUE_SIZE", str(DEFAULT_QUEUE_SIZE))),
        help="Maximum number of queued Gradio requests.",
    )
    parser.add_argument(
        "--concurrency-limit",
        type=int,
        default=int(os.getenv("LANCE_CONCURRENCY_LIMIT", str(DEFAULT_CONCURRENCY_LIMIT))),
        help="Maximum number of Gradio jobs that may execute concurrently. Use 2 for most GPU Spaces; raise it only when enough GPU memory/pipeline slots are available.",
    )
    return parser.parse_args()


def parse_gpu_ids(gpu_string: str) -> list[int]:
    gpu_ids: list[int] = []
    for item in gpu_string.split(","):
        item = item.strip()
        if not item:
            continue
        gpu_ids.append(int(item))
    if not gpu_ids:
        raise ValueError("No valid GPU IDs were parsed.")
    return gpu_ids


def prefetch_model_assets_before_launch() -> None:
    """Download and compact model files before the first ZeroGPU request.

    On ZeroGPU, time spent downloading model snapshots inside @spaces.GPU burns
    the first user's GPU reservation. Prefetching only touches CPU/disk and keeps
    the visible UI unchanged. Set LANCE_PREFETCH_MODEL_ASSETS=0 to skip this at
    Space startup, or LANCE_PREFETCH_MODEL_VARIANTS=video to prefetch less.
    """
    if running_on_space() or env_flag("LANCE_INSTALL_FLASH_ATTN_ON_STARTUP", False):
        try:
            ensure_flash_attn_installed()
        except Exception as exc:
            print(f"[startup] flash-attn startup install failed and will be retried lazily during inference: {exc}", flush=True)

    if not env_flag("LANCE_PREFETCH_MODEL_ASSETS", running_on_space()):
        print("[startup] Model asset prefetch disabled.", flush=True)
        return

    variants_text = os.getenv("LANCE_PREFETCH_MODEL_VARIANTS", f"{MODEL_VARIANT_VIDEO},{MODEL_VARIANT_IMAGE}")
    variants: list[str] = []
    for raw_variant in variants_text.split(","):
        raw_variant = raw_variant.strip()
        if not raw_variant:
            continue
        variant = normalize_model_variant(raw_variant)
        if variant not in variants:
            variants.append(variant)

    for variant in variants:
        try:
            start = time.perf_counter()
            model_path = ensure_model_assets(variant)
            elapsed = time.perf_counter() - start
            print(
                f"[startup][{variant}] Model assets are ready at {display_path(model_path)} "
                f"before ZeroGPU inference. elapsed={elapsed:.2f}s",
                flush=True,
            )
        except Exception as exc:
            print(
                f"[startup][{variant}] Model asset prefetch failed and will be retried lazily during inference: {exc}",
                flush=True,
            )


if __name__ == "__main__":
    args = parse_args()
    os.environ["LANCE_GPUS"] = args.gpus
    QUEUE_MAX_SIZE = args.queue_size
    QUEUE_CONCURRENCY_LIMIT = max(1, args.concurrency_limit)
    prefetch_model_assets_before_launch()
    print(
        "[startup] Skipping GPU model preload. UI will launch first, and Lance weights will be prefetched on CPU before ZeroGPU inference. If that prefetch fails, inference will fall back to lazy loading.",
        flush=True,
    )
    print(
        f"[startup] Gradio queue configured with max_size={QUEUE_MAX_SIZE}, default_concurrency_limit={QUEUE_CONCURRENCY_LIMIT}.",
        flush=True,
    )
    demo = build_demo()
    demo.queue(
        max_size=QUEUE_MAX_SIZE,
        default_concurrency_limit=QUEUE_CONCURRENCY_LIMIT,
    ).launch(
        server_name=args.server_name,
        server_port=args.server_port,
        share=args.share,
        allowed_paths=[str(REPO_ROOT.resolve()), str(GRADIO_TMP_ROOT.resolve())],
        ssr_mode=False,
    )