File size: 140,757 Bytes
a13c2bb
453c62c
 
1ca78b8
5e9023b
453c62c
 
547bcde
a791178
cf6c1c3
 
 
 
5e9023b
 
 
 
c96734b
453c62c
9dba8e1
 
547bcde
9dba8e1
453c62c
547bcde
9dba8e1
 
 
547bcde
9dba8e1
453c62c
547bcde
9dba8e1
 
 
547bcde
9dba8e1
453c62c
547bcde
9dba8e1
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a13c2bb
547bcde
 
 
 
301db48
 
84290a5
7717985
301db48
 
 
 
1ca78b8
547bcde
 
 
a791178
547bcde
 
 
453c62c
 
301db48
453c62c
9dba8e1
453c62c
9dba8e1
37f5ab3
5e9023b
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37f5ab3
5e9023b
453c62c
 
9dba8e1
453c62c
 
 
 
37f5ab3
5e9023b
453c62c
 
9dba8e1
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
37f5ab3
5e9023b
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dba8e1
453c62c
 
 
 
37f5ab3
5fd37a0
453c62c
 
301db48
5fd37a0
453c62c
5fd37a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453c62c
37f5ab3
a13c2bb
1ca78b8
547bcde
 
 
453c62c
547bcde
 
37f5ab3
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
7198760
 
301db48
 
 
7717985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a791178
cf6c1c3
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
 
547bcde
a791178
547bcde
 
fbaef79
 
547bcde
 
 
 
fbaef79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
a791178
fbaef79
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b53c0d
52f55d2
301db48
28966d8
52f55d2
 
 
301db48
 
28966d8
 
 
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f55d2
 
301db48
 
 
 
 
 
 
 
 
547bcde
 
84290a5
 
 
 
2bd0db0
 
84290a5
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac0709
 
 
 
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81d1619
5e9023b
453c62c
5e9023b
 
 
 
 
 
a791178
5e9023b
9dba8e1
 
453c62c
 
5e9023b
3dc43a9
 
 
 
 
 
 
 
 
 
 
 
7050196
 
 
5e9023b
 
 
 
37f5ab3
453c62c
37f5ab3
 
 
 
547bcde
453c62c
 
 
 
 
 
 
9dba8e1
453c62c
37f5ab3
 
453c62c
a791178
37f5ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9023b
 
37f5ab3
 
 
 
 
 
 
3dc43a9
 
 
37f5ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28966d8
5e9023b
37f5ab3
3dc43a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37f5ab3
 
3dc43a9
37f5ab3
 
5e9023b
 
 
a791178
 
 
bb79bb4
 
0b1b904
bb79bb4
 
 
 
a791178
 
 
 
 
 
7050196
3dc43a9
7050196
 
 
 
 
9dba8e1
547bcde
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
cf6c1c3
547bcde
 
 
 
 
 
 
7717985
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
2bd0db0
 
 
301db48
 
 
547bcde
a791178
 
547bcde
 
 
 
82b8835
301db48
 
234e0bf
 
 
 
301db48
 
 
 
 
7d23974
 
 
 
 
 
301db48
 
 
547bcde
 
 
 
 
 
 
301db48
547bcde
301db48
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
547bcde
 
 
 
 
 
 
84290a5
 
 
 
 
 
499fdba
84290a5
 
 
 
 
 
 
499fdba
84290a5
 
 
 
 
 
 
 
 
 
499fdba
84290a5
 
 
 
 
499fdba
84290a5
499fdba
 
84290a5
 
499fdba
 
84290a5
 
 
499fdba
 
 
 
 
 
 
 
 
84290a5
 
 
 
 
 
 
499fdba
 
 
 
 
 
 
 
 
84290a5
 
499fdba
 
 
84290a5
 
 
 
 
 
 
 
 
499fdba
84290a5
 
 
 
 
 
7717985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
a791178
 
547bcde
 
 
 
a791178
 
 
 
547bcde
fbaef79
a791178
 
 
 
 
 
547bcde
a791178
 
547bcde
 
a791178
547bcde
 
 
 
 
 
 
 
a791178
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d23974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
 
7d23974
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d23974
547bcde
 
 
 
 
 
 
abb48e6
547bcde
 
abb48e6
547bcde
 
 
 
 
 
 
 
 
abb48e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84290a5
 
 
 
 
 
 
 
 
 
abb48e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7717985
 
 
 
 
 
abb48e6
28966d8
abb48e6
28966d8
 
 
 
 
 
abb48e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
28966d8
547bcde
28966d8
 
 
 
 
 
 
301db48
547bcde
301db48
7d23974
28966d8
 
547bcde
 
52f55d2
28966d8
 
 
 
7d23974
28966d8
52f55d2
28966d8
52f55d2
 
 
 
 
 
 
 
 
 
 
28966d8
52f55d2
 
 
28966d8
 
 
 
 
7d23974
28966d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f55d2
28966d8
 
 
 
 
 
 
 
547bcde
 
 
301db48
 
 
 
 
 
7d23974
301db48
 
 
 
 
 
 
 
 
 
 
a84b4e4
301db48
 
 
 
a84b4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
7d23974
52f55d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a84b4e4
 
 
301db48
52f55d2
a84b4e4
52f55d2
 
 
 
 
 
 
 
 
 
 
 
a84b4e4
 
 
 
52f55d2
a84b4e4
301db48
a84b4e4
 
 
 
52f55d2
 
 
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
 
 
 
a84b4e4
 
301db48
 
 
 
a84b4e4
 
 
 
301db48
a84b4e4
301db48
 
 
 
a84b4e4
301db48
a84b4e4
301db48
a84b4e4
 
 
 
 
 
 
 
 
301db48
a84b4e4
 
 
 
301db48
a84b4e4
 
301db48
a84b4e4
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
a84b4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
 
 
a84b4e4
301db48
 
 
 
a84b4e4
301db48
 
547bcde
 
a791178
547bcde
 
 
 
bb79bb4
234e0bf
a791178
bb79bb4
 
 
a791178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
bb79bb4
 
a791178
 
 
 
 
bb79bb4
a791178
234e0bf
 
547bcde
234e0bf
 
 
547bcde
 
 
234e0bf
 
547bcde
 
 
234e0bf
547bcde
234e0bf
 
547bcde
234e0bf
 
 
547bcde
 
 
234e0bf
 
547bcde
 
 
234e0bf
547bcde
234e0bf
 
547bcde
234e0bf
 
 
547bcde
 
 
234e0bf
 
547bcde
301db48
547bcde
234e0bf
 
547bcde
 
 
 
 
 
 
 
 
a791178
9dba8e1
a791178
5e9023b
234e0bf
 
37f5ab3
9dba8e1
 
a791178
 
 
9dba8e1
5e9023b
9dba8e1
 
5e9023b
234e0bf
9dba8e1
32ae536
547bcde
 
a791178
 
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
 
a791178
547bcde
 
 
9dba8e1
547bcde
 
a791178
547bcde
 
a791178
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1b904
234e0bf
0b1b904
234e0bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1b904
234e0bf
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
84290a5
7717985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84290a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
 
0b1b904
547bcde
 
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1b904
234e0bf
0b1b904
234e0bf
 
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
a791178
 
547bcde
 
 
 
234e0bf
 
 
 
547bcde
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234e0bf
 
547bcde
 
 
234e0bf
 
 
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
547bcde
 
 
234e0bf
 
 
 
547bcde
301db48
547bcde
 
 
301db48
234e0bf
 
 
 
547bcde
301db48
547bcde
301db48
547bcde
 
 
 
 
a791178
301db48
 
547bcde
 
301db48
547bcde
 
 
234e0bf
 
547bcde
 
 
234e0bf
 
 
 
 
 
547bcde
 
 
 
 
 
234e0bf
 
 
 
547bcde
301db48
 
234e0bf
 
 
 
301db48
 
 
 
 
 
234e0bf
 
 
 
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
301db48
 
 
234e0bf
 
 
 
301db48
 
 
 
 
 
234e0bf
 
 
 
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
301db48
 
 
234e0bf
 
 
 
301db48
 
 
 
 
 
234e0bf
 
 
 
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234e0bf
 
 
 
301db48
 
547bcde
234e0bf
 
 
 
547bcde
 
 
234e0bf
 
 
 
a791178
9dba8e1
a791178
 
234e0bf
 
 
 
9dba8e1
 
453c62c
9dba8e1
 
547bcde
 
 
 
453c62c
fbaef79
a791178
fbaef79
a791178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
a791178
 
453c62c
fbaef79
453c62c
84290a5
453c62c
 
 
 
547bcde
453c62c
 
 
 
a791178
77b55d9
 
453c62c
 
 
 
 
 
a791178
547bcde
a791178
453c62c
 
 
 
a791178
453c62c
 
 
 
301db48
 
453c62c
 
3dc43a9
 
 
 
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbaef79
547bcde
 
 
7717985
547bcde
7717985
547bcde
 
 
 
fbaef79
 
 
 
 
 
 
 
 
 
7717985
 
 
 
 
e8e1e46
7717985
fbaef79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84290a5
 
 
 
 
 
 
 
fbaef79
 
 
 
 
 
547bcde
453c62c
 
 
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
 
 
7717985
 
 
 
 
 
 
 
 
453c62c
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301db48
 
84290a5
301db48
 
 
 
 
84290a5
 
 
 
 
 
301db48
 
 
 
 
 
 
 
547bcde
 
 
 
 
 
 
 
 
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
453c62c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
453c62c
301db48
 
 
 
 
 
453c62c
 
 
 
 
 
547bcde
453c62c
 
 
 
 
 
547bcde
 
 
 
 
 
301db48
84290a5
301db48
547bcde
453c62c
 
 
 
547bcde
453c62c
 
 
 
 
84290a5
547bcde
453c62c
7050196
547bcde
 
 
301db48
 
 
 
 
 
 
84290a5
7717985
301db48
 
547bcde
e8e1e46
547bcde
 
 
 
 
 
 
 
 
 
 
301db48
 
84290a5
 
e8e1e46
 
301db48
 
547bcde
 
5a2e582
547bcde
 
 
 
 
 
 
 
 
 
 
301db48
 
84290a5
 
5a2e582
 
301db48
 
547bcde
301db48
5611226
301db48
5611226
234e0bf
5611226
 
 
301db48
 
5611226
301db48
5611226
 
301db48
cf6c1c3
234e0bf
301db48
 
 
 
 
 
 
547bcde
301db48
 
 
 
cf6c1c3
301db48
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
 
e8e1e46
5611226
 
 
 
 
 
e8e1e46
5611226
e8e1e46
 
 
 
 
 
5611226
 
 
 
 
 
301db48
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
cf6c1c3
301db48
 
 
 
 
 
 
547bcde
84290a5
 
 
301db48
84290a5
301db48
 
 
 
 
 
84290a5
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547bcde
84290a5
7717985
547bcde
 
84290a5
547bcde
84290a5
547bcde
84290a5
547bcde
84290a5
547bcde
84290a5
301db48
84290a5
 
 
7717985
 
301db48
84290a5
547bcde
a791178
547bcde
 
 
 
 
 
 
 
 
 
 
77b55d9
 
 
 
 
 
 
84290a5
5611226
77b55d9
 
547bcde
 
5611226
 
 
 
 
 
 
 
 
 
 
 
547bcde
 
 
5611226
 
 
 
 
 
 
 
 
 
 
 
547bcde
301db48
5611226
 
 
301db48
 
5611226
 
 
301db48
547bcde
 
77b55d9
453c62c
77b55d9
 
 
 
 
 
 
84290a5
e8e1e46
77b55d9
 
547bcde
301db48
 
e8e1e46
301db48
453c62c
 
301db48
547bcde
 
 
7050196
547bcde
 
 
 
301db48
 
 
 
 
 
 
 
7050196
e8e1e46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7050196
547bcde
 
 
 
 
 
 
7050196
301db48
 
 
 
 
 
 
 
453c62c
 
547bcde
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
547bcde
453c62c
547bcde
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
453c62c
547bcde
 
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
 
547bcde
 
301db48
 
 
547bcde
 
301db48
 
547bcde
301db48
 
 
 
 
 
 
 
453c62c
 
84290a5
 
 
301db48
 
84290a5
 
301db48
 
84290a5
 
301db48
 
84290a5
 
301db48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77b55d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84290a5
 
77b55d9
 
 
cf6c1c3
547bcde
499fdba
7717985
84290a5
 
 
7717985
84290a5
547bcde
 
499fdba
7717985
547bcde
 
 
499fdba
 
234e0bf
 
 
fbaef79
 
 
499fdba
fbaef79
499fdba
fbaef79
499fdba
fbaef79
499fdba
fbaef79
499fdba
fbaef79
499fdba
fbaef79
499fdba
84290a5
711b35d
7717985
499fdba
fbaef79
 
547bcde
 
 
499fdba
 
547bcde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5611226
 
 
 
 
 
 
 
547bcde
 
453c62c
5611226
453c62c
547bcde
5611226
 
547bcde
453c62c
fbaef79
5611226
 
453c62c
a791178
 
 
 
 
 
453c62c
 
547bcde
453c62c
547bcde
453c62c
547bcde
5611226
 
547bcde
453c62c
fbaef79
5611226
 
453c62c
a791178
 
 
 
 
 
453c62c
 
547bcde
453c62c
 
 
 
 
 
 
 
 
 
 
 
9dba8e1
453c62c
c96734b
cf6c1c3
 
 
a791178
 
cf6c1c3
84290a5
7717985
 
 
 
 
84290a5
 
 
a791178
cf6c1c3
 
 
 
 
 
 
 
 
 
 
 
301db48
 
 
 
 
 
 
cf6c1c3
 
 
 
 
 
 
 
 
 
301db48
7198760
 
cf6c1c3
fbaef79
453c62c
a791178
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
import os
import gradio as gr
import requests
import json
import base64
import logging
import io
import time
from typing import List, Dict, Any, Union, Tuple, Optional
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Gracefully import libraries with fallbacks
try:
    from PIL import Image
    HAS_PIL = True
except ImportError:
    logger.warning("PIL not installed. Image processing will be limited.")
    HAS_PIL = False

try:
    import PyPDF2
    HAS_PYPDF2 = True
