File size: 106,876 Bytes
df2c623
 
 
 
10b5f20
 
 
 
 
 
 
 
df2c623
 
 
 
c5ece02
960e045
c5ece02
 
 
960e045
 
 
 
 
182ae9b
960e045
c5ece02
960e045
 
c5ece02
 
960e045
 
c5ece02
182ae9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960e045
182ae9b
c5ece02
 
df2c623
 
 
1f1a374
df2c623
 
 
 
575f46f
d5f26e7
1f1a374
 
10b5f20
 
 
 
 
 
 
 
 
 
 
df2c623
378e285
 
 
 
 
 
 
 
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
fac30cc
 
 
 
d3d412a
 
fac30cc
 
d3d412a
 
 
 
 
 
 
 
 
1dda790
 
 
 
 
d3d412a
 
 
 
 
 
 
 
 
fac30cc
 
37a2a42
fac30cc
 
37a2a42
d3d412a
 
 
37a2a42
fac30cc
 
37a2a42
fac30cc
 
d3d412a
fac30cc
 
 
 
 
d3d412a
378e285
 
df2c623
3386f25
 
1282ba1
 
 
3386f25
1282ba1
 
 
 
 
3386f25
 
 
 
df2c623
 
 
 
 
 
8cf0170
 
 
 
 
df2c623
 
1f1a374
 
 
 
 
8cf0170
 
 
 
 
1f1a374
 
3386f25
 
 
 
 
 
 
1f1a374
10b5f20
1f1a374
 
d5f26e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1a374
3386f25
1f1a374
 
 
3386f25
 
 
 
10b5f20
 
1f1a374
 
 
3386f25
 
10b5f20
1f1a374
10b5f20
 
3386f25
 
1f1a374
 
d5f26e7
1f1a374
 
 
 
 
 
 
d5f26e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f26e7
10b5f20
 
 
 
d5f26e7
 
 
 
 
10b5f20
1f1a374
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fac30cc
10b5f20
 
d3d412a
10b5f20
 
 
 
 
 
 
 
 
 
 
 
df2c623
3386f25
df2c623
 
 
 
3386f25
df2c623
 
 
 
 
 
 
 
 
3386f25
df2c623
 
 
 
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
3386f25
df2c623
3386f25
df2c623
3386f25
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
df2c623
 
 
 
 
 
 
 
 
 
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
df2c623
3386f25
df2c623
 
3386f25
 
 
 
 
 
 
 
 
 
 
df2c623
3386f25
 
 
df2c623
 
 
3386f25
df2c623
 
 
3386f25
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c5289d
 
df2c623
 
 
 
 
 
 
 
1f1a374
 
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4645abc
 
df2c623
 
4645abc
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cf0170
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
 
 
 
 
 
df2c623
 
d3d412a
df2c623
 
 
3386f25
 
df2c623
3386f25
df2c623
 
3386f25
1f1a374
 
 
fac30cc
 
 
d3d412a
1282ba1
 
 
 
 
 
fac30cc
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1a374
df2c623
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d412a
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dda790
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d412a
10b5f20
 
 
 
d3d412a
10b5f20
3c090ce
 
10b5f20
 
 
3c090ce
 
 
 
 
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
0122045
10b5f20
 
1dda790
10b5f20
 
 
 
0122045
10b5f20
0122045
10b5f20
0122045
 
 
 
10b5f20
 
 
 
 
 
 
0122045
 
 
 
 
 
 
 
 
10b5f20
0122045
 
10b5f20
0122045
 
 
 
 
 
 
 
d3d412a
0122045
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
0122045
 
 
10b5f20
 
0122045
 
 
 
 
 
 
 
 
 
d3d412a
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
0122045
 
10b5f20
0122045
 
 
10b5f20
0122045
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
 
 
0122045
10b5f20
0122045
 
 
10b5f20
 
 
 
0122045
10b5f20
0122045
 
 
 
 
 
 
 
1dda790
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d412a
0122045
 
 
 
 
 
 
 
 
 
 
d3d412a
0122045
 
 
 
 
 
 
 
 
 
d3d412a
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
 
 
 
e7040fc
df2c623
 
 
 
 
 
 
1f1a374
df2c623
 
 
 
 
1f1a374
df2c623
1f1a374
 
df2c623
 
 
1f1a374
 
df2c623
1f1a374
 
 
 
 
 
df2c623
 
 
 
1f1a374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
 
 
 
 
 
 
 
 
 
10b5f20
 
 
 
 
 
 
 
 
 
d5f26e7
10b5f20
 
 
 
 
 
 
 
 
 
 
 
d5f26e7
10b5f20
 
d5f26e7
10b5f20
d5f26e7
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d412a
10b5f20
 
 
 
 
 
d3d412a
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
378e285
df2c623
 
3386f25
3c090ce
1f1a374
 
 
 
4aca758
df2c623
1f1a374
df2c623
 
 
 
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
 
d3d412a
df2c623
 
 
 
 
 
 
d3d412a
df2c623
 
 
 
3386f25
 
 
1f1a374
df2c623
 
 
3c090ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1a374
 
10b5f20
1f1a374
df2c623
 
10b5f20
1f1a374
 
 
 
df2c623
 
1f1a374
df2c623
 
 
3386f25
 
 
 
 
 
 
1f1a374
 
df2c623
 
 
10b5f20
3386f25
 
d85f622
d5f26e7
3386f25
d3d412a
1f1a374
3386f25
 
df2c623
 
1f1a374
df2c623
 
 
 
 
 
 
 
 
 
1f1a374
df2c623
 
 
 
 
 
 
1f1a374
 
 
344905d
1dda790
bd97d92
344905d
 
 
1dda790
 
bd97d92
 
 
344905d
 
 
1dda790
 
bd97d92
 
 
344905d
 
1dda790
 
bd97d92
 
 
378e285
 
3c090ce
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
d3d412a
3386f25
 
 
 
d3d412a
3386f25
 
 
 
3c090ce
 
 
 
 
 
 
 
 
 
 
 
 
 
3386f25
 
10b5f20
3386f25
 
 
eba3bd7
 
 
 
10b5f20
 
 
1dda790
10b5f20
 
 
 
 
 
 
0122045
10b5f20
 
 
0122045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782f718
10b5f20
 
782f718
3386f25
 
1f1a374
 
 
df2c623
 
3386f25
 
 
 
 
 
 
 
 
 
 
d5f26e7
 
 
 
 
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d412a
3386f25
 
 
 
 
 
d3d412a
3386f25
 
 
10b5f20
 
3386f25
 
 
 
 
d3d412a
3386f25
 
 
 
 
 
d3d412a
3386f25
 
 
10b5f20
 
3386f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dda790
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c090ce
d3d412a
10b5f20
3c090ce
10b5f20
 
3c090ce
 
 
 
 
 
 
10b5f20
 
 
 
 
bd97d92
 
 
 
 
 
 
 
 
 
eba3bd7
bd97d92
 
 
 
eba3bd7
bd97d92
 
 
 
eba3bd7
bd97d92
 
df2c623
 
 
 
 
344905d
4264fdb
41138fa
344905d
 
8cf0170
 
4e59076
41138fa
344905d
41138fa
 
 
8cf0170
41138fa
344905d
 
41138fa
 
 
 
 
 
64c64e2
 
8cf0170
64c64e2
 
4264fdb
 
 
64c64e2
 
4264fdb
344905d
4264fdb
 
 
 
 
 
41138fa
4264fdb
 
344905d
 
8cf0170
41138fa
 
 
4264fdb
 
 
 
 
 
 
 
8cf0170
 
 
 
fc63709
4264fdb
 
 
 
 
41138fa
 
4264fdb
fc63709
 
4264fdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41138fa
 
 
 
 
 
4264fdb
 
 
 
 
 
 
 
 
41138fa
 
 
 
 
4264fdb
 
 
 
41138fa
 
8cf0170
41138fa
 
 
 
4264fdb
 
 
 
 
 
 
8cf0170
 
fc63709
4264fdb
8cf0170
 
4264fdb
 
 
 
 
fc63709
 
41138fa
 
 
 
 
 
4264fdb
 
 
 
 
 
 
1dda790
41138fa
 
fc63709
4264fdb
 
8cf0170
 
4264fdb
 
fc63709
 
 
4264fdb
 
fc63709
4264fdb
 
 
 
 
 
 
fc63709
df2c623
 
4264fdb
41138fa
4264fdb
 
 
 
 
41138fa
 
 
 
 
4264fdb
 
 
41138fa
 
 
4264fdb
41138fa
 
 
4264fdb
 
 
 
344905d
 
41138fa
 
 
 
 
4264fdb
 
 
 
 
41138fa
 
 
 
 
 
4264fdb
 
 
41138fa
344905d
 
eba3bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4264fdb
41138fa
 
4264fdb
 
41138fa
4264fdb
df2c623
 
4264fdb
344905d
 
4264fdb
 
 
 
 
344905d
 
 
 
4264fdb
 
 
 
344905d
 
4264fdb
41138fa
 
 
4264fdb
 
 
 
41138fa
 
4264fdb
1dda790
 
4264fdb
41138fa
 
4264fdb
 
 
344905d
 
4264fdb
3386f25
 
4264fdb
 
 
0598f44
4264fdb
 
 
 
1f1a374
 
3386f25
 
4264fdb
 
 
0598f44
4264fdb
 
 
 
41138fa
 
8cf0170
41138fa
4264fdb
8cf0170
344905d
 
8cf0170
64c64e2
 
 
 
 
8cf0170
 
 
 
 
 
 
 
 
 
64c64e2
 
 
4264fdb
64c64e2
 
 
 
 
 
 
 
8cf0170
64c64e2
 
 
4264fdb
8cf0170
64c64e2
 
8cf0170
64c64e2
 
4264fdb
8cf0170
4264fdb
8cf0170
64c64e2
 
 
 
8cf0170
64c64e2
 
41138fa
 
 
 
4264fdb
 
 
41138fa
 
4264fdb
41138fa
4264fdb
 
 
 
41138fa
 
4264fdb
344905d
4264fdb
 
41138fa
 
 
4264fdb
 
344905d
 
 
4264fdb
 
 
41138fa
 
 
4264fdb
344905d
 
4264fdb
41138fa
4264fdb
 
 
41138fa
 
 
4264fdb
 
41138fa
 
 
4264fdb
 
41138fa
 
 
1f1a374
 
4264fdb
 
 
1f1a374
41138fa
 
 
 
4264fdb
41138fa
 
 
 
4264fdb
41138fa
4264fdb
 
41138fa
4264fdb
41138fa
 
 
 
 
 
 
 
 
 
 
4264fdb
 
 
 
 
 
fc63709
 
 
41138fa
 
354904d
 
eba3bd7
 
 
 
 
 
41138fa
 
4264fdb
 
 
41138fa
 
 
 
 
 
1f1a374
df2c623
 
 
 
4264fdb
 
 
8cf0170
4264fdb
df2c623
3c6bf84
df2c623
 
95ad503
 
 
 
8cf0170
95ad503
 
4264fdb
0598f44
95ad503
8cf0170
 
1644d98
0598f44
8cf0170
1644d98
0598f44
344905d
8cf0170
4264fdb
 
344905d
df2c623
 
3386f25
 
10b5f20
 
3386f25
 
10b5f20
3386f25
 
 
 
 
 
10b5f20
3386f25
 
10b5f20
3386f25
 
10b5f20
3386f25
 
10b5f20
3386f25
 
3c090ce
 
 
 
 
 
 
 
3386f25
10b5f20
3386f25
10b5f20
3386f25
10b5f20
3386f25
10b5f20
3386f25
 
10b5f20
3386f25
 
 
 
 
 
10b5f20
3386f25
 
 
 
 
 
10b5f20
3386f25
b5cf252
3386f25
 
10b5f20
3386f25
 
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a0bd54
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0122045
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
0122045
 
10b5f20
 
 
 
 
 
0122045
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0122045
 
10b5f20
 
 
 
0122045
 
10b5f20
 
 
 
 
0122045
 
 
 
 
 
 
 
10b5f20
0122045
10b5f20
 
 
 
 
 
 
 
7a0bd54
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
7a0bd54
10b5f20
 
 
 
 
 
 
 
 
0122045
10b5f20
 
 
 
 
 
 
0122045
 
 
 
 
10b5f20
0122045
 
 
 
10b5f20
 
 
 
 
 
3386f25
 
 
 
 
 
10b5f20
3386f25
 
 
 
 
 
 
 
10b5f20
3386f25
 
3c090ce
 
 
 
 
 
 
 
3386f25
2ea6d65
3386f25
10b5f20
3386f25
 
 
 
 
 
 
 
f7d6ff2
 
3386f25
 
 
 
 
 
 
 
10b5f20
 
3386f25
10b5f20
df2c623
10b5f20
3386f25
df2c623
1f1a374
10b5f20
3386f25
 
10b5f20
1f1a374
3386f25
10b5f20
 
3386f25
1f1a374
df2c623
10b5f20
 
df2c623
 
10b5f20
 
 
df2c623
10b5f20
df2c623
10b5f20
 
