C
File size: 154,633 Bytes
d248e83
 
 
ccea004
d248e83
 
6f332fd
d248e83
 
c9447dd
d248e83
 
 
 
 
 
 
 
 
 
 
d2211e0
d248e83
b4e7e0f
d248e83
f2e0574
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f1440f
d248e83
 
 
 
 
 
 
 
 
 
 
 
7a107e5
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
add63f8
d248e83
 
c9447dd
cba1f21
30fb99f
d248e83
3bfb6f5
d248e83
 
 
 
 
4f1440f
 
 
d248e83
d2211e0
 
 
b4e7e0f
 
 
d2211e0
8763ff0
 
 
e22f338
d248e83
 
293d3de
d248e83
62c6f7a
293d3de
2a31992
62c6f7a
 
d248e83
62c6f7a
dd1b6a2
5a03917
d248e83
2af15f4
5a03917
dbd8d65
 
d248e83
 
 
add63f8
 
 
 
 
1b72208
add63f8
1b72208
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bd92c9
 
 
 
d248e83
 
 
 
 
 
 
 
62052f2
 
 
8734a4a
 
 
 
f2e0574
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af7a9ad
15e6046
d248e83
 
 
dbd8d65
d248e83
 
 
7a107e5
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2af15f4
 
 
 
 
 
 
 
d946b7e
a141f3c
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d218528
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba1f21
 
 
 
 
f3ac829
 
33f3623
 
 
 
30fb99f
 
33f3623
 
cba1f21
33f3623
cba1f21
 
 
 
 
 
33f3623
cba1f21
33f3623
 
 
 
 
 
 
 
 
 
cba1f21
 
d248e83
 
cba1f21
d248e83
 
 
 
 
 
274ec21
d248e83
 
 
35b7f6d
903b8fb
 
 
e22f338
 
 
35b7f6d
e22f338
 
 
 
903b8fb
35b7f6d
e22f338
 
 
 
 
 
 
 
 
 
 
 
35b7f6d
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6895a81
 
 
 
d248e83
 
 
6895a81
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8734a4a
 
 
 
 
d248e83
 
62052f2
 
 
 
 
 
 
 
 
 
 
 
 
 
8734a4a
62052f2
 
 
 
 
d248e83
62052f2
 
 
8734a4a
62052f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
8b673ee
 
8734a4a
 
 
 
 
 
 
 
 
 
8b673ee
 
8734a4a
 
f2e0574
 
 
 
 
 
 
 
 
 
8734a4a
 
 
 
f2e0574
 
 
 
8734a4a
f2e0574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bd92c9
 
 
 
 
f2e0574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b673ee
f2e0574
 
 
 
 
 
 
 
 
 
 
 
 
 
8734a4a
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2775d
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c78d1eb
96eddc2
d248e83
 
 
7a107e5
d248e83
 
 
0bc0be7
 
 
 
 
 
d248e83
 
 
 
 
96eddc2
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b94853
cba1f21
 
 
 
 
30fb99f
 
 
 
 
cba1f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30fb99f
b497277
cba1f21
 
 
30fb99f
 
 
 
 
 
cba1f21
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9447dd
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c393a97
 
 
 
 
 
 
d248e83
 
 
add63f8
 
 
 
d248e83
1b72208
 
 
 
 
 
 
 
 
 
 
 
add63f8
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
c393a97
 
 
 
 
d248e83
 
 
 
 
 
 
 
32c42f8
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b72208
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
421fe83
d248e83
 
 
 
 
 
 
 
1b72208
 
 
 
 
 
 
d248e83
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
3bfb6f5
d248e83
 
3bfb6f5
 
d248e83
3bfb6f5
d248e83
 
 
 
 
 
 
 
 
 
1b72208
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
15e6046
 
d248e83
 
1b72208
 
 
 
 
 
 
d248e83
 
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
1b72208
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
1b72208
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc0be7
 
 
 
d248e83
 
 
 
 
 
 
0bc0be7
 
d248e83
0bc0be7
d248e83
0bc0be7
 
 
 
d248e83
 
7a107e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
15e6046
d248e83
15e6046
 
 
 
 
 
 
d248e83
 
 
 
 
 
15e6046
0bc0be7
d248e83
 
15e6046
 
d248e83
0bc0be7
d248e83
 
 
0bc0be7
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
b1a40c0
 
 
 
d248e83
 
b530c46
 
b1a40c0
d248e83
 
b1a40c0
d248e83
 
 
b1a40c0
d248e83
 
 
b1a40c0
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b530c46
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc0be7
d248e83
51ee26e
d248e83
 
 
 
 
 
 
 
 
 
b530c46
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b530c46
 
 
 
0bc0be7
 
 
 
 
 
d248e83
 
 
96eddc2
d248e83
 
 
 
 
 
 
96eddc2
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b530c46
d248e83
 
 
 
 
 
2af15f4
 
 
 
 
 
 
 
 
 
 
 
 
 
2a31992
d07b8df
62c6f7a
d07b8df
62c6f7a
 
d07b8df
2a31992
62c6f7a
 
 
2a31992
 
62c6f7a
 
 
d07b8df
62c6f7a
 
0bc0be7
d248e83
 
 
 
 
 
 
 
 
 
51ee26e
d248e83
 
 
 
 
 
0bc0be7
d248e83
 
 
 
 
3bfb6f5
2ba680f
 
3bfb6f5
2ba680f
3bfb6f5
2ba680f
 
3bfb6f5
 
 
 
 
 
 
 
 
 
 
2ba680f
 
 
 
 
 
 
 
3bfb6f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ba680f
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6895a81
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15e6046
d248e83
15e6046
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2775d
 
 
 
 
 
d248e83
 
 
cba1f21
d248e83
 
 
 
 
 
 
0bc0be7
d248e83
 
0bc0be7
 
 
 
 
 
 
d248e83
0bc0be7
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
7a107e5
0bc0be7
d248e83
 
0bc0be7
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
1b72208
d248e83
1b72208
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc0be7
d248e83
 
c78d1eb
0bc0be7
7a107e5
 
 
 
0bc0be7
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c78d1eb
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8763ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
274ec21
 
d248e83
 
 
d2211e0
d248e83
 
 
 
 
 
 
 
 
 
4f1440f
 
 
 
 
 
 
ccea004
4f1440f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e7e0f
 
 
 
 
d248e83
b4e7e0f
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9447dd
 
b4e7e0f
 
c9447dd
 
 
cba1f21
 
 
 
 
 
 
 
 
 
b4e7e0f
 
 
 
 
 
 
 
c9447dd
 
 
cba1f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ee77c4
cba1f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9447dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
b4e7e0f
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
d2211e0
 
b4e7e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2211e0
b4e7e0f
 
 
 
d2211e0
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a107e5
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
7a107e5
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a107e5
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293d3de
 
 
 
62c6f7a
 
 
 
 
d07b8df
 
 
3bfb6f5
 
d07b8df
 
 
 
 
 
 
 
 
 
 
 
 
293d3de
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2af15f4
 
 
3aca7f0
 
 
 
 
 
 
 
 
 
 
 
 
 
2af15f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aca7f0
 
 
2af15f4
3aca7f0
 
 
2af15f4
 
3aca7f0
2af15f4
 
 
 
d248e83
 
 
 
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
1b72208
 
 
 
 
 
d248e83
 
 
 
 
1b72208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
1b72208
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f1440f
 
 
 
 
 
 
 
 
 
 
 
 
 
7c364aa
4f1440f
 
 
 
 
 
 
 
 
 
 
 
 
 
79459b6
4f1440f
 
ccea004
 
 
 
 
 
79459b6
 
4f1440f
 
 
 
 
 
 
 
 
 
 
 
 
 
79459b6
 
4f1440f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248e83
4f1440f
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2af15f4
 
 
 
 
 
 
 
 
 
 
 
 
3aca7f0
 
 
 
 
 
 
 
2af15f4
 
 
 
3aca7f0
 
2af15f4
 
 
 
 
 
3aca7f0
 
 
 
 
2af15f4
 
3aca7f0
2af15f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9447dd
 
 
 
 
 
 
 
 
b4e7e0f
c9447dd
 
d248e83
b4e7e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba1f21
b4e7e0f
 
cba1f21
b4e7e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba1f21
b4e7e0f
cba1f21
b4e7e0f
cba1f21
b4e7e0f
cba1f21
b4e7e0f
cba1f21
 
 
 
 
 
 
 
b4e7e0f
 
 
 
 
 
 
 
 
 
 
c9447dd
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
d2211e0
 
d248e83
d2211e0
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba1f21
 
 
 
 
 
d248e83
cba1f21
 
b4e7e0f
 
 
 
 
cba1f21
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421fe83
 
 
 
 
 
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
5a03917
 
d248e83
 
 
 
 
 
 
5a03917
d248e83
 
 
367519e
d248e83
 
 
421fe83
 
 
 
dbd8d65
 
d248e83
dbd8d65
 
d248e83
 
 
 
 
 
 
 
2ba680f
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d946b7e
 
 
 
 
 
 
 
d248e83
 
 
1b72208
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8734a4a
d218528
 
 
 
 
 
f2e0574
8734a4a
 
 
 
 
 
 
 
 
 
f2e0574
d218528
8734a4a
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a107e5
 
 
d248e83
 
 
7a107e5
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293d3de
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
2af15f4
d248e83
 
 
 
 
2af15f4
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a107e5
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293d3de
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d218528
 
8734a4a
d218528
f2e0574
8734a4a
 
 
 
d218528
8734a4a
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
2af15f4
 
 
 
 
 
 
d248e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521808c
d248e83
 
 
 
 
 
 
 
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
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
import base64
import binascii
import importlib.util
import json
import os
import re
import runpy
import secrets
import shutil
import sqlite3
import subprocess
import sys
import tempfile
import threading
import time
import urllib.request
import uuid
import warnings
from asyncio.base_events import BaseEventLoop
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from functools import lru_cache
from io import BytesIO
from itertools import islice
from pathlib import Path
from urllib.parse import quote, unquote, urlparse
from zoneinfo import ZoneInfo

ASYNCIO_FD_ERROR = "Invalid file descriptor: -1"
loop_del = BaseEventLoop.__del__


def close_loop(loop):
    try:
        loop_del(loop)
    except ValueError as error:
        if str(error) != ASYNCIO_FD_ERROR:
            raise


BaseEventLoop.__del__ = close_loop

import gradio as gr
import numpy as np
import py7zr
import spaces
import torch
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from fastapi import Body, Depends, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response
from gradio_client import Client
from gradio.routes import App
from huggingface_hub import (
    batch_bucket_files,
    download_bucket_files,
    list_bucket_tree,
)
from PIL import Image, ImageDraw, ImageFont, ImageOps
from PIL.PngImagePlugin import PngInfo
from pydantic import BaseModel, ConfigDict, Field
from starlette.background import BackgroundTask
from workflow_api import (
    ALIGN_MODEL_TYPE,
    ALIGN_SCHEDULER,
    ANIMA_CLIP,
    DETAILER_CROP,
    DETAILER_DILATION,
    DETAILER_DROP_SIZE,
    DETAILER_FEATHER,
    DETAILER_GUIDE_SIZE,
    DETAILER_MAX_SIZE,
    DETAILER_THRESHOLD,
    GRID_SIZE,
    LATENT_SCALE,
    image_metadata,
    import_custom_nodes,
    is_anima_model,
    mask_box,
    upscale_size,
)


os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics")

DATA_MOUNT = Path("/data")
BUCKET_MOUNT = Path("/CB")
HAS_DATA_MOUNT = (DATA_MOUNT / "img").is_dir()
DEFAULT_DATA_DIR = DATA_MOUNT if HAS_DATA_MOUNT else Path.cwd() / "data"
DATA_DIR = Path(os.environ.get("DATA_DIR", DEFAULT_DATA_DIR))
LOCAL_MODEL_DIR = Path(os.environ.get("LOCAL_MODEL_DIR", "/tmp/models"))
COMFYUI_PATH = Path(os.environ.get("COMFYUI_PATH", Path.cwd() / "ComfyUI"))
MOUNTED_CUSTOM_NODES_DIR = BUCKET_MOUNT / "custom_nodes"
CUSTOM_NODES_DIR = Path("/tmp/custom_nodes")
OUTPUT_DIR = DATA_DIR / "output"
IMAGE_DIR = DATA_DIR / "img"
STAR_DB = DATA_DIR / "explorer.db"
ARTIST_DB = Path(tempfile.gettempdir()) / "artists.sqlite"
ARTIST_PLACEHOLDER = re.compile(r"\{(?:artist|ar)(\d*)\}", re.I)
TIMEZONE = ZoneInfo("Asia/Singapore")
CPU_DEVICE = torch.device("cpu")
MIB = 1024 * 1024
SALT_SIZE = 16
NONCE_SIZE = 12
SCRYPT_N = 2**14
FILE_MAGIC = b"EPNG1"
PROXY_ENCRYPTION = b"aes-256-gcm"
PROXY_ENCRYPTION_HEADER = b"x-gradio-comfy-encryption"
PROXY_MAGIC = b"GCV1"
IMAGE_SUFFIXES = (".epng",)
PREVIEW_CACHE_SIZE = 256
PREVIEW_QUALITY = 70
PREVIEW_SIZE = 320
EXPLORER_PAGE_SIZE = 80
EXPLORER_MAX_PAGE_SIZE = 200
EXPLORER_DB_TIMEOUT = 30
IMAGE_KEY_CACHE_SIZE = 512
DUPLICATE_HASH_SIZE = 16
DUPLICATE_HASH_DISTANCE = 24
DUPLICATE_COLOR_DISTANCE = 24
DUPLICATE_CHECK_EXCLUDED_FOLDERS = {"2026-07-30"}
DEFAULT_RETURN_SCALE = 1
DEFAULT_BATCH_SIZE = 1
DEFAULT_UI_BATCH_SIZE = 1
MAX_BATCH_SIZE = 8
PING_MODEL_ID = 50
PING_SIZE = 64
PING_STEPS = 8
PING_SAMPLER = "euler"
PING_SCHEDULER = "simple"
STARTUP_ASSET_IDS = {
    "checkpoints": (16, 50),
    "diffusion_models": (4,6),
    "loras": (1, 3, 4, 7, 12, 30, 47, 49, 50),
    "ultralytics": (1,),
    "upscale_models": (1, 3),
    "vae": (2, 3),
    "ipadapter": (2,),
    "clip_vision": (1,),
}
ENVIRONMENT_START = .2
REGIONAL_GLOBAL_STRENGTH = .6
REQUIRED_CUSTOM_NODES = (
    "ComfyUI-Impact-Pack",
    "ComfyUI-Impact-Subpack",
    "ComfyUI-ppm",
    "ComfyUI_IPAdapter_plus",
    "RES4LYF",
)
CUSTOM_NODE_REPOS = {
    "RES4LYF": "https://github.com/ClownsharkBatwing/RES4LYF",
}
CUSTOM_NODE_MODULES = {
    "ComfyUI-Impact-Pack": (
        "segment_anything", "skimage", "piexif", "transformers", "cv2",
        "scipy", "dill", "matplotlib", "sam2",
    ),
    "ComfyUI-Impact-Subpack": (
        "ultralytics", "numpy", "cv2", "dill", "matplotlib",
    ),
}
AREA_PRESETS = {
    "full": "a1:e5",
    "tl": "a1:c3",
    "tc": "b1:d3",
    "tr": "c1:e3",
    "ml": "a2:c4",
    "mc": "b2:d4",
    "mr": "c2:e4",
    "bl": "a3:c5",
    "bc": "b3:d5",
    "br": "c3:e5",
    "th": "a1:e3",
    "mh": "a2:e4",
    "bh": "a3:e5",
    "lh": "a1:c5",
    "ch": "b1:d5",
    "rh": "c1:e5",
}
AUTO_LAYOUTS = {
    1: ((.2, 0, .6, 1),),
    2: ((0, 0, .55, 1), (.45, 0, .55, 1)),
    3: ((0, 0, .4, 1), (.3, 0, .4, 1), (.6, 0, .4, 1)),
}
REGIONAL_MODES = ("conditioning", "attention")
PORT = int(os.environ.get("PORT", "7860"))
LOCAL_URL = os.environ.get("LOCAL_URL", f"http://127.0.0.1:{PORT}")
PASSWORD = os.environ.get("pass")
if not PASSWORD:
    raise RuntimeError("pass environment variable is required")