except ImportError:
    logger.warning("PyPDF2 not installed. PDF processing will be limited.")
    HAS_PYPDF2 = False

try:
    import markdown
    HAS_MARKDOWN = True
except ImportError:
    logger.warning("Markdown not installed. Markdown processing will be limited.")
    HAS_MARKDOWN = False

try:
    import openai
    HAS_OPENAI = True
except ImportError:
    logger.warning("OpenAI package not installed. OpenAI models will be unavailable.")
    HAS_OPENAI = False

try:
    from groq import Groq
    HAS_GROQ = True
except ImportError:
    logger.warning("Groq client not installed. Groq API will be unavailable.")
    HAS_GROQ = False

try:
    import cohere
    HAS_COHERE = True
except ImportError:
    logger.warning("Cohere package not installed. Cohere models will be unavailable.")
    HAS_COHERE = False

try:
    from huggingface_hub import InferenceClient
    HAS_HF = True
except ImportError:
    logger.warning("HuggingFace hub not installed. HuggingFace models will be limited.")
    HAS_HF = False

# API keys from environment
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
COHERE_API_KEY = os.environ.get("COHERE_API_KEY", "")
HF_API_KEY = os.environ.get("HF_API_KEY", "")
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY", "")
GOOGLEAI_API_KEY = os.environ.get("GOOGLEAI_API_KEY", "")
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
POE_API_KEY = os.environ.get("POE_API_KEY", "")

# Print application startup message with timestamp
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
print(f"===== Application Startup at {current_time} =====\n")

# ==========================================================
# MODEL DEFINITIONS
# ==========================================================

# OPENROUTER MODELS
# These are the original models from the provided code
OPENROUTER_MODELS = [
    # 1M+ Context Models
    {"category": "1M+ Context", "models": [
        ("Google: Gemini Pro 2.0 Experimental", "google/gemini-2.0-pro-exp-02-05:free", 2000000),
        ("Google: Gemini 2.0 Flash Thinking Experimental 01-21", "google/gemini-2.0-flash-thinking-exp:free", 1048576),
        ("Google: Gemini Flash 2.0 Experimental", "google/gemini-2.0-flash-exp:free", 1048576),
        ("Google: Gemini Pro 2.5 Experimental", "google/gemini-2.5-pro-exp-03-25:free", 1000000),
        ("Google: Gemini Flash 1.5 8B Experimental", "google/gemini-flash-1.5-8b-exp", 1000000),
    ]},
    
    # 100K-1M Context Models
    {"category": "100K+ Context", "models": [
        ("DeepSeek: DeepSeek R1 Zero", "deepseek/deepseek-r1-zero:free", 163840),
        ("DeepSeek: R1", "deepseek/deepseek-r1:free", 163840),
        ("DeepSeek: DeepSeek V3 Base", "deepseek/deepseek-v3-base:free", 131072),
        ("DeepSeek: DeepSeek V3 0324", "deepseek/deepseek-chat-v3-0324:free", 131072),
        ("Google: Gemma 3 4B", "google/gemma-3-4b-it:free", 131072),
        ("Google: Gemma 3 12B", "google/gemma-3-12b-it:free", 131072),
        ("Nous: DeepHermes 3 Llama 3 8B Preview", "nousresearch/deephermes-3-llama-3-8b-preview:free", 131072),
        ("Qwen: Qwen2.5 VL 72B Instruct", "qwen/qwen2.5-vl-72b-instruct:free", 131072),
        ("DeepSeek: DeepSeek V3", "deepseek/deepseek-chat:free", 131072),
        ("NVIDIA: Llama 3.1 Nemotron 70B Instruct", "nvidia/llama-3.1-nemotron-70b-instruct:free", 131072),
        ("Meta: Llama 3.2 1B Instruct", "meta-llama/llama-3.2-1b-instruct:free", 131072),
        ("Meta: Llama 3.2 11B Vision Instruct", "meta-llama/llama-3.2-11b-vision-instruct:free", 131072),
        ("Meta: Llama 3.1 8B Instruct", "meta-llama/llama-3.1-8b-instruct:free", 131072),
        ("Mistral: Mistral Nemo", "mistralai/mistral-nemo:free", 128000),
    ]},
    
    # 64K-100K Context Models
    {"category": "64K-100K Context", "models": [
        ("Mistral: Mistral Small 3.1 24B", "mistralai/mistral-small-3.1-24b-instruct:free", 96000),
        ("Google: Gemma 3 27B", "google/gemma-3-27b-it:free", 96000),
        ("Qwen: Qwen2.5 VL 3B Instruct", "qwen/qwen2.5-vl-3b-instruct:free", 64000),
        ("DeepSeek: R1 Distill Qwen 14B", "deepseek/deepseek-r1-distill-qwen-14b:free", 64000),
        ("Qwen: Qwen2.5-VL 7B Instruct", "qwen/qwen-2.5-vl-7b-instruct:free", 64000),
    ]},
    
    # 32K-64K Context Models
    {"category": "32K-64K Context", "models": [
        ("Google: LearnLM 1.5 Pro Experimental", "google/learnlm-1.5-pro-experimental:free", 40960),
        ("Qwen: QwQ 32B", "qwen/qwq-32b:free", 40000),
        ("Google: Gemini 2.0 Flash Thinking Experimental", "google/gemini-2.0-flash-thinking-exp-1219:free", 40000),
        ("Bytedance: UI-TARS 72B", "bytedance-research/ui-tars-72b:free", 32768),
        ("Qwerky 72b", "featherless/qwerky-72b:free", 32768),
        ("OlympicCoder 7B", "open-r1/olympiccoder-7b:free", 32768),
        ("OlympicCoder 32B", "open-r1/olympiccoder-32b:free", 32768),
        ("Google: Gemma 3 1B", "google/gemma-3-1b-it:free", 32768),
        ("Reka: Flash 3", "rekaai/reka-flash-3:free", 32768),
        ("Dolphin3.0 R1 Mistral 24B", "cognitivecomputations/dolphin3.0-r1-mistral-24b:free", 32768),
        ("Dolphin3.0 Mistral 24B", "cognitivecomputations/dolphin3.0-mistral-24b:free", 32768),
        ("Mistral: Mistral Small 3", "mistralai/mistral-small-24b-instruct-2501:free", 32768),
        ("Qwen2.5 Coder 32B Instruct", "qwen/qwen-2.5-coder-32b-instruct:free", 32768),
        ("Qwen2.5 72B Instruct", "qwen/qwen-2.5-72b-instruct:free", 32768),
    ]},
    
    # 8K-32K Context Models
    {"category": "8K-32K Context", "models": [
        ("Meta: Llama 3.2 3B Instruct", "meta-llama/llama-3.2-3b-instruct:free", 20000),
        ("Qwen: QwQ 32B Preview", "qwen/qwq-32b-preview:free", 16384),
        ("DeepSeek: R1 Distill Qwen 32B", "deepseek/deepseek-r1-distill-qwen-32b:free", 16000),
        ("Qwen: Qwen2.5 VL 32B Instruct", "qwen/qwen2.5-vl-32b-instruct:free", 8192),
        ("Moonshot AI: Moonlight 16B A3B Instruct", "moonshotai/moonlight-16b-a3b-instruct:free", 8192),
        ("DeepSeek: R1 Distill Llama 70B", "deepseek/deepseek-r1-distill-llama-70b:free", 8192),
        ("Qwen 2 7B Instruct", "qwen/qwen-2-7b-instruct:free", 8192),
        ("Google: Gemma 2 9B", "google/gemma-2-9b-it:free", 8192),
        ("Mistral: Mistral 7B Instruct", "mistralai/mistral-7b-instruct:free", 8192),
        ("Microsoft: Phi-3 Mini 128K Instruct", "microsoft/phi-3-mini-128k-instruct:free", 8192),
        ("Microsoft: Phi-3 Medium 128K Instruct", "microsoft/phi-3-medium-128k-instruct:free", 8192),
        ("Meta: Llama 3 8B Instruct", "meta-llama/llama-3-8b-instruct:free", 8192),
        ("OpenChat 3.5 7B", "openchat/openchat-7b:free", 8192),
        ("Meta: Llama 3.3 70B Instruct", "meta-llama/llama-3.3-70b-instruct:free", 8000),
    ]},
    
    # <8K Context Models
    {"category": "4K Context", "models": [
        ("AllenAI: Molmo 7B D", "allenai/molmo-7b-d:free", 4096),
        ("Rogue Rose 103B v0.2", "sophosympatheia/rogue-rose-103b-v0.2:free", 4096),
        ("Toppy M 7B", "undi95/toppy-m-7b:free", 4096),
        ("Hugging Face: Zephyr 7B", "huggingfaceh4/zephyr-7b-beta:free", 4096),
        ("MythoMax 13B", "gryphe/mythomax-l2-13b:free", 4096),
    ]},

    # Vision-capable Models
    {"category": "Vision Models", "models": [
        ("Google: Gemini Pro 2.0 Experimental", "google/gemini-2.0-pro-exp-02-05:free", 2000000),
        ("Google: Gemini 2.0 Flash Thinking Experimental 01-21", "google/gemini-2.0-flash-thinking-exp:free", 1048576),
        ("Google: Gemini Flash 2.0 Experimental", "google/gemini-2.0-flash-exp:free", 1048576),
        ("Google: Gemini Pro 2.5 Experimental", "google/gemini-2.5-pro-exp-03-25:free", 1000000),
        ("Google: Gemini Flash 1.5 8B Experimental", "google/gemini-flash-1.5-8b-exp", 1000000),
        ("Google: Gemma 3 4B", "google/gemma-3-4b-it:free", 131072),
        ("Google: Gemma 3 12B", "google/gemma-3-12b-it:free", 131072),
        ("Qwen: Qwen2.5 VL 72B Instruct", "qwen/qwen2.5-vl-72b-instruct:free", 131072),
        ("Meta: Llama 3.2 11B Vision Instruct", "meta-llama/llama-3.2-11b-vision-instruct:free", 131072),
        ("Mistral: Mistral Small 3.1 24B", "mistralai/mistral-small-3.1-24b-instruct:free", 96000),
        ("Google: Gemma 3 27B", "google/gemma-3-27b-it:free", 96000),
        ("Qwen: Qwen2.5 VL 3B Instruct", "qwen/qwen2.5-vl-3b-instruct:free", 64000),
        ("Qwen: Qwen2.5-VL 7B Instruct", "qwen/qwen-2.5-vl-7b-instruct:free", 64000),
        ("Google: LearnLM 1.5 Pro Experimental", "google/learnlm-1.5-pro-experimental:free", 40960),
        ("Google: Gemini 2.0 Flash Thinking Experimental", "google/gemini-2.0-flash-thinking-exp-1219:free", 40000),
        ("Bytedance: UI-TARS 72B", "bytedance-research/ui-tars-72b:free", 32768),
        ("Google: Gemma 3 1B", "google/gemma-3-1b-it:free", 32768),
        ("Qwen: Qwen2.5 VL 32B Instruct", "qwen/qwen2.5-vl-32b-instruct:free", 8192),
        ("AllenAI: Molmo 7B D", "allenai/molmo-7b-d:free", 4096),
    ]},
]

# Flatten OpenRouter model list for easier access
OPENROUTER_ALL_MODELS = []
for category in OPENROUTER_MODELS:
    for model in category["models"]:
        if model not in OPENROUTER_ALL_MODELS:  # Avoid duplicates
            OPENROUTER_ALL_MODELS.append(model)

# VISION MODELS - For tracking which models support images
VISION_MODELS = {
    "OpenRouter": [model[0] for model in OPENROUTER_MODELS[-1]["models"]],  # Last category is Vision Models
    "OpenAI": [
        "gpt-4-vision-preview", "gpt-4o", "gpt-4o-mini", "gpt-4-turbo", 
        "gpt-4-turbo-preview", "gpt-4-0125-preview", "gpt-4-1106-preview",
        "o1-preview", "o1-mini"
    ],
    "HuggingFace": [
        "Qwen/Qwen2.5-VL-7B-Instruct", "Qwen/qwen2.5-vl-3b-instruct", 
        "Qwen/qwen2.5-vl-32b-instruct", "Qwen/qwen2.5-vl-72b-instruct"
    ],
    "Groq": ["llama-3.2-11b-vision", "llama-3.2-90b-vision"],
    "Together": ["Llama-3.2-11B-Vision-Instruct", "Llama-3.2-90B-Vision-Instruct"],
    #"OVH": ["llava-next-mistral-7b", "qwen2.5-vl-72b-instruct"],
    #"Cerebras": [],
    "GoogleAI": ["gemini-1.5-pro", "gemini-1.0-pro", "gemini-1.5-flash", "gemini-2.0-pro", "gemini-2.5-pro"]
}

# POE MODELS
POE_MODELS = {
    "claude_3_igloo": 4000,               # Claude-3.5-Sonnet
    "claude_2_1_cedar": 4000,             # Claude-3-Opus
    "claude_2_1_bamboo": 4000,            # Claude-3-Sonnet
    "claude_3_haiku": 4000,               # Claude-3-Haiku
    "claude_3_igloo_200k": 200000,        # Claude-3.5-Sonnet-200k
    "claude_3_opus_200k": 200000,         # Claude-3-Opus-200k
    "claude_3_sonnet_200k": 200000,       # Claude-3-Sonnet-200k
    "claude_3_haiku_200k": 200000,        # Claude-3-Haiku-200k
    "claude_2_short": 4000,               # Claude-2
    "a2_2": 100000,                       # Claude-2-100k
    "a2": 9000,                           # Claude-instant
    "a2_100k": 100000,                    # Claude-instant-100k
    "chinchilla": 4000,                   # GPT-3.5-Turbo
    "gpt3_5": 2000,                       # GPT-3.5-Turbo-Raw
    "chinchilla_instruct": 2000,          # GPT-3.5-Turbo-Instruct
    "agouti": 16000,                      # ChatGPT-16k
    "gpt4_classic": 2000,                 # GPT-4-Classic
    "beaver": 4000,                       # GPT-4-Turbo
    "vizcacha": 128000,                   # GPT-4-Turbo-128k
    "gpt4_o": 4000,                       # GPT-4o
    "gpt4_o_128k": 128000,                # GPT-4o-128k
    "gpt4_o_mini": 4000,                  # GPT-4o-Mini
    "gpt4_o_mini_128k": 128000,           # GPT-4o-Mini-128k
    "acouchy": 8000,                      # Google-PaLM
    "code_llama_13b_instruct": 4000,      # Code-Llama-13b
    "code_llama_34b_instruct": 4000,      # Code-Llama-34b
    "upstage_solar_0_70b_16bit": 2000,    # Solar-Mini
    "gemini_pro_search": 4000,            # Gemini-1.5-Flash-Search
    "gemini_1_5_pro_1m": 2000000,         # Gemini-1.5-Pro-2M
}