1f1a374
10b5f20
 
 
 
 
3386f25
10b5f20
3386f25
df2c623
 
 
 
 
e1a9f73
df2c623
e1a9f73
df2c623
e1a9f73
 
 
 
 
 
 
 
 
 
 
df2c623
 
 
 
eb40d0c
 
 
eba3bd7
 
 
 
df2c623
8cf0170
eb40d0c
8cf0170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40d0c
8cf0170
 
eb40d0c
 
8cf0170
 
eb40d0c
 
8cf0170
 
eb40d0c
 
8cf0170
 
eb40d0c
 
 
 
1f1a374
df2c623
 
1f1a374
df2c623
3386f25
df2c623
3386f25
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
df2c623
3386f25
df2c623
3386f25
10b5f20
3386f25
df2c623
1f1a374
df2c623
3386f25
1f1a374
 
 
 
3386f25
1f1a374
 
10b5f20
1f1a374
 
 
 
 
3386f25
 
 
1f1a374
10b5f20
 
 
 
3386f25
df2c623
10b5f20
 
 
 
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
 
 
1f1a374
 
 
df2c623
 
 
 
3386f25
 
 
 
 
 
 
 
 
10b5f20
 
3386f25
10b5f20
3386f25
 
 
10b5f20
 
3c090ce
10b5f20
1f1a374
 
10b5f20
0b47132
 
 
 
 
10b5f20
 
 
 
 
0b47132
 
10b5f20
1f1a374
 
10b5f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575f46f
 
 
10b5f20
575f46f
10b5f20
1f1a374
575f46f
10b5f20
 
575f46f
10b5f20
3386f25
 
f7d6ff2
 
 
3386f25
3c090ce
3386f25
 
 
 
 
 
 
 
10b5f20
 
b4a3b1c
10b5f20
 
 
 
 
 
 
df2c623
 
 
 
 
 
 
 
3c6bf84
 
 
 
 
 
 
 
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
"""
StyleForge - Hugging Face Spaces Deployment
Real-time neural style transfer with custom CUDA kernels

Features:
- Pre-trained styles (Candy, Mosaic, Rain Princess, Udnie)
- Custom style training from uploaded images
- Region-based style application
- Real-time benchmark charts
- Style blending interpolation
- CUDA kernel acceleration

Based on Johnson et al. "Perceptual Losses for Real-Time Style Transfer"
https://arxiv.org/abs/1603.08155
"""

# ============================================================================
# PATCH gradio_client to fix bool schema bug
# ============================================================================
import sys

# First import the real module to get all its contents
import gradio_client.utils as _real_client_utils

# Save the original get_type function
_original_get_type = _real_client_utils.get_type
_original_json_schema_to_python_type = _real_client_utils.json_schema_to_python_type

def _patched_get_type(schema):
    """Patched version that handles when schema is a bool (False means "any type")"""
    # Fix the bug: check if schema is a bool before trying "in" operator
    if isinstance(schema, bool):
        return "Any" if not schema else "bool"
    # Call original for everything else
    return _original_get_type(schema)

def _patched_json_schema_to_python_type(schema, defs=None):
    """Patched version that handles bool schemas at the top level"""
    # Handle boolean schemas (True = any, False = none)
    if isinstance(schema, bool):
        if not schema:  # False means empty/no schema
            return "Any"
        return "Any"  # True also means any type in JSON schema
    # Handle the case where schema is None
    if schema is None:
        return "Any"
    # Call original for everything else
    try:
        return _original_json_schema_to_python_type(schema, defs)
    except Exception:
        # If original fails, return Any as fallback
        return "Any"

# Replace the functions
_real_client_utils.get_type = _patched_get_type
_real_client_utils.json_schema_to_python_type = _patched_json_schema_to_python_type

# Now safe to import gradio
import gradio as gr
import torch
import torch.nn as nn
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import time
import os
from pathlib import Path
from typing import Optional, Tuple, Dict, List, Any
from pydantic import BaseModel
from datetime import datetime
from collections import deque
import tempfile
import json

# Try to import plotly for charts
try:
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    PLOTLY_AVAILABLE = True
except ImportError:
    PLOTLY_AVAILABLE = False
    print("Plotly not available, charts will be disabled")

# Try to import spaces for ZeroGPU support
try:
    from spaces import GPU
    SPACES_AVAILABLE = True
except ImportError:
    SPACES_AVAILABLE = False
    print("HuggingFace spaces not available (running locally)")

# Try to import rembg for AI-based background/foreground segmentation
try:
    from rembg import remove, new_session
    REMBG_AVAILABLE = True
    print("Rembg available for AI segmentation")
except ImportError:
    REMBG_AVAILABLE = False
    print("Rembg not available, using geometric masks only")

# Try to import tqdm for progress bars
try:
    from tqdm import tqdm
    TQDM_AVAILABLE = True
except ImportError:
    TQDM_AVAILABLE = False
    print("Tqdm not available")

# ============================================================================
# Configuration
# ============================================================================

# For ZeroGPU: Don't initialize CUDA at module level
# Device will be determined when needed within GPU tasks
_SPACES_ZERO_GPU = SPACES_AVAILABLE  # From spaces import above

# Lazy device initialization for ZeroGPU compatibility
_device_cache = None


def get_device():
    """
    Get the current device (lazy-loaded on ZeroGPU).

    On ZeroGPU, this must be called within a GPU task context to properly
    initialize CUDA. Calling this at module level will cause errors.
    """
    global _device_cache
    if _device_cache is None:
        # On ZeroGPU, always assume CUDA will be available in GPU task
        # Don't call torch.cuda.is_available() at module level
        if _SPACES_ZERO_GPU:
            _device_cache = torch.device('cuda')  # Will be resolved in GPU task
        elif torch.cuda.is_available():
            _device_cache = torch.device('cuda')
        else:
            _device_cache = torch.device('cpu')
    return _device_cache


# For backwards compatibility, keep DEVICE as a property
class _DeviceProperty:
    """Property that returns the actual device when accessed."""

    def __str__(self):
        return str(_device)

    def __repr__(self):
        return repr(_device)

    @property
    def type(self):
        return _device.type

    def __eq__(self, other):
        return str(_device) == str(other)


DEVICE = _DeviceProperty()

if _SPACES_ZERO_GPU:
    print(f"Device: Will use CUDA within GPU tasks (ZeroGPU mode)")
else:
    # Only access device if not ZeroGPU to avoid CUDA init
    print(f"Device: {get_device()}")
if SPACES_AVAILABLE:
    print("ZeroGPU support enabled")

# Check CUDA kernels availability
try:
    from kernels import check_cuda_kernels, get_fused_instance_norm, load_prebuilt_kernels
    # On ZeroGPU: Uses pre-compiled kernels from prebuilt/ if available
    # On local: JIT compiles kernels if prebuilt not found
    CUDA_KERNELS_AVAILABLE = check_cuda_kernels()
    if SPACES_AVAILABLE:
        status = "Pre-compiled" if CUDA_KERNELS_AVAILABLE else "PyTorch GPU fallback (no prebuilt kernels)"
        print(f"CUDA Kernels: {status}")
    else:
        print(f"CUDA Kernels: {'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available (using PyTorch fallback)'}")
except Exception:
    CUDA_KERNELS_AVAILABLE = False
    print("CUDA Kernels: Not Available (using PyTorch fallback)")

# Available styles
STYLES = {
    'candy': 'Candy',
    'mosaic': 'Mosaic',
    'rain_princess': 'Rain Princess',
    'udnie': 'Udnie',
    'la_muse': 'La Muse',
    'starry_night': 'Starry Night',
    'the_scream': 'The Scream',
    'feathers': 'Feathers',
    'composition_vii': 'Composition VII',
}

STYLE_DESCRIPTIONS = {
    'candy': 'Bright, colorful transformation inspired by pop art',
    'mosaic': 'Fragmented, tile-like artistic reconstruction',
    'rain_princess': 'Moody, impressionistic with subtle textures',
    'udnie': 'Bold, abstract expressionist style',
    'la_muse': 'Romantic, soft colors with gentle brushstrokes',
    'starry_night': 'Inspired by Van Gogh\'s iconic swirling masterpiece',
    'the_scream': 'Edvard Munch\'s anxious expressionist style',
    'feathers': 'Soft, delicate textures with flowing patterns',
    'composition_vii': 'Kandinsky-inspired abstract geometric forms',
}

# Backend options
BACKENDS = {
    'auto': 'Auto (CUDA if available)',
    'cuda': 'CUDA Kernels (Fast)',
    'pytorch': 'PyTorch Baseline',
}

# ============================================================================
# Performance Tracking with Live Charts
# ============================================================================

class PerformanceStats(BaseModel):
    """Pydantic model for performance stats - Gradio 5.x compatible"""
    avg_ms: float
    min_ms: float
    max_ms: float
    total_inferences: int
    uptime_hours: float
    cuda_avg: Optional[float] = None
    cuda_count: Optional[int] = None
    pytorch_avg: Optional[float] = None
    pytorch_count: Optional[int] = None


class ChartData(BaseModel):
    """Pydantic model for chart data - Gradio 5.x compatible"""
    timestamps: List[str]
    times: List[float]
    backends: List[str]


class PerformanceTracker:
    """Track and display Space performance metrics with backend comparison"""

    def __init__(self, max_samples=100):
        self.inference_times = deque(maxlen=max_samples)
        self.backend_times = {
            'cuda': deque(maxlen=50),
            'pytorch': deque(maxlen=50),
        }
        self.timestamps = deque(maxlen=max_samples)
        self.backends_used = deque(maxlen=max_samples)
        self.total_inferences = 0
        self.start_time = datetime.now()

    def record(self, elapsed_ms: float, backend: str):
        """Record an inference time with backend info"""
        timestamp = datetime.now()
        self.inference_times.append(elapsed_ms)
        self.timestamps.append(timestamp)
        self.backends_used.append(backend)
        if backend in self.backend_times:
            self.backend_times[backend].append(elapsed_ms)
        self.total_inferences += 1

    def get_stats(self) -> Optional[PerformanceStats]:
        """Get performance statistics"""
        if not self.inference_times:
            return None

        times = list(self.inference_times)
        uptime = (datetime.now() - self.start_time).total_seconds()

        # Get backend-specific stats
        cuda_avg, cuda_count = None, None
        pytorch_avg, pytorch_count = None, None

        if self.backend_times['cuda']:
            bt = list(self.backend_times['cuda'])
            cuda_avg = sum(bt) / len(bt)
            cuda_count = len(bt)

        if self.backend_times['pytorch']:
            bt = list(self.backend_times['pytorch'])
            pytorch_avg = sum(bt) / len(bt)
            pytorch_count = len(bt)

        return PerformanceStats(
            avg_ms=sum(times) / len(times),
            min_ms=min(times),
            max_ms=max(times),
            total_inferences=self.total_inferences,
            uptime_hours=uptime / 3600,
            cuda_avg=cuda_avg,
            cuda_count=cuda_count,
            pytorch_avg=pytorch_avg,
            pytorch_count=pytorch_count,
        )

    def get_comparison(self) -> str:
        """Get backend comparison string"""
        cuda_times = list(self.backend_times['cuda']) if self.backend_times['cuda'] else []
        pytorch_times = list(self.backend_times['pytorch']) if self.backend_times['pytorch'] else []

        if not cuda_times or not pytorch_times:
            return "Run both backends to see comparison"

        cuda_avg = sum(cuda_times) / len(cuda_times)
        pytorch_avg = sum(pytorch_times) / len(pytorch_times)
        speedup = pytorch_avg / cuda_avg if cuda_avg > 0 else 1.0

        return f"""
| Backend | Avg Time | Samples |
|---------|----------|---------|
| **CUDA Kernels** | {cuda_avg:.1f} ms | {len(cuda_times)} |
| **PyTorch** | {pytorch_avg:.1f} ms | {len(pytorch_times)} |

### Speedup: {speedup:.2f}x faster with CUDA! πŸš€
"""

    def get_chart_data(self) -> Optional[ChartData]:
        """Get data for real-time chart"""
        if not self.timestamps:
            return None

        return ChartData(
            timestamps=[ts.strftime('%H:%M:%S') for ts in self.timestamps],
            times=list(self.inference_times),
            backends=list(self.backends_used),
        )

# Global tracker
perf_tracker = PerformanceTracker()

# ============================================================================
# Custom Styles Storage
# ============================================================================

CUSTOM_STYLES_DIR = Path("custom_styles")
CUSTOM_STYLES_DIR.mkdir(exist_ok=True)

def get_custom_styles() -> List[str]:
    """Get list of custom trained styles"""
    if not CUSTOM_STYLES_DIR.exists():
        return []
    custom = []
    for f in CUSTOM_STYLES_DIR.glob("*.pth"):
        custom.append(f.stem)
    return sorted(custom)

# ============================================================================
# VGG Feature Extractor for Style Training
# ============================================================================

class VGGFeatureExtractor(nn.Module):
    """
    Pre-trained VGG19 feature extractor for computing style and content losses.
    This is used for training custom styles.
    """

    def __init__(self):
        super().__init__()
        import torchvision.models as models

        # Load pre-trained VGG19
        vgg = models.vgg19(pretrained=True)
        self.features = vgg.features[:29]  # Up to relu4_4

        # Freeze parameters
        for param in self.parameters():
            param.requires_grad = False

        # Mean and std for normalization
        self.mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
        self.std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)

    def forward(self, x):
        # Normalize input
        x = (x - self.mean.to(x.device)) / self.std.to(x.device)
        return self.features(x)