BUCKET_ID = "HyperHail/CB"
IMAGE_BUCKET_ID = "HyperHail/C"
IMAGE_BUCKET_PREFIX = "img"
IMAGE_TOKEN = os.environ.get("hh") or False
CIVITAI_TOKEN = os.environ.get("CIVIT_MODEL_READ")
CIVITAI_HOSTS = {
    "civitai.com", "www.civitai.com", "civitai.red", "www.civitai.red",
}
MODEL_KINDS = (
    "checkpoints", "diffusion_models", "clip", "clip_vision", "vae",
    "loras", "ipadapter", "upscale_models", "ultralytics",
)
COMFY_KINDS = {"clip": "text_encoders"}
MODEL_SUFFIXES = (
    ".bin", ".ckpt", ".pkl", ".pt", ".pt2", ".pth", ".safetensors", ".sft",
)
NUMBERED_MODEL_KINDS = ("checkpoints", "loras")
MODEL_NUMBER = re.compile(r"^(\d+)_(.+)$")
ANIMA_PREFIX = "anima"
MODEL_LOCATION_CHOICES = [
    (kind.replace("_", " ").title(), kind)
    for kind in MODEL_KINDS
]
DOWNLOAD_HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) "
        "Gecko/20100101 Firefox/140.0"
    ),
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Referer": "https://civitai.com/",
}
DEFAULT_NEGATIVE = (
    "(censored, mosaic censoring, bar censor:1.1), bad quality, worst quality, "
    "worst detail, bad anatomy, extra fingers, extra toes, extra legs, 4 toes, "
    "6 toes, 4 fingers, 6 fingers, malformed fingers, extra limbs, missing fingers, "
    "extra arms, censored, deformed, disfigured, text, (multiple views:1.1)"
)
DEFAULT_SAMPLER = "euler_ancestral"
DEFAULT_SCHEDULER = "karras"
DEFAULT_CFG = 4
DEFAULT_STEPS = 30
DEFAULT_MODEL = "52_novaAnimeXL_ilV190.safetensors"
DEFAULT_VAE = "3_sdxlVAE_sdxlVAE.safetensors"
ANIMA_VAE = "2_qwen_image_vae.safetensors"
NON_ANIMA_HEADER = "__non_anima_header__"
ANIMA_HEADER = "__anima_header__"
MODEL_HEADERS = (NON_ANIMA_HEADER, ANIMA_HEADER)
DEFAULT_LORAS = ()
DEFAULT_UPSCALE_METHOD = "bislerp"
DEFAULT_UPSCALE_MODEL = "3_1x-Archivist_Soft.pth"
DEFAULT_UPSCALE_SCALE = 1.1
DEFAULT_SECOND_SAMPLER = "euler"
DEFAULT_SECOND_SCHEDULER = "karras"
DEFAULT_SECOND_STEPS = 18
DEFAULT_SECOND_CFG = 5
DEFAULT_DENOISE = .5
DEFAULT_DETECTOR = "2_face_yolov9c.pt"
STYLE_IPADAPTER = "2_ip-adapter-plus_sdxl_vit-h.safetensors"
STYLE_CLIP_VISION = "1_CLIP-ViT-H-fp16.safetensors"
STYLE_WEIGHT_TYPE = "style transfer"
STYLE_EMBEDS_SCALING = "V only"
STYLE_SCOPES = ("first", "generation", "all")
STYLE_MODEL_KINDS = ("ipadapter", "clip_vision")
DEFAULT_STYLE_SCOPE = "generation"
DEFAULT_STYLE_WEIGHT = 1
DEFAULT_STYLE_END = 1
MAX_STYLE_IMAGES = 4
STYLE_IMAGE_SIZE = 1024
MAX_STYLE_IMAGE_SIZE = 20 * MIB
BUILTIN_ASSETS = {
    ("ipadapter", STYLE_IPADAPTER): (
        "https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/"
        "ip-adapter-plus_sdxl_vit-h.safetensors"
    ),
    ("clip_vision", STYLE_CLIP_VISION): (
        "https://huggingface.co/h94/IP-Adapter/resolve/main/models/"
        "image_encoder/model.safetensors"
    ),
}
MATRIX_WIDTH = 1152
MATRIX_HEIGHT = 896
MATRIX_SAMPLER = "dpmpp_2m"
MATRIX_SCHEDULER = "karras"
MATRIX_FIRST_STEPS = 24
MATRIX_FIRST_CFG = 6
MATRIX_UPSCALE_METHOD = "bislerp"
MATRIX_UPSCALE_SCALE = 1.1
MATRIX_SECOND_STEPS = 24
MATRIX_SECOND_CFG = 5
MATRIX_DENOISE = .35
MATRIX_CELL_WIDTH = round(MATRIX_WIDTH / 8 * MATRIX_UPSCALE_SCALE) * 8
MATRIX_CELL_HEIGHT = round(MATRIX_HEIGHT / 8 * MATRIX_UPSCALE_SCALE) * 8
MATRIX_LABEL_SIZE = 32
MATRIX_ROW_LABEL_WIDTH = 160
MATRIX_GRID_SCALE = .25
MATRIX_GRID_CELL_WIDTH = round(MATRIX_CELL_WIDTH * MATRIX_GRID_SCALE)
MATRIX_GRID_CELL_HEIGHT = round(MATRIX_CELL_HEIGHT * MATRIX_GRID_SCALE)
COMBINED_MATRIX_TYPE = "checkpoint+sampler+scheduler"
MATRIX_TYPES = (
    "checkpoint",
    "sampler",
    "scheduler",
    "sampler+scheduler",
    COMBINED_MATRIX_TYPE,
)
UPSCALE_GPU_DURATION = 5
MAX_UPSCALE_BYTES = 20 * MIB
MAX_UPSCALE_PIXELS = 4 * 1024 * 1024
UPSCALE_ASSETS = {
    "style.css": ("upscale.css", "text/css"),
    "image.js": ("upscale.js", "text/javascript"),
    "page.js": ("upscale-page.js", "text/javascript"),
}
SCAN_THREAD_COUNT = 8
GPU_DURATION = 25
GPU_ATTEMPTS = 3
GPU_RETRY_ERRORS = (
    "gpu task aborted",
    "uncorrectable ecc error",
)

jobs = {}
images = {}
remote_models = {kind: {} for kind in MODEL_KINDS}
state = {}
lock = threading.Lock()
matrix_lock = threading.Lock()
pool = ThreadPoolExecutor(max_workers=1)
backup_pool = ThreadPoolExecutor(max_workers=1)
matrix_pool = ThreadPoolExecutor(max_workers=1)
model_pool = ThreadPoolExecutor(max_workers=1)
model_upload_lock = threading.Lock()
matrix_grids = set()
local_client = None
local_client_lock = threading.Lock()


def log(message):
    print(message, flush=True)


def retry_gpu(call):
    for attempt in range(GPU_ATTEMPTS):
        try:
            return call()
        except Exception as error:
            if (
                attempt == GPU_ATTEMPTS - 1
                or not any(
                    text in str(error).casefold()
                    for text in GPU_RETRY_ERRORS
                )
            ):
                raise

            delay = 2**attempt
            log(f"GPU task failed, retrying in {delay}s")
            time.sleep(delay)


def get_local_client():
    global local_client
    with local_client_lock:
        if local_client is None:
            local_client = Client(LOCAL_URL, verbose=False)
    return local_client


def copy_artist_database():
    source = next(
        (
            path
            for path in (
                DATA_MOUNT / "_cache" / "artists.sqlite",
                DATA_DIR / "_cache" / "artists.sqlite",
                Path("_cache/artists.sqlite"),
                Path.cwd() / "_cache" / "artists.sqlite",
                Path.cwd().parent / "_cache" / "artists.sqlite",
                Path.cwd().parent / "data" / "_cache" / "artists.sqlite",
                DATA_MOUNT / "artists.sqlite",
                DATA_DIR / "artists.sqlite",
                Path("artists.sqlite"),
                Path("../artists.sqlite"),
                Path.cwd() / "artists.sqlite",
                Path.cwd().parent / "artists.sqlite",
            )
            if path.is_file()
        ),
        None,
    )
    if source is None:
        log("Cannot find artist database")
        return
    if source.resolve() != ARTIST_DB.resolve():
        temp = ARTIST_DB.with_suffix(".sqlite.part")
        shutil.copy2(source, temp)
        temp.replace(ARTIST_DB)
    try:
        with sqlite3.connect(ARTIST_DB) as database:
            count = database.execute("SELECT count(*) FROM artists").fetchone()[0]
        log(f"Found artist database: {count} tags loaded")
    except Exception:
        log("Cannot find artist database")


def cleanup_mount():
    DATA_DIR.mkdir(parents=True, exist_ok=True)
    copy_artist_database()
    if OUTPUT_DIR.exists():
        shutil.rmtree(OUTPUT_DIR)
        log("Deleted output directory")

    if IMAGE_DIR.is_dir():
        for path in IMAGE_DIR.rglob("*"):
            if path.suffix.casefold() == ".7z" and path.is_file():
                path.unlink()
                log(f"Deleted img/{path.relative_to(IMAGE_DIR)}")
        if HAS_DATA_MOUNT:
            for folder in IMAGE_DIR.glob("????-??-??"):
                pngs = list(folder.glob("*.png"))
                if not pngs:
                    continue
                check_duplicates = (
                    folder.name not in DUPLICATE_CHECK_EXCLUDED_FOLDERS
                )
                fingerprints = []
                if check_duplicates:
                    for path in folder.glob("*.epng"):
                        with Image.open(BytesIO(stored_bytes(path))) as image:
                            fingerprints.append(image_fingerprint(image))
                for path in pngs:
                    with Image.open(path) as image:
                        duplicate = False
                        if check_duplicates:
                            fingerprint = image_fingerprint(image)
                            duplicate = any(
                                (fingerprint[0] ^ known[0]).bit_count()
                                <= DUPLICATE_HASH_DISTANCE
                                and sum(
                                    abs(left - right)
                                    for left, right in zip(fingerprint[1], known[1])
                                )
                                <= DUPLICATE_COLOR_DISTANCE
                                for known in fingerprints
                            )
                        if not duplicate:
                            save_named_image(image, path.with_suffix(".epng"))
                    path.unlink()
                    if duplicate:
                        log(f"Deleted duplicate img/{path.relative_to(IMAGE_DIR)}")