# Add vision-capable models to vision models list
POE_VISION_MODELS = [
    "claude_3_igloo", "claude_2_1_cedar", "claude_2_1_bamboo", "claude_3_haiku",
    "claude_3_igloo_200k", "claude_3_opus_200k", "claude_3_sonnet_200k", "claude_3_haiku_200k",
    "gpt4_o", "gpt4_o_128k", "gpt4_o_mini", "gpt4_o_mini_128k", "beaver", "vizcacha"
]

VISION_MODELS["Poe"] = POE_VISION_MODELS

# OPENAI MODELS
OPENAI_MODELS = {
    "gpt-3.5-turbo": 16385,
    "gpt-3.5-turbo-0125": 16385,
    "gpt-3.5-turbo-1106": 16385,
    "gpt-3.5-turbo-instruct": 4096,
    "gpt-4": 8192,
    "gpt-4-0314": 8192,
    "gpt-4-0613": 8192,
    "gpt-4-turbo": 128000,
    "gpt-4-turbo-2024-04-09": 128000,
    "gpt-4-turbo-preview": 128000,
    "gpt-4-0125-preview": 128000,
    "gpt-4-1106-preview": 128000,
    "gpt-4o": 128000,
    "gpt-4o-2024-11-20": 128000,
    "gpt-4o-2024-08-06": 128000,
    "gpt-4o-2024-05-13": 128000,
    "gpt-4o-mini": 128000,
    "gpt-4o-mini-2024-07-18": 128000,
    "o1-preview": 128000,
    "o1-preview-2024-09-12": 128000,
    "o1-mini": 128000,
    "o1-mini-2024-09-12": 128000,
}

# HUGGINGFACE MODELS 
HUGGINGFACE_MODELS = {
    "microsoft/phi-3-mini-4k-instruct": 4096,
    "microsoft/Phi-3-mini-128k-instruct": 131072,
    "HuggingFaceH4/zephyr-7b-beta": 8192,
    "deepseek-ai/DeepSeek-Coder-V2-Instruct": 8192,
    "mistralai/Mistral-7B-Instruct-v0.3": 32768,
    "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": 32768,
    "microsoft/Phi-3.5-mini-instruct": 4096,
    "google/gemma-2-2b-it": 2048,
    "openai-community/gpt2": 1024,
    "microsoft/phi-2": 2048,
    "TinyLlama/TinyLlama-1.1B-Chat-v1.0": 2048,
    "VAGOsolutions/Llama-3-SauerkrautLM-8b-Instruct": 2048,
    "VAGOsolutions/Llama-3.1-SauerkrautLM-8b-Instruct": 4096,
    "VAGOsolutions/SauerkrautLM-Nemo-12b-Instruct": 4096,
    "openGPT-X/Teuken-7B-instruct-research-v0.4": 4096,
    "Qwen/Qwen2.5-7B-Instruct": 131072,
    "tiiuae/falcon-7b-instruct": 8192,
    "Qwen/QwQ-32B-preview": 32768,
    "Qwen/Qwen2.5-VL-7B-Instruct": 64000,
    "Qwen/qwen2.5-vl-3b-instruct": 64000,
    "Qwen/qwen2.5-vl-32b-instruct": 8192,
    "Qwen/qwen2.5-vl-72b-instruct": 131072,
}

# GROQ MODELS - We'll populate this dynamically
DEFAULT_GROQ_MODELS = {
    "deepseek-r1-distill-llama-70b": 8192,
    "deepseek-r1-distill-qwen-32b": 8192,
    "gemma2-9b-it": 8192,
    "llama-3.1-8b-instant": 131072,
    "llama-3.2-1b-preview": 131072,
    "llama-3.2-3b-preview": 131072,
    "llama-3.2-11b-vision-preview": 131072,
    "llama-3.2-90b-vision-preview": 131072,
    "llama-3.3-70b-specdec": 131072,
    "llama-3.3-70b-versatile": 131072,
    "llama-guard-3-8b": 8192,
    "llama3-8b-8192": 8192,
    "llama3-70b-8192": 8192,
    "mistral-saba-24b": 32768,
    "qwen-2.5-32b": 32768,
    "qwen-2.5-coder-32b": 32768,
    "qwen-qwq-32b": 32768,
    "playai-tts": 4096,       # Including TTS models but setting reasonable context limits
    "playai-tts-arabic": 4096,
    "distil-whisper-large-v3-en": 4096,
    "whisper-large-v3": 4096,
    "whisper-large-v3-turbo": 4096
}


# COHERE MODELS
COHERE_MODELS = {
    "command-r-plus-08-2024": 131072,
    "command-r-plus-04-2024": 131072,
    "command-r-plus": 131072,
    "command-r-08-2024": 131072,
    "command-r-03-2024": 131072,
    "command-r": 131072,
    "command": 4096,
    "command-nightly": 131072,
    "command-light": 4096,
    "command-light-nightly": 4096,
    "c4ai-aya-expanse-8b": 8192,
    "c4ai-aya-expanse-32b": 131072,
}

# TOGETHER MODELS in the free tier
TOGETHER_MODELS = {
    "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": 131072,
    "deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free": 8192,
    "meta-llama/Llama-Vision-Free": 8192,
    "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free": 8192,
}

# Add to the vision models list
VISION_MODELS["Together"] = ["meta-llama/Llama-Vision-Free"]

# OVH MODELS - OVH AI Endpoints (free beta)
OVH_MODELS = {
    "ovh/codestral-mamba-7b-v0.1": 131072,
    "ovh/deepseek-r1-distill-llama-70b": 8192,
    "ovh/llama-3.1-70b-instruct": 131072,
    "ovh/llama-3.1-8b-instruct": 131072,
    "ovh/llama-3.3-70b-instruct": 131072,
    "ovh/llava-next-mistral-7b": 8192,
    "ovh/mistral-7b-instruct-v0.3": 32768,
    "ovh/mistral-nemo-2407": 131072,
    "ovh/mixtral-8x7b-instruct": 32768,
    "ovh/qwen2.5-coder-32b-instruct": 32768,
    "ovh/qwen2.5-vl-72b-instruct": 131072,
}

# CEREBRAS MODELS
CEREBRAS_MODELS = {
    "llama3.1-8b": 8192,
    "llama-3.3-70b": 8192,
}

# GOOGLE AI MODELS
GOOGLEAI_MODELS = {
    "gemini-1.0-pro": 32768,
    "gemini-1.5-flash": 1000000,
    "gemini-1.5-pro": 1000000,
    "gemini-2.0-pro": 2000000,
    "gemini-2.5-pro": 2000000,
}

# ANTHROPIC MODELS
ANTHROPIC_MODELS = {
    "claude-3-7-sonnet-20250219": 128000,  # Claude 3.7 Sonnet
    "claude-3-5-sonnet-20241022": 200000,  # Claude 3.5 Sonnet
    "claude-3-5-haiku-20240307": 200000,   # Claude 3.5 Haiku 
    "claude-3-5-sonnet-20240620": 200000,  # Claude 3.5 Sonnet 2024-06-20
    "claude-3-opus-20240229": 200000,      # Claude 3 Opus
    "claude-3-haiku-20240307": 200000,     # Claude 3 Haiku
    "claude-3-sonnet-20240229": 200000,    # Claude 3 Sonnet
}

# Add Anthropic to the vision models list
VISION_MODELS["Anthropic"] = [
    "claude-3-7-sonnet-20250219",
    "claude-3-5-sonnet-20241022", 
    "claude-3-opus-20240229", 
    "claude-3-sonnet-20240229",
    "claude-3-5-haiku-20240307",
    "claude-3-haiku-20240307"
]

# Add all models with "vl", "vision", "visual" in their name to HF vision models
for model_name in list(HUGGINGFACE_MODELS.keys()):
    if any(x in model_name.lower() for x in ["vl", "vision", "visual", "llava"]):
        if model_name not in VISION_MODELS["HuggingFace"]:
            VISION_MODELS["HuggingFace"].append(model_name)

# ==========================================================
# HELPER FUNCTIONS
# ==========================================================

def fetch_groq_models():
    """Fetch available Groq models with proper error handling"""
    try:
        if not HAS_GROQ or not GROQ_API_KEY:
            logger.warning("Groq client not available or no API key. Using default model list.")
            return DEFAULT_GROQ_MODELS

        client = Groq(api_key=GROQ_API_KEY)
        models = client.models.list()
        
        # Create dictionary of model_id -> context size
        model_dict = {}
        for model in models.data:
            model_id = model.id
            # Map known context sizes or use a default
            if "llama-3" in model_id and "70b" in model_id:
                context_size = 131072
            elif "llama-3" in model_id and "8b" in model_id:
                context_size = 131072
            elif "mixtral" in model_id:
                context_size = 32768
            elif "gemma" in model_id:
                context_size = 8192
            elif "vision" in model_id:
                context_size = 131072
            else:
                context_size = 8192  # Default assumption
                
            model_dict[model_id] = context_size
            
        # Ensure we have models by combining with defaults
        if not model_dict:
            return DEFAULT_GROQ_MODELS
        return {**DEFAULT_GROQ_MODELS, **model_dict}
        
    except Exception as e:
        logger.error(f"Error fetching Groq models: {e}")
        return DEFAULT_GROQ_MODELS

# Initialize Groq models
GROQ_MODELS = fetch_groq_models()

def encode_image_to_base64(image_path):
    """Encode an image file to base64 string"""
    try:
        if isinstance(image_path, str):  # File path as string
            with open(image_path, "rb") as image_file:
                encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
                file_extension = image_path.split('.')[-1].lower()
                mime_type = f"image/{file_extension}"
                if file_extension in ["jpg", "jpeg"]:
                    mime_type = "image/jpeg"
                elif file_extension == "png":
                    mime_type = "image/png"
                elif file_extension == "webp":
                    mime_type = "image/webp"
                return f"data:{mime_type};base64,{encoded_string}"
        elif hasattr(image_path, 'name'):  # Handle Gradio file objects directly
            with open(image_path.name, "rb") as image_file:
                encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
                file_extension = image_path.name.split('.')[-1].lower()
                mime_type = f"image/{file_extension}"
                if file_extension in ["jpg", "jpeg"]:
                    mime_type = "image/jpeg"
                elif file_extension == "png":
                    mime_type = "image/png"
                elif file_extension == "webp":
                    mime_type = "image/webp"
                return f"data:{mime_type};base64,{encoded_string}"
        else:  # Handle file object or other types
            logger.error(f"Unsupported image type: {type(image_path)}")
            return None
    except Exception as e:
        logger.error(f"Error encoding image: {str(e)}")
        return None

def extract_text_from_file(file_path):
    """Extract text from various file types"""
    try:
        file_extension = file_path.split('.')[-1].lower()
        
        if file_extension == 'pdf':
            if HAS_PYPDF2:
                text = ""
                with open(file_path, 'rb') as file:
                    pdf_reader = PyPDF2.PdfReader(file)
                    for page_num in range(len(pdf_reader.pages)):
                        page = pdf_reader.pages[page_num]
                        text += page.extract_text() + "\n\n"
                return text
            else:
                return "PDF processing is not available (PyPDF2 not installed)"
        
        elif file_extension == 'md':
            with open(file_path, 'r', encoding='utf-8') as file:
                return file.read()
        
        elif file_extension == 'txt':
            with open(file_path, 'r', encoding='utf-8') as file:
                return file.read()
                
        else:
            return f"Unsupported file type: {file_extension}"
            
    except Exception as e:
        logger.error(f"Error extracting text from file: {str(e)}")
        return f"Error processing file: {str(e)}"

def prepare_message_with_media(text, images=None, documents=None):
    """Prepare a message with text, images, and document content"""
    # If no media, return text only
    if not images and not documents:
        return text
    
    # Start with text content
    if documents and len(documents) > 0:
        # If there are documents, append their content to the text
        document_texts = []
        for doc in documents:
            if doc is None:
                continue
            # Make sure to handle file objects properly
            doc_path = doc.name if hasattr(doc, 'name') else doc
            doc_text = extract_text_from_file(doc_path)
            if doc_text:
                document_texts.append(doc_text)
        
        # Add document content to text
        if document_texts:
            if not text:
                text = "Please analyze these documents:"
            else:
                text = f"{text}\n\nDocument content:\n\n"
            
            text += "\n\n".join(document_texts)
            
        # If no images, return text only
        if not images:
            return text
    
    # If we have images, create a multimodal content array
    content = [{"type": "text", "text": text or "Analyze this image:"}]
    
    # Add images if any
    if images:
        # Check if images is a list of image paths or file objects
        if isinstance(images, list):
            for img in images:
                if img is None:
                    continue
                
                encoded_image = encode_image_to_base64(img)
                if encoded_image:
                    content.append({
                        "type": "image_url",
                        "image_url": {"url": encoded_image}
                    })
        else:
            # For single image or Gallery component
            logger.warning(f"Images is not a list: {type(images)}")
            # Try to handle as single image
            encoded_image = encode_image_to_base64(images)
            if encoded_image:
                content.append({
                    "type": "image_url", 
                    "image_url": {"url": encoded_image}
                })
    
    return content

def format_to_message_dict(history):
    """Convert history to proper message format"""
    messages = []
    for item in history:
        if isinstance(item, dict) and "role" in item and "content" in item:
            # Already in the correct format
            messages.append(item)
        elif isinstance(item, list) and len(item) == 2:
            # Convert from old format [user_msg, ai_msg]
            human, ai = item
            if human:
                messages.append({"role": "user", "content": human})
            if ai:
                messages.append({"role": "assistant", "content": ai})
    return messages