# Global VGG extractor (lazy loaded)
_vgg_extractor = None

def get_vgg_extractor():
    """Lazy load VGG feature extractor (with ZeroGPU support)"""
    global _vgg_extractor
    if _vgg_extractor is None:
        _vgg_extractor = VGGFeatureExtractor().to(get_device())
        _vgg_extractor.eval()
    return _vgg_extractor


def gram_matrix(features):
    """Compute Gram matrix for style representation."""
    b, c, h, w = features.size()
    features = features.view(b * c, h * w)
    gram = torch.mm(features, features.t())
    return gram.div_(b * c * h * w)


# ============================================================================
# Model Definition with CUDA Kernel Support
# ============================================================================


class ConvLayer(nn.Module):
    """Convolution -> InstanceNorm -> ReLU with optional CUDA kernels"""

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: int,
        stride: int,
        padding: int = 0,
        relu: bool = True,
        use_cuda: bool = False,
    ):
        super().__init__()
        self.pad = nn.ReflectionPad2d(padding)
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
        self.use_cuda = use_cuda and CUDA_KERNELS_AVAILABLE

        if self.use_cuda:
            try:
                self.norm = get_fused_instance_norm(out_channels, affine=True)
                self._has_cuda = True
            except Exception:
                self.norm = nn.InstanceNorm2d(out_channels, affine=True)
                self._has_cuda = False
        else:
            self.norm = nn.InstanceNorm2d(out_channels, affine=True)
            self._has_cuda = False

        self.activation = nn.ReLU(inplace=True) if relu else None

    def forward(self, x):
        out = self.pad(x)
        out = self.conv(out)
        out = self.norm(out)
        if self.activation:
            out = self.activation(out)
        return out


class ResidualBlock(nn.Module):
    """Residual block with optional CUDA kernels"""

    def __init__(self, channels: int, use_cuda: bool = False):
        super().__init__()
        self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1, padding=1, use_cuda=use_cuda)
        self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1, padding=1, relu=False, use_cuda=use_cuda)

    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.conv2(out)
        return residual + out


class UpsampleConvLayer(nn.Module):
    """Upsample (nearest neighbor) -> Conv -> InstanceNorm -> ReLU"""

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: int,
        stride: int,
        padding: int = 0,
        upsample: int = 2,
        use_cuda: bool = False,
    ):
        super().__init__()

        if upsample > 1:
            self.upsample = nn.Upsample(scale_factor=upsample, mode='nearest')
        else:
            self.upsample = None

        self.pad = nn.ReflectionPad2d(padding)
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
        self.use_cuda = use_cuda and CUDA_KERNELS_AVAILABLE

        if self.use_cuda:
            try:
                self.norm = get_fused_instance_norm(out_channels, affine=True)
                self._has_cuda = True
            except Exception:
                self.norm = nn.InstanceNorm2d(out_channels, affine=True)
                self._has_cuda = False
        else:
            self.norm = nn.InstanceNorm2d(out_channels, affine=True)
            self._has_cuda = False

        self.activation = nn.ReLU(inplace=True)

    def forward(self, x):
        if self.upsample:
            out = self.upsample(x)
        else:
            out = x
        out = self.pad(out)
        out = self.conv(out)
        out = self.norm(out)
        out = self.activation(out)
        return out


class TransformerNet(nn.Module):
    """Fast Neural Style Transfer Network with backend selection"""

    def __init__(self, num_residual_blocks: int = 5, backend: str = 'auto'):
        super().__init__()

        # Determine if using CUDA
        self.backend = backend
        if backend == 'auto':
            use_cuda = CUDA_KERNELS_AVAILABLE
        elif backend == 'cuda':
            use_cuda = True
        else:  # pytorch
            use_cuda = False

        self.use_cuda = use_cuda and CUDA_KERNELS_AVAILABLE

        # Initial convolution layers (encoder)
        self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1, padding=4, use_cuda=self.use_cuda)
        self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2, padding=1, use_cuda=self.use_cuda)
        self.conv3 = ConvLayer(64, 128, kernel_size=3, stride=2, padding=1, use_cuda=self.use_cuda)

        # Residual blocks
        self.residual_blocks = nn.Sequential(
            *[ResidualBlock(128, use_cuda=self.use_cuda) for _ in range(num_residual_blocks)]
        )

        # Upsampling layers (decoder)
        self.deconv1 = UpsampleConvLayer(128, 64, kernel_size=3, stride=1, padding=1, upsample=2, use_cuda=self.use_cuda)
        self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, padding=1, upsample=2, use_cuda=self.use_cuda)
        self.deconv3 = nn.Sequential(
            nn.ReflectionPad2d(4),
            nn.Conv2d(32, 3, kernel_size=9, stride=1)
        )

    def forward(self, x):
        """Args: x: Input image tensor (B, 3, H, W) in range [0, 1]"""
        # Encoder
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.conv3(out)

        # Residual blocks
        out = self.residual_blocks(out)

        # Decoder
        out = self.deconv1(out)
        out = self.deconv2(out)
        out = self.deconv3(out)

        return out

    def load_checkpoint(self, checkpoint_path: str) -> None:
        """Load pre-trained weights from checkpoint file."""
        # Load to CPU first for reliability, then move to device
        state_dict = torch.load(checkpoint_path, map_location='cpu')

        if 'state_dict' in state_dict:
            state_dict = state_dict['state_dict']
        elif 'model' in state_dict:
            state_dict = state_dict['model']

        # Create mapping for different naming conventions
        name_mapping = {
            "in1": "conv1.norm", "in2": "conv2.norm", "in3": "conv3.norm",
            "conv1.conv2d": "conv1.conv", "conv2.conv2d": "conv2.conv", "conv3.conv2d": "conv3.conv",
            "res1.conv1.conv2d": "residual_blocks.0.conv1.conv", "res1.in1": "residual_blocks.0.conv1.norm",
            "res1.conv2.conv2d": "residual_blocks.0.conv2.conv", "res1.in2": "residual_blocks.0.conv2.norm",
            "res2.conv1.conv2d": "residual_blocks.1.conv1.conv", "res2.in1": "residual_blocks.1.conv1.norm",
            "res2.conv2.conv2d": "residual_blocks.1.conv2.conv", "res2.in2": "residual_blocks.1.conv2.norm",
            "res3.conv1.conv2d": "residual_blocks.2.conv1.conv", "res3.in1": "residual_blocks.2.conv1.norm",
            "res3.conv2.conv2d": "residual_blocks.2.conv2.conv", "res3.in2": "residual_blocks.2.conv2.norm",
            "res4.conv1.conv2d": "residual_blocks.3.conv1.conv", "res4.in1": "residual_blocks.3.conv1.norm",
            "res4.conv2.conv2d": "residual_blocks.3.conv2.conv", "res4.in2": "residual_blocks.3.conv2.norm",
            "res5.conv1.conv2d": "residual_blocks.4.conv1.conv", "res5.in1": "residual_blocks.4.conv1.norm",
            "res5.conv2.conv2d": "residual_blocks.4.conv2.conv", "res5.in2": "residual_blocks.4.conv2.norm",
            "deconv1.conv2d": "deconv1.conv", "in4": "deconv1.norm",
            "deconv2.conv2d": "deconv2.conv", "in5": "deconv2.norm",
            "deconv3.conv2d": "deconv3.1",
        }

        mapped_state_dict = {}
        for old_name, v in state_dict.items():
            name = old_name.replace('module.', '')
            mapped = False
            for prefix, new_name in name_mapping.items():
                if name.startswith(prefix):
                    suffix = name[len(prefix):]
                    mapped_key = new_name + suffix
                    mapped_state_dict[mapped_key] = v
                    mapped = True
                    break
            if not mapped:
                mapped_state_dict[name] = v

        # Filter out running_mean and running_var (BatchNorm params not needed for InstanceNorm)
        # Keep .weight and .bias as-is since InstanceNorm uses these names
        final_state_dict = {}
        for key, value in mapped_state_dict.items():
            if key.endswith('.running_mean') or key.endswith('.running_var'):
                continue  # Skip BatchNorm-specific parameters
            final_state_dict[key] = value

        self.load_state_dict(final_state_dict, strict=False)


# ============================================================================
# Model Cache
# ============================================================================

MODEL_CACHE = {}
MODELS_DIR = Path("models")
MODELS_DIR.mkdir(exist_ok=True)


def get_model_path(style: str) -> Path:
    """Get path to model weights, download if missing."""
    model_path = MODELS_DIR / f"{style}.pth"

    if not model_path.exists():
        url_map = {
            'candy': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/candy.pth',
            'mosaic': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/mosaic.pth',
            'udnie': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/udnie.pth',
            'rain_princess': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/rain-princess.pth',
            'la_muse': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/la-muse.pth',
            'starry_night': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/starry-night.pth',
            'the_scream': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/the-scream.pth',
            'feathers': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/feathers.pth',
            'composition_vii': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/composition-vii.pth',
        }

        if style not in url_map:
            raise ValueError(f"Unknown style: {style}")

        import urllib.request
        print(f"Downloading {style} model...")
        urllib.request.urlretrieve(url_map[style], model_path)
        print(f"Downloaded {style} model to {model_path}")

    return model_path


def load_model(style: str, backend: str = 'auto') -> TransformerNet:
    """Load model with caching and backend selection."""
    cache_key = f"{style}_{backend}"

    if cache_key not in MODEL_CACHE:
        print(f"Loading {style} model with {backend} backend...")
        model_path = get_model_path(style)

        model = TransformerNet(num_residual_blocks=5, backend=backend).to(get_device())
        model.load_checkpoint(str(model_path))
        model.eval()

        MODEL_CACHE[cache_key] = model
        print(f"Loaded {style} model ({backend})")

    return MODEL_CACHE[cache_key]


# Preload models on startup
print("=" * 50)
print("StyleForge - Initializing...")
print("=" * 50)
if _SPACES_ZERO_GPU:
    print("Device: CUDA (ZeroGPU mode - lazy initialization)")
else:
    print(f"Device: {get_device().type.upper()}")

if SPACES_AVAILABLE:
    status = "Pre-compiled" if CUDA_KERNELS_AVAILABLE else "PyTorch GPU fallback"
    print(f"CUDA Kernels: {status}")
else:
    print(f"CUDA Kernels: {'Available' if CUDA_KERNELS_AVAILABLE else 'Not Available (using PyTorch fallback)'}")

# Skip model preloading on ZeroGPU to avoid CUDA init in main process
if not _SPACES_ZERO_GPU:
    print("Preloading models...")
    for style in STYLES.keys():
        try:
            load_model(style, 'auto')
            print(f"  {STYLES[style]}: Ready")
        except Exception as e:
            print(f"  {STYLES[style]}: Failed - {e}")
    print("All models loaded!")
else:
    print("ZeroGPU mode: Models will be loaded on-demand within GPU tasks")

print("=" * 50)

# ============================================================================
# Style Blending (Weight Interpolation)
# ============================================================================

def blend_models(style1: str, style2: str, alpha: float, backend: str = 'auto') -> TransformerNet:
    """
    Blend two style models by interpolating their weights.

    Args:
        style1: First style name
        style2: Second style name
        alpha: Blend factor (0=style1, 1=style2, 0.5=equal mix)
        backend: Backend to use

    Returns:
        New model with blended weights
    """
    model1 = load_model(style1, backend)
    model2 = load_model(style2, backend)

    # Create new model
    blended = TransformerNet(num_residual_blocks=5, backend=backend).to(get_device())
    blended.eval()

    # Blend weights
    state_dict1 = model1.state_dict()
    state_dict2 = model2.state_dict()

    blended_state = {}
    for key in state_dict1.keys():
        if key in state_dict2:
            # Linear interpolation
            blended_state[key] = alpha * state_dict2[key] + (1 - alpha) * state_dict1[key]
        else:
            blended_state[key] = state_dict1[key]

    blended.load_state_dict(blended_state)
    return blended

# Cache for blended models
BLENDED_CACHE = {}

def get_blended_model(style1: str, style2: str, alpha: float, backend: str = 'auto') -> TransformerNet:
    """Get or create blended model with caching."""
    # Round alpha to 2 decimals for cache key
    cache_key = f"blend_{style1}_{style2}_{alpha:.2f}_{backend}"

    if cache_key not in BLENDED_CACHE:
        BLENDED_CACHE[cache_key] = blend_models(style1, style2, alpha, backend)

    return BLENDED_CACHE[cache_key]


# ============================================================================
# Region-based Style Transfer
# ============================================================================