def download(url, target):
    target.parent.mkdir(parents=True, exist_ok=True)
    temp = target.with_suffix(target.suffix + ".part")
    last = -1

    def report(blocks, block_size, total):
        nonlocal last
        if total > 0:
            mark = min(4, blocks * block_size * 4 // total)
            if mark > last:
                last = mark
                log(f"Downloading {target.name}: {mark * 25}%")

    urllib.request.urlretrieve(url, temp, report)
    temp.replace(target)
    log(f"Downloaded {target.name}: {target.stat().st_size // MIB} MiB")


def model_path(kind, name):
    return LOCAL_MODEL_DIR / kind / name


def download_assets(models):
    downloads = []
    for kind, name in models:
        target = model_path(kind, name)
        if target.is_file():
            continue
        remote = remote_models[kind].get(name)
        if remote is None:
            url = BUILTIN_ASSETS.get((kind, name))
            if url is None:
                raise ValueError(f"Unknown {kind} file: {name}")
            download(url, target)
            continue
        target.parent.mkdir(parents=True, exist_ok=True)
        temp = target.with_suffix(target.suffix + ".part")
        downloads.append((kind, name, remote, temp, target))

    if not downloads:
        return
    log(f"Downloading {len(downloads)} assets from {BUCKET_ID}")
    for kind, name, _, _, _ in downloads:
        log(f"Downloading {kind}/{name}")
    download_bucket_files(
        BUCKET_ID,
        files=[
            (remote, str(temp))
            for _, _, remote, temp, _ in downloads
        ],
        token=False,
    )
    for kind, name, _, temp, target in downloads:
        temp.replace(target)
        log(
            f"Downloaded {kind}/{name}: "
            f"{target.stat().st_size // MIB} MiB"
        )


def is_anima_asset(name):
    name = name.casefold()
    return name.startswith(ANIMA_PREFIX) and not name.startswith("animag")


def index_bucket_models():
    models = {kind: {} for kind in MODEL_KINDS}
    items = [
        item
        for item in list_bucket_tree(BUCKET_ID, recursive=True, token=False)
        if item.type == "file"
        and Path(item.path).suffix.casefold() in MODEL_SUFFIXES
    ]
    counters = {
        kind: {False: 0, True: 0}
        for kind in NUMBERED_MODEL_KINDS
    }
    for item in items:
        kind, separator, name = item.path.partition("/")
        match = MODEL_NUMBER.match(name)
        if separator and kind in counters and match:
            anima = is_anima_asset(match.group(2))
            counters[kind][anima] = max(counters[kind][anima], int(match.group(1)))

    copies = []
    deletes = []
    for item in sorted(items, key=lambda item: item.path.casefold()):
        kind, separator, name = item.path.partition("/")
        if not separator or kind not in models:
            continue
        if kind in counters and not MODEL_NUMBER.match(name):
            anima = is_anima_asset(name)
            counters[kind][anima] += 1
            name = f"{counters[kind][anima]}_{name}"
            path = f"{kind}/{name}"
            copies.append(("bucket", BUCKET_ID, item.xet_hash, path))
            deletes.append(item.path)
        else:
            path = item.path
        models[kind][name] = path

    if copies:
        batch_bucket_files(
            BUCKET_ID,
            copy=copies,
            delete=deletes,
            token=IMAGE_TOKEN,
        )

    added = {
        kind: set(models[kind]) - set(remote_models[kind])
        for kind in MODEL_KINDS
    }
    remote_models.clear()
    remote_models.update(models)
    return added


def bucket_numbers(kind, anima):
    numbers = set()
    for item in list_bucket_tree(
        BUCKET_ID,
        prefix=f"{kind}/",
        recursive=True,
        token=False,
    ):
        if item.type != "file" or "/" in item.path.removeprefix(f"{kind}/"):
            continue
        match = MODEL_NUMBER.match(Path(item.path).name)
        if match and is_anima_asset(match.group(2)) == anima:
            numbers.add(int(match.group(1)))
    return numbers


def bucket_url_filename(response):
    name = response.headers.get_filename()
    if not name:
        name = Path(urlparse(response.geturl()).path).name
    name = unquote(name).replace("\\", "/").rsplit("/", 1)[-1].strip()
    match = MODEL_NUMBER.match(name)
    return match.group(2) if match else name


def upload_bucket_assets(files, url, kind, anima, password):
    if not valid_pass(password):
        raise gr.Error("Invalid password")
    if kind not in MODEL_KINDS:
        raise gr.Error("Invalid CB location")
    files = files or []
    url = (url or "").strip()
    if not files and not url:
        raise gr.Error("Select a file or enter a URL")

    temp = None
    try:
        assets = []
        for file in files:
            source = Path(file)
            match = MODEL_NUMBER.match(source.name)
            number, name = (int(match.group(1)), match.group(2)) \
                if match else (None, source.name)
            if source.suffix.casefold() not in MODEL_SUFFIXES:
                raise gr.Error(f"Unsupported model file: {name}")
            if anima and not is_anima_asset(name):
                name = f"{ANIMA_PREFIX}_{name}"
            assets.append((source, number, name))

        if url:
            parsed = urlparse(url)
            if parsed.scheme not in ("http", "https") or not parsed.netloc:
                raise gr.Error("Enter a valid URL")
            request = urllib.request.Request(url, headers=DOWNLOAD_HEADERS)
            if CIVITAI_TOKEN and parsed.hostname in CIVITAI_HOSTS:
                request.add_unredirected_header(
                    "Authorization",
                    f"Bearer {CIVITAI_TOKEN}",
                )
            with urllib.request.urlopen(request) as response:
                name = bucket_url_filename(response)
                suffix = Path(name).suffix.casefold()
                if suffix not in MODEL_SUFFIXES:
                    raise gr.Error("URL did not return a model file")
                if anima and not is_anima_asset(name):
                    name = f"{ANIMA_PREFIX}_{name}"
                with tempfile.NamedTemporaryFile(
                    suffix=suffix,
                    delete=False,
                ) as file:
                    temp = Path(file.name)
                    shutil.copyfileobj(response, file)
            assets.append((temp, None, name))

        with model_upload_lock:
            used = bucket_numbers(kind, anima)
            reserved = {
                number for _, number, _ in assets
                if number is not None and number not in used
            }
            next_number = max(used, default=0) + 1
            additions = []
            paths = []
            for source, number, name in assets:
                if number is not None and number in reserved:
                    reserved.remove(number)
                else:
                    while next_number in used or next_number in reserved:
                        next_number += 1
                    number = next_number
                    next_number += 1
                used.add(number)
                path = f"{kind}/{number}_{name}"
                additions.append((source, path))
                paths.append(path)
            batch_bucket_files(
                BUCKET_ID,
                add=additions,
                token=IMAGE_TOKEN,
            )
            index_bucket_models()
        return "Added " + ", ".join(paths)
    finally:
        if temp:
            temp.unlink(missing_ok=True)


def model_kind(name):
    return "diffusion_models" if is_anima_model(name) else "checkpoints"


def vae_name(name):
    return ANIMA_VAE if is_anima_model(name) else DEFAULT_VAE


def generation_models():
    names = set(remote_models["checkpoints"]) | {
        name
        for name in remote_models["diffusion_models"]
        if is_anima_model(name)
    }
    return sorted(
        names,
        key=lambda name: (
            is_anima_model(name),
            int(name.partition("_")[0]),
            name.casefold(),
        ),
    )


def model_choices(models):
    non_anima = [name for name in models if not is_anima_model(name)]
    anima = [name for name in models if is_anima_model(name)]
    return [
        ("──────── Non-Anima ────────", NON_ANIMA_HEADER),
        *[(name, name) for name in non_anima],
        ("──────── Anima ────────", ANIMA_HEADER),
        *[(name, name) for name in anima],
    ]


def upscale_model_choices(models):
    return [
        ("None", ""),
        *[(name, name) for name in models],
    ]


class LoraRequest(BaseModel):
    name: str
    strength: float = 1
    clip: float = 0


class RegionRequest(BaseModel):
    prompt: str
    area: str
    strength: float = Field(1, gt=0, le=10)


class DetailerRequest(BaseModel):
    detector: str
    model: str = ""
    prompt: str = ""
    negative: str = ""
    sampler: str = DEFAULT_SECOND_SAMPLER
    scheduler: str = DEFAULT_SECOND_SCHEDULER
    steps: int = Field(DEFAULT_SECOND_STEPS, ge=1, le=100)
    cfg: float = Field(DEFAULT_SECOND_CFG, ge=0, le=100)
    denoise: float = Field(.35, gt=0, le=1)


def default_loras():
    return [
        LoraRequest(name=name, strength=strength, clip=clip)
        for name, strength, clip in DEFAULT_LORAS
    ]


class DirectRequest(BaseModel):
    prompt: str
    sillytavern: dict = Field(default_factory=dict)
    second_prompt: str = ""
    regions: list[RegionRequest] = Field(default_factory=list, max_length=3)
    regional_mode: str = REGIONAL_MODES[0]
    detailers: list[DetailerRequest] = Field(default_factory=list)
    style_images: list[str] = Field(default_factory=list, max_length=MAX_STYLE_IMAGES)
    style_scope: str = DEFAULT_STYLE_SCOPE
    style_weight: float = Field(DEFAULT_STYLE_WEIGHT, ge=0, le=5)
    style_end: float = Field(DEFAULT_STYLE_END, gt=0, le=1)
    second_style_images: list[str] = Field(
        default_factory=list,
        max_length=MAX_STYLE_IMAGES,
    )
    second_style_weight: float = Field(DEFAULT_STYLE_WEIGHT, ge=0, le=5)
    second_style_end: float = Field(DEFAULT_STYLE_END, gt=0, le=1)
    model: str = DEFAULT_MODEL
    loras: list[LoraRequest] = Field(default_factory=default_loras)
    second_model: str = ""
    second_loras: list[LoraRequest] = Field(default_factory=list)
    negative: str = DEFAULT_NEGATIVE
    second_negative: str = ""
    width: int = Field(1152, ge=64, le=2048)
    height: int = Field(896, ge=64, le=2048)
    batch_size: int = Field(DEFAULT_BATCH_SIZE, ge=1, le=MAX_BATCH_SIZE)
    sampler: str = DEFAULT_SAMPLER
    scheduler: str = DEFAULT_SCHEDULER
    steps: int = Field(DEFAULT_STEPS, ge=1, le=100)
    cfg: float = Field(DEFAULT_CFG, ge=0, le=100)
    upscale: bool = False
    upscale_method: str = DEFAULT_UPSCALE_METHOD
    upscale_model: str = DEFAULT_UPSCALE_MODEL
    upscale_scale: float = Field(DEFAULT_UPSCALE_SCALE, gt=0)
    second_sampler: str = DEFAULT_SECOND_SAMPLER
    second_scheduler: str = DEFAULT_SECOND_SCHEDULER
    second_steps: int = Field(DEFAULT_SECOND_STEPS, ge=1, le=100)
    second_cfg: float = Field(DEFAULT_SECOND_CFG, ge=0, le=100)
    denoise: float = Field(DEFAULT_DENOISE, ge=0, le=1)
    return_scale: float = Field(DEFAULT_RETURN_SCALE, ge=.01, le=1)
    artist_min_posts: int = Field(100, ge=0)
    artist_blacklist: str = ""
    selected_artists: list[str] = Field(default_factory=list)


def resolve_artist_prompts(request):
    prompts = [request.prompt, request.second_prompt]
    prompts.extend(d.prompt for d in getattr(request, "detailers", []) if getattr(d, "prompt", None))
    prompts.extend(r.prompt for r in getattr(request, "regions", []) if getattr(r, "prompt", None))
    matches = ARTIST_PLACEHOLDER.findall("\n".join(prompts))
    identifiers = list(dict.fromkeys(value.lstrip("0") or "1" for value in matches))
    if not identifiers:
        return
    if not ARTIST_DB.is_file():
        copy_artist_database()
    if not ARTIST_DB.is_file():
        raise ValueError("Artist database is unavailable")
    blacklist = {
        artist.strip().casefold()
        for artist in request.artist_blacklist.split(",")
        if artist.strip()
    }
    database = sqlite3.connect(ARTIST_DB)
    try:
        artists = [
            row[0]
            for row in database.execute(
                "SELECT artist FROM artists WHERE post_count >= ?",
                (request.artist_min_posts,),
            )
            if row[0].casefold() not in blacklist
        ]
    finally:
        database.close()
    if len(artists) < len(identifiers):
        raise ValueError("Not enough artists match the post minimum and blacklist")
    selected = secrets.SystemRandom().sample(artists, len(identifiers))
    replacements = dict(zip(identifiers, selected))

    def replace(match):
        artist = replacements[match.group(1).lstrip("0") or "1"]
        return re.sub(r"(?<!\\)[()]", r"\\\g<0>", artist)

    request.prompt = ARTIST_PLACEHOLDER.sub(replace, request.prompt)
    request.second_prompt = ARTIST_PLACEHOLDER.sub(replace, request.second_prompt)
    for d in getattr(request, "detailers", []):
        if getattr(d, "prompt", None):
            d.prompt = ARTIST_PLACEHOLDER.sub(replace, d.prompt)
    for r in getattr(request, "regions", []):
        if getattr(r, "prompt", None):
            r.prompt = ARTIST_PLACEHOLDER.sub(replace, r.prompt)
    request.selected_artists = list(dict.fromkeys(selected))


class ModelRequest(BaseModel):
    model: str = DEFAULT_MODEL
    loras: list[LoraRequest] = Field(default_factory=default_loras)
    instant_style: bool = False
    second_model: str = ""
    second_loras: list[LoraRequest] = Field(default_factory=list)
    upscale: bool = False
    upscale_model: str = DEFAULT_UPSCALE_MODEL
    detailers: list[DetailerRequest] = Field(default_factory=list)


class DownloadRequest(BaseModel):
    items: list[str]


class StarRequest(BaseModel):
    path: str
    starred: bool


class MatrixRequest(BaseModel):
    model_config = ConfigDict(extra="forbid")

    generation: str = Field(pattern=r"^g\d+$")
    type: str = "checkpoint"
    positive: str
    negative: str
    model_1: int = Field(0, ge=0)
    model_2: int = Field(0, ge=0)
    sampler: str = ""


class MatrixCellRequest(BaseModel):
    model_config = ConfigDict(extra="forbid")

    generation: str = Field(pattern=r"^g\d+$")
    positive: str
    negative: str
    folder: str
    first: tuple[str, str]
    second: tuple[str, str]
    sampler: str = MATRIX_SAMPLER
    scheduler: str = MATRIX_SCHEDULER
    second_sampler: str = MATRIX_SAMPLER
    second_scheduler: str = MATRIX_SCHEDULER
    output: str = ""


def ensure_comfy():
    if COMFYUI_PATH.is_dir():
        log(f"Found ComfyUI at {COMFYUI_PATH}")
    else:
        archive = Path.cwd() / "comfyui.zip"
        download(
            "https://github.com/Comfy-Org/ComfyUI/archive/refs/heads/master.zip",
            archive,
        )
        log("Extracting ComfyUI")
        shutil.unpack_archive(archive, Path.cwd())
        archive.unlink()
        next(Path.cwd().glob("ComfyUI-*")).replace(COMFYUI_PATH)
        log(f"Installed ComfyUI at {COMFYUI_PATH}")
        subprocess.run(
            [
                sys.executable, "-m", "pip", "install", "-q", "-r",
                str(COMFYUI_PATH / "requirements.txt"),
            ],
            check=True,
        )


def ensure_custom_nodes():
    shutil.rmtree(CUSTOM_NODES_DIR, ignore_errors=True)
    CUSTOM_NODES_DIR.mkdir(parents=True)
    for name in REQUIRED_CUSTOM_NODES:
        source = MOUNTED_CUSTOM_NODES_DIR / name
        target = CUSTOM_NODES_DIR / name
        if source.is_dir():
            target.symlink_to(source, target_is_directory=True)
        elif name in CUSTOM_NODE_REPOS:
            subprocess.run(
                [
                    "git", "clone", "-q", "--depth", "1",
                    CUSTOM_NODE_REPOS[name], str(target),
                ],
                check=True,
            )
        else:
            raise FileNotFoundError(f"Missing required custom node: {source}")
        modules = CUSTOM_NODE_MODULES.get(name)
        if modules and not any(importlib.util.find_spec(item) is None for item in modules):
            continue
        requirements = target / "requirements.txt"
        if requirements.is_file():
            subprocess.run(
                [
                    sys.executable, "-m", "pip", "install", "-q", "-r",
                    str(requirements),
                ],
                check=True,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )


def write_detector_whitelist():
    path = (
        COMFYUI_PATH
        / "user"
        / "default"
        / "ComfyUI-Impact-Subpack"
        / "model-whitelist.txt"
    )
    path.parent.mkdir(parents=True, exist_ok=True)
    names = set(path.read_text().splitlines()) if path.is_file() else set()
    names.update(remote_models["ultralytics"])
    path.write_text("\n".join(sorted(names, key=str.casefold)) + "\n")


def node_input_options(node, name):
    spec = node.INPUT_TYPES()["required"][name]
    if isinstance(spec, tuple):
        if isinstance(spec[0], (list, tuple)):
            return list(spec[0])
        if len(spec) > 1 and isinstance(spec[1], dict):
            return list(spec[1].get("options", []))
    return []


def init_comfy():
    if state:
        return

    log(f"Using data directory {DATA_DIR}")
    ensure_comfy()
    with ThreadPoolExecutor(max_workers=2) as startup:
        model_index = startup.submit(index_bucket_models)
        custom_nodes = startup.submit(ensure_custom_nodes)
        model_index.result()
        custom_nodes.result()
    write_detector_whitelist()
    log("Importing ComfyUI")

    sys.path.insert(0, str(COMFYUI_PATH))

    from comfy.cli_args import args

    args.cpu_vae = False
    args.disable_pinned_memory = True

    import comfy.sd
    import comfy.utils
    import folder_paths

    from nodes import (
        CLIPTextEncode,
        CheckpointLoaderSimple,
        ConditioningSetMask,
        EmptyLatentImage,
        KSampler,
        LatentUpscale,
        LatentUpscaleBy,
        LoraLoader,
        UNETLoader,
        VAEDecode,
        VAEEncode,
        VAELoader,
    )
    from comfy_extras.nodes_align_your_steps import AlignYourStepsScheduler
    from comfy_extras.nodes_custom_sampler import (
        BasicScheduler,
        KSamplerSelect,
        SamplerCustom,
    )
    from comfy_extras.nodes_upscale_model import (
        ImageUpscaleWithModel,
        UpscaleModelLoader,
    )

    for kind in MODEL_KINDS:
        local = LOCAL_MODEL_DIR / kind
        local.mkdir(parents=True, exist_ok=True)
        folder_paths.add_model_folder_path(
            COMFY_KINDS.get(kind, kind),
            str(local),
            is_default=True,
        )
    folder_paths.add_model_folder_path(
        "ultralytics_bbox",
        str(LOCAL_MODEL_DIR / "ultralytics"),
    )

    default_sample_options = KSampler.INPUT_TYPES()["required"]
    default_samplers = list(default_sample_options["sampler_name"][0])
    default_schedulers = list(default_sample_options["scheduler"][0])
    default_upscale_methods = list(
        LatentUpscaleBy.INPUT_TYPES()["required"]["upscale_method"][0]
    )

    folder_paths.add_model_folder_path("custom_nodes", str(CUSTOM_NODES_DIR))
    import_custom_nodes()
    from nodes import NODE_CLASS_MAPPINGS

    custom_samplers = {}
    custom_sampler_nodes = {}
    for node_name, prefix in (
        ("DynSamplerSelect", "ppm-dyn"),
        ("CFGPPSamplerSelect", "ppm-cfgpp"),
        ("PPMSamplerSelect", "ppm"),
    ):
        node = NODE_CLASS_MAPPINGS.get(node_name)
        if node is None:
            continue
        custom_sampler_nodes[node_name] = node()
        for name in node_input_options(node, "sampler_name"):
            if name not in default_samplers:
                custom_samplers[f"{prefix}:{name}"] = (node_name, name)

    current_options = KSampler.INPUT_TYPES()["required"]
    sampler_sources = {
        name: "RES4LYF"
        for name in current_options["sampler_name"][0]
        if name not in default_samplers
    }
    ppm_schedulers = {
        "ays", "ays+", "ays_30", "ays_30+", "gits", "beta_1_1",
    }
    scheduler_sources = {
        name: (
            "ComfyUI-ppm" if name in ppm_schedulers
            else "RES4LYF" if name in {"beta57", "bong_tangent"}
            else "Custom node"
        )
        for name in current_options["scheduler"][0]
        if name not in default_schedulers
    }

    state.update(
        apply_lora=comfy.sd.load_lora_for_models,
        chains={},
        checkpoint=CheckpointLoaderSimple(),
        clip_type=comfy.sd.CLIPType.STABLE_DIFFUSION,
        clips={},
        lora=LoraLoader(),
        load_checkpoint=comfy.sd.load_checkpoint_guess_config,
        load_clip=comfy.sd.load_clip,
        load_torch_file=comfy.utils.load_torch_file,
        model_management=comfy.sd.model_management,
        encode=CLIPTextEncode(),
        mask=ConditioningSetMask(),
        folders=folder_paths,
        loras={},
        models={},
        vae_loader=VAELoader(),
        vaes={},
        latent=EmptyLatentImage(),
        sample=KSampler(),
        align=AlignYourStepsScheduler(),
        basic_scheduler=BasicScheduler(),
        sampler_select=KSamplerSelect(),
        sample_custom=SamplerCustom(),
        decode=VAEDecode(),
        vae_encode=VAEEncode(),
        upscale=LatentUpscaleBy(),
        resize_latent=LatentUpscale(),
        upscale_image=ImageUpscaleWithModel(),
        upscale_model_loader=UpscaleModelLoader(),
        upscale_models={},
        unet=UNETLoader(),
        detector_provider=NODE_CLASS_MAPPINGS["UltralyticsDetectorProvider"](),
        detectors={},
        face_detailer=NODE_CLASS_MAPPINGS["FaceDetailer"](),
        attention_couple=NODE_CLASS_MAPPINGS["AttentionCouplePPM"](),
        clip_vision_loader=NODE_CLASS_MAPPINGS["CLIPVisionLoader"](),
        style_model_loader=NODE_CLASS_MAPPINGS["IPAdapterModelLoader"](),
        style_apply=NODE_CLASS_MAPPINGS["IPAdapterAdvanced"](),
        style_pipeline=None,
        custom_samplers=custom_samplers,
        custom_sampler_nodes=custom_sampler_nodes,
        default_samplers=default_samplers,
        default_schedulers=default_schedulers,
        default_upscale_methods=default_upscale_methods,
        sampler_sources=sampler_sources,
        scheduler_sources=scheduler_sources,
    )

    log("ComfyUI initialization complete")


def output_item(output):
    return getattr(output, "result", output)[0]


def sampler_names():
    options = state["sample"].INPUT_TYPES()["required"]
    return [*options["sampler_name"][0], *state["custom_samplers"]]


def scheduler_names():
    options = state["sample"].INPUT_TYPES()["required"]
    return [*options["scheduler"][0], ALIGN_SCHEDULER]


def select_sampler(model, seed, name):
    custom = state["custom_samplers"].get(name)
    if custom is None:
        return output_item(state["sampler_select"].get_sampler(name))

    node_name, sampler_name = custom
    node = state["custom_sampler_nodes"][node_name]
    if node_name == "PPMSamplerSelect":
        output = node.get_sampler(sampler_name=sampler_name, model=model)
    else:
        output = node.get_sampler(sampler_name=sampler_name)
    return output_item(output)


def run_sampler(

    model,

    seed,

    steps,

    cfg,

    sampler_name,

    scheduler,

    positive,

    negative,

    latent_image,

    denoise,

):
    custom_sampler = sampler_name in state["custom_samplers"]
    if scheduler != ALIGN_SCHEDULER and not custom_sampler:
        return state["sample"].sample(
            model=model,
            seed=seed,
            steps=steps,
            cfg=cfg,
            sampler_name=sampler_name,
            scheduler=scheduler,
            positive=positive,
            negative=negative,
            latent_image=latent_image,
            denoise=denoise,
        )[0]

    if scheduler == ALIGN_SCHEDULER:
        output = state["align"].get_sigmas(
            ALIGN_MODEL_TYPE,
            steps,
            denoise,
        )
    else:
        output = state["basic_scheduler"].get_sigmas(
            model,
            scheduler,
            steps,
            denoise,
        )
    sigmas = output_item(output)
    sampler = select_sampler(model, seed, sampler_name)
    output = state["sample_custom"].sample(
        model=model,
        add_noise=True,
        noise_seed=seed,
        cfg=cfg,
        positive=positive,
        negative=negative,
        sampler=sampler,
        sigmas=sigmas,
        latent_image=latent_image,
    )
    return output_item(output)


def convert_latent(samples, source_vae, target_vae):
    pixels = state["decode"].decode(vae=source_vae, samples=samples)[0]
    return state["vae_encode"].encode(vae=target_vae, pixels=pixels)[0]


def load_upscale_model(name):
    if name not in state["upscale_models"]:
        stage_model("upscale_models", name)
        log(f"Loading upscale model {name}")
        state["upscale_models"][name] = state[
            "upscale_model_loader"
        ].load_model(name)[0]
        log(f"Loaded upscale model {name}")
    return state["upscale_models"][name]


def load_detector(name):
    if name not in state["detectors"]:
        stage_model("ultralytics", name)
        log(f"Loading detector {name}")
        state["detectors"][name] = state["detector_provider"].doit(
            f"bbox/{name}"
        )[0]
        log(f"Loaded detector {name}")
    return state["detectors"][name]


def area_box(area):
    key = area.strip().lower()
    value = AREA_PRESETS.get(key, key)
    match = re.fullmatch(r"([a-e])([1-5])(?::([a-e])([1-5]))?", value)
    if not match:
        raise ValueError(f"Unsupported region area: {area}")
    left, top, right, bottom = match.groups()
    right = right or left
    bottom = bottom or top
    x1, x2 = sorted((ord(left) - ord("a"), ord(right) - ord("a")))
    y1, y2 = sorted((int(top) - 1, int(bottom) - 1))
    return (
        x1 / GRID_SIZE,
        y1 / GRID_SIZE,
        (x2 - x1 + 1) / GRID_SIZE,
        (y2 - y1 + 1) / GRID_SIZE,
    )


def prepare_regions(regions):
    if not regions:
        return []
    layout = AUTO_LAYOUTS[len(regions)]
    return [
        (
            region.prompt,
            *(layout[index] if region.area.strip().lower() == "auto"
              else area_box(region.area)),
            region.strength,
        )
        for index, region in enumerate(regions)
    ]


def region_mask(region, image_width, image_height):
    _, x, y, width, height, _ = region
    mask_width = image_width // LATENT_SCALE
    mask_height = image_height // LATENT_SCALE
    x, y, width, height, left, top, right, bottom = mask_box(
        x,
        y,
        width,
        height,
        mask_width,
        mask_height,
    )
    mask = torch.zeros((1, mask_height, mask_width))
    area = mask[:, y:y + height, x:x + width]
    area.fill_(1)
    if left:
        area[:, :, :left] *= torch.linspace(1 / left, 1, left)
    if top:
        area[:, :top, :] *= torch.linspace(1 / top, 1, top).view(1, -1, 1)
    if right:
        area[:, :, -right:] *= torch.linspace(1, 1 / right, right)
    if bottom:
        area[:, -bottom:, :] *= torch.linspace(1, 1 / bottom, bottom).view(
            1,
            -1,
            1,
        )
    return mask


def scale_conditioning(conditioning, strength):
    return [
        [item[0], {**item[1], "strength": strength}]
        for item in conditioning
    ]


def encode_positive(

    model,

    clip,

    prompt,

    regions,

    image_width,

    image_height,

    regional_mode,

):
    positive = state["encode"].encode(clip=clip, text=prompt)[0]
    if not regions:
        return model, positive
    if regional_mode == "conditioning":
        positive = [
            [
                item[0],
                {
                    **item[1],
                    "start_percent": ENVIRONMENT_START,
                    "end_percent": 1,
                    "strength": REGIONAL_GLOBAL_STRENGTH,
                },
            ]
            for item in positive
        ]
    conditionings = []
    masks = []
    for region in regions:
        region_prompt, _, _, _, _, strength = region
        conditioning = state["encode"].encode(
            clip=clip,
            text=region_prompt,
        )[0]
        mask = region_mask(region, image_width, image_height)
        if regional_mode == "attention":
            conditionings.append(scale_conditioning(conditioning, strength))
            masks.append(mask)
            continue
        conditioning = state["mask"].append(
            conditioning=conditioning,
            mask=mask,
            set_cond_area="mask bounds",
            strength=strength,
        )[0]
        positive += conditioning
    if regional_mode == "conditioning":
        return model, positive

    inputs = {
        "model": model,
        "base_cond": scale_conditioning(
            positive,
            REGIONAL_GLOBAL_STRENGTH,
        ),
        "base_mask": torch.ones_like(masks[0]),
    }
    for index, (conditioning, mask) in enumerate(
        zip(conditionings, masks),
        1,
    ):
        inputs[f"cond_{index}"] = conditioning
        inputs[f"mask_{index}"] = mask
    output = state["attention_couple"].execute(**inputs)
    return getattr(output, "result", output)[0], inputs["base_cond"]


def has_style(request):
    return bool(
        getattr(request, "style_images", [])
        or getattr(request, "second_style_images", [])
    )


def wants_style(request):
    return has_style(request) or getattr(request, "instant_style", False)


def style_stage_enabled(request, stage):
    first = bool(getattr(request, "style_images", []))
    second = bool(getattr(request, "second_style_images", []))
    if stage == "first":
        return first
    if stage == "second":
        return second or (
            first and request.style_scope in ("generation", "all")
        )
    return first and request.style_scope == "all"


def decode_style_images(values):
    images = []
    for value in values:
        encoded = value.partition(",")[2] if value.startswith("data:") else value
        if len(encoded) > (MAX_STYLE_IMAGE_SIZE + 2) // 3 * 4:
            raise ValueError("InstantStyle image exceeds 20 MiB")
        try:
            data = base64.b64decode(encoded, validate=True)
        except (ValueError, binascii.Error) as error:
            raise ValueError("Invalid InstantStyle image") from error
        if len(data) > MAX_STYLE_IMAGE_SIZE:
            raise ValueError("InstantStyle image exceeds 20 MiB")
        try:
            with Image.open(BytesIO(data)) as image:
                image = ImageOps.fit(
                    image.convert("RGB"),
                    (STYLE_IMAGE_SIZE, STYLE_IMAGE_SIZE),
                    Image.Resampling.LANCZOS,
                )
                images.append(np.asarray(image, dtype=np.float32) / 255)
        except Exception as error:
            raise ValueError("Invalid InstantStyle image") from error
    return torch.from_numpy(np.stack(images))


def load_style_pipeline():
    if state["style_pipeline"] is None:
        ipadapter = state["style_model_loader"].load_ipadapter_model(
            STYLE_IPADAPTER
        )[0]
        clip_vision = state["clip_vision_loader"].load_clip(
            STYLE_CLIP_VISION
        )[0]
        state["style_pipeline"] = ipadapter, clip_vision
    return state["style_pipeline"]


def apply_style(request, model, image, stage):
    if not style_stage_enabled(request, stage):
        return model
    ipadapter, clip_vision = load_style_pipeline()
    second = stage == "second" and request.second_style_images
    return state["style_apply"].apply_ipadapter(
        model=model,
        ipadapter=ipadapter,
        clip_vision=clip_vision,
        image=image,
        weight=request.second_style_weight if second else request.style_weight,
        weight_type=STYLE_WEIGHT_TYPE,
        combine_embeds="average",
        start_at=0,
        end_at=request.second_style_end if second else request.style_end,
        embeds_scaling=STYLE_EMBEDS_SCALING,
    )[0]


def run_detailers(

    request,

    image,

    base_model,

    base_clip,

    base_vae,

    seeds,

    style_image,

):
    final_prompt = request.second_prompt or request.prompt \
        if request.upscale else request.prompt
    final_negative = request.second_negative or request.negative \
        if request.upscale else request.negative
    for detailer, seed in zip(request.detailers, seeds):
        if detailer.model:
            model, clip = load_chain(detailer.model, [])
            vae = load_vae(vae_name(detailer.model))
            prompt, negative_prompt = request.prompt, request.negative
        else:
            model, clip, vae = base_model, base_clip, base_vae
            prompt, negative_prompt = final_prompt, final_negative
        model = apply_style(request, model, style_image, "detailer")
        positive = state["encode"].encode(
            clip=clip,
            text=detailer.prompt or prompt,
        )[0]
        negative = state["encode"].encode(
            clip=clip,
            text=detailer.negative or negative_prompt,
        )[0]
        image = state["face_detailer"].doit(
            image=image,
            model=model,
            clip=clip,
            vae=vae,
            guide_size=DETAILER_GUIDE_SIZE,
            guide_size_for=True,
            max_size=DETAILER_MAX_SIZE,
            seed=seed,
            steps=detailer.steps,
            cfg=detailer.cfg,
            sampler_name=detailer.sampler,
            scheduler=detailer.scheduler,
            positive=positive,
            negative=negative,
            denoise=detailer.denoise,
            feather=DETAILER_FEATHER,
            noise_mask=True,
            force_inpaint=True,
            bbox_threshold=DETAILER_THRESHOLD,
            bbox_dilation=DETAILER_DILATION,
            bbox_crop_factor=DETAILER_CROP,
            sam_detection_hint="none",
            sam_dilation=0,
            sam_threshold=.93,
            sam_bbox_expansion=0,
            sam_mask_hint_threshold=.7,
            sam_mask_hint_use_negative="False",
            drop_size=DETAILER_DROP_SIZE,
            bbox_detector=load_detector(detailer.detector),
            wildcard="",
            cycle=1,
        )[0]
    return image


@spaces.GPU(duration=GPU_DURATION)
def infer_image(

    request,

    regions,

    latent,

    first_seed,

    second_seed,

    detailer_seeds,

    style_image,

    second_style_image,

):
    request = DirectRequest.model_validate(request)
    with torch.inference_mode(), warnings.catch_warnings():
        warnings.filterwarnings(
            "ignore",
            message=r"Should have t[ab](?:<=|>=)t[01] but got",
            category=UserWarning,
            module=r"torchsde\._brownian\.brownian_interval",
        )

        first_model = request.model
        second_model = request.second_model or first_model
        first_vae = load_vae(vae_name(first_model))
        second_vae = load_vae(vae_name(second_model))
        base_model, clip = load_chain(first_model, request.loras)
        model = apply_style(request, base_model, style_image, "first")
        model, positive = encode_positive(
            model,
            clip,
            request.prompt,
            regions,
            request.width,
            request.height,
            request.regional_mode,
        )
        negative = state["encode"].encode(clip=clip, text=request.negative)[0]
        samples = run_sampler(
            model,
            first_seed,
            request.steps,
            request.cfg,
            request.sampler,
            request.scheduler,
            positive,
            negative,
            latent,
            1,
        )

        if request.upscale:
            width, height = upscale_size(
                request.width,
                request.height,
                request.upscale_scale,
            )
            samples = state["resize_latent"].upscale(
                samples=samples,
                upscale_method=request.upscale_method,
                width=width,
                height=height,
                crop="disabled",
            )[0]
            if is_anima_model(first_model) != is_anima_model(second_model):
                samples = convert_latent(samples, first_vae, second_vae)
            if request.second_model:
                base_model, clip = load_chain(
                    request.second_model,
                    request.second_loras,
                )
            model = apply_style(
                request,
                base_model,
                second_style_image,
                "second",
            )
            model, positive = encode_positive(
                model,
                clip,
                request.second_prompt or request.prompt,
                regions,
                width,
                height,
                request.regional_mode,
            )
            negative = state["encode"].encode(
                clip=clip,
                text=request.second_negative or request.negative,
            )[0]

            samples = run_sampler(
                model,
                second_seed,
                request.second_steps,
                request.second_cfg,
                request.second_sampler,
                request.second_scheduler,
                positive,
                negative,
                samples,
                request.denoise,
            )

        image = state["decode"].decode(
            vae=second_vae if request.upscale else first_vae,
            samples=samples,
        )[0]

        image = run_detailers(
            request,
            image,
            base_model,
            clip,
            second_vae if request.upscale else first_vae,
            detailer_seeds,
            style_image,
        )
        if request.upscale and request.upscale_model:
            image = state["upscale_image"].upscale(
                upscale_model=load_upscale_model(request.upscale_model),
                image=image,
            )[0]

        return image


@spaces.GPU(duration=UPSCALE_GPU_DURATION)
def infer_upscale(image, model):
    if state["model_management"].get_torch_device().type != "cuda":
        raise RuntimeError("CUDA GPU is required for upscaling")
    with torch.inference_mode():
        try:
            return state["upscale_image"].upscale(
                upscale_model=model,
                image=image,
            )[0].cpu()
        finally:
            model.to(CPU_DEVICE)


@spaces.GPU(duration=3)
def infer_ping(seed):
    with torch.inference_mode():
        model, vae, latent, positive, negative = state["ping"]
        samples = run_sampler(
            model,
            seed,
            PING_STEPS,
            1,
            PING_SAMPLER,
            PING_SCHEDULER,
            positive,
            negative,
            latent,
            1,
        )
        return state["decode"].decode(vae=vae, samples=samples)[0]


def generate_gpu(request, regions, style_image, second_style_image):
    latent = state["latent"].generate(
        width=request.width,
        height=request.height,
        batch_size=request.batch_size,
    )[0]
    first_seed = secrets.randbits(64)
    second_seed = secrets.randbits(64) if request.upscale else None
    detailer_seeds = [secrets.randbits(64) for _ in request.detailers]
    image = retry_gpu(
        lambda: infer_image(
            request.model_dump(),
            regions,
            latent,
            first_seed,
            second_seed,
            detailer_seeds,
            style_image,
            second_style_image,
        )
    )
    return image, first_seed, second_seed, detailer_seeds


def load_model(name):
    if name in state["models"]:
        return state["models"][name]
    stage_model(model_kind(name), name)
    if is_anima_model(name):
        stage_model("clip", ANIMA_CLIP)
        if ANIMA_CLIP not in state["clips"]:
            log(f"Loading text encoder {ANIMA_CLIP}")
            path = state["folders"].get_full_path_or_raise(
                COMFY_KINDS["clip"], ANIMA_CLIP
            )
            state["clips"][ANIMA_CLIP] = state["load_clip"](
                [path],
                embedding_directory=state["folders"].get_folder_paths(
                    "embeddings"
                ),
                clip_type=state["clip_type"],
                model_options={"initial_device": CPU_DEVICE},
            )
        log(f"Loading diffusion model {name}")
        model = state["unet"].load_unet(
            unet_name=name,
            weight_dtype="default",
        )[0]
        state["models"][name] = model, state["clips"][ANIMA_CLIP]
    else:
        log(f"Loading checkpoint {name}")
        path = state["folders"].get_full_path_or_raise("checkpoints", name)
        initial_device = state["model_management"].unet_inital_load_device
        state["model_management"].unet_inital_load_device = (
            lambda *_: CPU_DEVICE
        )
        try:
            state["models"][name] = state["load_checkpoint"](
                path,
                output_vae=False,
                embedding_directory=state["folders"].get_folder_paths(
                    "embeddings"
                ),
                te_model_options={"initial_device": CPU_DEVICE},
            )[:2]
        finally:
            state["model_management"].unet_inital_load_device = initial_device
    log(f"Loaded {name}")
    return state["models"][name]


def load_lora(name):
    if name not in state["loras"]:
        stage_model("loras", name)
        log(f"Loading LoRA {name}")
        path = state["folders"].get_full_path_or_raise("loras", name)
        state["loras"][name] = state["load_torch_file"](
            path,
            safe_load=True,
            return_metadata=True,
        )
        log(f"Loaded LoRA {name}")
    return state["loras"][name]


def load_vae(name):
    if name not in state["vaes"]:
        stage_model("vae", name)
        log(f"Loading VAE {name}")
        state["vaes"][name] = state["vae_loader"].load_vae(
            vae_name=name
        )[0]
        log(f"Loaded VAE {name}")
    return state["vaes"][name]


def chain_key(model_name, loras):
    return (
        model_name,
        tuple((lora.name, lora.strength, lora.clip) for lora in loras),
    )


def load_chain(model_name, loras):
    key = chain_key(model_name, loras)
    if key in state["chains"]:
        return state["chains"][key]

    model, clip = load_model(model_name)

    for lora in loras:
        data, metadata = load_lora(lora.name)
        model, clip = state["apply_lora"](
            model,
            clip,
            data,
            lora.strength,
            lora.clip,
            lora_metadata=metadata,
        )

    state["chains"][key] = model, clip
    return state["chains"][key]


def stage_model(kind, name):
    target = model_path(kind, name)
    if not target.is_file():
        stage_models([(kind, name)])
    return target


def stage_models(models):
    models = list(dict.fromkeys(models))
    if any(is_anima_model(name) for kind, name in models if kind in (
        "checkpoints", "diffusion_models",
    )):
        models.append(("clip", ANIMA_CLIP))
    download_assets(list(dict.fromkeys(models)))


def stage_request_models(request):
    models = [(model_kind(request.model), request.model)]
    models.extend(("loras", lora.name) for lora in request.loras)
    models.append(("vae", vae_name(request.model)))
    if wants_style(request):
        models.extend((
            ("ipadapter", STYLE_IPADAPTER),
            ("clip_vision", STYLE_CLIP_VISION),
        ))

    if request.upscale:
        if request.upscale_model:
            models.append(("upscale_models", request.upscale_model))
        if request.second_model:
            models.append((model_kind(request.second_model), request.second_model))
            models.extend(("loras", lora.name) for lora in request.second_loras)
        models.append(("vae", vae_name(request.second_model or request.model)))

    for detailer in request.detailers:
        models.append(("ultralytics", detailer.detector))
        if detailer.model:
            models.extend((
                (model_kind(detailer.model), detailer.model),
                ("vae", vae_name(detailer.model)),
            ))

    stage_models(models)


def request_models_loaded(request):
    chains = [chain_key(request.model, request.loras)]
    vaes = [vae_name(request.model)]
    upscalers = []
    detectors = []

    if request.upscale:
        if request.upscale_model:
            upscalers.append(request.upscale_model)
        if request.second_model:
            chains.append(chain_key(request.second_model, request.second_loras))
        vaes.append(vae_name(request.second_model or request.model))

    for detailer in request.detailers:
        detectors.append(detailer.detector)
        if detailer.model:
            chains.append(chain_key(detailer.model, []))
            vaes.append(vae_name(detailer.model))

    return (
        all(key in state["chains"] for key in chains)
        and (not wants_style(request) or state["style_pipeline"] is not None)
        and all(name in state["vaes"] for name in vaes)
        and all(name in state["upscale_models"] for name in upscalers)
        and all(name in state["detectors"] for name in detectors)
    )


def unloaded_model_counts(request):
    checkpoints = {request.model}
    loras = {lora.name for lora in request.loras}

    if request.upscale and request.second_model:
        checkpoints.add(request.second_model)
        loras.update(lora.name for lora in request.second_loras)

    checkpoints.update(
        detailer.model for detailer in request.detailers if detailer.model
    )
    return {
        "checkpoints": sum(name not in state["models"] for name in checkpoints),
        "loras": sum(name not in state["loras"] for name in loras),
    }


def load_request_models(request):
    stage_request_models(request)
    load_vae(vae_name(request.model))
    load_vae(vae_name(request.second_model or request.model))
    load_chain(request.model, request.loras)
    if wants_style(request):
        load_style_pipeline()
    if request.upscale and request.upscale_model:
        load_upscale_model(request.upscale_model)
    if request.upscale and request.second_model:
        load_chain(request.second_model, request.second_loras)
    for detailer in request.detailers:
        load_detector(detailer.detector)
        if detailer.model:
            load_vae(vae_name(detailer.model))
            load_chain(detailer.model, [])


def stored_image_tensor(path):
    with Image.open(BytesIO(stored_bytes(path))) as image:
        pixels = np.asarray(image.convert("RGB"), dtype=np.float32) / 255
    return torch.from_numpy(pixels).unsqueeze(0)


def tensor_image(image):
    return Image.fromarray(
        np.clip(
            image[0].detach().cpu().numpy() * 255,
            0,
            255,
        ).astype(np.uint8)
    )


def generate_comparison_cell(request, latent):
    first_name = request.first[1]
    second_name = request.second[1]
    first_vae = state["vaes"][vae_name(first_name)]
    second_vae = state["vaes"][vae_name(second_name)]
    first_model, first_clip = state["models"][first_name]
    positive = state["encode"].encode(
        clip=first_clip,
        text=request.positive,
    )[0]
    negative = state["encode"].encode(
        clip=first_clip,
        text=request.negative,
    )[0]
    samples = run_sampler(
        first_model,
        int(request.generation[1:]),
        MATRIX_FIRST_STEPS,
        MATRIX_FIRST_CFG,
        request.sampler,
        request.scheduler,
        positive,
        negative,
        latent,
        1,
    )
    samples = state["upscale"].upscale(
        samples=samples,
        upscale_method=MATRIX_UPSCALE_METHOD,
        scale_by=MATRIX_UPSCALE_SCALE,
    )[0]
    if is_anima_model(first_name) != is_anima_model(second_name):
        samples = convert_latent(samples, first_vae, second_vae)

    second_model, second_clip = state["models"][second_name]
    positive = state["encode"].encode(
        clip=second_clip,
        text=request.positive,
    )[0]
    negative = state["encode"].encode(
        clip=second_clip,
        text=request.negative,
    )[0]
    samples = run_sampler(
        second_model,
        int(request.generation[1:]),
        MATRIX_SECOND_STEPS,
        MATRIX_SECOND_CFG,
        request.second_sampler,
        request.second_scheduler,
        positive,
        negative,
        samples,
        MATRIX_DENOISE,
    )
    image = state["decode"].decode(
        vae=second_vae,
        samples=samples,
    )[0]
    return image


@spaces.GPU(duration=GPU_DURATION)
def infer_matrix_cell(request, pixels=None, latent=None):
    with torch.inference_mode():
        first_id, first_name = request.first
        second_id, second_name = request.second
        first_vae = state["vaes"][vae_name(first_name)]
        second_vae = state["vaes"][vae_name(second_name)]

        if request.output:
            return generate_comparison_cell(request, latent)

        if first_id == second_id:
            model, clip = state["models"][first_name]
            positive = state["encode"].encode(
                clip=clip,
                text=request.positive,
            )[0]
            negative = state["encode"].encode(
                clip=clip,
                text=request.negative,
            )[0]
            samples = run_sampler(
                model,
                int(request.generation[1:]),
                MATRIX_FIRST_STEPS,
                MATRIX_FIRST_CFG,
                MATRIX_SAMPLER,
                MATRIX_SCHEDULER,
                positive,
                negative,
                latent,
                1,
            )
            image = state["decode"].decode(
                vae=first_vae,
                samples=samples,
            )[0]
            return image

        samples = state["vae_encode"].encode(
            vae=first_vae,
            pixels=pixels,
        )[0]
        samples = state["upscale"].upscale(
            samples=samples,
            upscale_method=MATRIX_UPSCALE_METHOD,
            scale_by=MATRIX_UPSCALE_SCALE,
        )[0]
        if is_anima_model(first_name) != is_anima_model(second_name):
            samples = convert_latent(samples, first_vae, second_vae)
        model, clip = state["models"][second_name]
        positive = state["encode"].encode(
            clip=clip,
            text=request.positive,
        )[0]
        negative = state["encode"].encode(
            clip=clip,
            text=request.negative,
        )[0]
        samples = run_sampler(
            model,
            int(request.generation[1:]),
            MATRIX_SECOND_STEPS,
            MATRIX_SECOND_CFG,
            MATRIX_SAMPLER,
            MATRIX_SCHEDULER,
            positive,
            negative,
            samples,
            MATRIX_DENOISE,
        )
        image = state["decode"].decode(
            vae=second_vae,
            samples=samples,
        )[0]
        return image


def generate_matrix_cell(body):
    request = MatrixCellRequest.model_validate_json(body)
    first_id, first_name = request.first
    second_id, second_name = request.second
    folder = (IMAGE_DIR / request.folder).resolve()
    if folder.parent != IMAGE_DIR.resolve():
        raise ValueError("Invalid matrix folder")

    with lock:
        init_comfy()
        models = [
            ("vae", vae_name(first_name)),
            ("vae", vae_name(second_name)),
        ]
        if request.output:
            models.extend((
                (model_kind(first_name), first_name),
                (model_kind(second_name), second_name),
            ))
        else:
            name = first_name if first_id == second_id else second_name
            models.append((model_kind(name), name))
        stage_models(models)
        load_vae(vae_name(first_name))
        load_vae(vae_name(second_name))

        if request.output:
            output = (folder / request.output).resolve()
            if output.parent != folder:
                raise ValueError("Invalid matrix output")
            if output.is_file():
                return request.output
            load_model(first_name)
            load_model(second_name)
            latent = state["latent"].generate(
                width=MATRIX_WIDTH,
                height=MATRIX_HEIGHT,
                batch_size=1,
            )[0]
            image = infer_matrix_cell(request, latent=latent)
            result = request.output
        else:
            output = matrix_image_path(
                request.folder,
                first_id,
                second_id,
                request.generation,
            )
            if output.is_file():
                return (
                    first_id
                    if first_id == second_id
                    else f"{first_id}-{second_id}"
                )
            if first_id == second_id:
                load_model(first_name)
                latent = state["latent"].generate(
                    width=MATRIX_WIDTH,
                    height=MATRIX_HEIGHT,
                    batch_size=1,
                )[0]
                image = infer_matrix_cell(request, latent=latent)
                result = first_id
            else:
                diagonal = matrix_image_path(
                    request.folder,
                    first_id,
                    first_id,
                    request.generation,
                )
                pixels = stored_image_tensor(diagonal)
                load_model(second_name)
                image = infer_matrix_cell(request, pixels)
                result = f"{first_id}-{second_id}"

        save_named_image(tensor_image(image), output)
        return result


def model_options(kind, local):
    local = [local] if isinstance(local, str) else local
    names = set(local) | remote_models[kind].keys()
    return sorted(
        (name for name in names if Path(name).suffix.casefold() in MODEL_SUFFIXES),
        key=str.casefold,
    )


def generate_images(request, from_api=True):
    resolve_artist_prompts(request)
    if (
        request.width % 8
        or request.height % 8
    ):
        raise ValueError("Width and height must be multiples of 8")
    if request.regional_mode not in REGIONAL_MODES:
        raise ValueError("Unsupported regional mode")
    if request.style_images and request.style_scope not in STYLE_SCOPES:
        raise ValueError("Unsupported InstantStyle scope")
    if has_style(request):
        style_models = [request.model] if request.style_images else []
        if request.upscale and (
            request.second_style_images
            or (
                request.style_images and request.style_scope != "first"
            )
        ):
            style_models.append(request.second_model or request.model)
        if request.style_images and request.style_scope == "all":
            final_model = (
                request.second_model
                if request.upscale and request.second_model
                else request.model
            )
            style_models.extend(
                detailer.model or final_model
                for detailer in request.detailers
            )
        if any(is_anima_model(name) for name in style_models):
            raise ValueError("InstantStyle only supports SDXL models")

    regions = prepare_regions(request.regions)
    style_image = (
        decode_style_images(request.style_images)
        if request.style_images
        else None
    )
    second_style_image = (
        decode_style_images(request.second_style_images)
        if request.second_style_images
        else style_image
    )

    with lock:
        init_comfy()
        detailer_options = state["face_detailer"].INPUT_TYPES()["required"]
        samplers = [request.sampler]
        schedulers = [request.scheduler]
        if request.upscale:
            samplers.append(request.second_sampler)
            schedulers.append(request.second_scheduler)
        if any(value not in sampler_names() for value in samplers):
            raise ValueError("Unsupported sampler or scheduler")
        if any(value not in scheduler_names() for value in schedulers):
            raise ValueError("Unsupported sampler or scheduler")
        if any(
            detailer.sampler not in detailer_options["sampler_name"][0]
            or detailer.scheduler not in detailer_options["scheduler"][0]
            for detailer in request.detailers
        ):
            raise ValueError("Unsupported detailer sampler or scheduler")

        first_vae = vae_name(request.model)
        second_vae = vae_name(request.second_model or request.model)
        load_request_models(request)

        image, first_seed, second_seed, detailer_seeds = generate_gpu(
            request,
            regions,
            style_image,
            second_style_image,
        )

        metadata_config = request.model_dump(exclude={"sillytavern"})
        if request.style_images:
            metadata_config["style_images"] = [
                f"style-reference-{index}.png"
                for index in range(1, len(request.style_images) + 1)
            ]
        if request.second_style_images:
            metadata_config["second_style_images"] = [
                f"second-style-reference-{index}.png"
                for index in range(1, len(request.second_style_images) + 1)
            ]
        metadata = image_metadata(
            metadata_config,
            [first_seed, second_seed],
            detailer_seeds,
            [
                vae_name(detailer.model)
                if detailer.model
                else second_vae if request.upscale else first_vae
                for detailer in request.detailers
            ],
            [first_vae, second_vae],
            regions,
            ENVIRONMENT_START,
            REGIONAL_GLOBAL_STRENGTH,
        )
        if request.sillytavern:
            metadata["sillytavern"] = json.dumps(
                request.sillytavern,
                separators=(",", ":"),
            )
        results = [
            Image.fromarray(
                np.clip(item.detach().numpy() * 255, 0, 255).astype(np.uint8)
            )
            for item in image
        ]
        for result in results:
            result.info.update(metadata)
            backup_pool.submit(archive_image, result, from_api)
        return results


def combine_images(images, width, height):
    if len(images) == 1:
        return images[0]
    vertical = width > height
    image_width, image_height = images[0].size
    size = (
        (image_width, image_height * len(images))
        if vertical
        else (image_width * len(images), image_height)
    )
    combined = Image.new(images[0].mode, size)
    for index, image in enumerate(images):
        combined.paste(
            image,
            (0, index * image_height) if vertical else (index * image_width, 0),
        )
    combined.info.update(images[0].info)
    return combined


def scale_image(image, scale):
    if scale == 1:
        return image
    return image.resize(
        (round(image.width * scale), round(image.height * scale)),
        Image.Resampling.LANCZOS,
    )


def archive_image(image, from_api):
    try:
        if HAS_DATA_MOUNT:
            save_image(image, from_api)
        else:
            upload_image(image)
    except Exception as error:
        log(f"Archive failed: {error}")


def image_fingerprint(image):
    pixels = np.asarray(
        ImageOps.exif_transpose(image).convert("RGB").resize(
            (DUPLICATE_HASH_SIZE + 1, DUPLICATE_HASH_SIZE),
            Image.Resampling.LANCZOS,
        )
    )
    differences = pixels[:, 1:] > pixels[:, :-1]
    return (
        int.from_bytes(np.packbits(differences).tobytes()),
        tuple(int(value) for value in pixels.mean(axis=(0, 1))),
    )


def image_png_bytes(image):
    pnginfo = PngInfo()
    for key, value in image.info.items():
        if isinstance(value, str):
            pnginfo.add_text(key, value)
    return png_bytes(image, pnginfo)


@lru_cache(maxsize=IMAGE_KEY_CACHE_SIZE)
def image_key(salt):
    return Scrypt(
        salt=salt,
        length=32,
        n=SCRYPT_N,
        r=8,
        p=1,
    ).derive(PASSWORD.encode())


def proxy_key(salt):
    return Scrypt(
        salt=salt,
        length=32,
        n=SCRYPT_N,
        r=8,
        p=1,
    ).derive(PASSWORD.encode())


def encrypt_proxy_payload(data):
    salt = os.urandom(SALT_SIZE)
    nonce = os.urandom(NONCE_SIZE)
    return (
        PROXY_MAGIC
        + salt
        + nonce
        + AESGCM(proxy_key(salt)).encrypt(nonce, data, PROXY_MAGIC)
    )


def decrypt_proxy_payload(data):
    if len(data) < len(PROXY_MAGIC) + SALT_SIZE + NONCE_SIZE + 16:
        raise ValueError("invalid encrypted payload")
    if not data.startswith(PROXY_MAGIC):
        raise ValueError("invalid encrypted payload")
    salt_start = len(PROXY_MAGIC)
    nonce_start = salt_start + SALT_SIZE
    data_start = nonce_start + NONCE_SIZE
    return AESGCM(proxy_key(data[salt_start:nonce_start])).decrypt(
        data[nonce_start:data_start],
        data[data_start:],
        PROXY_MAGIC,
    )


def save_image(image, from_api):
    path = IMAGE_DIR / datetime.now(TIMEZONE).date().isoformat()
    path.mkdir(parents=True, exist_ok=True)
    suffix = "ST.epng" if from_api else "C.epng"
    number = max(
        (
            int(file.name.removesuffix(suffix))
            for file in path.iterdir()
            if file.name.endswith(suffix)
            and file.name.removesuffix(suffix).isdigit()
        ),
        default=0,
    ) + 1
    save_named_image(image, path / f"{number}{suffix}")


def upload_image(image):
    date = datetime.now(TIMEZONE).date().isoformat()
    folder = f"{IMAGE_BUCKET_PREFIX}/{date}"
    suffix = "ST.epng"
    number = max(
        (
            int(Path(item.path).name.removesuffix(suffix))
            for item in list_bucket_tree(
                IMAGE_BUCKET_ID,
                prefix=f"{folder}/",
                recursive=True,
                token=IMAGE_TOKEN,
            )
            if item.type == "file"
            and Path(item.path).name.endswith(suffix)
            and Path(item.path).name.removesuffix(suffix).isdigit()
        ),
        default=0,
    ) + 1
    batch_bucket_files(
        IMAGE_BUCKET_ID,
        add=[(encrypted_image_bytes(image), f"{folder}/{number}{suffix}")],
        token=IMAGE_TOKEN,
    )


def encrypted_image_bytes(image):
    return encrypt_image_data(image_png_bytes(image))


def encrypt_image_data(data, salt=None):
    salt = salt if salt is not None else os.urandom(SALT_SIZE)
    nonce = os.urandom(NONCE_SIZE)
    encrypted = AESGCM(image_key(salt)).encrypt(nonce, data, FILE_MAGIC)
    return FILE_MAGIC + salt + nonce + encrypted


def save_named_image(image, output):
    output.parent.mkdir(parents=True, exist_ok=True)
    temp = output.with_suffix(".epng.part")
    temp.write_bytes(encrypted_image_bytes(image))
    temp.replace(output)
    log(f"Saved img/{output.relative_to(IMAGE_DIR)}")


def png_bytes(image, pnginfo=None):
    data = BytesIO()
    image.save(data, format="PNG", pnginfo=pnginfo)
    return data.getvalue()


def stored_path(value):
    root = IMAGE_DIR.resolve()
    path = (root / value).resolve()
    try:
        path.relative_to(root)
    except ValueError as error:
        raise HTTPException(404, "image not found") from error
    if not path.is_file() or path.suffix.casefold() not in IMAGE_SUFFIXES:
        raise HTTPException(404, "image not found")
    return path


def natural_key(path):
    return [
        int(part) if part.isdigit() else part.casefold()
        for part in re.split(r"(\d+)", path.name)
    ]


def star_database():
    STAR_DB.parent.mkdir(parents=True, exist_ok=True)
    database = sqlite3.connect(STAR_DB, timeout=EXPLORER_DB_TIMEOUT)
    database.execute("PRAGMA journal_mode=WAL")
    database.execute(
        "CREATE TABLE IF NOT EXISTS stars (path TEXT PRIMARY KEY)"
    )
    database.execute(
        """CREATE TABLE IF NOT EXISTS image_prompts (

            path TEXT PRIMARY KEY,

            modified INTEGER NOT NULL,

            size INTEGER NOT NULL,

            prompt TEXT NOT NULL,

            second_prompt TEXT NOT NULL,

            artists TEXT NOT NULL

        )"""
    )
    database.execute(
        """CREATE TABLE IF NOT EXISTS image_previews (

            path TEXT PRIMARY KEY,

            modified INTEGER NOT NULL,

            size INTEGER NOT NULL,

            data BLOB NOT NULL

        )"""
    )
    return database


def stored_prompt(path, relative, database):
    stat = path.stat()
    cached = database.execute(
        """SELECT prompt, second_prompt, artists

        FROM image_prompts

        WHERE path = ? AND modified = ? AND size = ?""",
        (relative, stat.st_mtime_ns, stat.st_size),
    ).fetchone()
    if cached is not None:
        return cached[0], cached[1], json.loads(cached[2])
    prompt = ""
    second_prompt = ""
    artists = []
    try:
        with Image.open(BytesIO(stored_bytes(path))) as image:
            parameters = json.loads(image.info.get("parameters", "{}"))
        prompt = str(parameters.get("prompt", ""))
        second_prompt = str(parameters.get("second_prompt") or (prompt if parameters.get("upscale") else ""))
        selected = parameters.get("selected_artists", [])
        if isinstance(selected, list):
            artists = list(dict.fromkeys(
                artist for artist in selected if isinstance(artist, str)
            ))
    except (json.JSONDecodeError, OSError, TypeError, ValueError):
        pass
    database.execute(
        """INSERT OR REPLACE INTO image_prompts

        (path, modified, size, prompt, second_prompt, artists)

        VALUES (?, ?, ?, ?, ?, ?)""",
        (
            relative,
            stat.st_mtime_ns,
            stat.st_size,
            prompt,
            second_prompt,
            json.dumps(artists, separators=(",", ":")),
        ),
    )
    return prompt, second_prompt, artists


def starred_paths():
    database = star_database()
    try:
        return {row[0] for row in database.execute("SELECT path FROM stars")}
    finally:
        database.close()


def set_star(path, starred):
    database = star_database()
    try:
        if starred:
            database.execute("INSERT OR IGNORE INTO stars VALUES (?)", (path,))
        else:
            database.execute("DELETE FROM stars WHERE path = ?", (path,))
        database.commit()
    finally:
        database.close()


def stored_bytes(path):
    return decrypt_image_data(path.read_bytes())


def decrypt_image_data(data):
    if not data.startswith(FILE_MAGIC):
        raise HTTPException(500, "invalid encrypted image")
    salt_start = len(FILE_MAGIC)
    nonce_start = salt_start + SALT_SIZE
    data_start = nonce_start + NONCE_SIZE
    return AESGCM(image_key(data[salt_start:nonce_start])).decrypt(
        data[nonce_start:data_start],
        data[data_start:],
        FILE_MAGIC,
    )


@lru_cache(maxsize=PREVIEW_CACHE_SIZE)
def stored_preview(path, modified, size):
    relative = path.relative_to(IMAGE_DIR.resolve()).as_posix()
    database = star_database()
    try:
        cached = database.execute(
            """SELECT data FROM image_previews

            WHERE path = ? AND modified = ? AND size = ?""",
            (relative, modified, size),
        ).fetchone()
        if cached is not None:
            return decrypt_image_data(cached[0])
        source = path.read_bytes()
        with Image.open(BytesIO(decrypt_image_data(source))) as image:
            image.thumbnail((PREVIEW_SIZE, PREVIEW_SIZE))
            data = BytesIO()
            image.save(
                data,
                format="WEBP",
                quality=PREVIEW_QUALITY,
                method=0,
            )
        preview = data.getvalue()
        salt = source[len(FILE_MAGIC):len(FILE_MAGIC) + SALT_SIZE]
        database.execute(
            "INSERT OR REPLACE INTO image_previews VALUES (?, ?, ?, ?)",
            (relative, modified, size, encrypt_image_data(preview, salt)),
        )
        database.commit()
        return preview
    finally:
        database.close()


def reserve_matrix_grid(folder):
    path = IMAGE_DIR / folder
    path.mkdir(parents=True, exist_ok=True)
    with matrix_lock:
        number = 1
        while (
            (path / f"{number}gr.epng").exists()
            or (folder, number) in matrix_grids
        ):
            number += 1
        matrix_grids.add((folder, number))
    return number


def matrix_models():
    with lock:
        init_comfy()
        inputs = state["checkpoint"].INPUT_TYPES()["required"]
        names = model_options("checkpoints", inputs["ckpt_name"][0])

    models = []
    ids = set()
    for name in names:
        match = re.match(r"(\d+)_", name)
        if match is None:
            raise ValueError(f"Checkpoint has no numeric prefix: {name}")
        model_id = str(int(match.group(1)))
        if model_id in ids:
            raise ValueError(f"Duplicate checkpoint prefix: {model_id}")
        ids.add(model_id)
        models.append((model_id, name))
    return sorted(models, key=lambda item: int(item[0]))


def matrix_options():
    with lock:
        init_comfy()
        options = state["sample"].INPUT_TYPES()["required"]
        samplers = list(options["sampler_name"][0])
        schedulers = [*options["scheduler"][0], ALIGN_SCHEDULER]
    return samplers, schedulers


def matrix_model(models, model_id):
    model_id = str(model_id)
    for model in models:
        if model[0] == model_id:
            return model
    raise ValueError(f"Unknown checkpoint number: {model_id}")


def comparison_plan(request):
    models = matrix_models()
    first = matrix_model(models, request.model_1)
    second = matrix_model(models, request.model_2)
    samplers, schedulers = matrix_options()

    if request.type == "sampler":
        return first, second, [""], samplers
    if request.type == "scheduler":
        sampler = request.sampler or MATRIX_SAMPLER
        if sampler not in samplers:
            raise ValueError(f"Unsupported sampler: {sampler}")
        return first, second, [""], schedulers
    return first, second, schedulers, samplers


def combined_plan():
    models = matrix_models()
    samplers, schedulers = matrix_options()
    options = [
        (sampler, scheduler)
        for sampler in samplers
        for scheduler in schedulers
    ]
    rows = [
        (first, second)
        for first in options
        for second in options
    ]
    columns = []
    for index, model in enumerate(models):
        columns.extend((first, model) for first in models[:index])
        columns.extend(
            (model, second)
            for second in reversed(models[:index])
        )
    return rows, columns


def matrix_image_path(folder, first_id, second_id, generation):
    name = (
        f"{first_id}{generation}.epng"
        if first_id == second_id
        else f"{first_id}x{second_id}{generation}.epng"
    )
    return IMAGE_DIR / folder / name


def create_matrix_grid(folder, number, models, generation):
    count = len(models)
    width = MATRIX_LABEL_SIZE + MATRIX_CELL_WIDTH * count
    height = MATRIX_LABEL_SIZE + MATRIX_CELL_HEIGHT * count
    grid = Image.new("RGB", (width, height), "#111")
    draw = ImageDraw.Draw(grid)
    font = ImageFont.load_default(size=18)

    for index, (model_id, _) in enumerate(models):
        x = MATRIX_LABEL_SIZE + index * MATRIX_CELL_WIDTH
        y = MATRIX_LABEL_SIZE + index * MATRIX_CELL_HEIGHT
        draw.text(
            (x + MATRIX_CELL_WIDTH // 2, MATRIX_LABEL_SIZE // 2),
            model_id,
            fill="#6cf",
            font=font,
            anchor="mm",
        )
        draw.text(
            (MATRIX_LABEL_SIZE // 2, y + MATRIX_CELL_HEIGHT // 2),
            model_id,
            fill="#6cf",
            font=font,
            anchor="mm",
        )

        for column, (second_id, _) in enumerate(models):
            path = matrix_image_path(
                folder,
                model_id,
                second_id,
                generation,
            )
            with Image.open(BytesIO(stored_bytes(path))) as source:
                image = source.convert("RGB")
            cell_x = MATRIX_LABEL_SIZE + column * MATRIX_CELL_WIDTH
            grid.paste(
                image,
                (
                    cell_x + (MATRIX_CELL_WIDTH - image.width) // 2,
                    y + (MATRIX_CELL_HEIGHT - image.height) // 2,
                ),
            )

    output = IMAGE_DIR / folder / f"{number}gr.epng"
    save_named_image(grid, output)


def comparison_image_path(folder, number, row, column):
    return IMAGE_DIR / folder / f"{number}-{row + 1}x{column + 1}.epng"


def create_comparison_grid(folder, number, rows, columns):
    font = ImageFont.load_default(size=18)
    label_width = (
        max(
            MATRIX_ROW_LABEL_WIDTH,
            *(
                round(font.getlength(label)) + MATRIX_LABEL_SIZE
                for label in rows
            ),
        )
        if len(rows) > 1
        else 0
    )
    width = label_width + MATRIX_GRID_CELL_WIDTH * len(columns)
    height = MATRIX_LABEL_SIZE + MATRIX_GRID_CELL_HEIGHT * len(rows)
    grid = Image.new("RGB", (width, height), "#111")
    draw = ImageDraw.Draw(grid)

    for column, label in enumerate(columns):
        draw.text(
            (
                label_width + column * MATRIX_GRID_CELL_WIDTH
                + MATRIX_GRID_CELL_WIDTH // 2,
                MATRIX_LABEL_SIZE // 2,
            ),
            label,
            fill="#6cf",
            font=font,
            anchor="mm",
        )

    for row, label in enumerate(rows):
        y = MATRIX_LABEL_SIZE + row * MATRIX_GRID_CELL_HEIGHT
        if label_width:
            draw.text(
                (label_width // 2, y + MATRIX_GRID_CELL_HEIGHT // 2),
                label,
                fill="#6cf",
                font=font,
                anchor="mm",
            )
        for column in range(len(columns)):
            path = comparison_image_path(folder, number, row, column)
            with Image.open(BytesIO(stored_bytes(path))) as source:
                image = source.convert("RGB")
            image.thumbnail(
                (MATRIX_GRID_CELL_WIDTH, MATRIX_GRID_CELL_HEIGHT),
            )
            x = label_width + column * MATRIX_GRID_CELL_WIDTH
            grid.paste(
                image,
                (
                    x + (MATRIX_GRID_CELL_WIDTH - image.width) // 2,
                    y + (MATRIX_GRID_CELL_HEIGHT - image.height) // 2,
                ),
            )

    save_named_image(grid, IMAGE_DIR / folder / f"{number}gr.epng")


def selected_paths(items):
    root = IMAGE_DIR.resolve()
    selected = {}
    for value in items:
        path = (root / value).resolve()
        try:
            path.relative_to(root)
        except ValueError as error:
            raise HTTPException(404, "image not found") from error
        paths = path.iterdir() if path.is_dir() else (stored_path(value),)
        for image in paths:
            if image.is_file() and image.suffix.casefold() in IMAGE_SUFFIXES:
                name = image.relative_to(root).with_suffix(".png").as_posix()
                selected[name] = image
    if not selected:
        raise HTTPException(422, "no images selected")
    return selected


def valid_pass(p):
    return secrets.compare_digest(str(p), PASSWORD)


def clean_regions(rows):
    return [
        RegionRequest(
            prompt=str(row[0]).strip(),
            area=str(row[1] or "full").strip(),
            strength=row[2] if len(row) > 2 and row[2] is not None else 1,
        )
        for row in (rows or [])[:4]
        if row and str(row[0] or '').strip()
    ]


def clean_detailers(rows):
    return [
        DetailerRequest(
            detector=str(row[0]),
            model=str(row[1] or ""),
            prompt=str(row[2] or ""),
            negative=str(row[3] or ""),
            sampler=str(row[4]),
            scheduler=str(row[5]),
            steps=row[6],
            cfg=row[7],
            denoise=row[8],
        )
        for row in rows or []
        if row and row[0]
    ]


def select_model(model, current):
    model = current if model in MODEL_HEADERS else model
    return model, model


def add_detailer(

    rows,

    detector,

    model,

    prompt,

    negative,

    sampler,

    scheduler,

    steps,

    cfg,

    denoise,

):
    if not detector:
        return rows
    return [
        *(rows or []),
        [
            detector, model, prompt, negative, sampler, scheduler,
            steps, cfg, denoise,
        ],
    ]


def encode_style_images(files):
    files = files or []
    if len(files) > MAX_STYLE_IMAGES:
        raise gr.Error("InstantStyle accepts up to 4 images")
    images = []
    for file in files:
        data = Path(file).read_bytes()
        if len(data) > MAX_STYLE_IMAGE_SIZE:
            raise gr.Error("InstantStyle image exceeds 20 MiB")
        images.append(base64.b64encode(data).decode())
    return images


def generate(

    prompt,

    negative,

    regions,

    regional_mode,

    model,

    style_images,

    style_scope,

    style_weight,

    style_end,

    detailers,

    width=1152,

    height=896,

    batch_size=DEFAULT_UI_BATCH_SIZE,

    sampler=DEFAULT_SAMPLER,

    scheduler=DEFAULT_SCHEDULER,

    steps=DEFAULT_STEPS,

    cfg=DEFAULT_CFG,

    upscale=False,

    upscale_method=DEFAULT_UPSCALE_METHOD,

    upscale_model=DEFAULT_UPSCALE_MODEL,

    upscale_scale=DEFAULT_UPSCALE_SCALE,

    second_model="",

    second_sampler=DEFAULT_SECOND_SAMPLER,

    second_scheduler=DEFAULT_SECOND_SCHEDULER,

    second_steps=DEFAULT_SECOND_STEPS,

    second_cfg=DEFAULT_SECOND_CFG,

    denoise=DEFAULT_DENOISE,

    return_scale=DEFAULT_RETURN_SCALE,

    p="",

):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    request = DirectRequest(
        prompt=prompt,
        regions=clean_regions(regions),
        regional_mode=regional_mode,
        negative=negative,
        model=model,
        loras=[],
        style_images=encode_style_images(style_images),
        style_scope=style_scope,
        style_weight=style_weight,
        style_end=style_end,
        detailers=clean_detailers(detailers),
        width=width,
        height=height,
        batch_size=batch_size,
        sampler=sampler,
        scheduler=scheduler,
        steps=steps,
        cfg=cfg,
        upscale=upscale,
        upscale_method=upscale_method,
        upscale_model=upscale_model,
        upscale_scale=upscale_scale,
        second_model=second_model,
        second_loras=[],
        second_sampler=second_sampler,
        second_scheduler=second_scheduler,
        second_steps=second_steps,
        second_cfg=second_cfg,
        denoise=denoise,
        return_scale=return_scale,
    )
    return [
        scale_image(image, return_scale)
        for image in generate_images(request, False)
    ]


def ping_image(p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    with lock:
        prefix = f"{PING_MODEL_ID}_"
        models = [name for name in generation_models() if name.startswith(prefix)]
        if len(models) != 1:
            raise gr.Error(f"Expected one checkpoint with prefix {prefix}")
        model_name = models[0]
        model, clip = load_chain(model_name, [])
        vae = load_vae(vae_name(model_name))
        clip.patcher.load_device = CPU_DEVICE
        clip.patcher.offload_device = CPU_DEVICE
        latent = state["latent"].generate(
            width=PING_SIZE,
            height=PING_SIZE,
            batch_size=1,
        )[0]
        positive = state["encode"].encode(clip=clip, text="1girl")[0]
        negative = state["encode"].encode(clip=clip, text="")[0]
        state["ping"] = model, vae, latent, positive, negative
        try:
            image = infer_ping(secrets.randbits(64))
        finally:
            del state["ping"]
        return [tensor_image(image)]


def api_generate(body, p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    request = DirectRequest.model_validate_json(body)
    image = combine_images(
        generate_images(request),
        request.width,
        request.height,
    )
    temp = tempfile.NamedTemporaryFile(suffix=".epng", delete=False)
    temp.write(encrypted_image_bytes(image))
    temp.close()
    return temp.name


def api_upscale(body, p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")
    model_name = DEFAULT_UPSCALE_MODEL
    scale = 1
    try:
        payload = json.loads(body)
        if isinstance(payload, dict):
            body = payload["image"]
            model_name = payload.get("model", model_name)
            scale = float(payload.get("scale", scale))
    except json.JSONDecodeError:
        pass
    except (KeyError, TypeError, ValueError) as error:
        raise gr.Error("Invalid upscale request") from error
    if not 1 <= scale <= 4:
        raise gr.Error("Scale must be between 1 and 4")
    if len(body) > (MAX_UPSCALE_BYTES + 2) // 3 * 4:
        raise gr.Error("Image exceeds 20 MiB")
    try:
        data = base64.b64decode(body, validate=True)
        if len(data) > MAX_UPSCALE_BYTES:
            raise ValueError("Image exceeds 20 MiB")
        with Image.open(BytesIO(data)) as source:
            if source.width * source.height > MAX_UPSCALE_PIXELS:
                raise ValueError("Image exceeds 4 megapixels")
            if getattr(source, "is_animated", False):
                raise ValueError("Animated images are not supported")
            image = ImageOps.exif_transpose(source).convert("RGBA")
    except (ValueError, OSError, Image.DecompressionBombError) as error:
        raise gr.Error(str(error)) from error
    pixels = torch.from_numpy(
        np.asarray(image.convert("RGB"), dtype=np.float32) / 255
    ).unsqueeze(0)
    with lock:
        init_comfy()
        if model_name not in model_options("upscale_models", []):
            raise gr.Error(f"Unknown upscale model: {model_name}")
        model = load_upscale_model(model_name)
        result = tensor_image(infer_upscale(pixels, model))
    size = round(image.width * scale), round(image.height * scale)
    if result.size != size:
        result = result.resize(size, Image.Resampling.LANCZOS)
    alpha = image.getchannel("A")
    if alpha.getextrema() != (255, 255):
        result.putalpha(alpha.resize(size, Image.Resampling.LANCZOS))
    result.info.update(image.info)
    return base64.b64encode(image_png_bytes(result)).decode()


def api_health(p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    return {"status": True}


def grouped_options(defaults, values, sources=None):
    defaults = set(defaults)
    sources = sources or {}
    groups = []
    standard = [
        {"value": value, "text": value}
        for value in values
        if value in defaults
    ]
    custom = []
    for value in values:
        if value in defaults:
            continue
        source = sources.get(value)
        text = value
        if value in state.get("custom_samplers", {}):
            text = value.partition(":")[2]
            source = "ComfyUI-ppm"
        custom.append({
            "value": value,
            "text": f"{text} [{source or 'Custom node'}]",
        })
    if standard:
        groups.append({"label": "Default", "options": standard})
    if custom:
        groups.append({"label": "Custom", "options": custom})
    return groups


def api_options(p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    data = object_info()
    detailer = state["face_detailer"].INPUT_TYPES()["required"]
    samplers = sampler_names()
    schedulers = scheduler_names()
    scheduler_sources = {
        **state["scheduler_sources"],
        ALIGN_SCHEDULER: "ComfyUI",
    }
    return {
        "models": data["CheckpointLoaderSimple"]["input"]["required"][
            "ckpt_name"
        ][0],
        "loras": data["LoraLoader"]["input"]["required"]["lora_name"][0],
        "samplers": grouped_options(
            state["default_samplers"],
            samplers,
            state["sampler_sources"],
        ),
        "schedulers": grouped_options(
            state["default_schedulers"],
            schedulers,
            scheduler_sources,
        ),
        "detailer-samplers": grouped_options(
            state["default_samplers"],
            detailer["sampler_name"][0],
            state["sampler_sources"],
        ),
        "detailer-schedulers": grouped_options(
            state["default_schedulers"],
            detailer["scheduler"][0],
            state["scheduler_sources"],
        ),
        "instant-style-scopes": list(STYLE_SCOPES),
        "upscale-methods": grouped_options(
            state["default_upscale_methods"],
            data["LatentUpscaleBy"]["input"]["required"][
                "upscale_method"
            ][0],
        ),
        "upscale-models": data["UpscaleModelLoader"]["input"]["required"][
            "model_name"
        ][0],
        "ultralytics": data["UltralyticsDetectorProvider"]["input"][
            "required"
        ]["model_name"][0],
    }


def api_refresh(p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    return refresh_models()


def api_load_models(body, p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    request = ModelRequest.model_validate_json(body)
    with lock:
        init_comfy()
        unloaded = unloaded_model_counts(request)
        changed = not request_models_loaded(request)
    if changed:
        model_pool.submit(load_models, request)
    return {"loaded": not changed, "changed": changed, **unloaded}


def load_models(request):
    with lock:
        if not request_models_loaded(request):
            load_request_models(request)


def api_matrix_cell(body, p=""):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    return generate_matrix_cell(body)


def queued_generate(request):
    result = get_local_client().predict(
        request.model_dump_json(),
        PASSWORD,
        api_name="/generate",
    )
    return Image.open(BytesIO(stored_bytes(Path(result)))).copy()


def queued_matrix_cell(request):
    return retry_gpu(
        lambda: get_local_client().predict(
            request.model_dump_json(),
            PASSWORD,
            api_name="/matrix_cell",
        )
    )


def run_checkpoint_matrix(request, folder, number):
    models = matrix_models()
    for index, model in enumerate(models):
        queued_matrix_cell(
            MatrixCellRequest(
                generation=request.generation,
                positive=request.positive,
                negative=request.negative,
                folder=folder,
                first=model,
                second=model,
            )
        )
        for first in models[:index]:
            queued_matrix_cell(
                MatrixCellRequest(
                    generation=request.generation,
                    positive=request.positive,
                    negative=request.negative,
                    folder=folder,
                    first=first,
                    second=model,
                )
            )
        for second in reversed(models[:index]):
            queued_matrix_cell(
                MatrixCellRequest(
                    generation=request.generation,
                    positive=request.positive,
                    negative=request.negative,
                    folder=folder,
                    first=model,
                    second=second,
                )
            )
    create_matrix_grid(folder, number, models, request.generation)


def run_comparison_matrix(request, folder, number):
    first, second, rows, columns = comparison_plan(request)
    for row, row_label in enumerate(rows):
        for column, column_label in enumerate(columns):
            sampler = column_label
            scheduler = MATRIX_SCHEDULER
            if request.type == "scheduler":
                sampler = request.sampler or MATRIX_SAMPLER
                scheduler = column_label
            elif request.type == "sampler+scheduler":
                scheduler = row_label
            output = comparison_image_path(
                folder,
                number,
                row,
                column,
            ).name
            queued_matrix_cell(
                MatrixCellRequest(
                    generation=request.generation,
                    positive=request.positive,
                    negative=request.negative,
                    folder=folder,
                    first=first,
                    second=second,
                    sampler=sampler,
                    scheduler=scheduler,
                    second_sampler=sampler,
                    second_scheduler=scheduler,
                    output=output,
                )
            )
    create_comparison_grid(folder, number, rows, columns)


def run_combined_matrix(request, folder, number):
    rows, columns = combined_plan()
    for column, (first, second) in enumerate(columns):
        for row, (first_options, second_options) in enumerate(rows):
            first_sampler, first_scheduler = first_options
            second_sampler, second_scheduler = second_options
            output = comparison_image_path(
                folder,
                number,
                row,
                column,
            ).name
            queued_matrix_cell(
                MatrixCellRequest(
                    generation=request.generation,
                    positive=request.positive,
                    negative=request.negative,
                    folder=folder,
                    first=first,
                    second=second,
                    sampler=first_sampler,
                    scheduler=first_scheduler,
                    second_sampler=second_sampler,
                    second_scheduler=second_scheduler,
                    output=output,
                )
            )
    create_comparison_grid(
        folder,
        number,
        [
            f"{first[0]}+{first[1]}x{second[0]}+{second[1]}"
            for first, second in rows
        ],
        [f"{first[0]}x{second[0]}" for first, second in columns],
    )


def run_matrix(request, folder, number):
    try:
        if request.type == "checkpoint":
            run_checkpoint_matrix(request, folder, number)
        elif request.type == COMBINED_MATRIX_TYPE:
            run_combined_matrix(request, folder, number)
        else:
            run_comparison_matrix(request, folder, number)
    except Exception as error:
        log(f"Matrix failed: {error}")
    finally:
        with matrix_lock:
            matrix_grids.discard((folder, number))


def linked_value(workflow, value):
    if not isinstance(value, list) or len(value) < 2:
        return value

    node = workflow.get(str(value[0]), {})
    inputs = node.get("inputs", {})

    if node.get("class_type") == "StringConcatenate":
        parts = [
            linked_value(
                workflow,
                inputs.get("string_a", ""),
            ),
            linked_value(
                workflow,
                inputs.get("string_b", ""),
            ),
        ]
        return str(inputs.get("delimiter", ",")).join(map(str, parts))

    return inputs.get("text", "")


def workflow_values(workflow):
    sampler = next(
        (
            node
            for node in workflow.values()
            if node.get("class_type") == "KSampler"
        ),
        {},
    )
    inputs = sampler.get("inputs", {})

    latent_id = inputs.get("latent_image", [None])[0]
    latent = workflow.get(str(latent_id), {}).get("inputs", {})

    positive_id = inputs.get("positive", [None])[0]
    positive = (
        workflow.get(str(positive_id), {})
        .get("inputs", {})
        .get("text", "")
    )

    negative_id = inputs.get("negative", [None])[0]
    negative = (
        workflow.get(str(negative_id), {})
        .get("inputs", {})
        .get("text", DEFAULT_NEGATIVE)
    )

    return (
        linked_value(workflow, positive),
        latent.get("width", 1152),
        latent.get("height", 896),
        inputs.get("steps", 16),
        negative,
        inputs.get("sampler_name", DEFAULT_SAMPLER),
        inputs.get("scheduler", DEFAULT_SCHEDULER),
        inputs.get("cfg", DEFAULT_CFG),
        latent.get("batch_size", DEFAULT_BATCH_SIZE),
    )


def run_job(job_id, workflow):
    try:
        (
            prompt,
            width,
            height,
            steps,
            negative,
            sampler,
            scheduler,
            cfg,
            batch_size,
        ) = workflow_values(workflow)

        image = queued_generate(
            DirectRequest(
                prompt=prompt,
                width=width,
                height=height,
                steps=steps,
                negative=negative,
                sampler=sampler,
                scheduler=scheduler,
                cfg=cfg,
                batch_size=batch_size,
            )
        )

        filename = f"{job_id}.png"
        images[filename] = png_bytes(image)

        jobs[job_id] = {
            "outputs": {
                "output": {
                    "images": [
                        {
                            "filename": filename,
                            "subfolder": "",
                            "type": "output",
                        }
                    ]
                }
            },
            "status": {
                "status_str": "success",
                "completed": True,
                "messages": [],
            },
        }
    except Exception as error:
        jobs[job_id] = {
            "outputs": {},
            "status": {
                "status_str": "error",
                "completed": True,
                "messages": [
                    [
                        "execution_error",
                        {
                            "node_id": "output",
                            "node_type": "Generate",
                            "exception_type": type(error).__name__,
                            "exception_message": str(error),
                        },
                    ]
                ],
            },
        }


def replace_asgi_headers(headers, replacements):
    names = {name for name, _ in replacements}
    return [item for item in headers if item[0].lower() not in names] + replacements


class GradioEncryptionMiddleware:
    def __init__(self, app):
        self.app = app

    async def __call__(self, scope, receive, send):
        if scope["type"] != "http":
            await self.app(scope, receive, send)
            return
        headers = dict(scope.get("headers", []))
        encrypted = headers.get(PROXY_ENCRYPTION_HEADER) == PROXY_ENCRYPTION
        if not encrypted:
            await self.app(scope, receive, send)
            return

        decrypted_receive = receive
        if scope.get("method") not in {"GET", "HEAD"}:
            chunks = []
            while True:
                message = await receive()
                if message["type"] == "http.disconnect":
                    return
                chunks.append(message.get("body", b""))
                if not message.get("more_body", False):
                    break
            direct = False
            try:
                body = decrypt_proxy_payload(b"".join(chunks))
                if scope.get("path", "").startswith("/gradio_api/call/"):
                    payload = json.loads(body)
                    if not isinstance(payload, dict) or not isinstance(payload.get("data"), list):
                        raise ValueError("invalid Gradio payload")
                    payload["data"].append(PASSWORD)
                    body = json.dumps(payload, separators=(",", ":")).encode()
                else:
                    direct = True
            except (InvalidTag, ValueError):
                content = b'{"detail":"Invalid encrypted payload"}'
                await send({
                    "type": "http.response.start",
                    "status": 400,
                    "headers": [
                        (b"content-type", b"application/json"),
                        (b"content-length", str(len(content)).encode()),
                    ],
                })
                await send({"type": "http.response.body", "body": content})
                return

            scope = dict(scope)
            if direct:
                scope["query_string"] = f"p={quote(PASSWORD)}".encode()
            scope["headers"] = replace_asgi_headers(
                scope.get("headers", []),
                [
                    (b"content-type", b"application/json"),
                    (b"content-length", str(len(body)).encode()),
                ],
            )
            delivered = False

            async def decrypted_receive():
                nonlocal delivered
                if delivered:
                    return {"type": "http.request", "body": b"", "more_body": False}
                delivered = True
                return {"type": "http.request", "body": body, "more_body": False}

        start = None
        response_chunks = []

        async def encrypted_send(message):
            nonlocal start
            if message["type"] == "http.response.start":
                start = message
                return
            if message["type"] == "http.response.pathsend":
                await send(start)
                await send(message)
                return
            if message["type"] != "http.response.body":
                await send(message)
                return
            response_chunks.append(message.get("body", b""))
            if message.get("more_body", False):
                return
            content = b"".join(response_chunks)
            if not content.startswith(FILE_MAGIC):
                content = encrypt_proxy_payload(content)
                start = dict(start)
                start["headers"] = replace_asgi_headers(
                    start.get("headers", []),
                    [
                        (PROXY_ENCRYPTION_HEADER, PROXY_ENCRYPTION),
                        (b"content-length", str(len(content)).encode()),
                    ],
                )
            await send(start)
            await send({"type": "http.response.body", "body": content})

        await self.app(scope, decrypted_receive, encrypted_send)


api = App()
api.add_middleware(GradioEncryptionMiddleware)
api.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


def require_pass(p: str = Query(...)):
    if not valid_pass(p):
        raise HTTPException(401, "invalid password")


@api.get(

    "/health",

    dependencies=[Depends(require_pass)],

)
def health():
    return {"status": True}


@api.post(

    "/start",

    dependencies=[Depends(require_pass)],

)
def start_matrix(body: MatrixRequest):
    try:
        if body.type not in MATRIX_TYPES:
            raise ValueError(f"Unsupported matrix type: {body.type}")
        if body.type == "checkpoint":
            matrix_models()
        elif body.type == COMBINED_MATRIX_TYPE:
            combined_plan()
        else:
            comparison_plan(body)
    except ValueError as error:
        raise HTTPException(422, str(error)) from error

    folder = datetime.now(TIMEZONE).date().isoformat()
    number = reserve_matrix_grid(folder)
    matrix_pool.submit(run_matrix, body, folder, number)
    return {
        "status": "started",
        "grid": f"{folder}/{number}gr.epng",
    }


@api.get(

    "/system_stats",

    dependencies=[Depends(require_pass)],

)
def system_stats():
    return {
        "system": {"os": os.name},
        "devices": [],
    }


@api.get(

    "/object_info",

    dependencies=[Depends(require_pass)],

)
def object_info():
    init_comfy()
    options = state["sample"].INPUT_TYPES()["required"]
    loras = state["lora"].INPUT_TYPES()["required"]
    vaes = state["vae_loader"].INPUT_TYPES()["required"]
    upscalers = state["upscale"].INPUT_TYPES()["required"]
    upscale_models = state["upscale_model_loader"].INPUT_TYPES()["required"]
    model_names = generation_models()
    lora_names = model_options("loras", loras["lora_name"][0])
    vae_names = model_options("vae", vaes["vae_name"][0])
    detector_names = model_options(
        "ultralytics",
        state["folders"].get_filename_list("ultralytics_bbox"),
    )

    return {
        "KSampler": {
            "input": {
                "required": {
                    "sampler_name": [
                        options["sampler_name"][0]
                    ],
                    "scheduler": [
                        [*options["scheduler"][0], ALIGN_SCHEDULER]
                    ],
                }
            }
        },
        "CheckpointLoaderSimple": {
            "input": {
                "required": {
                    "ckpt_name": [
                        model_names
                    ]
                }
            }
        },
        "LoraLoader": {
            "input": {
                "required": {
                    "lora_name": [
                        lora_names
                    ]
                }
            }
        },
        "LatentUpscaleBy": {
            "input": {
                "required": {
                    "upscale_method": [
                        upscalers["upscale_method"][0]
                    ]
                }
            }
        },
        "UpscaleModelLoader": {
            "input": {
                "required": {
                    "model_name": [
                        model_options(
                            "upscale_models",
                            upscale_models["model_name"][0],
                        )
                    ]
                }
            }
        },
        "UltralyticsDetectorProvider": {
            "input": {
                "required": {
                    "model_name": [detector_names]
                }
            }
        },
        "UNETLoader": {
            "input": {
                "required": {
                    "unet_name": [[
                        name
                        for name in model_names
                        if is_anima_model(name)
                    ]]
                }
            }
        },
        "VAELoader": {
            "input": {
                "required": {
                    "vae_name": [
                        vae_names
                    ]
                }
            }
        },
    }


@api.post(

    "/refresh_models",

    dependencies=[Depends(require_pass)],

)
def refresh_models():
    with lock:
        init_comfy()
        added = index_bucket_models()

    return {
        kind: sorted(names, key=str.casefold)
        for kind, names in added.items()
    }


@api.post(

    "/prompt",

    dependencies=[Depends(require_pass)],

)
def queue_prompt(body: dict):
    workflow = body.get("prompt")

    if not isinstance(workflow, dict):
        raise HTTPException(
            400,
            "prompt must contain a ComfyUI workflow object",
        )

    job_id = str(uuid.uuid4())
    pool.submit(run_job, job_id, workflow)

    return {
        "prompt_id": job_id,
        "number": len(jobs),
        "node_errors": {},
    }


@api.get(

    "/history",

    dependencies=[Depends(require_pass)],

)
def history():
    return jobs


@api.get(

    "/history/{job_id}",

    dependencies=[Depends(require_pass)],

)
def history_item(job_id):
    return {job_id: jobs[job_id]} if job_id in jobs else {}


@api.get(

    "/view",

    dependencies=[Depends(require_pass)],

)
def view(filename: str = Query(...)):
    data = images.pop(Path(filename).name, None)

    if data is None:
        raise HTTPException(404, "image not found")

    return Response(content=data, media_type="image/png")


@api.post(

    "/interrupt",

    dependencies=[Depends(require_pass)],

)
def interrupt():
    return {}


@api.post(

    "/api/generate",

    dependencies=[Depends(require_pass)],

)
def direct(

    body: DirectRequest,

):
    try:
        image = queued_generate(body)
    except ValueError as error:
        raise HTTPException(422, str(error)) from error

    image = scale_image(image, body.return_scale)

    return Response(
        content=encrypted_image_bytes(image),
        media_type="application/octet-stream",
        headers={"Content-Disposition": 'attachment; filename="image.epng"'},
    )


@api.post(

    "/api/archive",

    dependencies=[Depends(require_pass)],

)
def archive(

    body: bytes = Body(),

    from_api: bool = Query(True),

):
    image = Image.open(BytesIO(body)).copy()
    backup_pool.submit(archive_image, image, from_api).result()
    return {"status": True}


@api.get(

    "/explorer",

    dependencies=[Depends(require_pass)],

)
def explorer():
    return FileResponse(Path.cwd() / "explorer.html")


@api.get("/explorer.css")
def explorer_css():
    return FileResponse(Path.cwd() / "explorer.css", media_type="text/css")


@api.get("/explorer.js")
def explorer_js():
    return FileResponse(
        Path.cwd() / "explorer.js",
        media_type="text/javascript",
    )


@api.get("/upscale", dependencies=[Depends(require_pass)])
def upscale_page():
    return FileResponse(Path.cwd() / "upscale.html")


@api.get("/upscale/{name}")
def upscale_asset(name: str):
    if name not in UPSCALE_ASSETS:
        raise HTTPException(404, "asset not found")
    filename, media_type = UPSCALE_ASSETS[name]
    return FileResponse(Path.cwd() / filename, media_type=media_type)


@api.get("/api/upscale/options", dependencies=[Depends(require_pass)])
def upscale_options():
    return {
        "models": model_options("upscale_models", []),
        "default": DEFAULT_UPSCALE_MODEL,
    }


@api.post("/api/upscale", dependencies=[Depends(require_pass)])
def upscale_download(

    body: bytes = Body(b"", media_type="application/octet-stream"),

    path: str = Query(None),

    model: str = Query(DEFAULT_UPSCALE_MODEL),

    scale: float = Query(1, ge=1, le=4),

):
    data = stored_bytes(stored_path(path)) if path is not None else body
    if len(data) > MAX_UPSCALE_BYTES:
        raise HTTPException(413, "Image exceeds 20 MiB")
    if not data:
        raise HTTPException(422, "An image is required")
    request = json.dumps({
        "image": base64.b64encode(data).decode(),
        "model": model,
        "scale": scale,
    })
    try:
        result = get_local_client().predict(
            request,
            PASSWORD,
            api_name="/upscale",
        )
    except Exception as error:
        raise HTTPException(502, str(error)) from error
    return Response(
        content=base64.b64decode(result, validate=True),
        media_type="image/png",
        headers={
            "Content-Disposition": 'attachment; filename="upscaled.png"',
            "Cache-Control": "no-store",
        },
    )


def explorer_folder_has_images(path):
    with os.scandir(path) as entries:
        return any(
            item.is_file()
            and Path(item.name).suffix.casefold() in IMAGE_SUFFIXES
            for item in entries
        )


def explorer_files(folder):
    path = (IMAGE_DIR / folder).resolve()
    if path.parent != IMAGE_DIR.resolve() or not path.is_dir():
        raise HTTPException(404, "folder not found")
    with os.scandir(path) as entries:
        return [
            (Path(folder) / item.name).as_posix()
            for item in sorted(entries, key=natural_key, reverse=True)
            if Path(item.name).suffix.casefold() in IMAGE_SUFFIXES
            and item.is_file()
        ]


@api.get("/api/images", dependencies=[Depends(require_pass)])
def image_list(

    folder: str = Query(None),

    infinite: bool = Query(False),

    favorites: bool = Query(False),

    search: str = Query(""),

    offset: int = Query(0, ge=0),

    limit: int = Query(EXPLORER_PAGE_SIZE, ge=1, le=EXPLORER_MAX_PAGE_SIZE),

):
    grouped = infinite or (favorites and folder is None)
    root = IMAGE_DIR
    if not root.is_dir():
        key = "groups" if grouped else "folders" if folder is None else "images"
        return {key: [], "next_offset": None}
    if grouped or folder is None:
        with os.scandir(root) as entries:
            folders = [
                item.name
                for item in sorted(entries, key=natural_key, reverse=True)
                if item.is_dir(follow_symlinks=False)
                and (grouped or explorer_folder_has_images(item.path))
            ]
        if not grouped:
            return {"folders": [{"name": name} for name in folders]}
    else:
        folders = [folder]
    starred = starred_paths()
    if favorites:
        favorite_folders = {relative.split("/", 1)[0] for relative in starred}
        folders = [name for name in folders if name in favorite_folders]
    files = (relative for name in folders for relative in explorer_files(name)
             if not favorites or relative in starred)
    database = star_database()
    try:
        query = search.strip().casefold()
        if query:
            files = (relative for relative in files if (
                query in Path(relative).with_suffix(".png").name.casefold()
                or any(query in prompt.casefold() for prompt in stored_prompt(
                    root / relative, relative, database
                )[:2])
            ))
        page = list(islice(files, offset, offset + limit + 1))
        images = []
        for relative in page[:limit]:
            prompt, second_prompt, artists = stored_prompt(
                root / relative, relative, database
            )
            stat = (root / relative).stat()
            images.append({
                "name": Path(relative).with_suffix(".png").name,
                "path": relative,
                "version": f"{stat.st_mtime_ns}-{stat.st_size}",
                "starred": relative in starred,
                "prompt": prompt,
                "second_prompt": second_prompt,
                "artists": artists,
            })
        database.commit()
    finally:
        database.close()
    result = {"next_offset": offset + limit if len(page) > limit else None}
    if grouped:
        groups = {}
        for image in images:
            name = image["path"].split("/", 1)[0]
            groups.setdefault(name, []).append(image)
        result["groups"] = [{"name": name, "images": items}
                            for name, items in groups.items()]
    else:
        result["images"] = images
    return result


@api.post("/api/images/star", dependencies=[Depends(require_pass)])
def image_star(body: StarRequest):
    path = stored_path(body.path).relative_to(IMAGE_DIR.resolve()).as_posix()
    set_star(path, body.starred)
    return {"starred": body.starred}


@api.get(

    "/api/images/preview",

    dependencies=[Depends(require_pass)],

)
def image_preview(path: str = Query(...)):
    target = stored_path(path)
    stat = target.stat()
    return Response(
        content=stored_preview(target, stat.st_mtime_ns, stat.st_size),
        media_type="image/webp",
        headers={"Cache-Control": "private, max-age=86400"},
    )


@api.get(

    "/api/images/original",

    dependencies=[Depends(require_pass)],

)
def image_original(path: str = Query(...)):
    return Response(
        content=stored_bytes(stored_path(path)),
        media_type="image/png",
        headers={"Cache-Control": "private, max-age=86400"},
    )


@api.post(

    "/api/images/download",

    dependencies=[Depends(require_pass)],

)
def image_download(body: DownloadRequest):
    temp = tempfile.NamedTemporaryFile(suffix=".7z", delete=False)
    temp.close()
    try:
        with py7zr.SevenZipFile(
            temp.name,
            "w",
            password=PASSWORD,
            header_encryption=True,
        ) as archive_file:
            for name, path in selected_paths(body.items).items():
                archive_file.writestr(stored_bytes(path), name)
    except Exception:
        Path(temp.name).unlink(missing_ok=True)
        raise
    return FileResponse(
        temp.name,
        filename="images.7z",
        media_type="application/x-7z-compressed",
        background=BackgroundTask(Path(temp.name).unlink, missing_ok=True),
    )


@api.post(

    "/api/images/delete",

    dependencies=[Depends(require_pass)],

)
def image_delete(body: DownloadRequest):
    paths = selected_paths(body.items)
    relatives = [
        path.relative_to(IMAGE_DIR.resolve()).as_posix()
        for path in paths.values()
    ]
    for path, relative in zip(paths.values(), relatives):
        set_star(relative, False)
        path.unlink()
    database = star_database()
    try:
        for table in ("image_prompts", "image_previews"):
            database.executemany(
                f"DELETE FROM {table} WHERE path = ?",
                ((relative,) for relative in relatives),
            )
        database.commit()
    finally:
        database.close()
    for value in body.items:
        path = (IMAGE_DIR / value).resolve()
        if path.is_dir() and not any(path.iterdir()):
            path.rmdir()
    return {"deleted": len(paths)}


def login(p):
    if not valid_pass(p):
        raise gr.Error("Invalid password")

    return (
        gr.Column(visible=False),
        gr.Column(visible=True),
    )


def copy_mounted_asset(kind, name):
    target = model_path(kind, name)
    if target.is_file():
        return
    log(f"Copying {kind}/{name}")
    target.parent.mkdir(parents=True, exist_ok=True)
    temp = target.with_suffix(target.suffix + ".part")
    shutil.copy2(BUCKET_MOUNT / kind / name, temp)
    temp.replace(target)
    log(f"Copied {kind}/{name}: {target.stat().st_size // MIB} MiB")


def preload_assets():
    assets = []
    for kind, asset_ids in STARTUP_ASSET_IDS.items():
        for asset_id in asset_ids:
            prefix = f"{asset_id}_"
            names = [
                name
                for name in remote_models[kind]
                if name.startswith(prefix)
                and (kind == "diffusion_models" or not is_anima_model(name))
            ]
            if len(names) != 1:
                raise RuntimeError(f"Expected one {kind} file with prefix {prefix}")
            assets.append((kind, names[0]))

    loaders = {
        "checkpoints": load_model,
        "diffusion_models": load_model,
        "loras": load_lora,
        "ultralytics": load_detector,
        "upscale_models": load_upscale_model,
        "vae": load_vae,
    }
    for kind, name in assets:
        log(f"Preloading {kind}/{name}")
        if (BUCKET_MOUNT / kind / name).is_file():
            copy_mounted_asset(kind, name)
        else:
            stage_model(kind, name)
        if kind not in STYLE_MODEL_KINDS:
            loaders[kind](name)
        log(f"Preloaded {kind}/{name}")
    load_style_pipeline()
    log("Preloaded InstantStyle pipeline")


def preload_startup_assets():
    log("Starting startup asset preload")
    started = time.monotonic()
    with lock:
        preload_assets()
    elapsed = time.monotonic() - started
    log(f"Finished preloading all startup assets in {elapsed:.1f}s")


def refresh_ui(

    first_model,

    second_model,

    upscale_model,

    detailer_model,

    detector,

):
    with lock:
        index_bucket_models()
    models = generation_models()
    second = [
        ("Reuse first-pass model", ""),
        *model_choices(models),
    ]
    return (
        gr.Dropdown(choices=model_choices(models), value=first_model),
        gr.Dropdown(choices=second, value=second_model),
        gr.Dropdown(
            choices=upscale_model_choices(
                model_options("upscale_models", []),
            ),
            value=upscale_model,
        ),
        gr.Dropdown(
            choices=[("Reuse final model", ""), *model_choices(models)],
            value=detailer_model,
        ),
        gr.Dropdown(
            choices=model_options("ultralytics", []),
            value=detector,
        ),
    )


cleanup_mount()
if __name__ == "__main__":
    for _ in range(SCAN_THREAD_COUNT):
        threading.Thread(
            target=runpy.run_path,
            args=("scan.py",),
            kwargs={"run_name": "__main__"},
            daemon=True,
        ).start()
init_comfy()
MODEL_NAMES = generation_models()
SAMPLE_OPTIONS = state["sample"].INPUT_TYPES()["required"]
SAMPLER_NAMES = sampler_names()
SCHEDULER_NAMES = scheduler_names()
DETAILER_OPTIONS = state["face_detailer"].INPUT_TYPES()["required"]
DETAILER_SAMPLER_NAMES = DETAILER_OPTIONS["sampler_name"][0]
DETAILER_SCHEDULER_NAMES = DETAILER_OPTIONS["scheduler"][0]
UPSCALE_NAMES = state["upscale"].INPUT_TYPES()["required"]["upscale_method"][0]
UPSCALE_MODEL_NAMES = model_options("upscale_models", [])
ULTRALYTICS_NAMES = model_options("ultralytics", [])
SECOND_MODELS = [
    ("Reuse first-pass model", ""),
    *model_choices(MODEL_NAMES),
]
DETAILER_MODELS = [
    ("Reuse final model", ""),
    *model_choices(MODEL_NAMES),
]


with gr.Blocks(title="Image generation") as demo:
    with gr.Column() as login_panel:
        pass_input = gr.Textbox(
            label="Password",
            type="password",
        )
        login_button = gr.Button(
            "Login",
            variant="primary",
        )

    with gr.Column(visible=False) as generate_panel:
        gr.HTML(
            "<model-note>"
            "ComfyUI-compatible generation"
            "</model-note>"
        )
        with gr.Row(elem_id="workspace"):
            with gr.Column(scale=4, min_width=360):
                with gr.Row():
                    model_input = gr.Dropdown(
                        model_choices(MODEL_NAMES),
                        value=DEFAULT_MODEL,
                        label="Model",
                        scale=8,
                    )
                    model_state = gr.State(DEFAULT_MODEL)
                    refresh_button = gr.Button("Refresh", scale=1)
                with gr.Accordion("Add CB asset", open=False):
                    model_files_input = gr.File(
                        label="Files",
                        file_count="multiple",
                        file_types=list(MODEL_SUFFIXES),
                        type="filepath",
                    )
                    model_url_input = gr.Textbox(label="URL")
                    with gr.Row():
                        model_location_input = gr.Dropdown(
                            MODEL_LOCATION_CHOICES,
                            value="checkpoints",
                            label="CB location",
                        )
                        anima_model_input = gr.Checkbox(
                            False,
                            label="Anima model",
                        )
                        model_upload_button = gr.Button("Add")
                    model_upload_status = gr.Textbox(
                        label="Status",
                        interactive=False,
                    )
                prompt_input = gr.Textbox(label="Prompt", lines=6)
                negative_input = gr.Textbox(
                    DEFAULT_NEGATIVE,
                    label="Negative prompt",
                    lines=3,
                )
                regions_input = gr.Dataframe(
                    value=[["", "full", 1]],
                    headers=["Prompt", "Area", "Strength"],
                    datatype=["str", "str", "number"],
                    type="array",
                    row_count=(1, "dynamic"),
                    column_count=(3, "fixed"),
                    label="Regions: auto, preset, or grid range, up to 3",
                )
                regional_mode_input = gr.Dropdown(
                    [
                        ("Soft conditioning", "conditioning"),
                        ("Attention Couple (PPM)", "attention"),
                    ],
                    value=REGIONAL_MODES[0],
                    label="Regional method",
                )

                with gr.Accordion("InstantStyle", open=False):
                    gr.HTML(
                        "<model-note>"
                        "SDXL and Illustrious only. Add up to four references; "
                        "their center crops are averaged. Use varied subjects and "
                        "palettes. The default styles both generation passes but leaves "
                        "ADetailer focused on anatomy."
                        "</model-note>"
                    )
                    style_images_input = gr.File(
                        label="Style references",
                        file_count="multiple",
                        file_types=["image"],
                        type="filepath",
                    )
                    style_scope_input = gr.Dropdown(
                        [
                            ("First and second passes", "generation"),
                            ("First pass only", "first"),
                            ("All passes, including ADetailer", "all"),
                        ],
                        value=DEFAULT_STYLE_SCOPE,
                        label="Apply to",
                    )
                    with gr.Row():
                        style_weight_input = gr.Slider(
                            0,
                            5,
                            DEFAULT_STYLE_WEIGHT,
                            step=.05,
                            label="Strength",
                        )
                        style_end_input = gr.Slider(
                            .05,
                            1,
                            DEFAULT_STYLE_END,
                            step=.05,
                            label="End at",
                        )

                with gr.Accordion("ADetailers", open=False):
                    detailer_input = gr.Dataframe(
                        headers=[
                            "Detector", "Model", "Prompt", "Negative",
                            "Sampler", "Scheduler", "Steps", "CFG", "Denoise",
                        ],
                        datatype=[
                            "str", "str", "str", "str", "str", "str",
                            "number", "number", "number",
                        ],
                        type="array",
                        row_count=(1, "dynamic"),
                        column_count=(9, "fixed"),
                        label="Ordered detail passes",
                    )
                    with gr.Row():
                        detailer_detector_add = gr.Dropdown(
                            ULTRALYTICS_NAMES,
                            value=DEFAULT_DETECTOR,
                            label="Detector",
                        )
                        detailer_model_add = gr.Dropdown(
                            DETAILER_MODELS,
                            value="",
                            label="Model",
                        )
                    detailer_prompt_add = gr.Textbox(
                        label="Prompt override",
                        placeholder="Blank reuses the main prompt",
                        lines=2,
                    )
                    detailer_negative_add = gr.Textbox(
                        label="Negative override",
                        placeholder="Blank reuses the main negative prompt",
                        lines=2,
                    )
                    with gr.Row():
                        detailer_sampler_add = gr.Dropdown(
                            DETAILER_SAMPLER_NAMES,
                            value=DEFAULT_SECOND_SAMPLER,
                            label="Sampler",
                        )
                        detailer_scheduler_add = gr.Dropdown(
                            DETAILER_SCHEDULER_NAMES,
                            value=DEFAULT_SECOND_SCHEDULER,
                            label="Scheduler",
                        )
                    with gr.Row():
                        detailer_steps_add = gr.Number(
                            DEFAULT_SECOND_STEPS,
                            label="Steps",
                            precision=0,
                        )
                        detailer_cfg_add = gr.Number(
                            DEFAULT_CFG,
                            label="CFG",
                        )
                        detailer_denoise_add = gr.Number(
                            .35,
                            label="Denoise",
                        )
                        detailer_button = gr.Button("Add", scale=1)

                with gr.Accordion("First pass", open=True):
                    with gr.Row():
                        width_input = gr.Number(1152, label="Width", precision=0)
                        height_input = gr.Number(896, label="Height", precision=0)
                        batch_size_input = gr.Slider(
                            1,
                            MAX_BATCH_SIZE,
                            DEFAULT_UI_BATCH_SIZE,
                            step=1,
                            label="Images",
                        )
                    with gr.Row():
                        sampler_input = gr.Dropdown(
                            SAMPLER_NAMES,
                            value=DEFAULT_SAMPLER,
                            label="Sampler",
                        )
                        scheduler_input = gr.Dropdown(
                            SCHEDULER_NAMES,
                            value=DEFAULT_SCHEDULER,
                            label="Scheduler",
                        )
                    with gr.Row():
                        steps_input = gr.Slider(
                            1,
                            100,
                            DEFAULT_STEPS,
                            step=1,
                            label="Steps",
                        )
                        cfg_input = gr.Slider(
                            0,
                            20,
                            DEFAULT_CFG,
                            step=.1,
                            label="CFG",
                        )

                upscale_input = gr.Checkbox(
                    False,
                    label="Upscale and run a second pass",
                )
                with gr.Column(visible=False) as second_panel:
                    with gr.Accordion("Second pass", open=True):
                        second_model_input = gr.Dropdown(
                            SECOND_MODELS,
                            value="",
                            label="Second-pass model",
                        )
                        second_model_state = gr.State("")
                        with gr.Row():
                            upscale_method_input = gr.Dropdown(
                                UPSCALE_NAMES,
                                value=DEFAULT_UPSCALE_METHOD,
                                label="Upscale method",
                            )
                            upscale_scale_input = gr.Number(
                                DEFAULT_UPSCALE_SCALE,
                                label="Scale by",
                                minimum=.01,
                            )
                        upscale_model_input = gr.Dropdown(
                            upscale_model_choices(UPSCALE_MODEL_NAMES),
                            value=DEFAULT_UPSCALE_MODEL,
                            label="Upscale model",
                        )
                        with gr.Row():
                            second_sampler_input = gr.Dropdown(
                                SAMPLER_NAMES,
                                value=DEFAULT_SECOND_SAMPLER,
                                label="Sampler",
                            )
                            second_scheduler_input = gr.Dropdown(
                                SCHEDULER_NAMES,
                                value=DEFAULT_SECOND_SCHEDULER,
                                label="Scheduler",
                            )
                        with gr.Row():
                            second_steps_input = gr.Slider(
                                1,
                                100,
                                DEFAULT_SECOND_STEPS,
                                step=1,
                                label="Steps",
                            )
                            second_cfg_input = gr.Slider(
                                0,
                                20,
                                DEFAULT_SECOND_CFG,
                                step=.1,
                                label="CFG",
                            )
                            denoise_input = gr.Slider(
                                0,
                                1,
                                DEFAULT_DENOISE,
                                step=.01,
                                label="Denoise",
                            )
                return_scale_input = gr.Slider(
                    .01,
                    1,
                    DEFAULT_RETURN_SCALE,
                    step=.01,
                    label="Return scale",
                )
                with gr.Row():
                    button = gr.Button("Generate", variant="primary")
                    ping_button = gr.Button("Ping")
            with gr.Column(scale=6, min_width=420):
                output = gr.Gallery(
                    label="Preview",
                    format="png",
                    elem_id="output",
                    columns=2,
                )

    api_button = gr.Button(visible=False)
    health_api_button = gr.Button(visible=False)
    options_api_button = gr.Button(visible=False)
    refresh_api_button = gr.Button(visible=False)
    load_models_api_button = gr.Button(visible=False)
    matrix_api_button = gr.Button(visible=False)
    upscale_api_button = gr.Button(visible=False)
    api_pass_input = gr.Textbox(visible=False)
    request_input = gr.Textbox(visible=False)
    api_json_output = gr.JSON(visible=False)
    api_file_output = gr.File(visible=False)
    matrix_output = gr.Textbox(visible=False)
    upscale_output = gr.Textbox(visible=False)
    login_button.click(
        login,
        pass_input,
        [login_panel, generate_panel],
        queue=False,
        api_visibility="private",
    )

    button.click(
        generate,
        [
            prompt_input,
            negative_input,
            regions_input,
            regional_mode_input,
            model_input,
            style_images_input,
            style_scope_input,
            style_weight_input,
            style_end_input,
            detailer_input,
            width_input,
            height_input,
            batch_size_input,
            sampler_input,
            scheduler_input,
            steps_input,
            cfg_input,
            upscale_input,
            upscale_method_input,
            upscale_model_input,
            upscale_scale_input,
            second_model_input,
            second_sampler_input,
            second_scheduler_input,
            second_steps_input,
            second_cfg_input,
            denoise_input,
            return_scale_input,
            pass_input,
        ],
        output,
        api_name="ui_generate",
        api_visibility="private",
    )
    ping_button.click(
        ping_image,
        pass_input,
        output,
        api_visibility="private",
    )
    upscale_input.change(
        lambda enabled: gr.Column(visible=enabled),
        upscale_input,
        second_panel,
        queue=False,
    )
    detailer_button.click(
        add_detailer,
        [
            detailer_input,
            detailer_detector_add,
            detailer_model_add,
            detailer_prompt_add,
            detailer_negative_add,
            detailer_sampler_add,
            detailer_scheduler_add,
            detailer_steps_add,
            detailer_cfg_add,
            detailer_denoise_add,
        ],
        detailer_input,
        queue=False,
    )
    model_input.change(
        select_model,
        [model_input, model_state],
        [model_input, model_state],
        queue=False,
    )
    second_model_input.change(
        select_model,
        [second_model_input, second_model_state],
        [second_model_input, second_model_state],
        queue=False,
    )
    refresh_button.click(
        refresh_ui,
        inputs=[
            model_input,
            second_model_input,
            upscale_model_input,
            detailer_model_add,
            detailer_detector_add,
        ],
        outputs=[
            model_input,
            second_model_input,
            upscale_model_input,
            detailer_model_add,
            detailer_detector_add,
        ],
        queue=False,
    )
    model_upload_button.click(
        upload_bucket_assets,
        [
            model_files_input,
            model_url_input,
            model_location_input,
            anima_model_input,
            pass_input,
        ],
        model_upload_status,
        queue=False,
        api_visibility="private",
    )

    api_button.click(
        api_generate,
        [
            request_input,
            api_pass_input,
        ],
        api_file_output,
        api_name="generate",
    )

    upscale_api_button.click(
        api_upscale,
        [request_input, api_pass_input],
        upscale_output,
        api_name="upscale",
    )

    health_api_button.click(
        api_health,
        api_pass_input,
        api_json_output,
        api_name="health",
    )

    options_api_button.click(
        api_options,
        api_pass_input,
        api_json_output,
        api_name="options",
    )

    refresh_api_button.click(
        api_refresh,
        api_pass_input,
        api_json_output,
        api_name="refresh",
    )

    load_models_api_button.click(
        api_load_models,
        [request_input, api_pass_input],
        api_json_output,
        api_name="load_models",
        queue=False,
    )

    matrix_api_button.click(
        api_matrix_cell,
        [
            request_input,
            api_pass_input,
        ],
        matrix_output,
        api_name="matrix_cell",
    )

demo.queue(default_concurrency_limit=1)


if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=PORT,
        share=True,
        ssr_mode=False,
        css_paths="style.css",
        head='<link rel="icon" href="data:,">',
        _app=api,
        prevent_thread_lock=True,
    )
    threading.Thread(target=preload_startup_assets, daemon=True).start()
    demo.block_thread()