def process_uploaded_images(files):
    """Process uploaded image files"""
    file_paths = []
    for file in files:
        if hasattr(file, 'name'):
            file_paths.append(file.name)
    return file_paths

def filter_models(provider, search_term):
    """Filter models based on search term and provider"""
    if provider == "OpenRouter":
        all_models = [model[0] for model in OPENROUTER_ALL_MODELS]
    elif provider == "OpenAI":
        all_models = list(OPENAI_MODELS.keys())
    elif provider == "HuggingFace":
        all_models = list(HUGGINGFACE_MODELS.keys())
    elif provider == "Groq":
        all_models = list(GROQ_MODELS.keys())
    elif provider == "Cohere":
        all_models = list(COHERE_MODELS.keys())
    elif provider == "Together":
        all_models = list(TOGETHER_MODELS.keys())
    elif provider == "OVH":
        all_models = list(OVH_MODELS.keys())
    elif provider == "Cerebras":
        all_models = list(CEREBRAS_MODELS.keys())
    elif provider == "GoogleAI":
        all_models = list(GOOGLEAI_MODELS.keys())
    else:
        return [], None
        
    if not search_term:
        return all_models, all_models[0] if all_models else None
        
    filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
    
    if filtered_models:
        return filtered_models, filtered_models[0]
    else:
        return all_models, all_models[0] if all_models else None

def get_model_info(provider, model_choice):
    """Get model ID and context size based on provider and model name"""
    if provider == "OpenRouter":
        for name, model_id, ctx_size in OPENROUTER_ALL_MODELS:
            if name == model_choice:
                return model_id, ctx_size
    elif provider == "Poe":
        if model_choice in POE_MODELS:
            return model_choice, POE_MODELS[model_choice]
    elif provider == "OpenAI":
        if model_choice in OPENAI_MODELS:
            return model_choice, OPENAI_MODELS[model_choice]
    elif provider == "HuggingFace":
        if model_choice in HUGGINGFACE_MODELS:
            return model_choice, HUGGINGFACE_MODELS[model_choice]
    elif provider == "Groq":
        if model_choice in GROQ_MODELS:
            return model_choice, GROQ_MODELS[model_choice]
    elif provider == "Cohere":
        if model_choice in COHERE_MODELS:
            return model_choice, COHERE_MODELS[model_choice]
    elif provider == "Together":
        if model_choice in TOGETHER_MODELS:
            return model_choice, TOGETHER_MODELS[model_choice]
    elif provider == "Anthropic":
        if model_choice in ANTHROPIC_MODELS:
            return model_choice, ANTHROPIC_MODELS[model_choice]
    elif provider == "GoogleAI":
        if model_choice in GOOGLEAI_MODELS:
            return model_choice, GOOGLEAI_MODELS[model_choice]
    
    return None, 0

def update_context_display(provider, model_name):
    """Update context size display for the selected model"""
    _, ctx_size = get_model_info(provider, model_name)
    return f"{ctx_size:,}" if ctx_size else "Unknown"

def is_vision_model(provider, model_name):
    """Check if a model supports vision/images"""
    # Safety check for None model name
    if model_name is None:
        return False
        
    if provider in VISION_MODELS:
        if model_name in VISION_MODELS[provider]:
            return True
            
        # Also check for common vision indicators in model names
        try:
            if any(x in model_name.lower() for x in ["vl", "vision", "visual", "llava", "gemini"]):
                return True
        except AttributeError:
            # In case model_name is not a string or has no lower method
            return False
    
    return False

def update_model_info(provider, model_name):
    """Generate HTML info display for the selected model"""
    model_id, ctx_size = get_model_info(provider, model_name)
    if not model_id:
        return "<p>Model information not available</p>"
        
    # Check if this is a vision model
    is_vision = is_vision_model(provider, model_name)
    
    vision_badge = '<span style="background-color: #4CAF50; color: white; padding: 3px 6px; border-radius: 3px; font-size: 0.8em; margin-left: 5px;">Vision</span>' if is_vision else ''
    
    # For OpenRouter, show the model ID
    model_id_html = f"<p><strong>Model ID:</strong> {model_id}</p>" if provider == "OpenRouter" else ""
    
    # For others, the ID is the same as the name
    if provider != "OpenRouter":
        model_id_html = ""
    
    return f"""
    <div class="model-info">
        <h3>{model_name} {vision_badge}</h3>
        {model_id_html}
        <p><strong>Context Size:</strong> {ctx_size:,} tokens</p>
        <p><strong>Provider:</strong> {provider}</p>
        {f'<p><strong>Features:</strong> Supports image understanding</p>' if is_vision else ''}
    </div>
    """

# ==========================================================
# API HANDLERS
# ==========================================================

def call_anthropic_api(payload, api_key_override=None):
    """Make a call to Anthropic API with error handling"""
    try:
        # Try to import Anthropic
        try:
            import anthropic
            from anthropic import Anthropic
        except ImportError:
            raise ImportError("Anthropic package not installed. Install it with: pip install anthropic")
            
        api_key = api_key_override if api_key_override else os.environ.get("ANTHROPIC_API_KEY", "")
        if not api_key:
            raise ValueError("Anthropic API key is required")
            
        client = Anthropic(api_key=api_key)
        
        # Extract parameters from payload
        model = payload.get("model", "claude-3-5-sonnet-20241022")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        
        # Format messages for Anthropic
        # Find system message if any
        system_prompt = None
        chat_messages = []
        
        for msg in messages:
            if msg["role"] == "system":
                system_prompt = msg["content"]
            else:
                # Format content
                if isinstance(msg["content"], list):
                    # Handle multimodal content (images)
                    anthropic_content = []
                    for item in msg["content"]:
                        if item["type"] == "text":
                            anthropic_content.append({
                                "type": "text",
                                "text": item["text"]
                            })
                        elif item["type"] == "image_url":
                            # Extract base64 from data URL if present
                            image_url = item["image_url"]["url"]
                            if image_url.startswith("data:"):
                                # Extract media type and base64 data
                                parts = image_url.split(",", 1)
                                media_type = parts[0].split(":")[1].split(";")[0]
                                base64_data = parts[1]
                                
                                anthropic_content.append({
                                    "type": "image",
                                    "source": {
                                        "type": "base64",
                                        "media_type": media_type,
                                        "data": base64_data
                                    }
                                })
                            else:
                                # URL not supported by Anthropic yet
                                anthropic_content.append({
                                    "type": "text",
                                    "text": f"[Image URL: {image_url}]"
                                })
                    chat_messages.append({
                        "role": msg["role"],
                        "content": anthropic_content
                    })
                else:
                    # Simple text content
                    chat_messages.append({
                        "role": msg["role"],
                        "content": msg["content"]
                    })
        
        # Make request to Anthropic
        response = client.messages.create(
            model=model,
            max_tokens=max_tokens,
            temperature=temperature,
            system=system_prompt,
            messages=chat_messages
        )
        
        return response
    except Exception as e:
        logger.error(f"Anthropic API error: {str(e)}")
        raise e

def call_poe_api(payload, api_key_override=None):
    """Make a call to Poe API with error handling"""
    try:
        # Try to import fastapi_poe
        try:
            import fastapi_poe as fp
        except ImportError:
            raise ImportError("fastapi_poe package not installed. Install it with: pip install fastapi_poe")
            
        api_key = api_key_override if api_key_override else os.environ.get("POE_API_KEY", "")
        if not api_key:
            raise ValueError("Poe API key is required")
            
        # Extract parameters from payload
        model = payload.get("model", "chinchilla")  # Default to GPT-3.5-Turbo
        messages = payload.get("messages", [])
        
        # Convert messages to Poe format
        poe_messages = []
        for msg in messages:
            role = msg["role"]
            content = msg["content"]
            
            # Skip system messages as Poe doesn't support them directly
            if role == "system":
                continue
                
            # Convert content format
            if isinstance(content, list):
                # Handle multimodal content (images)
                text_parts = []
                for item in content:
                    if item["type"] == "text":
                        text_parts.append(item["text"])
                
                # For images, we'll need to extract and handle them separately
                # This is a simplified approach - in reality, you'd need to handle images properly
                content = "\n".join(text_parts)
                
            # Add message to Poe messages
            poe_messages.append(fp.ProtocolMessage(role=role, content=content))
        
        # Make synchronous request to Poe
        response_content = ""
        for partial in fp.get_bot_response_sync(messages=poe_messages, bot_name=model, api_key=api_key):
            if hasattr(partial, "text"):
                response_content += partial.text
        
        # Create a response object with a structure similar to other APIs
        response = {
            "id": f"poe-{int(time.time())}",
            "choices": [
                {
                    "message": {
                        "role": "assistant",
                        "content": response_content
                    }
                }
            ]
        }
        
        return response
    except Exception as e:
        logger.error(f"Poe API error: {str(e)}")
        raise e

def call_openrouter_api(payload, api_key_override=None):
    """Make a call to OpenRouter API with error handling"""
    try:
        api_key = api_key_override if api_key_override else OPENROUTER_API_KEY
        if not api_key:
            raise ValueError("OpenRouter API key is required")
            
        response = requests.post(
            "https://openrouter.ai/api/v1/chat/completions",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {api_key}",
                "HTTP-Referer": "https://huggingface.co/spaces/cstr/CrispChat"
            },
            json=payload,
            timeout=180  # Longer timeout for document processing
        )
        return response
    except requests.RequestException as e:
        logger.error(f"OpenRouter API request error: {str(e)}")
        raise e

def call_openai_api(payload, api_key_override=None):
    """Make a call to OpenAI API with error handling"""
    try:
        if not HAS_OPENAI:
            raise ImportError("OpenAI package not installed")
            
        api_key = api_key_override if api_key_override else OPENAI_API_KEY
        if not api_key:
            raise ValueError("OpenAI API key is required")
            
        client = openai.OpenAI(api_key=api_key)
        
        # Extract parameters from payload
        model = payload.get("model", "gpt-3.5-turbo")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        stream = payload.get("stream", False)
        top_p = payload.get("top_p", 0.9)
        presence_penalty = payload.get("presence_penalty", 0)
        frequency_penalty = payload.get("frequency_penalty", 0)
        
        # Handle response format if specified
        response_format = None
        if payload.get("response_format") == "json_object":
            response_format = {"type": "json_object"}
            
        # Create completion
        response = client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens,
            stream=stream,
            top_p=top_p,
            presence_penalty=presence_penalty,
            frequency_penalty=frequency_penalty,
            response_format=response_format
        )
        
        return response
    except Exception as e:
        logger.error(f"OpenAI API error: {str(e)}")
        raise e

def call_huggingface_api(payload, api_key_override=None):
    """Make a call to HuggingFace API with error handling"""
    try:
        if not HAS_HF:
            raise ImportError("HuggingFace hub not installed")
            
        api_key = api_key_override if api_key_override else HF_API_KEY
        
        # Extract parameters from payload
        model_id = payload.get("model", "mistralai/Mistral-7B-Instruct-v0.3")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 500)
        
        # Create a prompt from messages
        prompt = ""
        for msg in messages:
            role = msg["role"].upper()
            content = msg["content"]
            
            # Handle multimodal content
            if isinstance(content, list):
                text_parts = []
                for item in content:
                    if item["type"] == "text":
                        text_parts.append(item["text"])
                content = "\n".join(text_parts)
                
            prompt += f"{role}: {content}\n"
            
        prompt += "ASSISTANT: "
        
        # Create client with or without API key
        client = InferenceClient(token=api_key) if api_key else InferenceClient()
        
        # Generate response
        response = client.text_generation(
            prompt,
            model=model_id,
            max_new_tokens=max_tokens,
            temperature=temperature,
            repetition_penalty=1.1
        )
        
        return {"generated_text": str(response)}
    except Exception as e:
        logger.error(f"HuggingFace API error: {str(e)}")
        raise e

def call_groq_api(payload, api_key_override=None):
    """Make a call to Groq API with error handling"""
    try:
        if not HAS_GROQ:
            raise ImportError("Groq client not installed")
            
        api_key = api_key_override if api_key_override else GROQ_API_KEY
        if not api_key:
            raise ValueError("Groq API key is required")
            
        client = Groq(api_key=api_key)
        
        # Extract parameters from payload
        model = payload.get("model", "llama-3.1-8b-instant")
        
        # Clean up messages - remove any unexpected properties
        messages = []
        for msg in payload.get("messages", []):
            clean_msg = {
                "role": msg["role"],
                "content": msg["content"]
            }
            messages.append(clean_msg)
        
        # Basic parameters
        groq_payload = {
            "model": model,
            "messages": messages,
            "temperature": payload.get("temperature", 0.7),
            "max_tokens": payload.get("max_tokens", 1000),
            "stream": payload.get("stream", False),
            "top_p": payload.get("top_p", 0.9)
        }
        
        # Create completion
        response = client.chat.completions.create(**groq_payload)
        
        return response
    except Exception as e:
        logger.error(f"Groq API error: {str(e)}")
        raise e

def call_cohere_api(payload, api_key_override=None):
    """Make a call to Cohere API with error handling"""
    try:
        if not HAS_COHERE:
            raise ImportError("Cohere package not installed")
            
        api_key = api_key_override if api_key_override else COHERE_API_KEY
        if not api_key:
            raise ValueError("Cohere API key is required")
            
        client = cohere.ClientV2(api_key=api_key)
        
        # Extract parameters from payload
        model = payload.get("model", "command-r-plus")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        
        # Create chat completion
        response = client.chat(
            model=model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens
        )
        
        return response
    except Exception as e:
        logger.error(f"Cohere API error: {str(e)}")
        raise e