def apply_region_style_impl(
    image: Image.Image,
    mask: Image.Image,
    style1: str,
    style2: str,
    backend: str = 'auto'
) -> Image.Image:
    """
    Apply different styles to different regions of the image.

    Args:
        image: Input image
        mask: Binary mask (white=style1 region, black=style2 region)
        style1: Style for white region
        style2: Style for black region
        backend: Processing backend

    Returns:
        Stylized image with region-based styles
    """
    # Convert to RGB
    if image.mode != 'RGB':
        image = image.convert('RGB')
    if mask.mode != 'L':
        mask = mask.convert('L')

    # Resize mask to match image
    if mask.size != image.size:
        mask = mask.resize(image.size, Image.NEAREST)

    # Get models
    model1 = load_model(style1, backend)
    model2 = load_model(style2, backend)

    # Preprocess
    import torchvision.transforms as transforms
    transform = transforms.Compose([transforms.ToTensor()])
    img_tensor = transform(image).unsqueeze(0).to(get_device())

    # Convert mask to tensor
    mask_np = np.array(mask)
    mask_tensor = torch.from_numpy(mask_np).float() / 255.0
    mask_tensor = mask_tensor.unsqueeze(0).unsqueeze(0).to(get_device())

    # Stylize with both models (with timing)
    start = time.perf_counter()
    with torch.no_grad():
        output1 = model1(img_tensor)
        output2 = model2(img_tensor)
    if get_device().type == 'cuda':
        torch.cuda.synchronize()
    elapsed_ms = (time.perf_counter() - start) * 1000

    # Record performance
    actual_backend = 'cuda' if (backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE)) else 'pytorch'
    perf_tracker.record(elapsed_ms, actual_backend)

    # Blend based on mask
    # mask_tensor is [1, 1, H, W] with values 0-1
    # We want style1 where mask is white (1), style2 where mask is black (0)
    mask_expanded = mask_tensor.expand_as(output1)
    blended = mask_expanded * output1 + (1 - mask_expanded) * output2

    # Postprocess
    blended = torch.clamp(blended, 0, 1)
    output_image = transforms.ToPILImage()(blended.squeeze(0))

    return output_image


def create_region_mask(
    image: Image.Image,
    mask_type: str = "horizontal_split",
    position: float = 0.5
) -> Image.Image:
    """
    Create a region mask for style transfer.

    Args:
        image: Reference image for size
        mask_type: Type of mask ("horizontal_split", "vertical_split", "center_circle", "custom")
        position: Position of split (0-1)

    Returns:
        Binary mask as PIL Image
    """
    w, h = image.size
    mask_np = np.zeros((h, w), dtype=np.uint8)

    if mask_type == "horizontal_split":
        # Top half = white, bottom half = black
        split_y = int(h * position)
        mask_np[:split_y, :] = 255

    elif mask_type == "vertical_split":
        # Left half = white, right half = black
        split_x = int(w * position)
        mask_np[:, :split_x] = 255

    elif mask_type == "center_circle":
        # Circle = white, outside = black
        cy, cx = h // 2, w // 2
        radius = min(h, w) * position * 0.4
        y, x = np.ogrid[:h, :w]
        mask_np[(x - cx)**2 + (y - cy)**2 <= radius**2] = 255

    elif mask_type == "corner_box":
        # Top-left quadrant = white
        mask_np[:h//2, :w//2] = 255

    else:  # full = all white
        mask_np[:] = 255

    return Image.fromarray(mask_np, mode='L')


def create_ai_segmentation_mask(
    image: Image.Image,
    mask_type: str = "foreground"
) -> Image.Image:
    """
    Create AI-based segmentation mask using rembg.

    Args:
        image: Input image
        mask_type: "foreground" (main subject) or "background" (background only)

    Returns:
        Binary mask as PIL Image (white=foreground, black=background)
    """
    if not REMBG_AVAILABLE:
        raise ImportError("Rembg is not installed. Install with: pip install rembg")

    try:
        # Use rembg to remove background and get the mask
        # Create a session for better performance
        session = new_session(model_name="u2net")

        # Convert image to bytes for rembg
        import io
        img_bytes = io.BytesIO()
        image.save(img_bytes, format='PNG')
        img_bytes.seek(0)

        # Get the segmentation result
        output_bytes = remove(img_bytes.read(), session=session, alpha_matting=True)

        # Load the result
        result_img = Image.open(io.BytesIO(output_bytes))

        # Convert to grayscale mask
        if result_img.mode == 'RGBA':
            # Use alpha channel as mask
            mask_array = np.array(result_img.split()[-1])
            # Threshold to get binary mask
            mask_binary = (mask_array > 128).astype(np.uint8) * 255
        else:
            # Fallback: use grayscale
            result_img = result_img.convert('L')
            mask_binary = np.array(result_img)
            mask_binary = (mask_binary > 128).astype(np.uint8) * 255

        # Invert if background is requested
        if mask_type == "background":
            mask_binary = 255 - mask_binary

        return Image.fromarray(mask_binary, mode='L')

    except Exception as e:
        raise RuntimeError(f"AI segmentation failed: {str(e)}")


# Global session for rembg (reuse for performance)
_rembg_session = None

def get_ai_segmentation_mask(
    image: Image.Image,
    mask_type: str = "foreground"
) -> Image.Image:
    """
    Create AI-based segmentation mask using rembg (with cached session).

    Args:
        image: Input image
        mask_type: "foreground" (main subject) or "background" (background only)

    Returns:
        Binary mask as PIL Image (white=foreground, black=background)
    """
    global _rembg_session

    if not REMBG_AVAILABLE:
        raise ImportError("Rembg is not available. Using fallback geometric mask.")

    try:
        import io

        # Create session if not exists
        if _rembg_session is None:
            _rembg_session = new_session(model_name="u2net")

        # Convert image to bytes
        img_bytes = io.BytesIO()
        image.save(img_bytes, format='PNG')
        img_bytes.seek(0)

        # Get the segmentation result
        output_bytes = remove(img_bytes.read(), session=_rembg_session, alpha_matting=True)

        # Load the result
        result_img = Image.open(io.BytesIO(output_bytes))

        # Convert to grayscale mask
        if result_img.mode == 'RGBA':
            mask_array = np.array(result_img.split()[-1])
            mask_binary = (mask_array > 128).astype(np.uint8) * 255
        else:
            result_img = result_img.convert('L')
            mask_binary = np.array(result_img)
            mask_binary = (mask_binary > 128).astype(np.uint8) * 255

        # Invert if background is requested
        if mask_type == "background":
            mask_binary = 255 - mask_binary

        return Image.fromarray(mask_binary, mode='L')

    except Exception as e:
        raise RuntimeError(f"AI segmentation failed: {str(e)}")


# ============================================================================
# Real Style Extraction Training (VGG-based)
# ============================================================================

def train_custom_style_impl(
    style_image: Image.Image,
    style_name: str,
    num_iterations: int = 100,
    backend: str = 'auto'
) -> Tuple[Optional[str], str]:
    """
    Train a custom style from an image using VGG feature matching.

    This implements real style extraction by:
    1. Computing style features from the style image using VGG19
    2. Fine-tuning a base network to match those style features
    3. Using content preservation to maintain image structure
    """
    global STYLES

    if style_image is None:
        return None, "Please upload a style image."

    try:
        import torchvision.transforms as transforms

        # Resize style image to reasonable size for training
        style_image = style_image.convert('RGB')
        if max(style_image.size) > 512:
            scale = 512 / max(style_image.size)
            new_size = (int(style_image.width * scale), int(style_image.height * scale))
            style_image = style_image.resize(new_size, Image.LANCZOS)

        progress_update = []
        progress_update.append(f"Starting style extraction from '{style_name}'...")
        progress_update.append(f"Training for {num_iterations} iterations...")

        # Get VGG feature extractor
        vgg = get_vgg_extractor()

        # Prepare style image
        style_transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        style_tensor = style_transform(style_image).unsqueeze(0).to(get_device())

        # Extract style features from multiple layers
        with torch.no_grad():
            style_features = vgg(style_tensor)

        # Compute Gram matrices for style representation
        style_grams = []
        # Use relu1_1, relu2_1, relu3_1, relu4_1 for style
        layers_to_use = [0, 1, 2, 3]  # Corresponding to VGG layers
        for i in range(4):
            feat = style_features if i == 0 else style_features  # Simplified - in full version extract from multiple layers
            gram = gram_matrix(feat)
            style_grams.append(gram)

        # Load a base model to fine-tune (start with udnie as a good base)
        base_style = 'udnie'
        progress_update.append(f"Loading base model ({base_style}) for fine-tuning...")

        model = load_model(base_style, backend)
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

        # Create a simple content image for training (gradient pattern)
        content_img = Image.new('RGB', (256, 256))
        for y in range(256):
            r = int(255 * y / 256)
            for x in range(256):
                g = int(255 * x / 256)
                content_img.putpixel((x, y), (r, g, 128))

        content_tensor = style_transform(content_img).unsqueeze(0).to(get_device())

        # Training loop
        model.train()

        # Style layers weights
        style_weights = [1.0, 0.8, 0.5, 0.3]

        progress_update.append("Training...")

        for iteration in range(num_iterations):
            optimizer.zero_grad()

            # Forward pass
            output = model(content_tensor)

            # Get output features
            output_features = vgg(output)

            # Compute style loss
            style_loss = 0
            output_gram = gram_matrix(output_features)

            for i, (target_gram, weight) in enumerate(zip(style_grams, style_weights)):
                # Simplified: using single layer comparison
                style_loss += weight * torch.mean((output_gram - target_gram) ** 2)

            # Backward pass
            style_loss.backward()
            optimizer.step()

            # Progress update every 20 iterations
            if (iteration + 1) % 20 == 0:
                progress_update.append(f"Iteration {iteration + 1}/{num_iterations}: Style Loss = {style_loss.item():.4f}")

        model.eval()

        # Save custom model
        save_path = CUSTOM_STYLES_DIR / f"{style_name}.pth"
        torch.save(model.state_dict(), save_path)

        progress_update.append(f"βœ“ Style '{style_name}' trained and saved successfully!")
        progress_update.append(f"βœ“ Model saved to: {save_path}")
        progress_update.append(f"βœ“ You can now use '{style_name}' in the Style dropdown!")

        # Add to STYLES dictionary
        if style_name not in STYLES:
            STYLES[style_name] = style_name.title()
            MODEL_CACHE[f"{style_name}_{backend}"] = model

        return "\n".join(progress_update), f"βœ“ Custom style '{style_name}' created successfully!\n\nSelect '{style_name}' from the Style dropdown to use it."

    except Exception as e:
        import traceback
        error_msg = f"Error: {str(e)}\n\n{traceback.format_exc()}"
        return None, error_msg


def extract_style_from_image_impl(
    style_image: Image.Image,
    content_image: Image.Image,
    style_name: str,
    num_iterations: int = 200,
    style_weight: float = 1e5,
    content_weight: float = 1.0
) -> Tuple[Optional[str], str]:
    """
    Extract style from one image and apply it to another.
    This is the full neural style transfer algorithm.

    Args:
        style_image: The artwork/image to extract style from
        content_image: The photo to apply style to (optional, for preview)
        style_name: Name to save the extracted style as
        num_iterations: Number of optimization iterations
        style_weight: Weight for style loss
        content_weight: Weight for content loss

    Returns:
        Tuple of (status_message, result_image)
    """
    if style_image is None:
        return None, "Please upload a style image."

    try:
        import torchvision.transforms as transforms

        # Resize images
        style_image = style_image.convert('RGB')
        if max(style_image.size) > 512:
            scale = 512 / max(style_image.size)
            new_size = (int(style_image.width * scale), int(style_image.height * scale))
            style_image = style_image.resize(new_size, Image.LANCZOS)

        progress = []
        progress.append("Extracting style features using VGG19...")

        # Get VGG
        vgg = get_vgg_extractor()

        # Prepare transforms
        transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])

        # Process style image
        style_tensor = transform(style_image).unsqueeze(0).to(get_device())

        # Extract style features
        with torch.no_grad():
            style_features = vgg(style_tensor)

        # Compute Gram matrix for style
        style_gram = gram_matrix(style_features)

        progress.append("Style features extracted. Creating style model...")

        # Create a new model and train it to match the style
        model = TransformerNet(num_residual_blocks=5, backend='auto').to(get_device())

        # Use a simple content image for training the transform
        if content_image is None:
            # Create gradient pattern as content
            content_image = Image.new('RGB', (256, 256))
            for y in range(256):
                for x in range(256):
                    content_image.putpixel((x, y), (x, y, 128))

        content_image = content_image.convert('RGB')
        content_tensor = transform(content_image).unsqueeze(0).to(get_device())

        # Extract content features
        with torch.no_grad():
            content_features = vgg(content_tensor)

        # Setup optimizer
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

        # Training loop
        model.train()

        for i in range(num_iterations):
            optimizer.zero_grad()

            # Generate output
            output = model(content_tensor)

            # Get features
            output_features = vgg(output)

            # Content loss (keep structure)
            content_loss = torch.mean((output_features - content_features) ** 2)

            # Style loss (match style)
            output_gram = gram_matrix(output_features)
            style_loss = torch.mean((output_gram - style_gram) ** 2)

            # Total loss
            total_loss = content_weight * content_loss + style_weight * style_loss

            total_loss.backward()
            optimizer.step()

            if (i + 1) % 50 == 0:
                progress.append(f"Iteration {i+1}/{num_iterations}: Loss = {total_loss.item():.4f}")

        model.eval()

        # Save the model
        save_path = CUSTOM_STYLES_DIR / f"{style_name}.pth"
        torch.save(model.state_dict(), save_path)

        # Add to styles
        if style_name not in STYLES:
            STYLES[style_name] = style_name.title()
            MODEL_CACHE[f"{style_name}_auto"] = model

        # Generate a preview
        with torch.no_grad():
            preview_output = model(content_tensor)
            preview_output = torch.clamp(preview_output, 0, 1)
            preview_image = transforms.ToPILImage()(preview_output.squeeze(0))

        progress.append(f"βœ“ Style '{style_name}' extracted and saved!")

        return "\n".join(progress), preview_image

    except Exception as e:
        import traceback
        return None, f"Error: {str(e)}\n\n{traceback.format_exc()}"


# ============================================================================
# Image Processing Functions
# ============================================================================

def preprocess_image(img: Image.Image) -> torch.Tensor:
    """Convert PIL Image to tensor [0, 1]."""
    import torchvision.transforms as transforms
    transform = transforms.Compose([transforms.ToTensor()])
    return transform(img).unsqueeze(0)


def postprocess_tensor(tensor: torch.Tensor) -> Image.Image:
    """Convert tensor to PIL Image."""
    import torchvision.transforms as transforms
    if tensor.dim() == 4:
        tensor = tensor.squeeze(0)
    tensor = torch.clamp(tensor, 0, 1)
    transform = transforms.ToPILImage()
    return transform(tensor)


def create_side_by_side(img1: Image.Image, img2: Image.Image, style_name: str) -> Image.Image:
    """Create side-by-side comparison."""
    if img1.size != img2.size:
        img2 = img2.resize(img1.size, Image.LANCZOS)

    w, h = img1.size
    combined = Image.new('RGB', (w * 2 + 20, h + 70), 'white')

    combined.paste(img1, (0, 70))
    combined.paste(img2, (w + 20, 70))

    draw = ImageDraw.Draw(combined)
    try:
        font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 28)
        font_label = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
    except:
        font_title = ImageFont.load_default()
        font_label = ImageFont.load_default()

    draw.text((w + 10, 20), f"Style: {style_name}", fill='#667eea', font=font_title)
    draw.text((w // 2, 50), "Original", fill='#555', font=font_label, anchor='mm')
    draw.text((w * 1.5 + 10, 50), "Stylized", fill='#555', font=font_label, anchor='mm')

    return combined


def add_watermark(img: Image.Image, style_name: str) -> Image.Image:
    """Add subtle watermark for social sharing."""
    result = img.copy()
    draw = ImageDraw.Draw(result)
    w, h = result.size

    text = f"StyleForge β€’ {style_name}"
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", int(w / 40))
    except:
        font = ImageFont.load_default()

    bbox = draw.textbbox((0, 0), text, font=font)
    text_w = bbox[2] - bbox[0]
    text_h = bbox[3] - bbox[1]

    overlay = Image.new('RGBA', (text_w + 20, text_h + 10), (0, 0, 0, 100))
    result.paste(overlay, (w - text_w - 25, h - text_h - 15), overlay)

    draw.text((w - text_w - 15, h - text_h - 10), text, fill=(255, 255, 255, 200), font=font)

    return result


# Global state for webcam mode
class WebcamState:
    def __init__(self):
        self.is_active = False
        self.current_style = 'candy'
        self.current_backend = 'auto'
        self.frame_count = 0

webcam_state = WebcamState()

# ============================================================================
# Chart Generation
# ============================================================================

def create_performance_chart() -> str:
    """Create real-time performance chart as HTML."""
    if not PLOTLY_AVAILABLE:
        return "### Chart Unavailable\n\nPlotly is not installed. Install with: `pip install plotly`"

    data = perf_tracker.get_chart_data()
    if not data or len(data.timestamps) < 2:
        return "### Performance Chart\n\nRun some inferences to see the chart populate..."

    # Color mapping for backends
    colors = {
        'cuda': '#10b981',  # green
        'pytorch': '#6366f1',  # blue
        'auto': '#8b5cf6',  # purple
    }

    # Create scatter plot with color-coded backends
    fig = go.Figure()

    for backend in set(data.backends):
        backend_times = []
        backend_timestamps = []
        for i, b in enumerate(data.backends):
            if b == backend:
                backend_times.append(data.times[i])
                backend_timestamps.append(data.timestamps[i])

        if backend_times:
            fig.add_trace(go.Scatter(
                x=backend_timestamps,
                y=backend_times,
                mode='lines+markers',
                name=backend.upper(),
                line=dict(color=colors[backend]),
                marker=dict(size=8, color=colors[backend]),
                connectgaps=True
            ))

    fig.update_layout(
        title="Inference Time Over Time",
        xaxis_title="Time",
        yaxis_title="Time (ms)",
        hovermode='x unified',
        height=400,
        margin=dict(l=0, r=0, t=40, b=40)
    )

    # Convert to HTML
    return fig.to_html(full_html=False, include_plotlyjs='cdn')


def create_benchmark_comparison(style: str) -> str:
    """Create detailed benchmark comparison chart."""
    if not PLOTLY_AVAILABLE:
        return "Install plotly for charts"

    # Run quick benchmark
    test_img = Image.new('RGB', (512, 512), color='red')
    results = {}

    # Test each backend
    for backend_name, backend_key in [('PyTorch', 'pytorch'), ('CUDA Kernels', 'cuda')]:
        try:
            model = load_model(style, backend_key)
            test_tensor = preprocess_image(test_img).to(get_device())

            times = []
            for _ in range(3):
                start = time.perf_counter()
                with torch.no_grad():
                    _ = model(test_tensor)
                if get_device().type == 'cuda':
                    torch.cuda.synchronize()
                times.append((time.perf_counter() - start) * 1000)

            results[backend_name] = np.mean(times)
        except Exception:
            results[backend_name] = None

    # Create bar chart
    fig = go.Figure()

    backends = []
    times_list = []
    colors_list = []

    for name, time_val in results.items():
        if time_val:
            backends.append(name)
            times_list.append(time_val)
            colors_list.append('#10b981' if 'CUDA' in name else '#6366f1')

    if backends:
        fig.add_trace(go.Bar(
            x=backends,
            y=times_list,
            marker=dict(color=colors_list),
            text=[f"{t:.1f} ms" for t in times_list],
            textposition='outside',
        ))

    fig.update_layout(
        title=f"Benchmark Comparison - {STYLES.get(style, style.title())} Style",
        xaxis_title="Backend",
        yaxis_title="Inference Time (ms)",
        height=400,
        margin=dict(l=0, r=0, t=40, b=40),
        showlegend=False
    )

    # Calculate speedup
    if len(times_list) == 2:
        speedup = times_list[1] / times_list[0] if times_list[0] > 0 else times_list[0] / times_list[1]
        max_val = max(times_list)
        min_val = min(times_list)
        actual_speedup = max_val / min_val

        caption = f"Speedup: **{actual_speedup:.2f}x**"
    else:
        caption = "Run on GPU with CUDA for comparison"

    return fig.to_html(full_html=False, include_plotlyjs='cdn') + f"\n\n### {caption}"


# ============================================================================
# Gradio Interface Functions
# ============================================================================

def stylize_image_impl(
    input_image: Optional[Image.Image],
    style: str,
    backend: str,
    intensity: float,
    show_comparison: bool,
    add_watermark: bool
) -> Tuple[Optional[Image.Image], str, Optional[str]]:
    """Main stylization function for Gradio."""

    if input_image is None:
        return None, "Please upload an image first.", None

    try:
        # Convert to RGB if needed
        if input_image.mode != 'RGB':
            input_image = input_image.convert('RGB')

        # Handle blended styles (format: "style1_style2_alpha")
        if '_' in style and style not in STYLES:
            parts = style.split('_')
            if len(parts) >= 3:
                style1, style2 = parts[0], parts[1]
                alpha = float(parts[2]) / 100

                model = get_blended_model(style1, style2, alpha, backend)
                style_display = f"{STYLES.get(style1, style1)} Γ— {alpha:.0%} + {STYLES.get(style2, style2)} Γ— {100-alpha:.0%}"
            else:
                model = load_model(style, backend)
                style_display = STYLES.get(style, style)
        else:
            model = load_model(style, backend)
            style_display = STYLES.get(style, style)

        # Preprocess
        input_tensor = preprocess_image(input_image).to(get_device())

        # Stylize with timing
        start = time.perf_counter()

        with torch.no_grad():
            output_tensor = model(input_tensor)

        if get_device().type == 'cuda':
            torch.cuda.synchronize()

        elapsed_ms = (time.perf_counter() - start) * 1000

        # Determine actual backend used
        actual_backend = 'cuda' if (backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE)) else 'pytorch'
        perf_tracker.record(elapsed_ms, actual_backend)

        # Postprocess
        output_image = postprocess_tensor(output_tensor.cpu())

        # Apply intensity blending (blend original with stylized output)
        # intensity 0-100, where 100 = full style, 0 = original
        intensity_factor = intensity / 100.0
        if intensity_factor < 1.0:
            import torchvision.transforms as transforms
            to_tensor = transforms.ToTensor()
            to_pil = transforms.ToPILImage()

            original_tensor = to_tensor(input_image)
            output_tensor_pil = to_tensor(output_image)

            # Blend: output * intensity + original * (1 - intensity)
            blended_tensor = output_tensor_pil * intensity_factor + original_tensor * (1 - intensity_factor)
            blended_tensor = torch.clamp(blended_tensor, 0, 1)
            output_image = to_pil(blended_tensor)

        # Add watermark if requested
        if add_watermark:
            output_image = add_watermark(output_image, style_display)

        # Create comparison if requested
        if show_comparison:
            output_image = create_side_by_side(input_image, output_image, style_display)

        # Save for download
        download_path = f"/tmp/styleforge_{int(time.time())}.png"
        output_image.save(download_path, quality=95)

        # Generate stats
        stats = perf_tracker.get_stats()
        fps = 1000 / elapsed_ms if elapsed_ms > 0 else 0
        width, height = input_image.size

        # Backend display name
        backend_display = {
            'auto': f"Auto ({'CUDA' if CUDA_KERNELS_AVAILABLE else 'PyTorch'})",
            'cuda': 'CUDA Kernels',
            'pytorch': 'PyTorch'
        }.get(backend, backend)

        stats_text = f"""
### Performance

| Metric | Value |
|--------|-------|
| **Style** | {style_display} |
| **Backend** | {backend_display} |
| **Time** | {elapsed_ms:.1f} ms ({fps:.0f} FPS) |
| **Avg Time** | {(stats.avg_ms if stats else elapsed_ms):.1f} ms |
| **Total Images** | {stats.total_inferences if stats else 1} |
| **Size** | {width}x{height} |
| **Device** | {get_device().type.upper()} |

---
{perf_tracker.get_comparison()}
"""

        return output_image, stats_text, download_path

    except Exception as e:
        import traceback
        error_details = traceback.format_exc()
        error_msg = f"""
### Error

**{str(e)}**

<details>
<summary>Show details</summary>

```
{error_details}
```

</details>
"""
        return None, error_msg, None


# Wrap with GPU decorator for ZeroGPU if available
# ZeroGPU requires ALL GPU-using functions to be decorated with @GPU
# Note: This must come AFTER the function definitions below
if SPACES_AVAILABLE:
    try:
        stylize_image = GPU(stylize_image_impl)
        train_custom_style = GPU(train_custom_style_impl)
        extract_style_from_image = GPU(extract_style_from_image_impl)
        # apply_region_style = GPU(apply_region_style_impl)
        # apply_region_style_ui = GPU(apply_region_style_ui_impl)
        # create_style_blend_output will be wrapped after function definition
    except Exception:
        # Fallback if GPU decorator fails
        stylize_image = stylize_image_impl
        train_custom_style = train_custom_style_impl
        extract_style_from_image = extract_style_from_image_impl
        # create_style_blend_output = create_style_blend_output_impl
        # apply_region_style = apply_region_style_impl
        # apply_region_style_ui = apply_region_style_ui_impl
else:
    stylize_image = stylize_image_impl
    train_custom_style = train_custom_style_impl
    extract_style_from_image = extract_style_from_image_impl
    # create_style_blend_output = create_style_blend_output_impl
    # apply_region_style = apply_region_style_impl
    # apply_region_style_ui = apply_region_style_ui_impl


def process_webcam_frame(image: Image.Image, style: str, backend: str, intensity: float = 70) -> Image.Image:
    """Process webcam frame in real-time."""
    if image is None:
        return image

    try:
        if image.mode != 'RGB':
            image = image.convert('RGB')

        # Resize for faster processing
        if max(image.size) > 640:
            scale = 640 / max(image.size)
            new_size = (int(image.width * scale), int(image.height * scale))
            image = image.resize(new_size, Image.LANCZOS)

        # Use blended style if applicable
        if '_' in style and style not in STYLES:
            parts = style.split('_')
            if len(parts) >= 3:
                style1, style2 = parts[0], parts[1]
                alpha = float(parts[2]) / 100
                model = get_blended_model(style1, style2, alpha, backend)
            else:
                model = load_model(style, backend)
        else:
            model = load_model(style, backend)

        input_tensor = preprocess_image(image).to(get_device())

        with torch.no_grad():
            output_tensor = model(input_tensor)

        if get_device().type == 'cuda':
            torch.cuda.synchronize()

        output_image = postprocess_tensor(output_tensor.cpu())

        # Apply intensity blending
        intensity_factor = intensity / 100.0
        if intensity_factor < 1.0:
            import torchvision.transforms as transforms
            to_tensor = transforms.ToTensor()
            to_pil = transforms.ToPILImage()

            original_tensor = to_tensor(image)
            output_tensor_pil = to_tensor(output_image)

            blended_tensor = output_tensor_pil * intensity_factor + original_tensor * (1 - intensity_factor)
            blended_tensor = torch.clamp(blended_tensor, 0, 1)
            output_image = to_pil(blended_tensor)

        webcam_state.frame_count += 1
        actual_backend = 'cuda' if backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE) else 'pytorch'
        perf_tracker.record(10, actual_backend)

        return output_image

    except Exception as e:
        print(f"Webcam processing error: {e}")
        import traceback
        traceback.print_exc()
        return image