def extract_ai_response(result, provider):
    """Extract AI response based on provider format"""
    try:
        if provider == "OpenRouter":
            if isinstance(result, dict):
                if "choices" in result and len(result["choices"]) > 0:
                    if "message" in result["choices"][0]:
                        message = result["choices"][0]["message"]
                        if message.get("reasoning") and not message.get("content"):
                            reasoning = message.get("reasoning")
                            lines = reasoning.strip().split('\n')
                            for line in lines:
                                if line and not line.startswith('I should') and not line.startswith('Let me'):
                                    return line.strip()
                            for line in lines:
                                if line.strip():
                                    return line.strip()
                        return message.get("content", "")
                    elif "delta" in result["choices"][0]:
                        return result["choices"][0]["delta"].get("content", "")
                    
        elif provider == "OpenAI":
            if hasattr(result, "choices") and len(result.choices) > 0:
                return result.choices[0].message.content
            
        elif provider == "Anthropic":
            if hasattr(result, "content"):
                # Combine text from all content blocks
                full_text = ""
                for block in result.content:
                    if block.type == "text":
                        full_text += block.text
                return full_text
            return "No content returned from Anthropic"
                
        elif provider == "HuggingFace":
            return result.get("generated_text", "")
                
        elif provider == "Groq":
            if hasattr(result, "choices") and len(result.choices) > 0:
                return result.choices[0].message.content
                
        elif provider == "Cohere":
            # Specific handling for Cohere's response format
            if hasattr(result, "message") and hasattr(result.message, "content"):
                # Extract text from content items
                text_content = ""
                for content_item in result.message.content:
                    if hasattr(content_item, "text") and content_item.text:
                        text_content += content_item.text
                return text_content
            else:
                return "No response content from Cohere"

        elif provider == "Poe":
            if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
                return result["choices"][0]["message"]["content"]
            return "No response content from Poe"

        elif provider == "Together":
            # Handle response from Together's native client
            if hasattr(result, "choices") and len(result.choices) > 0:
                if hasattr(result.choices[0], "message") and hasattr(result.choices[0].message, "content"):
                    return result.choices[0].message.content
                elif hasattr(result.choices[0], "delta") and hasattr(result.choices[0].delta, "content"):
                    return result.choices[0].delta.content
            # Fallback
            return str(result)
                
        elif provider == "OVH":
            if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
                return result["choices"][0]["message"]["content"]
                
        elif provider == "Cerebras":
            if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
                return result["choices"][0]["message"]["content"]
                
        elif provider == "GoogleAI":
            if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
                return result["choices"][0]["message"]["content"]
            
        logger.error(f"Unexpected response structure from {provider}: {result}")
        return f"Error: Could not extract response from {provider} API result"
    except Exception as e:
        logger.error(f"Error extracting AI response: {str(e)}")
        return f"Error: {str(e)}"

def call_together_api(payload, api_key_override=None):
    """Make a call to Together API with error handling using their native client"""
    try:
        # Import Together's native client
        # Note: This might need to be installed with: pip install together
        try:
            from together import Together
        except ImportError:
            raise ImportError("The Together Python package is not installed. Please install it with: pip install together")
        
        api_key = api_key_override if api_key_override else TOGETHER_API_KEY
        if not api_key:
            raise ValueError("Together API key is required")
        
        # Create the Together client
        client = Together(api_key=api_key)
        
        # Extract parameters from payload
        requested_model = payload.get("model", "")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        stream = payload.get("stream", False)
        
        # Use one of the free, serverless models
        free_models = [
            "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
            "deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free",
            "meta-llama/Llama-Vision-Free",
            "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
        ]
        
        # Default to the first free model
        model = free_models[0]
        
        # Try to match a requested model with a free model if possible
        if requested_model:
            for free_model in free_models:
                if requested_model.lower() in free_model.lower() or free_model.lower() in requested_model.lower():
                    model = free_model
                    break
        
        # Process messages for possible image content
        processed_messages = []
        for msg in messages:
            role = msg["role"]
            content = msg["content"]
            
            # Handle multimodal content for vision models
            if isinstance(content, list) and "vision" in model.lower():
                # Format according to Together's expected multimodal format
                parts = []
                for item in content:
                    if item["type"] == "text":
                        parts.append({"type": "text", "text": item["text"]})
                    elif item["type"] == "image_url":
                        parts.append({
                            "type": "image_url",
                            "image_url": item["image_url"]
                        })
                processed_messages.append({"role": role, "content": parts})
            else:
                # Regular text messages
                processed_messages.append({"role": role, "content": content})
        
        # Create completion with Together's client
        response = client.chat.completions.create(
            model=model,
            messages=processed_messages,
            temperature=temperature,
            max_tokens=max_tokens,
            stream=stream
        )
        
        return response
    except Exception as e:
        logger.error(f"Together API error: {str(e)}")
        raise e

def call_ovh_api(payload, api_key_override=None):
    """Make a call to OVH AI Endpoints API with error handling"""
    try:
        # Extract parameters from payload
        model = payload.get("model", "ovh/llama-3.1-8b-instruct")
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
                
        headers = {
            "Content-Type": "application/json"
        }
        
        data = {
            "model": model,
            "messages": messages,
            "temperature": temperature,
            "max_tokens": max_tokens
        }
        
        # Use a try-except to handle DNS resolution errors and provide a more helpful message
        try:
            # Correct endpoint URL based on documentation
            response = requests.post(
                "https://endpoints.ai.cloud.ovh.net/v1/chat/completions",  # Updated endpoint
                headers=headers,
                json=data,
                timeout=10  # Add timeout to avoid hanging
            )
            
            if response.status_code != 200:
                raise ValueError(f"OVH API returned status code {response.status_code}: {response.text}")
                
            return response.json()
        except requests.exceptions.ConnectionError as e:
            raise ValueError(f"Connection error to OVH API. This may be due to network restrictions in the environment: {str(e)}")
            
    except Exception as e:
        logger.error(f"OVH API error: {str(e)}")
        raise e

def call_cerebras_api(payload, api_key_override=None):
    """Make a call to Cerebras API with error handling"""
    try:
        # Extract parameters from payload
        requested_model = payload.get("model", "")
        
        # Map the full model name to the correct Cerebras model ID
        model_mapping = {
            "cerebras/llama-3.1-8b": "llama3.1-8b",
            "cerebras/llama-3.3-70b": "llama-3.3-70b",
            "llama-3.1-8b": "llama3.1-8b",
            "llama-3.3-70b": "llama-3.3-70b",
            "llama3.1-8b": "llama3.1-8b"
        }
        
        # Default to the 8B model
        model = "llama3.1-8b"
        
        # If the requested model matches any of our mappings, use that instead
        if requested_model in model_mapping:
            model = model_mapping[requested_model]
        elif "3.3" in requested_model or "70b" in requested_model.lower():
            model = "llama-3.3-70b"
        
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        
        # Try-except block for network issues
        try:
            headers = {
                "Content-Type": "application/json",
                "Authorization": f"Bearer {api_key_override or os.environ.get('CEREBRAS_API_KEY', '')}"
            }
            
            data = {
                "model": model,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            response = requests.post(
                "https://api.cloud.cerebras.ai/v1/chat/completions",
                headers=headers,
                json=data,
                timeout=30  # Increased timeout
            )
            
            if response.status_code != 200:
                raise ValueError(f"Cerebras API returned status code {response.status_code}: {response.text}")
                
            return response.json()
        except requests.exceptions.RequestException as e:
            # More specific error handling for network issues
            if "NameResolution" in str(e):
                raise ValueError(
                    "Unable to connect to the Cerebras API. This might be due to network "
                    "restrictions in your environment. The API requires direct internet access. "
                    "Please try a different provider or check your network settings."
                )
            else:
                raise ValueError(f"Request to Cerebras API failed: {str(e)}")
    except Exception as e:
        logger.error(f"Cerebras API error: {str(e)}")
        raise e

def call_googleai_api(payload, api_key_override=None):
    """Make a call to Google AI (Gemini) API with error handling"""
    try:
        api_key = api_key_override if api_key_override else GOOGLEAI_API_KEY
        if not api_key:
            raise ValueError("Google AI API key is required")
            
        # Use regular requests instead of the SDK since it might be missing
        gemini_api_url = "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent"
        
        # Extract parameters from payload
        messages = payload.get("messages", [])
        temperature = payload.get("temperature", 0.7)
        max_tokens = payload.get("max_tokens", 1000)
        
        # Convert to Google's format
        content_parts = []
        
        # Add all messages
        for msg in messages:
            role = msg["role"]
            content = msg["content"]
            
            # Handle different roles
            if role == "system":
                # For system messages, we add it as part of the first user message
                continue
            elif role == "user":
                # For user messages, add as regular content
                if isinstance(content, str):
                    content_parts.append({"text": content})
                else:
                    # Handle multimodal content
                    for item in content:
                        if item["type"] == "text":
                            content_parts.append({"text": item["text"]})
            
        # Form the request data
        data = {
            "contents": [{"parts": content_parts}],
            "generationConfig": {
                "temperature": temperature,
                "maxOutputTokens": max_tokens,
                "topP": payload.get("top_p", 0.95),
            }
        }
        
        headers = {
            "Content-Type": "application/json",
            "x-goog-api-key": api_key
        }
        
        # Make the request
        response = requests.post(
            gemini_api_url,
            headers=headers,
            json=data,
            timeout=30
        )
        
        if response.status_code != 200:
            error_msg = f"Google AI API error: {response.status_code} - {response.text}"
            logger.error(error_msg)
            raise ValueError(error_msg)
            
        # Parse response and convert to standard format
        result = response.json()
        text_content = ""
        
        # Extract text from response
        if "candidates" in result and len(result["candidates"]) > 0:
            candidate = result["candidates"][0]
            if "content" in candidate and "parts" in candidate["content"]:
                for part in candidate["content"]["parts"]:
                    if "text" in part:
                        text_content += part["text"]
        
        # Create a standardized response format
        return {
            "choices": [
                {
                    "message": {
                        "role": "assistant",
                        "content": text_content
                    }
                }
            ]
        }
        
    except Exception as e:
        logger.error(f"Google AI API error: {str(e)}")
        raise e


# ==========================================================
# STREAMING HANDLERS
# ==========================================================

def openrouter_streaming_handler(response, history, message):
    """Handle streaming responses from OpenRouter"""
    try:
        updated_history = history + [{"role": "user", "content": message}]
        assistant_response = ""
        
        for line in response.iter_lines():
            if not line:
                continue
                
            line = line.decode('utf-8')
            if not line.startswith('data: '):
                continue
                
            data = line[6:]
            if data.strip() == '[DONE]':
                break
                
            try:
                chunk = json.loads(data)
                if "choices" in chunk and len(chunk["choices"]) > 0:
                    delta = chunk["choices"][0].get("delta", {})
                    if "content" in delta and delta["content"]:
                        # Update the current response
                        assistant_response += delta["content"]
                        yield updated_history + [{"role": "assistant", "content": assistant_response}]
            except json.JSONDecodeError:
                logger.error(f"Failed to parse JSON from chunk: {data}")
    except Exception as e:
        logger.error(f"Error in streaming handler: {str(e)}")
        # Add error message to the current response
        yield updated_history + [{"role": "assistant", "content": f"Error during streaming: {str(e)}"}]

def openai_streaming_handler(response, history, message):
    """Handle streaming responses from OpenAI"""
    try:
        updated_history = history + [{"role": "user", "content": message}]
        assistant_response = ""
        
        for chunk in response:
            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                content = chunk.choices[0].delta.content
                assistant_response += content
                yield updated_history + [{"role": "assistant", "content": assistant_response}]
    except Exception as e:
        logger.error(f"Error in OpenAI streaming handler: {str(e)}")
        # Add error message to the current response
        yield updated_history + [{"role": "assistant", "content": f"Error during streaming: {str(e)}"}]

def groq_streaming_handler(response, history, message):
    """Handle streaming responses from Groq"""
    try:
        updated_history = history + [{"role": "user", "content": message}]
        assistant_response = ""
        
        for chunk in response:
            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                content = chunk.choices[0].delta.content
                assistant_response += content
                yield updated_history + [{"role": "assistant", "content": assistant_response}]
    except Exception as e:
        logger.error(f"Error in Groq streaming handler: {str(e)}")
        # Add error message to the current response
        yield updated_history + [{"role": "assistant", "content": f"Error during streaming: {str(e)}"}]

def together_streaming_handler(response, history, message):
    """Handle streaming responses from Together"""
    try:
        updated_history = history + [{"role": "user", "content": message}]
        assistant_response = ""
        
        for chunk in response:
            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                content = chunk.choices[0].delta.content
                assistant_response += content
                yield updated_history + [{"role": "assistant", "content": assistant_response}]
    except Exception as e:
        logger.error(f"Error in Together streaming handler: {str(e)}")
        # Add error message to the current response
        yield updated_history + [{"role": "assistant", "content": f"Error during streaming: {str(e)}"}]
        
# ==========================================================
# MAIN FUNCTION TO ASK AI
# ==========================================================

def ask_ai(message, history, provider, model_choice, temperature, max_tokens, top_p, 
           frequency_penalty, presence_penalty, repetition_penalty, top_k, min_p, 
           seed, top_a, stream_output, response_format, images, documents, 
           reasoning_effort, system_message, transforms, api_key_override=None):
    """Enhanced AI query function with support for multiple providers"""
    # Validate input
    if not message.strip() and not images and not documents:
        return history
    
    # Create messages from chat history for API requests
    messages = format_to_message_dict(history)
    
    # Add system message if provided
    if system_message and system_message.strip():
        # Remove any existing system message
        messages = [msg for msg in messages if msg.get("role") != "system"]
        # Add new system message at the beginning
        messages.insert(0, {"role": "system", "content": system_message.strip()})
    
    # Prepare message with images and documents if any
    content = prepare_message_with_media(message, images, documents)
    
    # Add current message to API messages
    messages.append({"role": "user", "content": content})
    
    # Common parameters for all providers
    common_params = {
        "temperature": temperature,
        "max_tokens": max_tokens,
        "top_p": top_p,
        "frequency_penalty": frequency_penalty,
        "presence_penalty": presence_penalty,
        "stream": stream_output
    }
    
    try:
        # Process based on provider
        if provider == "OpenRouter":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in OpenRouter"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build OpenRouter payload
            payload = {
                "model": model_id,
                "messages": messages,
                **common_params
            }
            
            # Add optional parameters if set
            if repetition_penalty != 1.0:
                payload["repetition_penalty"] = repetition_penalty
            
            if top_k > 0:
                payload["top_k"] = top_k
            
            if min_p > 0:
                payload["min_p"] = min_p
            
            if seed > 0:
                payload["seed"] = seed
            
            if top_a > 0:
                payload["top_a"] = top_a
            
            # Add response format if JSON is requested
            if response_format == "json_object":
                payload["response_format"] = {"type": "json_object"}
            
            # Add reasoning if selected
            if reasoning_effort != "none":
                payload["reasoning"] = {
                    "effort": reasoning_effort
                }
            
            # Add transforms if selected
            if transforms:
                payload["transforms"] = transforms
                
            # Call OpenRouter API
            logger.info(f"Sending request to OpenRouter model: {model_id}")
            
            response = call_openrouter_api(payload, api_key_override)
            
            # Handle streaming response
            if stream_output and response.status_code == 200:
                # Set up generator for streaming updates
                def streaming_generator():
                    updated_history = history + [{"role": "user", "content": message}]
                    assistant_response = ""
                    
                    for line in response.iter_lines():
                        if not line:
                            continue
                            
                        line = line.decode('utf-8')
                        if not line.startswith('data: '):
                            continue
                            
                        data = line[6:]
                        if data.strip() == '[DONE]':
                            break
                            
                        try:
                            chunk = json.loads(data)
                            if "choices" in chunk and len(chunk["choices"]) > 0:
                                delta = chunk["choices"][0].get("delta", {})
                                if "content" in delta and delta["content"]:
                                    # Update the current response
                                    assistant_response += delta["content"]
                                    # Return updated history with current response
                                    yield updated_history + [{"role": "assistant", "content": assistant_response}]
                        except json.JSONDecodeError:
                            logger.error(f"Failed to parse JSON from chunk: {data}")
                
                return streaming_generator()
            
            # Handle normal response
            elif response.status_code == 200:
                result = response.json()
                logger.info(f"Response content: {result}")
                
                # Extract AI response
                ai_response = extract_ai_response(result, provider)
                
                # Add response to history with proper format
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            
            # Handle error response
            else:
                error_message = f"Error: Status code {response.status_code}"
                try:
                    response_data = response.json()
                    error_message += f"\n\nDetails: {json.dumps(response_data, indent=2)}"
                except:
                    error_message += f"\n\nResponse: {response.text}"
                
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
        elif provider == "Poe":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Poe"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build Poe payload
            payload = {
                "model": model_id,
                "messages": messages
                # Poe doesn't support most parameters directly
            }
            
            # Call Poe API
            logger.info(f"Sending request to Poe model: {model_id}")
            
            try:
                response = call_poe_api(payload, api_key_override)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"Poe API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
        elif provider == "Anthropic":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Anthropic"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build Anthropic payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            # Call Anthropic API
            logger.info(f"Sending request to Anthropic model: {model_id}")
            
            try:
                response = call_anthropic_api(payload, api_key_override)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"Anthropic API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "OpenAI":
            # Process OpenAI similarly as above...
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in OpenAI"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build OpenAI payload
            payload = {
                "model": model_id,
                "messages": messages,
                **common_params
            }
            
            # Add response format if JSON is requested
            if response_format == "json_object":
                payload["response_format"] = {"type": "json_object"}
            
            # Call OpenAI API
            logger.info(f"Sending request to OpenAI model: {model_id}")
            
            try:
                response = call_openai_api(payload, api_key_override)
                
                # Handle streaming response
                if stream_output:
                    # Set up generator for streaming updates
                    def streaming_generator():
                        updated_history = history + [{"role": "user", "content": message}]
                        assistant_response = ""
                        
                        for chunk in response:
                            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                                content = chunk.choices[0].delta.content
                                assistant_response += content
                                yield updated_history + [{"role": "assistant", "content": assistant_response}]
                    
                    return streaming_generator()
                
                # Handle normal response
                else:
                    ai_response = extract_ai_response(response, provider)
                    return history + [
                        {"role": "user", "content": message},
                        {"role": "assistant", "content": ai_response}
                    ]
            except Exception as e:
                error_message = f"OpenAI API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "HuggingFace":
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in HuggingFace"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build HuggingFace payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            # Call HuggingFace API
            logger.info(f"Sending request to HuggingFace model: {model_id}")
            
            try:
                response = call_huggingface_api(payload, api_key_override)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"HuggingFace API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "Groq":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Groq"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build Groq payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens,
                "top_p": top_p,
                "stream": stream_output
            }
            
            # Call Groq API
            logger.info(f"Sending request to Groq model: {model_id}")
            
            try:
                response = call_groq_api(payload, api_key_override)
                
                # Handle streaming response
                if stream_output:
                    # Add message to history
                    updated_history = history + [{"role": "user", "content": message}]
                    
                    # Set up generator for streaming updates
                    def streaming_generator():
                        assistant_response = ""
                        for chunk in response:
                            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                                content = chunk.choices[0].delta.content
                                assistant_response += content
                                yield updated_history + [{"role": "assistant", "content": assistant_response}]
                    
                    return streaming_generator()
                
                # Handle normal response
                else:
                    ai_response = extract_ai_response(response, provider)
                    return history + [
                        {"role": "user", "content": message},
                        {"role": "assistant", "content": ai_response}
                    ]
            except Exception as e:
                error_message = f"Groq API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "Cohere":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Cohere"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
            # Build Cohere payload (doesn't support streaming the same way)
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            # Call Cohere API
            logger.info(f"Sending request to Cohere model: {model_id}")
            
            try:
                response = call_cohere_api(payload, api_key_override)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"Cohere API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "Together":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Together"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
            # Build Together payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens,
                "stream": stream_output
            }
            
            # Call Together API
            logger.info(f"Sending request to Together model: {model_id}")
            
            try:
                response = call_together_api(payload, api_key_override)
                
                # Handle streaming response
                if stream_output:
                    # Add message to history
                    updated_history = history + [{"role": "user", "content": message}]
                    
                    # Set up generator for streaming updates
                    def streaming_generator():
                        assistant_response = ""
                        for chunk in response:
                            if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
                                content = chunk.choices[0].delta.content
                                assistant_response += content
                                yield updated_history + [{"role": "assistant", "content": assistant_response}]
                    
                    return streaming_generator()
                
                # Handle normal response
                else:
                    ai_response = extract_ai_response(response, provider)
                    return history + [
                        {"role": "user", "content": message},
                        {"role": "assistant", "content": ai_response}
                    ]
            except Exception as e:
                error_message = f"Together API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "OVH":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in OVH"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
            # Build OVH payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            # Call OVH API
            logger.info(f"Sending request to OVH model: {model_id}")
            
            try:
                response = call_ovh_api(payload)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"OVH API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "Cerebras":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in Cerebras"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
            # Build Cerebras payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            }
            
            # Call Cerebras API
            logger.info(f"Sending request to Cerebras model: {model_id}")
            
            try:
                response = call_cerebras_api(payload)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"Cerebras API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
                
        elif provider == "GoogleAI":
            # Get model ID from registry
            model_id, _ = get_model_info(provider, model_choice)
            if not model_id:
                error_message = f"Error: Model '{model_choice}' not found in GoogleAI"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
            # Build GoogleAI payload
            payload = {
                "model": model_id,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens,
                "top_p": top_p
            }
            
            # Call GoogleAI API
            logger.info(f"Sending request to GoogleAI model: {model_id}")
            
            try:
                response = call_googleai_api(payload, api_key_override)
                
                # Extract response
                ai_response = extract_ai_response(response, provider)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": ai_response}
                ]
            except Exception as e:
                error_message = f"GoogleAI API Error: {str(e)}"
                logger.error(error_message)
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
        else:
            error_message = f"Error: Unsupported provider '{provider}'"
            return history + [
                {"role": "user", "content": message},
                {"role": "assistant", "content": error_message}
            ]
            
    except Exception as e:
        error_message = f"Error: {str(e)}"
        logger.error(f"Exception during API call: {error_message}")
        return history + [
            {"role": "user", "content": message},
            {"role": "assistant", "content": error_message}
        ]