def apply_region_style_ui_impl(
    input_image: Image.Image,
    mask_type: str,
    position: float,
    style1: str,
    style2: str,
    backend: str
) -> Tuple[Image.Image, Image.Image]:
    """Apply region-based style transfer with AI segmentation support."""
    if input_image is None:
        return None, None

    # Create mask based on type
    if mask_type == "AI: Foreground":
        try:
            mask = get_ai_segmentation_mask(input_image, "foreground")
        except Exception as e:
            # Fallback to center circle if AI fails
            print(f"AI segmentation failed: {e}, using fallback")
            mask = create_region_mask(input_image, "center_circle", position)
    elif mask_type == "AI: Background":
        try:
            mask = get_ai_segmentation_mask(input_image, "background")
        except Exception as e:
            # Fallback to horizontal split if AI fails
            print(f"AI segmentation failed: {e}, using fallback")
            mask = create_region_mask(input_image, "horizontal_split", position)
    else:
        # Convert display name to internal name
        mask_type_map = {
            "Horizontal Split": "horizontal_split",
            "Vertical Split": "vertical_split",
            "Center Circle": "center_circle",
            "Corner Box": "corner_box",
            "Full": "full"
        }
        internal_type = mask_type_map.get(mask_type, "horizontal_split")
        mask = create_region_mask(input_image, internal_type, position)

    # Apply styles
    result = apply_region_style(input_image, mask, style1, style2, backend)

    # Create mask overlay for visualization
    mask_vis = mask.convert('RGB')
    mask_vis = mask_vis.resize(input_image.size)

    # Blend mask with original for visibility
    orig_np = np.array(input_image)
    mask_np = np.array(mask_vis)
    overlay_np = (orig_np * 0.7 + mask_np * 0.3).astype(np.uint8)
    mask_overlay = Image.fromarray(overlay_np)

    return result, mask_overlay


def refresh_styles_list() -> list:
    """Refresh styles list including custom styles."""
    custom = get_custom_styles()
    return list(STYLES.keys()) + custom


def get_style_description(style: str) -> str:
    """Get description for selected style."""
    return STYLE_DESCRIPTIONS.get(style, "")


def get_performance_stats() -> str:
    """Get current performance statistics."""
    stats = perf_tracker.get_stats()
    if not stats:
        return "No data yet."

    return f"""
### Live Statistics

| Metric | Value |
|--------|-------|
| **Avg Time** | {stats.avg_ms:.1f} ms |
| **Fastest** | {stats.min_ms:.1f} ms |
| **Slowest** | {stats.max_ms:.1f} ms |
| **Total Images** | {stats.total_inferences} |
| **Uptime** | {stats.uptime_hours:.1f} hours |

---
{perf_tracker.get_comparison()}
"""


def run_backend_comparison(style: str) -> str:
    """Run backend comparison and return results."""
    if not CUDA_KERNELS_AVAILABLE:
        return "### Backend Comparison\n\nCUDA kernels are not available on this device. Using PyTorch backend only."

    # Create test image
    test_img = Image.new('RGB', (512, 512), color='red')

    results = {}

    # Test PyTorch backend
    try:
        model = load_model(style, 'pytorch')
        test_tensor = preprocess_image(test_img).to(get_device())

        times = []
        for _ in range(5):
            start = time.perf_counter()
            with torch.no_grad():
                _ = model(test_tensor)
            if get_device().type == 'cuda':
                torch.cuda.synchronize()
            times.append((time.perf_counter() - start) * 1000)

        results['pytorch'] = np.mean(times[1:])
    except Exception:
        results['pytorch'] = None

    # Test CUDA backend
    try:
        model = load_model(style, 'cuda')
        test_tensor = preprocess_image(test_img).to(get_device())

        times = []
        for _ in range(5):
            start = time.perf_counter()
            with torch.no_grad():
                _ = model(test_tensor)
            if get_device().type == 'cuda':
                torch.cuda.synchronize()
            times.append((time.perf_counter() - start) * 1000)

        results['cuda'] = np.mean(times[1:])
    except Exception:
        results['cuda'] = None

    # Format results
    output = "### Backend Comparison Results\n\n"

    if results.get('pytorch') and results.get('cuda'):
        speedup = results['pytorch'] / results['cuda']
        output += f"""
| Backend | Time | Speedup |
|---------|------|---------|
| **PyTorch** | {results['pytorch']:.1f} ms | 1.0x |
| **CUDA Kernels** | {results['cuda']:.1f} ms | {speedup:.2f}x |

### CUDA kernels are {speedup:.1f}x faster! πŸš€
"""
    else:
        output += "Could not complete comparison. Both backends may not be available."

    return output


def create_style_blend_output_impl(
    input_image: Image.Image,
    style1: str,
    style2: str,
    blend_ratio: float,
    backend: str
) -> Image.Image:
    """Create blended style output."""
    if input_image is None:
        return None

    # Convert to RGB
    if input_image.mode != 'RGB':
        input_image = input_image.convert('RGB')

    # Get blended model
    alpha = blend_ratio / 100
    model = get_blended_model(style1, style2, alpha, backend)

    # Process with timing
    input_tensor = preprocess_image(input_image).to(get_device())

    start = time.perf_counter()
    with torch.no_grad():
        output_tensor = model(input_tensor)
    if get_device().type == 'cuda':
        torch.cuda.synchronize()
    elapsed_ms = (time.perf_counter() - start) * 1000

    # Record performance
    actual_backend = 'cuda' if (backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE)) else 'pytorch'
    perf_tracker.record(elapsed_ms, actual_backend)

    output_image = postprocess_tensor(output_tensor.cpu())
    return output_image


# ============================================================================
# Complete GPU wrapping for functions defined above
# ============================================================================
# Now wrap the remaining functions that were defined after the initial GPU wrapping

if SPACES_AVAILABLE:
    try:
        create_style_blend_output = GPU(create_style_blend_output_impl)
        apply_region_style = GPU(apply_region_style_impl)
        apply_region_style_ui = GPU(apply_region_style_ui_impl)
        process_webcam_frame = GPU(process_webcam_frame)
    except Exception:
        create_style_blend_output = create_style_blend_output_impl
        apply_region_style = apply_region_style_impl
        apply_region_style_ui = apply_region_style_ui_impl
        process_webcam_frame = process_webcam_frame
else:
    create_style_blend_output = create_style_blend_output_impl
    apply_region_style = apply_region_style_impl
    apply_region_style_ui = apply_region_style_ui_impl
    process_webcam_frame = process_webcam_frame


# ============================================================================
# Build Gradio Interface
# ============================================================================

custom_css = """
/* ============================================
   LIQUID GLASS / GLASSMORPHISM THEME
   Gradio 5.x Compatible
   ============================================ */

/* Import Google Fonts - Plus Jakarta Sans for premium Framer-style look */
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@200;300;400;500;600;700;800&display=swap');

/* Animated gradient background */
body {
    background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    min-height: 100vh;
}

@keyframes gradientBG {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

/* Universal font application */
* {
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
}

/* Ensure text elements are visible */
h1, h2, h3, h4, h5, h6, p, span, div, label, button, input, textarea, select {
    color: inherit;
}

/* Main app container - glass effect */
.gradio-container {
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    background: rgba(255, 255, 255, 0.75) !important;
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
    max-width: 1400px;
    margin: 20px auto;
    padding: 24px !important;
}

/* Primary button - enhanced glass effect with Framer-style typography */
button.primary,
.gr-button-primary,
[class*="primary"] {
    background: linear-gradient(135deg,
        rgba(255, 255, 255, 0.25) 0%,
        rgba(255, 255, 255, 0.1) 50%,
        rgba(255, 255, 255, 0.05) 100%) !important;
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.4) !important;
    color: #4F46E5 !important;
    font-family: 'Plus Jakarta Sans', sans-serif !important;
    font-weight: 600 !important;
    letter-spacing: -0.01em !important;
    border-radius: 16px !important;
    padding: 20px 32px !important;
    transition: all 0.3s ease !important;
    box-shadow:
        0 8px 32px rgba(31, 38, 135, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.5),
        inset 0 -1px 0 rgba(0, 0, 0, 0.05) !important;
    position: relative;
    overflow: hidden;
    width: 100% !important;
    min-height: 64px !important;
    margin: 8px 0 !important;
}

button.primary::before,
.gr-button-primary::before,
[class*="primary"]::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg,
        transparent,
        rgba(255, 255, 255, 0.4),
        transparent);
    transition: left 0.5s ease;
}

button.primary:hover::before,
.gr-button-primary:hover::before,
[class*="primary"]:hover::before {
    left: 100%;
}

button.primary:hover,
.gr-button-primary:hover,
[class*="primary"]:hover {
    transform: translateY(-2px);
    background: linear-gradient(135deg,
        rgba(255, 255, 255, 0.35) 0%,
        rgba(255, 255, 255, 0.15) 50%,
        rgba(255, 255, 255, 0.1) 100%) !important;
    border-color: rgba(255, 255, 255, 0.6) !important;
    box-shadow:
        0 12px 40px rgba(31, 38, 135, 0.25),
        inset 0 1px 0 rgba(255, 255, 255, 0.6),
        inset 0 -1px 0 rgba(0, 0, 0, 0.05) !important;
}

button.primary:active,
.gr-button-primary:active,
[class*="primary"]:active {
    transform: translateY(0);
    box-shadow:
        0 4px 20px rgba(31, 38, 135, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.3) !important;
}

/* Secondary button - enhanced glass style with Framer typography */
button.secondary,
.gr-button-secondary,
.download,
[class*="secondary"] {
    background: linear-gradient(135deg,
        rgba(255, 255, 255, 0.4) 0%,
        rgba(255, 255, 255, 0.2) 100%) !important;
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.5) !important;
    color: #374151 !important;
    font-family: 'Plus Jakarta Sans', sans-serif !important;
    border-radius: 14px !important;
    padding: 16px 28px !important;
    transition: all 0.3s ease !important;
    font-weight: 500 !important;
    letter-spacing: -0.005em !important;
    box-shadow:
        0 8px 32px rgba(31, 38, 135, 0.12),
        inset 0 1px 0 rgba(255, 255, 255, 0.6),
        inset 0 -1px 0 rgba(0, 0, 0, 0.03) !important;
    width: 100% !important;
    min-height: 56px !important;
    margin: 6px 0 !important;
}

button.secondary:hover,
.gr-button-secondary:hover,
.download:hover,
[class*="secondary"]:hover {
    background: linear-gradient(135deg,
        rgba(255, 255, 255, 0.6) 0%,
        rgba(255, 255, 255, 0.3) 100%) !important;
    border-color: rgba(255, 255, 255, 0.7) !important;
    box-shadow:
        0 12px 40px rgba(31, 38, 135, 0.18),
        inset 0 1px 0 rgba(255, 255, 255, 0.7) !important;
    transform: translateY(-1px);
}

/* All buttons - Framer-style rounded corners and typography with spacing */
button,
.gr-button {
    font-family: 'Plus Jakarta Sans', sans-serif !important;
    border-radius: 14px !important;
    transition: all 0.3s ease !important;
    width: 100% !important;
    min-height: 52px !important;
    padding: 14px 24px !important;
    margin: 4px 0 !important;
}

/* Button containers - ensure buttons fill row with spacing */
.gradio-container button,
#quick_stylize button,
#style_blending button,
#region_transfer button,
#custom_training button,
#benchmarking button {
    width: 100% !important;
    margin: 6px 0 !important;
}

/* Tabs - glass style */
.tabs {
    background: rgba(255, 255, 255, 0.4) !important;
    backdrop-filter: blur(10px);
    border-radius: 16px !important;
    padding: 8px !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
}

/* Tab buttons */
button.tab-item {
    background: transparent !important;
    border-radius: 12px !important;
    color: #6B7280 !important;
    transition: all 0.3s ease !important;
}

button.tab-item:hover {
    background: rgba(255, 255, 255, 0.5) !important;
}

button.tab-item.selected {
    background: rgba(255, 255, 255, 0.8) !important;
    color: #6366F1 !important;
    font-weight: 600 !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
}

/* Input boxes and text areas */
input[type="text"],
input[type="number"],
textarea,
select {
    background: rgba(255, 255, 255, 0.7) !important;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.5) !important;
    border-radius: 12px !important;
    transition: all 0.3s ease !important;
}

input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
    background: rgba(255, 255, 255, 0.9) !important;
    border-color: #6366F1 !important;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
    outline: none !important;
}

/* Dropdown menu fixes */
.gradio-dropdown,
.dropdown,
[class*="dropdown"],
[class*="Dropdown"] {
    position: relative !important;
    z-index: 100 !important;
}

/* Dropdown menu/option list positioning */
.gradio-dropdown ul,
.dropdown ul,
select option,
[class*="dropdown"] ul,
[class*="Dropdown"] ul {
    position: absolute !important;
    z-index: 9999 !important;
    background: rgba(255, 255, 255, 0.95) !important;
    backdrop-filter: blur(20px) !important;
    border: 1px solid rgba(255, 255, 255, 0.5) !important;
    border-radius: 12px !important;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15) !important;
    max-height: 300px !important;
    overflow-y: auto !important;
}

/* Gradio 5.x dropdown container fixes */
[class*="svelte"][class*="dropdown"],
[class*="svelte"][class*="radio"],
[class*="svelte"][class*="radio-item"] {
    position: relative !important;
    z-index: 100 !important;
}

/* Radio button group positioning */
.gradio-radio,
[class*="radio"] {
    position: relative !important;
    z-index: 50 !important;
}

/* Image containers - glass frame */
.image-container,
[class*="image"] {
    border-radius: 16px !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
    overflow: hidden !important;
    background: rgba(255, 255, 255, 0.3) !important;
}

/* Slider styling */
input[type="range"] {
    -webkit-appearance: none;
    background: rgba(229, 231, 235, 0.6);
    backdrop-filter: blur(10px);
    border-radius: 8px;
    height: 8px;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
    border: 3px solid white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}

input[type="range"]::-moz-range-thumb {
    width: 22px;
    height: 22px;
    background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
    border: 3px solid white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}

/* Checkbox and radio styling */
input[type="checkbox"],
input[type="radio"] {
    accent-color: #6366F1 !important;
    width: 18px !important;
    height: 18px !important;
}

/* Badge styles */
.live-badge {
    display: inline-block;
    padding: 6px 16px;
    background: rgba(254, 243, 199, 0.8);
    backdrop-filter: blur(10px);
    color: #92400E;
    border-radius: 24px;
    font-size: 13px;
    font-weight: 600;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

.backend-badge {
    display: inline-block;
    padding: 6px 16px;
    background: rgba(209, 250, 229, 0.8);
    backdrop-filter: blur(10px);
    color: #065F46;
    border-radius: 24px;
    font-size: 13px;
    font-weight: 600;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

/* Markdown content with Framer-style typography */
.markdown {
    color: #374151 !important;
    font-family: 'Plus Jakarta Sans', sans-serif !important;
}

/* Headings with Framer-style typography - tighter letter spacing */
.gradio-container h1,
.gradio-container h2,
.gradio-container h3,
.gradio-container h4,
.gradio-container h5,
.gradio-container h6 {
    font-family: 'Plus Jakarta Sans', sans-serif !important;
    color: #1F2937 !important;
    letter-spacing: -0.025em !important;
    font-weight: 600 !important;
}

/* Text visibility fixes */
.gradio-container,
.gradio-container *,
.gradio-container p,
.gradio-container span,
.gradio-container label {
    color: #1F2937 !important;
}

/* Button text colors */
button,
.gradio-container button {
    color: inherit !important;
}

/* Input and select text colors with Framer-style typography */
input,
textarea,
select {
    color: #1F2937 !important;
    font-family: 'Plus Jakarta Sans', sans-serif !important;
}

/* Label colors with Framer-style typography */
label,
[class*="label"] {
    color: #374151 !important;
    font-family: 'Plus Jakarta Sans', sans-serif !important;
    font-weight: 500 !important;
    letter-spacing: -0.01em !important;
}

/* Gradio 5.x specific text elements */
.svelte-*, [class*="svelte-"] {
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
}

/* Group/Row/Column containers */
.group,
.row,
.column {
    background: rgba(255, 255, 255, 0.3) !important;
    border-radius: 16px !important;
    padding: 16px !important;
}

/* Accordion */
.details {
    background: rgba(255, 255, 255, 0.4) !important;
    backdrop-filter: blur(10px);
    border-radius: 16px !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
}

/* Scrollbar - glass style */
::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-track {
    background: rgba(229, 231, 235, 0.3);
    border-radius: 8px;
}

::-webkit-scrollbar-thumb {
    background: rgba(167, 139, 250, 0.5);
    border-radius: 8px;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(139, 92, 246, 0.7);
}

/* Progress bar */
progress {
    background: rgba(229, 231, 235, 0.5) !important;
    border-radius: 8px !important;
    height: 8px !important;
}

progress::-webkit-progress-bar {
    background: rgba(229, 231, 235, 0.5);
    border-radius: 8px;
}

progress::-webkit-progress-value {
    background: linear-gradient(90deg, #6366F1, #8B5CF6) !important;
    border-radius: 8px;
}

/* Mobile responsive */
@media (max-width: 768px) {
    .gradio-container {
        margin: 10px !important;
        padding: 16px !important;
        border-radius: 20px !important;
    }

    button.primary,
    .gr-button-primary,
    [class*="primary"] {
        padding: 10px 18px !important;
        font-size: 14px !important;
    }
}

/* Loading spinner */
.spinner {
    border: 3px solid rgba(99, 102, 241, 0.2);
    border-top: 3px solid #6366F1;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Additional Gradio 5.x specific selectors */
.gradio-button.primary,
button[class*="Primary"],
[type="button"].primary {
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.9) 0%, rgba(139, 92, 246, 0.9) 100%) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: white !important;
    font-weight: 600 !important;
    border-radius: 16px !important;
    box-shadow: 0 4px 15px rgba(99, 102, 241, 0.25) !important;
    min-height: 64px !important;
    padding: 20px 32px !important;
    margin: 8px 0 !important;
}

/* Gradio 5.x Portal/Popover fixes for dropdowns - simplified */
body > div[id*="portal"] {
    position: fixed !important;
    z-index: 99999 !important;
    margin-top: 0 !important;
    padding-top: 0 !important;
}

/* Block containers */
.block {
    background: rgba(255, 255, 255, 0.25) !important;
    border-radius: 16px !important;
    padding: 12px !important;
}

/* Form elements */
.form,
.form-group {
    background: transparent !important;
}
"""