def clear_chat():
    """Reset all inputs"""
    return [], "", [], [], 0.7, 1000, 0.8, 0.0, 0.0, 1.0, 40, 0.1, 0, 0.0, False, "default", "none", "", []

# ==========================================================
# UI CREATION
# ==========================================================

def create_app():
    """Create the CrispChat Gradio application"""
    with gr.Blocks(
        title="CrispChat",
        css="""
            .context-size { 
                font-size: 0.9em;
                color: #666;
                margin-left: 10px;
            }
            footer { display: none !important; }
            .model-selection-row {
                display: flex;
                align-items: center;
            }
            .parameter-grid {
                display: grid;
                grid-template-columns: 1fr 1fr;
                gap: 10px;
            }
            .vision-badge {
                background-color: #4CAF50;
                color: white;
                padding: 3px 6px;
                border-radius: 3px;
                font-size: 0.8em;
                margin-left: 5px;
            }
            .provider-selection {
                margin-bottom: 10px;
                padding: 10px;
                border-radius: 5px;
                background-color: #f5f5f5;
            }
        """
    ) as demo:
        gr.Markdown("""
        # 🤖 CrispChat
        
        Chat with AI models from multiple providers: OpenRouter, OpenAI, HuggingFace, Groq, Cohere, Together, Anthropic, and Google AI.
        """)
        
        with gr.Row():
            with gr.Column(scale=2):
                # Chatbot interface
                chatbot = gr.Chatbot(
                    height=500, 
                    show_copy_button=True, 
                    show_label=False,
                    avatar_images=(None, "https://upload.wikimedia.org/wikipedia/commons/0/04/ChatGPT_logo.svg"),
                    elem_id="chat-window",
                    type="messages"  # use the new format
                )
                
                with gr.Row():
                    message = gr.Textbox(
                        placeholder="Type your message here...",
                        label="Message",
                        lines=2,
                        elem_id="message-input",
                        scale=4
                    )
                
                with gr.Row():
                    with gr.Column(scale=3):
                        submit_btn = gr.Button("Send", variant="primary", elem_id="send-btn")
                    
                    with gr.Column(scale=1):
                        clear_btn = gr.Button("Clear Chat", variant="secondary")
                
                # Container for conditionally showing image upload
                with gr.Row(visible=True) as image_upload_container:
                    # Image upload
                    with gr.Accordion("Upload Images (for vision models)", open=False):
                        images = gr.File(
                            label="Uploaded Images",
                            file_types=["image"],
                            file_count="multiple"
                        )
                        
                        image_upload_btn = gr.UploadButton(
                            label="Upload Images",
                            file_types=["image"],
                            file_count="multiple"
                        )
                    
                    # Document upload
                    with gr.Accordion("Upload Documents (PDF, MD, TXT)", open=False):
                        documents = gr.File(
                            label="Uploaded Documents",
                            file_types=[".pdf", ".md", ".txt"], 
                            file_count="multiple"
                        )
            
            with gr.Column(scale=1):
                                
                with gr.Group(elem_classes="provider-selection"):
                    gr.Markdown("### Provider Selection")
                    
                    # Provider selection
                    provider_choice = gr.Radio(
                        choices=["OpenRouter", "OpenAI", "HuggingFace", "Groq", "Cohere", "Together", "Anthropic", "Poe", "GoogleAI"],
                        value="OpenRouter",
                        label="AI Provider"
                    )
                    
                    # API key input with separate fields for each provider
                    with gr.Accordion("API Keys", open=False):
                        gr.Markdown("Enter API keys directly or set them as environment variables")
                        
                        openrouter_api_key = gr.Textbox(
                            placeholder="Enter OpenRouter API key",
                            label="OpenRouter API Key",
                            type="password",
                            value=OPENROUTER_API_KEY if OPENROUTER_API_KEY else ""
                        )

                        poe_api_key = gr.Textbox(
                            placeholder="Enter Poe API key",
                            label="Poe API Key",
                            type="password",
                            value=POE_API_KEY if POE_API_KEY else ""
                        )
                        
                        openai_api_key = gr.Textbox(
                            placeholder="Enter OpenAI API key",
                            label="OpenAI API Key",
                            type="password",
                            value=OPENAI_API_KEY if OPENAI_API_KEY else ""
                        )
                        
                        hf_api_key = gr.Textbox(
                            placeholder="Enter HuggingFace API key",
                            label="HuggingFace API Key",
                            type="password",
                            value=HF_API_KEY if HF_API_KEY else ""
                        )
                        
                        groq_api_key = gr.Textbox(
                            placeholder="Enter Groq API key",
                            label="Groq API Key",
                            type="password",
                            value=GROQ_API_KEY if GROQ_API_KEY else ""
                        )
                        
                        cohere_api_key = gr.Textbox(
                            placeholder="Enter Cohere API key",
                            label="Cohere API Key",
                            type="password",
                            value=COHERE_API_KEY if COHERE_API_KEY else ""
                        )
                        
                        together_api_key = gr.Textbox(
                            placeholder="Enter Together API key",
                            label="Together API Key",
                            type="password",
                            value=TOGETHER_API_KEY if TOGETHER_API_KEY else ""
                        )
                        
                        # Add Anthropic API key
                        anthropic_api_key = gr.Textbox(
                            placeholder="Enter Anthropic API key",
                            label="Anthropic API Key",
                            type="password",
                            value=os.environ.get("ANTHROPIC_API_KEY", "")
                        )
                        
                        googleai_api_key = gr.Textbox(
                            placeholder="Enter Google AI API key",
                            label="Google AI API Key",
                            type="password",
                            value=GOOGLEAI_API_KEY if GOOGLEAI_API_KEY else ""
                        )
                
                with gr.Group():
                    gr.Markdown("### Model Selection")
                    
                    with gr.Row(elem_classes="model-selection-row"):
                        model_search = gr.Textbox(
                            placeholder="Search models...",
                            label="",
                            show_label=False
                        )
                    
                    # Provider-specific model dropdowns
                    openrouter_model = gr.Dropdown(
                        choices=[model[0] for model in OPENROUTER_ALL_MODELS],
                        value=OPENROUTER_ALL_MODELS[0][0] if OPENROUTER_ALL_MODELS else None,
                        label="OpenRouter Model",
                        elem_id="openrouter-model-choice",
                        visible=True
                    )

                    # Add Poe model dropdown
                    poe_model = gr.Dropdown(
                        choices=list(POE_MODELS.keys()),
                        value="chinchilla" if "chinchilla" in POE_MODELS else None,
                        label="Poe Model",
                        elem_id="poe-model-choice",
                        visible=False
                    )
                    
                    openai_model = gr.Dropdown(
                        choices=list(OPENAI_MODELS.keys()),
                        value="gpt-3.5-turbo" if "gpt-3.5-turbo" in OPENAI_MODELS else None,
                        label="OpenAI Model",
                        elem_id="openai-model-choice",
                        visible=False
                    )
                    
                    hf_model = gr.Dropdown(
                        choices=list(HUGGINGFACE_MODELS.keys()),
                        value="mistralai/Mistral-7B-Instruct-v0.3" if "mistralai/Mistral-7B-Instruct-v0.3" in HUGGINGFACE_MODELS else None,
                        label="HuggingFace Model",
                        elem_id="hf-model-choice",
                        visible=False
                    )
                    
                    groq_model = gr.Dropdown(
                        choices=list(GROQ_MODELS.keys()),
                        value="llama-3.1-8b-instant" if "llama-3.1-8b-instant" in GROQ_MODELS else None,
                        label="Groq Model",
                        elem_id="groq-model-choice",
                        visible=False
                    )
                    
                    cohere_model = gr.Dropdown(
                        choices=list(COHERE_MODELS.keys()),
                        value="command-r-plus" if "command-r-plus" in COHERE_MODELS else None,
                        label="Cohere Model",
                        elem_id="cohere-model-choice",
                        visible=False
                    )
                    
                    together_model = gr.Dropdown(
                        choices=list(TOGETHER_MODELS.keys()),
                        value="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo" if "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo" in TOGETHER_MODELS else None,
                        label="Together Model",
                        elem_id="together-model-choice",
                        visible=False
                    )
                    
                    # Add Anthropic model dropdown
                    anthropic_model = gr.Dropdown(
                        choices=list(ANTHROPIC_MODELS.keys()),
                        value="claude-3-5-sonnet-20241022" if "claude-3-5-sonnet-20241022" in ANTHROPIC_MODELS else None,
                        label="Anthropic Model",
                        elem_id="anthropic-model-choice",
                        visible=False
                    )
                    
                    googleai_model = gr.Dropdown(
                        choices=list(GOOGLEAI_MODELS.keys()),
                        value="gemini-1.5-pro" if "gemini-1.5-pro" in GOOGLEAI_MODELS else None,
                        label="Google AI Model",
                        elem_id="googleai-model-choice",
                        visible=False
                    )
                    
                    context_display = gr.Textbox(
                        value=update_context_display("OpenRouter", OPENROUTER_ALL_MODELS[0][0]),
                        label="Context Size",
                        interactive=False,
                        elem_classes="context-size"
                    )
                
                with gr.Accordion("Generation Parameters", open=False):
                    with gr.Group(elem_classes="parameter-grid"):
                        temperature = gr.Slider(
                            minimum=0.0, 
                            maximum=2.0, 
                            value=0.7, 
                            step=0.1,
                            label="Temperature"
                        )
                        
                        max_tokens = gr.Slider(
                            minimum=100, 
                            maximum=4000, 
                            value=1000, 
                            step=100,
                            label="Max Tokens"
                        )
                        
                        top_p = gr.Slider(
                            minimum=0.1, 
                            maximum=1.0, 
                            value=0.8, 
                            step=0.1,
                            label="Top P"
                        )
                        
                        frequency_penalty = gr.Slider(
                            minimum=-2.0, 
                            maximum=2.0, 
                            value=0.0, 
                            step=0.1,
                            label="Frequency Penalty"
                        )
                        
                        presence_penalty = gr.Slider(
                            minimum=-2.0, 
                            maximum=2.0, 
                            value=0.0, 
                            step=0.1,
                            label="Presence Penalty"
                        )
                        
                        reasoning_effort = gr.Radio(
                            ["none", "low", "medium", "high"],
                            value="none",
                            label="Reasoning Effort (OpenRouter)"
                        )
                
                with gr.Accordion("Advanced Options", open=False):
                    with gr.Row():
                        with gr.Column():
                            repetition_penalty = gr.Slider(
                                minimum=0.1, 
                                maximum=2.0, 
                                value=1.0, 
                                step=0.1,
                                label="Repetition Penalty"
                            )
                            
                            top_k = gr.Slider(
                                minimum=1, 
                                maximum=100, 
                                value=40, 
                                step=1,
                                label="Top K"
                            )
                            
                            min_p = gr.Slider(
                                minimum=0.0, 
                                maximum=1.0, 
                                value=0.1, 
                                step=0.05,
                                label="Min P"
                            )
                        
                        with gr.Column():
                            seed = gr.Number(
                                value=0,
                                label="Seed (0 for random)",
                                precision=0
                            )
                            
                            top_a = gr.Slider(
                                minimum=0.0, 
                                maximum=1.0, 
                                value=0.0, 
                                step=0.05,
                                label="Top A"
                            )
                            
                            stream_output = gr.Checkbox(
                                label="Stream Output",
                                value=False
                            )
                    
                    with gr.Row():
                        response_format = gr.Radio(
                            ["default", "json_object"],
                            value="default",
                            label="Response Format"
                        )
                        
                        gr.Markdown("""
                        * **json_object**: Forces the model to respond with valid JSON only.
                        * Only available on certain models - check model support.
                        """)
                
                # Custom instructing options
                with gr.Accordion("Custom Instructions", open=False):
                    system_message = gr.Textbox(
                        placeholder="Enter a system message to guide the model's behavior...",
                        label="System Message",
                        lines=3
                    )
                    
                    transforms = gr.CheckboxGroup(
                        ["prompt_optimize", "prompt_distill", "prompt_compress"],
                        label="Prompt Transforms (OpenRouter specific)"
                    )
                    
                    gr.Markdown("""
                    * **prompt_optimize**: Improve prompt for better responses.
                    * **prompt_distill**: Compress prompt to use fewer tokens without changing meaning.
                    * **prompt_compress**: Aggressively compress prompt to fit larger contexts.
                    """)
                
                # Add a model information section
                with gr.Accordion("About Selected Model", open=False):
                    model_info_display = gr.HTML(
                        value=update_model_info("OpenRouter", OPENROUTER_ALL_MODELS[0][0])
                    )
                    
                    is_vision_indicator = gr.Checkbox(
                        label="Supports Images",
                        value=is_vision_model("OpenRouter", OPENROUTER_ALL_MODELS[0][0]),
                        interactive=False
                    )
        
        # Add usage instructions
        with gr.Accordion("Usage Instructions", open=False):
            gr.Markdown("""
            ## Basic Usage
            1. Type your message in the input box
            2. Select a provider and model
            3. Click "Send" or press Enter
            
            ## Working with Files
            - **Images**: Upload images to use with vision-capable models
            - **Documents**: Upload PDF, Markdown, or text files to analyze their content
            
            ## Provider Information
            - **OpenRouter**: Free access to various models with context window sizes up to 2M tokens
            - **OpenAI**: Requires an API key, includes GPT-3.5 and GPT-4 models
            - **HuggingFace**: Direct access to open models, some models require API key
            - **Groq**: High-performance inference, requires API key
            - **Cohere**: Specialized in language understanding, requires API key
            - **Together**: Access to high-quality open models, requires API key
            - **Anthropic**: Claude models with strong reasoning capabilities, requires API key
            - **GoogleAI**: Google's Gemini models, requires API key
            
            ## Advanced Parameters
            - **Temperature**: Controls randomness (higher = more creative, lower = more deterministic)
            - **Max Tokens**: Maximum length of the response
            - **Top P**: Nucleus sampling threshold (higher = consider more tokens)
            - **Reasoning Effort**: Some models can show their reasoning process (OpenRouter only)
            """)
        
        # Add a footer with version info
        footer_md = gr.Markdown("""
        ---
        ### CrispChat v1.2
        Built with ❤️ using Gradio and multiple AI provider APIs | Context sizes shown next to model names
        """)
        
        # Define event handlers
        def toggle_model_dropdowns(provider):
            """Show/hide model dropdowns based on provider selection"""
            return {
                openrouter_model: gr.update(visible=(provider == "OpenRouter")),
                openai_model: gr.update(visible=(provider == "OpenAI")),
                hf_model: gr.update(visible=(provider == "HuggingFace")),
                groq_model: gr.update(visible=(provider == "Groq")),
                cohere_model: gr.update(visible=(provider == "Cohere")),
                together_model: gr.update(visible=(provider == "Together")),
                anthropic_model: gr.update(visible=(provider == "Anthropic")),
                poe_model: gr.update(visible=(provider == "Poe")),
                googleai_model: gr.update(visible=(provider == "GoogleAI"))
            }
            
        def update_context_for_provider(provider, openrouter_model, openai_model, hf_model, groq_model, cohere_model, together_model, anthropic_model, poe_model, googleai_model):
            """Update context display based on selected provider and model"""
            if provider == "OpenRouter":
                return update_context_display(provider, openrouter_model)
            elif provider == "OpenAI":
                return update_context_display(provider, openai_model)
            elif provider == "HuggingFace":
                return update_context_display(provider, hf_model)
            elif provider == "Groq":
                return update_context_display(provider, groq_model)
            elif provider == "Cohere":
                return update_context_display(provider, cohere_model)
            elif provider == "Together":
                return update_context_display(provider, together_model)
            elif provider == "Anthropic":
                return update_context_display(provider, anthropic_model)
            elif provider == "Poe":
                return update_context_display(provider, poe_model)
            elif provider == "GoogleAI":
                return update_context_display(provider, googleai_model)
            return "Unknown"
            
        def update_model_info_for_provider(provider, openrouter_model, openai_model, hf_model, groq_model, cohere_model, together_model, anthropic_model, poe_model, googleai_model):
            """Update model info based on selected provider and model"""
            if provider == "OpenRouter":
                return update_model_info(provider, openrouter_model)
            elif provider == "OpenAI":
                return update_model_info(provider, openai_model)
            elif provider == "HuggingFace":
                return update_model_info(provider, hf_model)
            elif provider == "Groq":
                return update_model_info(provider, groq_model)
            elif provider == "Cohere":
                return update_model_info(provider, cohere_model)
            elif provider == "Together":
                return update_model_info(provider, together_model)
            elif provider == "Anthropic":
                return update_model_info(provider, anthropic_model)
            elif provider == "Poe":
                return update_model_info(provider, poe_model)
            elif provider == "GoogleAI":
                return update_model_info(provider, googleai_model)
            return "<p>Model information not available</p>"
            
        def update_vision_indicator(provider, model_choice=None):
            """Update the vision capability indicator"""
            # Simplified - don't call get_current_model since it causes issues
            if model_choice is None:
                # Just check if the provider generally supports vision
                return provider in VISION_MODELS and len(VISION_MODELS[provider]) > 0
            
            return is_vision_model(provider, model_choice)
            
        def update_image_upload_visibility(provider, model_choice=None):
            """Show/hide image upload based on model vision capabilities"""
            # Simplified
            is_vision = update_vision_indicator(provider, model_choice)
            return gr.update(visible=is_vision)
        
        # Search model function
        def search_openrouter_models(search_term):
            """Filter OpenRouter models based on search term"""
            all_models = [model[0] for model in OPENROUTER_ALL_MODELS]
            if not search_term:
                return gr.update(choices=all_models, value=all_models[0] if all_models else None)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                return gr.update(choices=all_models, value=all_models[0] if all_models else None)
                
        def search_openai_models(search_term):
            """Filter OpenAI models based on search term"""
            all_models = list(OPENAI_MODELS.keys())
            if not search_term:
                return gr.update(choices=all_models, value="gpt-3.5-turbo" if "gpt-3.5-turbo" in all_models else all_models[0] if all_models else None)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                return gr.update(choices=all_models, value="gpt-3.5-turbo" if "gpt-3.5-turbo" in all_models else all_models[0] if all_models else None)
                
        def search_hf_models(search_term):
            """Filter HuggingFace models based on search term"""
            all_models = list(HUGGINGFACE_MODELS.keys())
            if not search_term:
                default_model = "mistralai/Mistral-7B-Instruct-v0.3" if "mistralai/Mistral-7B-Instruct-v0.3" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                default_model = "mistralai/Mistral-7B-Instruct-v0.3" if "mistralai/Mistral-7B-Instruct-v0.3" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
            
        def search_models_generic(search_term, model_dict, default_model=None):
            """Generic model search function to reduce code duplication"""
            all_models = list(model_dict.keys())
            if not all_models:
                return gr.update(choices=[], value=None)
                
            if not search_term:
                return gr.update(choices=all_models, value=default_model if default_model in all_models else all_models[0])
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                return gr.update(choices=all_models, value=default_model if default_model in all_models else all_models[0])

        def search_poe_models(search_term):
            """Filter Poe models based on search term"""
            return search_models_generic(search_term, POE_MODELS, "chinchilla")    
        
        def search_groq_models(search_term):
            """Filter Groq models based on search term"""
            all_models = list(GROQ_MODELS.keys())
            if not search_term:
                default_model = "llama-3.1-8b-instant" if "llama-3.1-8b-instant" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                default_model = "llama-3.1-8b-instant" if "llama-3.1-8b-instant" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
        def search_cohere_models(search_term):
            """Filter Cohere models based on search term"""
            all_models = list(COHERE_MODELS.keys())
            if not search_term:
                default_model = "command-r-plus" if "command-r-plus" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                default_model = "command-r-plus" if "command-r-plus" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
        def search_together_models(search_term):
            """Filter Together models based on search term"""
            all_models = list(TOGETHER_MODELS.keys())
            if not search_term:
                default_model = "meta-llama/Llama-3.1-8B-Instruct" if "meta-llama/Llama-3.1-8B-Instruct" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                default_model = "meta-llama/Llama-3.1-8B-Instruct" if "meta-llama/Llama-3.1-8B-Instruct" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
        def search_anthropic_models(search_term):
            """Filter Anthropic models based on search term"""
            all_models = list(ANTHROPIC_MODELS.keys())
            if not search_term:
                return gr.update(choices=all_models, value="claude-3-5-sonnet-20241022" if "claude-3-5-sonnet-20241022" in all_models else all_models[0] if all_models else None)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                return gr.update(choices=all_models, value="claude-3-5-sonnet-20241022" if "claude-3-5-sonnet-20241022" in all_models else all_models[0] if all_models else None)
                
        def search_googleai_models(search_term):
            """Filter GoogleAI models based on search term"""
            all_models = list(GOOGLEAI_MODELS.keys())
            if not search_term:
                default_model = "gemini-1.5-pro" if "gemini-1.5-pro" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)
                
            filtered_models = [model for model in all_models if search_term.lower() in model.lower()]
            
            if filtered_models:
                return gr.update(choices=filtered_models, value=filtered_models[0])
            else:
                default_model = "gemini-1.5-pro" if "gemini-1.5-pro" in all_models else all_models[0] if all_models else None
                return gr.update(choices=all_models, value=default_model)

            
        def get_current_model(provider, openrouter_model, openai_model, hf_model, groq_model, cohere_model, 
                            together_model, anthropic_model, poe_model, googleai_model):
            """Get the currently selected model based on provider"""
            if provider == "OpenRouter":
                return openrouter_model
            elif provider == "OpenAI":
                return openai_model
            elif provider == "HuggingFace":
                return hf_model
            elif provider == "Groq":
                return groq_model
            elif provider == "Cohere":
                return cohere_model
            elif provider == "Together":
                return together_model
            elif provider == "Anthropic":
                return anthropic_model
            elif provider == "Poe":
                return poe_model
            elif provider == "GoogleAI":
                return googleai_model
            return None
        
        # Process uploaded images
        image_upload_btn.upload(
            fn=lambda files: files,
            inputs=image_upload_btn,
            outputs=images
        )
        
        # Set up provider selection event
        provider_choice.change(
            fn=toggle_model_dropdowns,
            inputs=provider_choice,
            outputs=[
                openrouter_model,
                openai_model,
                hf_model,
                groq_model,
                cohere_model,
                together_model,
                anthropic_model,
                poe_model,
                googleai_model
            ]
        ).then(
            fn=update_context_for_provider,
            inputs=[
                provider_choice, 
                openrouter_model, 
                openai_model, 
                hf_model, 
                groq_model, 
                cohere_model, 
                together_model, 
                anthropic_model, 
                poe_model, 
                googleai_model
            ],
            outputs=context_display
        ).then(
            fn=update_model_info_for_provider,
            inputs=[
                provider_choice, 
                openrouter_model, 
                openai_model, 
                hf_model, 
                groq_model, 
                cohere_model, 
                together_model, 
                anthropic_model, 
                poe_model, 
                googleai_model
            ],
            outputs=model_info_display
        ).then(
            # Fix this with correct number of args using a simpler approach
            fn=lambda provider: update_vision_indicator(provider, None),
            inputs=provider_choice,
            outputs=is_vision_indicator
        ).then(
            # Same here
            fn=lambda provider: update_image_upload_visibility(provider, None),
            inputs=provider_choice,
            outputs=image_upload_container
        )
        
        # Set up model search event - return model dropdown updates
        model_search.change(
            fn=lambda provider, search: [
                search_openrouter_models(search) if provider == "OpenRouter" else gr.update(),
                search_openai_models(search) if provider == "OpenAI" else gr.update(),
                search_hf_models(search) if provider == "HuggingFace" else gr.update(),
                search_groq_models(search) if provider == "Groq" else gr.update(),
                search_cohere_models(search) if provider == "Cohere" else gr.update(),
                search_together_models(search) if provider == "Together" else gr.update(),
                search_anthropic_models(search) if provider == "Anthropic" else gr.update(),
                search_poe_models(search) if provider == "Poe" else gr.update(),
                search_googleai_models(search) if provider == "GoogleAI" else gr.update()
            ],
            inputs=[provider_choice, model_search],
            outputs=[
                openrouter_model, openai_model, hf_model, groq_model, 
                cohere_model, together_model, anthropic_model, poe_model, googleai_model
            ]
        )
        
        # Set up model change events to update context display and model info
        openrouter_model.change(
            fn=lambda model: update_context_display("OpenRouter", model),
            inputs=openrouter_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("OpenRouter", model),
            inputs=openrouter_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("OpenRouter", model),
            inputs=openrouter_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("OpenRouter", model),
            inputs=openrouter_model,
            outputs=image_upload_container
        )

        # Event handler for Poe model change
        poe_model.change(
            fn=lambda model: update_context_display("Poe", model),
            inputs=poe_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("Poe", model),
            inputs=poe_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("Poe", model),
            inputs=poe_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("Poe", model),
            inputs=poe_model,
            outputs=image_upload_container
        )
        
        openai_model.change(
            fn=lambda model: update_context_display("OpenAI", model),
            inputs=openai_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("OpenAI", model),
            inputs=openai_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("OpenAI", model),
            inputs=openai_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("OpenAI", model),
            inputs=openai_model,
            outputs=image_upload_container
        )
        
        hf_model.change(
            fn=lambda model: update_context_display("HuggingFace", model),
            inputs=hf_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("HuggingFace", model),
            inputs=hf_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("HuggingFace", model),
            inputs=hf_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("HuggingFace", model),
            inputs=hf_model,
            outputs=image_upload_container
        )
        
        groq_model.change(
            fn=lambda model: update_context_display("Groq", model),
            inputs=groq_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("Groq", model),
            inputs=groq_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("Groq", model),
            inputs=groq_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("Groq", model),
            inputs=groq_model,
            outputs=image_upload_container
        )
        
        cohere_model.change(
            fn=lambda model: update_context_display("Cohere", model),
            inputs=cohere_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("Cohere", model),
            inputs=cohere_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("Cohere", model),
            inputs=cohere_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("Cohere", model),
            inputs=cohere_model,
            outputs=image_upload_container
        )
        
        together_model.change(
            fn=lambda model: update_context_display("Together", model),
            inputs=together_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("Together", model),
            inputs=together_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("Together", model),
            inputs=together_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("Together", model),
            inputs=together_model,
            outputs=image_upload_container
        )
        
        anthropic_model.change(
            fn=lambda model: update_context_display("Anthropic", model),
            inputs=anthropic_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("Anthropic", model),
            inputs=anthropic_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("Anthropic", model),
            inputs=anthropic_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("Anthropic", model),
            inputs=anthropic_model,
            outputs=image_upload_container
        )
        
        googleai_model.change(
            fn=lambda model: update_context_display("GoogleAI", model),
            inputs=googleai_model,
            outputs=context_display
        ).then(
            fn=lambda model: update_model_info("GoogleAI", model),
            inputs=googleai_model,
            outputs=model_info_display
        ).then(
            fn=lambda model: update_vision_indicator("GoogleAI", model),
            inputs=googleai_model,
            outputs=is_vision_indicator
        ).then(
            fn=lambda model: update_image_upload_visibility("GoogleAI", model),
            inputs=googleai_model,
            outputs=image_upload_container
        )

        def handle_search(provider, search_term):
            """Handle search based on provider"""
            if provider == "OpenRouter":
                return search_openrouter_models(search_term)
            elif provider == "OpenAI":
                return search_openai_models(search_term)
            elif provider == "HuggingFace":
                return search_hf_models(search_term)
            elif provider == "Groq":
                return search_groq_models(search_term)
            elif provider == "Cohere":
                return search_cohere_models(search_term)
            elif provider == "Together":
                return search_together_models(search_term)
            elif provider == "Anthropic":
                return search_anthropic_models(search_term)
            elif provider == "GoogleAI":
                return search_googleai_models(search_term)
            return None
        
        # Set up submission event
        def submit_message(message, history, provider, 
                openrouter_model, openai_model, hf_model, groq_model, cohere_model, together_model, anthropic_model, poe_model, googleai_model, 
                temperature, max_tokens, top_p, frequency_penalty, presence_penalty, repetition_penalty, 
                top_k, min_p, seed, top_a, stream_output, response_format,
                images, documents, reasoning_effort, system_message, transforms, 
                openrouter_api_key, openai_api_key, hf_api_key, groq_api_key, cohere_api_key, together_api_key, anthropic_api_key, poe_api_key, googleai_api_key):

            """Submit message to selected provider and model"""
            # Get the currently selected model
            model_choice = get_current_model(provider, openrouter_model, openai_model, hf_model, groq_model, cohere_model, 
                                    together_model, anthropic_model, poe_model, googleai_model)
            
            # Check if model is selected
            if not model_choice:
                error_message = f"Error: No model selected for provider {provider}"
                return history + [
                    {"role": "user", "content": message},
                    {"role": "assistant", "content": error_message}
                ]
            
            # Select the appropriate API key based on the provider
            api_key_override = None
            if provider == "OpenRouter" and openrouter_api_key:
                api_key_override = openrouter_api_key
            elif provider == "OpenAI" and openai_api_key:
                api_key_override = openai_api_key
            elif provider == "HuggingFace" and hf_api_key:
                api_key_override = hf_api_key
            elif provider == "Groq" and groq_api_key:
                api_key_override = groq_api_key
            elif provider == "Cohere" and cohere_api_key:
                api_key_override = cohere_api_key
            elif provider == "Together" and together_api_key:
                api_key_override = together_api_key
            elif provider == "Anthropic" and anthropic_api_key:
                api_key_override = anthropic_api_key
            elif provider == "Poe" and poe_api_key:
                api_key_override = poe_api_key
            elif provider == "GoogleAI" and googleai_api_key:
                api_key_override = googleai_api_key
            
            # Call the ask_ai function with the appropriate parameters
            return ask_ai(
                message=message,
                history=history,
                provider=provider,
                model_choice=model_choice,
                temperature=temperature,
                max_tokens=max_tokens,
                top_p=top_p,
                frequency_penalty=frequency_penalty,
                presence_penalty=presence_penalty,
                repetition_penalty=repetition_penalty,
                top_k=top_k,
                min_p=min_p,
                seed=seed,
                top_a=top_a,
                stream_output=stream_output,
                response_format=response_format,
                images=images,
                documents=documents,
                reasoning_effort=reasoning_effort,
                system_message=system_message,
                transforms=transforms,
                api_key_override=api_key_override
            )

        def clean_message(message):
            """Clean the message from style tags"""
            if isinstance(message, str):
                import re
                # Remove style tags
                message = re.sub(r'<userStyle>.*?</userStyle>', '', message)
            return message
        
        # Submit button click event
        submit_btn.click(
            fn=lambda *args: submit_message(clean_message(args[0]), *args[1:]),
            inputs=[
                message, chatbot, provider_choice, 
                openrouter_model, openai_model, hf_model, groq_model, cohere_model, 
                together_model, anthropic_model, poe_model, googleai_model,
                temperature, max_tokens, top_p, frequency_penalty, presence_penalty, repetition_penalty, 
                top_k, min_p, seed, top_a, stream_output, response_format,
                images, documents, reasoning_effort, system_message, transforms,
                openrouter_api_key, openai_api_key, hf_api_key, groq_api_key, cohere_api_key, 
                together_api_key, anthropic_api_key, poe_api_key, googleai_api_key
            ],
            outputs=chatbot,
            show_progress="minimal",
        ).then(
            fn=lambda: "",  # Clear message box after sending
            inputs=None,
            outputs=message
        )
        
        # Also submit on Enter key
        message.submit(
            fn=submit_message,
            inputs=[
                message, chatbot, provider_choice, 
                openrouter_model, openai_model, hf_model, groq_model, cohere_model, 
                together_model, anthropic_model, poe_model, googleai_model,
                temperature, max_tokens, top_p, frequency_penalty, presence_penalty, repetition_penalty, 
                top_k, min_p, seed, top_a, stream_output, response_format,
                images, documents, reasoning_effort, system_message, transforms,
                openrouter_api_key, openai_api_key, hf_api_key, groq_api_key, cohere_api_key, 
                together_api_key, anthropic_api_key, poe_api_key, googleai_api_key
            ],
            outputs=chatbot,
            show_progress="minimal",
        ).then(
            fn=lambda: "",  # Clear message box after sending
            inputs=None,
            outputs=message
        )
        
        # Clear chat button
        clear_btn.click(
            fn=clear_chat,
            inputs=[],
            outputs=[
                chatbot, message, images, documents, temperature, 
                max_tokens, top_p, frequency_penalty, presence_penalty,
                repetition_penalty, top_k, min_p, seed, top_a, stream_output,
                response_format, reasoning_effort, system_message, transforms
            ]
        )
        
        return demo

# Launch the app
if __name__ == "__main__":
    # Check API keys and print status
    missing_keys = []
    
    if not OPENROUTER_API_KEY:
        logger.warning("WARNING: OPENROUTER_API_KEY environment variable is not set")
        missing_keys.append("OpenRouter")

    # Add Poe
    if not POE_API_KEY:
        logger.warning("WARNING: POE_API_KEY environment variable is not set")
        missing_keys.append("Poe")

    if not ANTHROPIC_API_KEY:
        logger.warning("WARNING: ANTHROPIC_API_KEY environment variable is not set")
        missing_keys.append("Anthropic")
    
    if not OPENAI_API_KEY:
        logger.warning("WARNING: OPENAI_API_KEY environment variable is not set")
        missing_keys.append("OpenAI")
        
    if not GROQ_API_KEY:
        logger.warning("WARNING: GROQ_API_KEY environment variable is not set")
        missing_keys.append("Groq")
        
    if not COHERE_API_KEY:
        logger.warning("WARNING: COHERE_API_KEY environment variable is not set")
        missing_keys.append("Cohere")
        
    if not TOGETHER_API_KEY:
        logger.warning("WARNING: TOGETHER_API_KEY environment variable is not set")
        missing_keys.append("Together")
        
    if not GOOGLEAI_API_KEY:
        logger.warning("WARNING: GOOGLEAI_API_KEY environment variable is not set")
        missing_keys.append("GoogleAI")
        
    if missing_keys:
        print("Missing API keys for the following providers:")
        for key in missing_keys:
            print(f"- {key}")
        print("\nYou can still use the application, but some providers will require API keys.")
        print("You can provide API keys through environment variables or use the API Key Override field.")
        
        if "OpenRouter" in missing_keys:
            print("\nNote: OpenRouter offers free tier access to many models!")
        
        #if "OVH" not in missing_keys and "Cerebras" not in missing_keys:
        #    print("\nNote: OVH AI Endpoints (beta) and Cerebras offer free usage tiers!")
            
    print("\nStarting CrispChat application...")
    demo = create_app()
    demo.launch(
        server_name="0.0.0.0", 
        server_port=7860, 
        debug=True,
        show_error=True
    )