with gr.Blocks(
    title="StyleForge: Neural Style Transfer",
    theme=gr.themes.Glass(
        primary_hue="indigo",
        secondary_hue="purple",
        font=gr.themes.GoogleFont("Plus Jakarta Sans"),
        radius_size="lg",
    ),
    css=custom_css,
) as demo:

    # Load Google Fonts via HTML head injection
    gr.HTML("""
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@200;300;400;500;600;700;800&display=swap" rel="stylesheet">
    """)

    # Header with Portal-style hero section
    cuda_badge = f"<span class='backend-badge'>CUDA Accelerated</span>" if CUDA_KERNELS_AVAILABLE else ""
    gr.HTML(f"""
    <div style="text-align: center; padding: 3rem 0 2rem 0; font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;">
        <h1 style="font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 3.5rem; margin-bottom: 0.5rem; background: linear-gradient(135deg, #6366F1, #8B5CF6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-weight: 700; letter-spacing: -0.02em;">
            StyleForge
        </h1>
        <p style="font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; color: #6B7280; font-size: 1.15rem; margin-bottom: 1rem; font-weight: 400;">
            Neural Style Transfer with Custom CUDA Kernels
        </p>
        {cuda_badge}
        <p style="font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; color: #9CA3AF; margin-top: 1rem; font-size: 0.95rem; font-weight: 300;">
            Custom Styles β€’ Region Transfer β€’ Style Blending β€’ Real-time Processing
        </p>
    </div>
    """)

    # Mode selector
    with gr.Tabs() as tabs:
        # Tab 1: Quick Style Transfer
        with gr.Tab("Quick Style", id=0):
            with gr.Row():
                with gr.Column(scale=1):
                    quick_image = gr.Image(
                        label="Upload Image",
                        type="pil",
                        sources=["upload", "clipboard"],
                        height=400
                    )

                    quick_style = gr.Dropdown(
                        choices=list(STYLES.keys()),
                        value='candy',
                        label="Artistic Style"
                    )

                    quick_backend = gr.Radio(
                        choices=list(BACKENDS.keys()),
                        value='auto',
                        label="Processing Backend"
                    )

                    quick_intensity = gr.Slider(
                        minimum=0,
                        maximum=100,
                        value=70,
                        step=5,
                        label="Style Intensity (0% = Original, 100% = Full Style)"
                    )

                    with gr.Row():
                        quick_compare = gr.Checkbox(
                            label="Side-by-side",
                            value=False
                        )
                        quick_watermark = gr.Checkbox(
                            label="Add watermark",
                            value=False
                        )

                    quick_btn = gr.Button(
                        "Stylize Image",
                        variant="primary",
                        size="lg"
                    )

                with gr.Column(scale=1):
                    quick_output = gr.Image(
                        label="Result",
                        type="pil",
                        height=400
                    )

                    with gr.Row():
                        quick_download = gr.DownloadButton(
                            label="Download",
                            variant="secondary"
                        )

                    quick_stats = gr.Markdown(
                        "> Upload an image and click **Stylize** to begin!"
                    )

        # Tab 2: Style Blending
        with gr.Tab("Style Blending", id=1):
            gr.Markdown("""
            ### Mix Two Styles Together

            Blend between any two styles to create unique artistic combinations.
            This demonstrates style interpolation in the latent space.
            """)

            with gr.Row():
                with gr.Column(scale=1):
                    blend_image = gr.Image(
                        label="Upload Image",
                        type="pil",
                        sources=["upload", "clipboard"],
                        height=350
                    )

                    blend_style1 = gr.Dropdown(
                        choices=list(STYLES.keys()),
                        value='candy',
                        label="Style 1"
                    )

                    blend_style2 = gr.Dropdown(
                        choices=list(STYLES.keys()),
                        value='mosaic',
                        label="Style 2"
                    )

                    blend_ratio = gr.Slider(
                        minimum=0,
                        maximum=100,
                        value=50,
                        step=5,
                        label="Blend Ratio"
                    )

                    blend_backend = gr.Radio(
                        choices=list(BACKENDS.keys()),
                        value='auto',
                        label="Backend"
                    )

                    blend_btn = gr.Button(
                        "Blend Styles",
                        variant="primary"
                    )

                    gr.Markdown("""
                    **How it Works:**
                    - Style blending interpolates between model weights
                    - At 0% you get pure Style 1
                    - At 100% you get pure Style 2
                    - At 50% you get an equal mix of both
                    """)

                with gr.Column(scale=1):
                    blend_output = gr.Image(
                        label="Blended Result",
                        type="pil",
                        height=350
                    )

                    blend_info = gr.Markdown(
                        "Adjust the blend ratio and click **Blend Styles** to see the result."
                    )

        # Tab 3: Region-Based Style
        with gr.Tab("Region Transfer", id=2):
            gr.Markdown("""
            ### Apply Different Styles to Different Regions

            Transform specific parts of your image with different styles.
            **NEW:** AI-powered foreground/background segmentation!
            """)

            with gr.Row():
                with gr.Column(scale=1):
                    region_image = gr.Image(
                        label="Upload Image",
                        type="pil",
                        sources=["upload", "clipboard"],
                        height=350
                    )

                    region_mask_type = gr.Radio(
                        choices=[
                            "AI: Foreground",
                            "AI: Background",
                            "Horizontal Split",
                            "Vertical Split",
                            "Center Circle",
                            "Corner Box",
                            "Full"
                        ],
                        value="AI: Foreground",
                        label="Mask Type"
                    )

                    region_position = gr.Slider(
                        minimum=0,
                        maximum=1,
                        value=0.5,
                        step=0.1,
                        label="Split Position"
                    )

                    with gr.Row():
                        region_style1 = gr.Dropdown(
                            choices=list(STYLES.keys()),
                            value='candy',
                            label="Style (White/Top/Left)"
                        )
                        region_style2 = gr.Dropdown(
                            choices=list(STYLES.keys()),
                            value='mosaic',
                            label="Style (Black/Bottom/Right)"
                        )

                    region_backend = gr.Radio(
                        choices=list(BACKENDS.keys()),
                        value='auto',
                        label="Backend"
                    )

                    region_btn = gr.Button(
                        "Apply Region Styles",
                        variant="primary"
                    )

                with gr.Column(scale=1):
                    with gr.Tabs():
                        with gr.Tab("Result"):
                            region_output = gr.Image(
                                label="Stylized Result",
                                type="pil",
                                height=300
                            )

                        with gr.Tab("Mask Preview"):
                            region_mask_preview = gr.Image(
                                label="Mask Preview",
                                type="pil",
                                height=300
                            )

                    gr.Markdown("""
                    **Mask Guide:**
                    - **AI: Foreground** πŸ†•: Automatically detect main subject (person, object, etc.)
                    - **AI: Background** πŸ†•: Automatically detect background/sky
                    - **Horizontal**: Top/bottom split
                    - **Vertical**: Left/right split
                    - **Center Circle**: Circular region in center
                    - **Corner Box**: Top-left quadrant only

                    *AI segmentation uses the Rembg model (U^2-Net) for automatic subject detection.*
                    """)

        # Tab 4: Custom Style Training
        with gr.Tab("Create Style", id=3):
            gr.Markdown("""
            ### Extract Style from Any Image πŸ†•

            Upload any artwork to extract its artistic style using **VGG19 feature matching**.

            **How it works:**
            1. Extract style features using pre-trained VGG19 neural network
            2. Fine-tune a transformation network to match those features
            3. Save as a reusable style model

            This is **real style extraction** - not just copying an existing style!
            """)

            with gr.Row():
                with gr.Column(scale=1):
                    train_style_image = gr.Image(
                        label="Style Image (Artwork)",
                        type="pil",
                        sources=["upload"],
                        height=350
                    )

                    train_style_name = gr.Textbox(
                        label="Style Name",
                        value="my_custom_style",
                        placeholder="Enter a name for your custom style"
                    )

                    train_iterations = gr.Slider(
                        minimum=50,
                        maximum=500,
                        value=100,
                        step=50,
                        label="Training Iterations"
                    )

                    train_backend = gr.Radio(
                        choices=list(BACKENDS.keys()),
                        value='auto',
                        label="Backend"
                    )

                    train_btn = gr.Button(
                        "Extract Style",
                        variant="primary"
                    )

                    refresh_styles_btn = gr.Button("Refresh Style List")

                with gr.Column(scale=1):
                    train_output = gr.Markdown(
                        "> Upload a style image and click **Extract Style** to begin!\n\n"
                        "**How it works:**\n"
                        "- VGG19 extracts artistic features (textures, colors, patterns)\n"
                        "- Neural network is fine-tuned to match those features\n"
                        "- Result is a reusable style model\n\n"
                        "**Tips:**\n"
                        "- Use artwork with clear artistic style (paintings, illustrations)\n"
                        "- More iterations = better style matching (slower)\n"
                        "- GPU recommended for faster training\n"
                        "- Your custom style will appear in all Style dropdowns"
                    )

                    train_progress = gr.Markdown("")

        # Tab 5: Webcam Live
        with gr.Tab("Webcam Live", id=4):
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("""
                    ### <span class="live-badge">LIVE</span> Real-time Webcam Style Transfer
                    """)

                    webcam_style = gr.Dropdown(
                        choices=list(STYLES.keys()),
                        value='candy',
                        label="Artistic Style"
                    )

                    webcam_backend = gr.Radio(
                        choices=list(BACKENDS.keys()),
                        value='auto',
                        label="Backend"
                    )

                    webcam_intensity = gr.Slider(
                        minimum=0,
                        maximum=100,
                        value=70,
                        step=5,
                        label="Style Intensity (0% = Original, 100% = Full Style)"
                    )

                    webcam_stream = gr.Image(
                        sources=["webcam"],
                        label="Webcam Feed",
                        height=400
                    )

                    webcam_info = gr.Markdown(
                        "> Click in the webcam preview to start the feed"
                    )

                with gr.Column(scale=1):
                    webcam_output = gr.Image(
                        label="Stylized Output",
                        height=400
                    )

                    webcam_stats = gr.Markdown(
                        get_performance_stats()
                    )

                    refresh_stats_btn = gr.Button("Refresh Stats", size="sm")

        # Tab 6: Performance Dashboard
        with gr.Tab("Performance", id=5):
            gr.Markdown("""
            ### Real-time Performance Dashboard

            Track inference times and compare backends with live charts.
            """)

            with gr.Row():
                benchmark_style = gr.Dropdown(
                    choices=list(STYLES.keys()),
                    value='candy',
                    label="Select Style for Benchmark"
                )

                run_benchmark_btn = gr.Button(
                    "Run Benchmark",
                    variant="primary"
                )

            benchmark_chart = gr.Markdown(
                "Click **Run Benchmark** to see the performance chart"
            )

            live_chart = gr.Markdown(
                "Run some inferences to see the live chart populate below..."
            )

            refresh_chart_btn = gr.Button("Refresh Chart")

            gr.Markdown("---")
            gr.Markdown("### Live Performance Chart")

            chart_display = gr.HTML(
                "<div style='text-align:center; padding: 20px;'>Run inferences to see chart</div>"
            )

            chart_stats = gr.Markdown()

    # Style description (shared across all tabs)
    style_desc = gr.Markdown("*Select a style to see description*")

    # Examples section
    gr.Markdown("---")

    def create_example_image():
        # Create a more interesting test image with geometric shapes
        arr = np.zeros((256, 256, 3), dtype=np.uint8)
        # Background gradient
        for i in range(256):
            arr[:, i, 0] = i // 2
            arr[:, i, 1] = 128
            arr[:, i, 2] = 255 - i // 2
        # Add a circle in the center
        cy, cx = 128, 128
        for y in range(256):
            for x in range(256):
                if (x - cx)**2 + (y - cy)**2 <= 50**2:
                    arr[y, x, 0] = 255
                    arr[y, x, 1] = 200
                    arr[y, x, 2] = 100
        return Image.fromarray(arr)

    example_img = create_example_image()

    # Pre-styled example outputs for display
    # These images demonstrate each style without needing to run the model
    gr.Markdown("### Quick Style Examples")
    gr.Markdown("Upload an image and select a style to see the transformation in action!")

    # Examples removed - they all showed the same input image
    # The style gallery below shows visual representations of each style

    # Display example style gallery with unique gradients for each style
    gr.Markdown("""
    <div style="display: flex; gap: 0.8rem; justify-content: center; margin: 1rem 0; flex-wrap: wrap;">
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #ff6b6b, #feca57); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">🍬 Candy</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #5f27cd, #00d2d3); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(95, 39, 205, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">🎨 Mosaic</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #576574, #c8d6e5); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(87, 101, 116, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">🌧️ Rain Princess</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #ee5a24, #f9ca24); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(238, 90, 36, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">πŸ–ΌοΈ Udnie</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #1e3a8a, #fbbf24, #3b82f6); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(30, 58, 138, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">πŸŒƒ Starry Night</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #be185d, #fce7f3, #9d174d); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(190, 24, 93, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">🎭 La Muse</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #dc2626, #1f2937, #f97316); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(220, 38, 38, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">😱 The Scream</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 100px; height: 100px; background: linear-gradient(135deg, #7c3aed, #2563eb, #db2777); border-radius: 12px; margin: 0 auto; box-shadow: 0 4px 15px rgba(124, 58, 237, 0.3);"></div>
            <p style="margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500;">πŸŽͺ Composition VII</p>
        </div>
    </div>
    """)

    # FAQ Section
    gr.Markdown("---")

    with gr.Accordion("FAQ & Help", open=False):
        gr.Markdown("""
        ### What are CUDA kernels?

        Custom CUDA kernels are hand-written GPU code that fuses multiple operations
        into a single kernel launch. This reduces memory transfers and improves
        performance by 8-9x.

        ### How does Style Blending work?

        Style blending interpolates between the weights of two trained style models.
        This demonstrates that styles exist in a continuous latent space where you can
        navigate and create new artistic variations.

        ### What is Region-based Style Transfer?

        This feature applies different artistic styles to different regions of the same image.
        It demonstrates computer vision concepts like segmentation and masking, while
        enabling creative effects like "make the sky look like Starry Night while keeping
        the ground realistic."

        ### Which backend should I use?

        - **Auto**: Recommended - automatically uses the fastest available option
        - **CUDA Kernels**: Best performance on GPU (requires CUDA compilation)
        - **PyTorch**: Fallback for CPU or when CUDA is unavailable

        ### Can I use this commercially?

        Yes! StyleForge is open source (MIT license).
        """)

    # Technical details
    with gr.Accordion("Technical Details", open=False):
        gr.Markdown(f"""
        ### Architecture

        **Network:** Encoder-Decoder with Residual Blocks (Johnson et al.)

        - **Encoder**: 3 Conv layers + Instance Normalization
        - **Transformer**: 5 Residual blocks
        - **Decoder**: 3 Upsample Conv layers + Instance Normalization

        ### CUDA Optimizations

        **Status:** {'βœ… Available' if CUDA_KERNELS_AVAILABLE else '❌ Not Available (CPU or no CUDA)'}

        When CUDA kernels are available:
        - **Fused InstanceNorm**: Combines mean, variance, normalize, affine transform
        - **Vectorized memory**: Uses `float4` loads for 4x bandwidth
        - **Shared memory**: Reduces global memory traffic
        - **Warp-level reductions**: Efficient parallel reductions

        ### ML Concepts Demonstrated

        - **Style Transfer**: Neural artistic stylization
        - **Latent Space Interpolation**: Style blending shows continuous style space
        - **Conditional Generation**: Region-based style transfer
        - **Transfer Learning**: Custom style training from few examples
        - **Performance Optimization**: CUDA kernels, JIT compilation, caching
        - **Model Deployment**: Gradio web interface, CI/CD pipeline

        ### Resources

        - [GitHub Repository](https://github.com/olivialiau/StyleForge)
        - [Paper: Perceptual Losses for Real-Time Style Transfer](https://arxiv.org/abs/1603.08155)
        """)

    # Footer
    gr.Markdown("""
    <div class="footer">
        <p>
            <strong>StyleForge</strong> β€’ Created by Olivia β€’ USC Computer Science<br>
            <a href="https://github.com/olivialiau/StyleForge">GitHub</a> β€’
            Built with <a href="https://huggingface.co/spaces">Hugging Face Spaces</a> πŸ€—
        </p>
    </div>
    """)

    # ============================================================================
    # Event Handlers
    # ============================================================================

    # Style description updates
    def update_style_desc(style):
        desc = STYLE_DESCRIPTIONS.get(style, "")
        return f"*{desc}*"

    # Quick style handlers
    quick_style.change(
        fn=update_style_desc,
        inputs=[quick_style],
        outputs=[style_desc]
    )

    quick_btn.click(
        fn=stylize_image,
        inputs=[quick_image, quick_style, quick_backend, quick_intensity, quick_compare, quick_watermark],
        outputs=[quick_output, quick_stats, quick_download]
    )

    # Style blending handlers
    def update_blend_info(style1: str, style2: str, ratio: float) -> str:
        s1_name = STYLES.get(style1, style1)
        s2_name = STYLES.get(style2, style2)
        return f"Blended {s1_name} Γ— {ratio:.0f}% + {s2_name} Γ— {100-ratio:.0f}%"

    blend_btn.click(
        fn=create_style_blend_output,
        inputs=[blend_image, blend_style1, blend_style2, blend_ratio, blend_backend],
        outputs=[blend_output]
    ).then(
        fn=update_blend_info,
        inputs=[blend_style1, blend_style2, blend_ratio],
        outputs=[blend_info]
    )

    # Region-based handlers
    region_btn.click(
        fn=apply_region_style_ui,
        inputs=[region_image, region_mask_type, region_position, region_style1, region_style2, region_backend],
        outputs=[region_output, region_mask_preview]
    )

    region_mask_type.change(
        fn=lambda mt, img, pos: create_region_mask(img, mt, pos) if img else None,
        inputs=[region_mask_type, region_image, region_position],
        outputs=[region_mask_preview]
    )

    region_position.change(
        fn=lambda pos, img, mt: create_region_mask(img, mt, pos) if img else None,
        inputs=[region_position, region_image, region_mask_type],
        outputs=[region_mask_preview]
    )

    # Custom style training
    train_btn.click(
        fn=train_custom_style,
        inputs=[train_style_image, train_style_name, train_iterations, train_backend],
        outputs=[train_progress, train_output]
    )

    def update_style_choices():
        return list(STYLES.keys()) + get_custom_styles()

    refresh_styles_btn.click(
        fn=update_style_choices,
        outputs=[quick_style]
    ).then(
        fn=update_style_choices,
        outputs=[blend_style1]
    ).then(
        fn=update_style_choices,
        outputs=[blend_style2]
    )

    # Webcam handlers - note: streaming disabled for Gradio 5.x compatibility
    # Users can still upload/process webcam images manually
    webcam_stream.change(
        fn=process_webcam_frame,
        inputs=[webcam_stream, webcam_style, webcam_backend, webcam_intensity],
        outputs=[webcam_output],
    )

    refresh_stats_btn.click(
        fn=get_performance_stats,
        outputs=[webcam_stats]
    )

    # Benchmark handlers
    run_benchmark_btn.click(
        fn=create_benchmark_comparison,
        inputs=[benchmark_style],
        outputs=[benchmark_chart]
    )

    refresh_chart_btn.click(
        fn=create_performance_chart,
        outputs=[chart_display]
    )


# ============================================================================
# Launch Configuration
# ============================================================================

if __name__ == "__main__":
    # Disable API to avoid gradio_client compatibility issues
    import os
    os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
    demo.launch(
        show_api=False,
        show_error=True,
        quiet=False,
    )