File size: 111,636 Bytes
94e74f0
59aaeae
94e74f0
59aaeae
94e74f0
59aaeae
94e74f0
59aaeae
aabc02c
 
94e74f0
 
e65ba1a
aabc02c
94e74f0
 
aabc02c
 
94e74f0
 
 
 
836388f
94e74f0
 
836388f
94e74f0
 
836388f
94e74f0
59aaeae
94e74f0
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
e65ba1a
94e74f0
e65ba1a
 
 
 
 
 
94e74f0
e65ba1a
c04ffe5
 
94e74f0
 
 
c04ffe5
94e74f0
c04ffe5
94e74f0
59aaeae
94e74f0
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
836388f
 
94e74f0
 
 
 
 
 
 
 
836388f
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836388f
 
94e74f0
 
 
 
 
836388f
94e74f0
836388f
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
836388f
94e74f0
 
 
 
 
 
 
 
 
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836388f
 
94e74f0
 
59aaeae
94e74f0
59aaeae
94e74f0
 
 
 
 
 
 
 
59aaeae
94e74f0
59aaeae
94e74f0
 
59aaeae
94e74f0
 
836388f
94e74f0
59aaeae
94e74f0
 
 
aabc02c
 
 
94e74f0
aabc02c
deb9332
94e74f0
 
 
59aaeae
94e74f0
 
 
deb9332
 
94e74f0
 
 
 
 
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
59aaeae
 
 
94e74f0
 
59aaeae
 
94e74f0
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
59aaeae
 
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
59aaeae
94e74f0
 
59aaeae
94e74f0
 
59aaeae
94e74f0
 
 
 
59aaeae
94e74f0
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
59aaeae
94e74f0
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
59aaeae
94e74f0
 
59aaeae
 
94e74f0
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
75ead00
94e74f0
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836388f
59aaeae
94e74f0
 
 
 
 
 
 
c04ffe5
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
 
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deb9332
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
94e74f0
 
59aaeae
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
94e74f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Standard library imports
import os
import sys
import time
import random
import json
import base64
import logging
from enum import Enum
from pathlib import Path
from functools import lru_cache
from typing import Optional, Dict, Any, List, Union, Tuple

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

# Third-party imports
from pydantic import BaseModel

# Try to import pycountry, provide fallback if not available
try:
    import pycountry
    PYCOUNTRY_AVAILABLE = True
except ImportError:
    PYCOUNTRY_AVAILABLE = False
    logger.warning("pycountry module not available - using language code fallback")

# Try to import Mistral AI, provide fallback if not available
try:
    from mistralai import Mistral
    from mistralai import DocumentURLChunk, ImageURLChunk, TextChunk
    from mistralai.models import OCRImageObject
    MISTRAL_AVAILABLE = True
except ImportError:
    MISTRAL_AVAILABLE = False
    logger.warning("mistralai module not available - OCR functionality will be limited")

# Import our language detection module
try:
    from utils.helpers.language_detection import LanguageDetector
    LANG_DETECTOR_AVAILABLE = True
except ImportError:
    LANG_DETECTOR_AVAILABLE = False
    logger.warning("language_detection module not available - using fallback language detection")

# Import utilities for OCR processing
try:
    from utils.image_utils import replace_images_in_markdown, get_combined_markdown
except ImportError:
    # Define minimal fallback functions if module not found
    logger.warning("Could not import utils.image_utils - using minimal fallback functions")
    
    def replace_images_in_markdown(markdown_str, images_dict):
        """Minimal fallback implementation of replace_images_in_markdown"""
        import re
        for img_id, base64_str in images_dict.items():
            # Match alt text OR link part, ignore extension
            base_id = img_id.split('.')[0]
            pattern = re.compile(rf"!\[[^\]]*{base_id}[^\]]*\]\([^\)]+\)")
            markdown_str = pattern.sub(f"![{img_id}](data:image/jpeg;base64,{base64_str})", markdown_str)
        return markdown_str
        
    def get_combined_markdown(ocr_response):
        """Minimal fallback implementation of get_combined_markdown"""
        markdowns = []
        for page in ocr_response.pages:
            image_data = {}
            if hasattr(page, "images"):
                for img in page.images:
                    if hasattr(img, "id") and hasattr(img, "image_base64"):
                        image_data[img.id] = img.image_base64
            page_markdown = page.markdown if hasattr(page, "markdown") else ""
            processed_markdown = replace_images_in_markdown(page_markdown, image_data)
            markdowns.append(processed_markdown)
        return "\n\n".join(markdowns)

# Import config directly (now local to historical-ocr)
try:
    from config import MISTRAL_API_KEY, OCR_MODEL, TEXT_MODEL, VISION_MODEL, TEST_MODE, IMAGE_PREPROCESSING
except ImportError:
    # Fallback defaults if config is not available
    import os
    MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY", "")
    OCR_MODEL = "mistral-ocr-latest"
    TEXT_MODEL = "mistral-large-latest"
    VISION_MODEL = "mistral-large-latest"
    TEST_MODE = True
    # Default image preprocessing settings if config not available
    IMAGE_PREPROCESSING = {
        "max_size_mb": 8.0,
        # Add basic defaults for preprocessing
        "enhance_contrast": 1.2,
        "denoise": True,
        "compression_quality": 95
    }
    logging.warning("Config module not found. Using environment variables and defaults.")

# Helper function to make OCR objects JSON serializable
# Removed caching to fix unhashable type error
def serialize_ocr_response(obj):
    """
    Convert OCR response objects to JSON serializable format
    Optimized for speed and memory usage
    """
    # Fast path: Handle primitive types directly
    if obj is None or isinstance(obj, (str, int, float, bool)):
        return obj
        
    # Handle collections with optimized recursion
    if isinstance(obj, list):
        return [serialize_ocr_response(item) for item in obj]
    elif isinstance(obj, dict):
        return {k: serialize_ocr_response(v) for k, v in obj.items()}
    elif hasattr(obj, '__dict__'):
        # For OCR objects with __dict__ attribute
        result = {}
        for key, value in obj.__dict__.items():
            if key.startswith('_'):
                continue  # Skip private attributes
                
            # Fast path for OCRImageObject - most common complex object
            if isinstance(value, OCRImageObject):
                # Get image base64 data for validation
                image_base64 = value.image_base64 if hasattr(value, 'image_base64') else None
                
                # COMPLETELY REWRITTEN validation logic using proven test approach
                # Default to FALSE (treating as text) unless proven to be an image
                is_valid_image = False
                
                # Quick exit conditions
                if not image_base64 or not isinstance(image_base64, str):
                    # No data or not a string - not a valid image
                    is_valid_image = False
                    logging.warning("Invalid image data (not a string)")
                    
                # Case 1: Definite image with proper data URL prefix
                elif image_base64.startswith('data:image/'):
                    is_valid_image = True
                    logging.debug("Valid image with data:image/ prefix")
                    
                # Case 2: Markdown image reference, not an actual image
                elif image_base64.startswith('![') and '](' in image_base64 and image_base64.endswith(')'):
                    is_valid_image = False
                    logging.warning("Markdown image reference detected")
                    
                    # Extract the image ID for logging
                    try:
                        img_id = image_base64.split('![')[1].split('](')[0]
                        logging.debug(f"Markdown reference for image: {img_id}")
                    except:
                        img_id = "unknown"
                    
                # Case 3: Needs detailed text content detection
                else:
                    # Use the same proven approach as in our tests
                    # Take a sample for efficiency
                    sample = image_base64[:min(len(image_base64), 1000)]
                    sample_lower = sample.lower()
                    
                    # Check for obvious text features using multiple indicators
                    has_spaces = ' ' in sample
                    has_newlines = '\n' in sample
                    has_punctuation = any(p in sample for p in ',.;:!?"\'()[]{}')
                    
                    # Check for sentence-like structures
                    has_sentences = False
                    for i in range(len(sample) - 5):
                        if sample[i] in '.!?\n' and i+2 < len(sample) and sample[i+1] == ' ' and sample[i+2].isupper():
                            has_sentences = True
                            break
                    
                    # Check for common words with word boundary protection
                    common_words = ['the', 'and', 'of', 'to', 'a', 'in', 'is', 'that', 'this', 'for']
                    has_common_words = any(f" {word} " in f" {sample_lower} " for word in common_words)
                    
                    # Count the text indicators
                    text_indicators = [has_spaces, has_newlines, has_punctuation, has_sentences, has_common_words]
                    text_indicator_count = sum(1 for indicator in text_indicators if indicator)
                    
                    # Log detailed findings for debugging
                    logging.debug(f"Text detection - spaces: {has_spaces}, newlines: {has_newlines}, " +
                               f"punctuation: {has_punctuation}, sentences: {has_sentences}, " +
                               f"common words: {has_common_words}")
                    logging.debug(f"Text indicators found: {text_indicator_count}/5")
                    
                    # CRITICAL FIX: If we detect 2 or more text indicators, this is TEXT not an image!
                    if text_indicator_count >= 2:
                        is_valid_image = False
                        logging.warning(f"Content identified as TEXT with {text_indicator_count}/5 indicators")
                    # Only if we have no clear text indicators AND valid base64 chars, treat as image
                    elif all(c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' 
                            for c in image_base64[:100]):
                        is_valid_image = True
                        logging.debug("Valid base64 data with no text indicators")
                    else:
                        # Default to TEXT for anything else - safer approach
                        is_valid_image = False
                        logging.warning("No clear image patterns detected - treating as text by default")
                
                # Final validation result with definitive message
                logging.warning(f"FINAL CLASSIFICATION: OCRImageObject content type = {'IMAGE' if is_valid_image else 'TEXT'}")
                
                # Process based on final validation result
                if is_valid_image:
                    # Process as image if validation passes
                    result[key] = {
                        'id': value.id if hasattr(value, 'id') else None,
                        'image_base64': image_base64
                    }
                else:
                    # Process as text if validation fails, but properly handle markdown references
                    if image_base64 and isinstance(image_base64, str):
                        # Special handling for markdown image references
                        if image_base64.startswith('![') and '](' in image_base64 and image_base64.endswith(')'):
                            # Extract the image description (alt text) if available
                            try:
                                # Parse the alt text from ![alt_text](url)
                                alt_text = image_base64.split('![')[1].split('](')[0]
                                # Use the alt text or a placeholder if it's just the image name
                                if alt_text and not alt_text.endswith('.jpeg') and not alt_text.endswith('.jpg'):
                                    result[key] = f"[Image: {alt_text}]"
                                else:
                                    # Just note that there's an image without the reference
                                    result[key] = "[Image]"
                                logging.info(f"Converted markdown reference to text placeholder: {result[key]}")
                            except:
                                # Fallback for parsing errors
                                result[key] = "[Image]"
                        else:
                            # Regular text content
                            result[key] = image_base64
                    else:
                        result[key] = str(value)
            # Handle collections
            elif isinstance(value, list):
                result[key] = [serialize_ocr_response(item) for item in value]
            # Handle nested objects
            elif hasattr(value, '__dict__'):
                result[key] = serialize_ocr_response(value)
            # Handle primitives and other types
            else:
                result[key] = value
        return result
    else:
        return obj

# Create language enum for structured output - cache language lookup to avoid repeated processing
@lru_cache(maxsize=1)
def get_language_dict():
    if PYCOUNTRY_AVAILABLE:
        return {lang.alpha_2: lang.name for lang in pycountry.languages if hasattr(lang, 'alpha_2')}
    else:
        # Fallback with basic languages when pycountry is not available
        return {
            "en": "English",
            "es": "Spanish",
            "fr": "French",
            "de": "German",
            "it": "Italian",
            "pt": "Portuguese",
            "ru": "Russian",
            "zh": "Chinese",
            "ja": "Japanese",
            "ar": "Arabic",
            "hi": "Hindi",
            "la": "Latin"
        }

class LanguageMeta(Enum.__class__):
    def __new__(metacls, cls, bases, classdict):
        languages = get_language_dict()
        for code, name in languages.items():
            classdict[name.upper().replace(' ', '_')] = name
        return super().__new__(metacls, cls, bases, classdict)

class Language(Enum, metaclass=LanguageMeta):
    pass

class StructuredOCRModel(BaseModel):
    file_name: str
    topics: list[str]
    languages: list[Language]
    ocr_contents: dict

class StructuredOCR:
    def __init__(self, api_key=None):
        """Initialize the OCR processor with API key"""
        # Set up logger for this class instance
        self.logger = logging.getLogger(__name__)
        
        # Check if we're running in test mode or if Mistral is not available
        self.test_mode = TEST_MODE or not MISTRAL_AVAILABLE
        # Initialize current filename for language detection
        self.current_filename = None
        
        if not MISTRAL_AVAILABLE:
            self.logger.warning("Mistral AI package not available - running in test mode")
            self.api_key = "placeholder_key"
            self.client = None
            return
        
        # Initialize API key - use provided key, or environment var
        if self.test_mode and not api_key:
            self.api_key = "placeholder_key"
        else:
            self.api_key = api_key or MISTRAL_API_KEY
        
        # Ensure we have a valid API key when not in test mode
        if not self.api_key and not self.test_mode:
            raise ValueError("No Mistral API key provided. Please set the MISTRAL_API_KEY environment variable or enable TEST_MODE.")
        
        # Clean the API key by removing any whitespace
        self.api_key = self.api_key.strip()
        
        # Check if API key exists but don't enforce length requirements
        if not self.test_mode and not self.api_key:
            self.logger.warning("Warning: No API key provided")
            
        # Initialize client with the API key
        try:
            self.client = Mistral(api_key=self.api_key)
            # Skip validation to avoid unnecessary API calls 
        except Exception as e:
            error_msg = str(e).lower()
            if "unauthorized" in error_msg or "401" in error_msg:
                raise ValueError(f"API key authentication failed. Please check your Mistral API key: {str(e)}")
            else:
                self.logger.warning(f"Failed to initialize Mistral client: {str(e)}")
                self.test_mode = True
                self.client = None
        
        # Initialize language detector
        if LANG_DETECTOR_AVAILABLE:
            self.logger.info("Using statistical language detection module")
            self.language_detector = LanguageDetector()
        else:
            self.logger.warning("External language detection not available - using internal fallback")
            self.language_detector = None
    
    def process_file(self, file_path, file_type=None, use_vision=True, max_pages=None, file_size_mb=None, custom_pages=None, custom_prompt=None):
        """Process a file and return structured OCR results
        
        Args:
            file_path: Path to the file to process
            file_type: 'pdf' or 'image' (will be auto-detected if None)
            use_vision: Whether to use vision model for improved analysis
            max_pages: Optional limit on number of pages to process
            file_size_mb: Optional file size in MB (used for automatic page limiting)
            custom_pages: Optional list of specific page numbers to process
            custom_prompt: Optional instructions for the AI to handle unusual document formatting or specific extraction needs
            
        Returns:
            Dictionary with structured OCR results
        """
        # Convert file_path to Path object if it's a string
        file_path = Path(file_path)
        
        # Store current filename for language detection
        self.current_filename = file_path.name
        
        # Auto-detect file type if not provided
        if file_type is None:
            suffix = file_path.suffix.lower()
            file_type = "pdf" if suffix == ".pdf" else "image"
            
        # Check for handwritten document by filename
        filename_lower = file_path.name.lower()
        if "handwritten" in filename_lower or "manuscript" in filename_lower or "letter" in filename_lower:
            logger.info(f"Detected likely handwritten document from filename: {file_path.name}")
            # This will be used during processing to apply handwritten-specific handling
        
        # Get file size if not provided
        if file_size_mb is None and file_path.exists():
            file_size_mb = file_path.stat().st_size / (1024 * 1024)  # Convert bytes to MB
            
        # Check if file exceeds API limits (50 MB)
        if file_size_mb and file_size_mb > 50:
            logging.warning(f"File size {file_size_mb:.2f} MB exceeds Mistral API limit of 50 MB")
            return {
                "file_name": file_path.name,
                "topics": ["Document"],
                "languages": ["English"],
                "confidence_score": 0.0,
                "error": f"File size {file_size_mb:.2f} MB exceeds API limit of 50 MB",
                "ocr_contents": {
                    "error": f"Failed to process file: File size {file_size_mb:.2f} MB exceeds Mistral API limit of 50 MB",
                    "partial_text": "Document could not be processed due to size limitations."
                }
            }
            
        # For PDF files, limit pages based on file size if no explicit limit is given
        if file_type == "pdf" and file_size_mb and max_pages is None and custom_pages is None:
            if file_size_mb > 100:  # Very large files
                max_pages = 3
            elif file_size_mb > 50:  # Large files
                max_pages = 5
            elif file_size_mb > 20:  # Medium files
                max_pages = 10
            else:  # Small files
                max_pages = None  # Process all pages
        
        # Start processing timer
        start_time = time.time()
        
        # Read and process the file
        if file_type == "pdf":
            result = self._process_pdf(file_path, use_vision, max_pages, custom_pages, custom_prompt)
        else:
            result = self._process_image(file_path, use_vision, custom_prompt)
            
        # Add processing time information
        processing_time = time.time() - start_time
        result['processing_time'] = processing_time
        
        # Add a default confidence score if not present
        if 'confidence_score' not in result:
            result['confidence_score'] = 0.85  # Default confidence
            
        # Ensure the entire result is fully JSON serializable by running it through our serializer
        try:
            # First convert to a standard dict if it's not already
            if not isinstance(result, dict):
                result = serialize_ocr_response(result)
                
            # Make a final pass to check for any remaining non-serializable objects
            # Proactively check for OCRImageObject instances to avoid serialization warnings
            def has_ocr_image_objects(obj):
                """Check if object contains any OCRImageObject instances recursively"""
                if isinstance(obj, dict):
                    return any(has_ocr_image_objects(v) for v in obj.values())
                elif isinstance(obj, list):
                    return any(has_ocr_image_objects(item) for item in obj)
                else:
                    return 'OCRImageObject' in str(type(obj))
            
            # Apply serialization preemptively if OCRImageObjects are detected
            if has_ocr_image_objects(result):
                # Quietly apply full serialization before any errors occur
                result = serialize_ocr_response(result)
            else:
                # Test JSON serialization to catch any other issues
                json.dumps(result)
        except TypeError as e:
            # If there's still a serialization error, run the whole result through our serializer
            logger = logging.getLogger("serializer")
            logger.warning(f"JSON serialization error in result: {str(e)}. Applying full serialization.")
            # Use a more robust approach to ensure complete serialization
            try:
                # First attempt with our custom serializer
                result = serialize_ocr_response(result)
                # Test if it's fully serializable now
                json.dumps(result)
            except Exception as inner_e:
                # If still not serializable, convert to a simpler format
                logger.warning(f"Secondary serialization error: {str(inner_e)}. Converting to basic format.")
                # Create a simplified result with just the essential information
                simplified_result = {
                    "file_name": result.get("file_name", "unknown"),
                    "topics": result.get("topics", ["Document"]),
                    "languages": [str(lang) for lang in result.get("languages", ["English"]) if lang is not None],
                    "ocr_contents": {
                        "raw_text": result.get("ocr_contents", {}).get("raw_text", "Text extraction failed due to serialization error")
                    },
                    "serialization_error": f"Original result could not be fully serialized: {str(e)}"
                }
                result = simplified_result
            
        return result
    
    def _process_pdf(self, file_path, use_vision=True, max_pages=None, custom_pages=None, custom_prompt=None):
        """
        Process a PDF file with OCR - optimized version with smart page handling and memory management
        
        Args:
            file_path: Path to the PDF file
            use_vision: Whether to use vision model for enhanced analysis
            max_pages: Optional limit on the number of pages to process
            custom_pages: Optional list of specific page numbers to process
            custom_prompt: Optional custom prompt for specialized extraction
        """
        logger = logging.getLogger("pdf_processor")
        logger.info(f"Processing PDF: {file_path}")
        
        # Track processing time
        start_time = time.time()
        
        # Fast path: Return placeholder if in test mode
        if self.test_mode:
            logger.info("Test mode active, returning placeholder response")
            # Enhanced test mode placeholder that's more realistic
            return {
                "file_name": file_path.name,
                "topics": ["Historical Document", "Literature", "American History"],
                "languages": ["English"],
                "ocr_contents": {
                    "title": "Harper's New Monthly Magazine",
                    "publication_date": "1855",
                    "publisher": "Harper & Brothers, New York",
                    "raw_text": "This is a test mode placeholder for Harper's New Monthly Magazine from 1855. The actual document contains articles on literature, politics, science, and culture from mid-19th century America.",
                    "content": "The magazine includes various literary pieces, poetry, political commentary, and illustrations typical of 19th century periodicals. Known for publishing works by prominent American authors including Herman Melville and Charles Dickens.",
                    "key_figures": ["Herman Melville", "Charles Dickens", "Henry Wadsworth Longfellow"],
                    "noted_articles": ["Continued serialization of popular novels", "Commentary on contemporary political events", "Scientific discoveries and technological advancements"]
                },
                "pdf_processing_method": "enhanced_test_mode",
                "total_pages": 12,
                "processed_pages": 3,
                "processing_time": 0.5,
                "confidence_score": 0.9
            }
        
        try:
            # PDF processing strategy decision based on file size
            file_size_mb = file_path.stat().st_size / (1024 * 1024)
            logger.info(f"PDF size: {file_size_mb:.2f} MB")
            
            # Always use pdf2image for better control and consistency across all PDF files
            use_pdf2image = True
            
            # First try local PDF processing for better performance and control
            if use_pdf2image:
                try:
                    import tempfile
                    from pdf2image import convert_from_path
                    
                    logger.info("Processing PDF using pdf2image for better multi-page handling")
                    
                    # Convert PDF to images with optimized parameters
                    conversion_start = time.time()
                    
                    # Use consistent DPI for all files to ensure reliable results
                    dpi = 200  # Higher quality DPI for all files to ensure better text recognition
                        
                    # Only convert first page initially to check document type
                    pdf_first_page = convert_from_path(file_path, dpi=dpi, first_page=1, last_page=1)
                    logger.info(f"First page converted in {time.time() - conversion_start:.2f}s")
                    
                    # Quick check if PDF has readable content
                    if not pdf_first_page:
                        logger.warning("PDF conversion produced no images, falling back to API")
                        raise Exception("PDF conversion failed to produce images")
                    
                    # Determine total pages in the document
                    # First, try simple estimate from first page conversion
                    total_pages = 1
                    
                    # Try pdf2image info extraction
                    try:
                        # Try with pdf2image page counting - use simpler parameters
                        logger.info("Determining PDF page count...")
                        count_start = time.time()
                        
                        # Use a lightweight approach with multi-threading for faster processing
                        pdf_info = convert_from_path(
                            file_path, 
                            dpi=72,  # Low DPI just for info
                            first_page=1, 
                            last_page=1,
                            size=(100, 100),  # Tiny image to save memory
                            fmt="jpeg", 
                            thread_count=4,  # Increased thread count for faster processing
                            output_file=None
                        )
                        
                        # Extract page count
                        if hasattr(pdf_info, 'n_pages'):
                            total_pages = pdf_info.n_pages
                        elif isinstance(pdf_info, dict) and "Pages" in pdf_info:
                            total_pages = int(pdf_info.get("Pages", "1"))
                        elif len(pdf_first_page) > 0:
                            # Just estimate based on first page - at least we have one
                            total_pages = 1
                            
                        logger.info(f"Page count determined in {time.time() - count_start:.2f}s")
                    except Exception as count_error:
                        logger.warning(f"Error determining page count: {str(count_error)}. Using default of 1")
                        total_pages = 1
                            
                    logger.info(f"PDF has {total_pages} total pages")
                    
                    # Determine which pages to process
                    pages_to_process = []
                    
                    # Handle custom page selection if provided
                    if custom_pages and any(0 < p <= total_pages for p in custom_pages):
                        # Filter valid page numbers
                        pages_to_process = [p for p in custom_pages if 0 < p <= total_pages]
                        logger.info(f"Processing {len(pages_to_process)} custom-selected pages: {pages_to_process}")
                    # Otherwise use max_pages limit if provided
                    elif max_pages and max_pages < total_pages:
                        pages_to_process = list(range(1, max_pages + 1))
                        logger.info(f"Processing first {max_pages} pages of {total_pages} total")
                    # Or process all pages if reasonable count
                    elif total_pages <= 10:
                        pages_to_process = list(range(1, total_pages + 1))
                        logger.info(f"Processing all {total_pages} pages")
                    # For large documents without limits, process subset of pages
                    else:
                        # Smart sampling: first page, last page, and some pages in between
                        pages_to_process = [1]  # Always include first page
                        
                        if total_pages > 1:
                            if total_pages <= 5:
                                # For few pages, process all
                                pages_to_process = list(range(1, total_pages + 1))
                            else:
                                # For many pages, sample intelligently
                                # Add pages from the middle of the document
                                middle = total_pages // 2
                                # Add last page if more than 3 pages
                                if total_pages > 3:
                                    pages_to_process.append(total_pages)
                                # Add up to 3 pages from middle if document is large
                                if total_pages > 5:
                                    pages_to_process.append(middle)
                                if total_pages > 10:
                                    pages_to_process.append(middle // 2)
                                    pages_to_process.append(middle + (middle // 2))
                                    
                        # Sort pages for sequential processing
                        pages_to_process = sorted(list(set(pages_to_process)))
                        logger.info(f"Processing {len(pages_to_process)} sampled pages out of {total_pages} total: {pages_to_process}")
                    
                    # Convert only the selected pages to minimize memory usage
                    selected_images = []
                    combined_text = []
                    detected_languages = set()  # Track detected languages across all pages
                    
                    # Process pages in larger batches for better efficiency
                    batch_size = 5  # Process 5 pages at a time for better throughput
                    for i in range(0, len(pages_to_process), batch_size):
                        batch_pages = pages_to_process[i:i+batch_size]
                        logger.info(f"Converting batch of pages {batch_pages}")
                        
                        # Convert batch of pages with multi-threading for better performance
                        batch_start = time.time()
                        batch_images = convert_from_path(
                            file_path, 
                            dpi=dpi,
                            first_page=min(batch_pages),
                            last_page=max(batch_pages),
                            thread_count=4,  # Use multi-threading for faster PDF processing
                            fmt="jpeg"       # Use JPEG format for better compatibility
                        )
                        logger.info(f"Batch conversion completed in {time.time() - batch_start:.2f}s")
                        
                        # Map converted images to requested page numbers
                        for idx, page_num in enumerate(range(min(batch_pages), max(batch_pages) + 1)):
                            if page_num in pages_to_process and idx < len(batch_images):
                                if page_num == pages_to_process[0]:  # First page to process
                                    selected_images.append(batch_images[idx])
                                
                                # Process each page individually
                                with tempfile.NamedTemporaryFile(suffix='.jpeg', delete=False) as tmp:
                                    batch_images[idx].save(tmp.name, format='JPEG')
                                    # Simple OCR to extract text
                                    try:
                                        page_result = self._process_image(Path(tmp.name), False, None)
                                        if 'ocr_contents' in page_result and 'raw_text' in page_result['ocr_contents']:
                                            # Add page text to combined text without obvious page markers
                                            page_text = page_result['ocr_contents']['raw_text']
                                            combined_text.append(f"{page_text}")
                                            
                                            # Collect detected languages from each page
                                            if 'languages' in page_result:
                                                for lang in page_result['languages']:
                                                    detected_languages.add(lang)
                                    except Exception as page_e:
                                        logger.warning(f"Error processing page {page_num}: {str(page_e)}")
                                    # Clean up temp file
                                    import os
                                    os.unlink(tmp.name)
                    
                    # If we have processed pages
                    if selected_images and combined_text:
                        # Save first image to temp file for vision model
                        with tempfile.NamedTemporaryFile(suffix='.jpeg', delete=False) as tmp:
                            selected_images[0].save(tmp.name, format='JPEG', quality=95)
                            first_image_path = tmp.name
                        
                        # Combine all extracted text
                        all_text = "\n\n".join(combined_text)
                        
                        # For custom prompts, use specialized processing
                        if custom_prompt:
                            try:
                                # Process image with vision model
                                result = self._process_image(Path(first_image_path), use_vision, None)
                                
                                # Enhance with text analysis using combined text from all pages
                                enhanced_result = self._extract_structured_data_text_only(all_text, file_path.name, custom_prompt)
                                
                                # Merge results, keeping images from original result
                                for key, value in enhanced_result.items():
                                    if key not in ('raw_response_data', 'pages_data', 'has_images'):
                                        result[key] = value
                                        
                                # Update raw text with full document text
                                if 'ocr_contents' in result:
                                    result['ocr_contents']['raw_text'] = all_text
                                    
                                # Add flag to indicate custom prompt was applied
                                result['custom_prompt_applied'] = 'text_only'
                                
                                # Simplified approach - no document type detection
                                    
                            except Exception as e:
                                logger.warning(f"Custom prompt processing failed: {str(e)}. Using standard processing.")
                                # Fall back to standard processing
                                result = self._process_image(Path(first_image_path), use_vision, None)
                                if 'ocr_contents' in result:
                                    result['ocr_contents']['raw_text'] = all_text
                        else:
                            # Standard processing with combined text
                            result = self._process_image(Path(first_image_path), use_vision, None)
                            if 'ocr_contents' in result:
                                result['ocr_contents']['raw_text'] = all_text
                        
                        # Merge detected languages if available
                        if detected_languages:
                            result['languages'] = list(detected_languages)
                            
                        # Add PDF metadata
                        result['file_name'] = file_path.name
                        result['pdf_processing_method'] = 'pdf2image_optimized'
                        result['total_pages'] = total_pages
                        result['processed_pages'] = len(pages_to_process)
                        result['pages_processed'] = pages_to_process
                        
                        # Add processing info
                        result['processing_info'] = {
                            'method': 'local_pdf_processing',
                            'dpi': dpi,
                            'pages_sampled': pages_to_process,
                            'processing_time': time.time() - start_time
                        }
                        
                        # Clean up
                        os.unlink(first_image_path)
                        
                        return result
                    else:
                        logger.warning("No pages successfully processed with pdf2image, falling back to API")
                        raise Exception("Failed to process PDF pages locally")
                        
                except Exception as pdf2image_error:
                    logger.warning(f"Local PDF processing failed, falling back to API: {str(pdf2image_error)}")
                    # Fall back to API processing
            
            # API-based PDF processing
            logger.info("Processing PDF via Mistral API")
            
            # Optimize file upload for faster processing
            logger.info("Uploading PDF file to Mistral API")
            upload_start = time.time()
            
            # Set appropriate timeout based on file size
            upload_timeout = max(60, min(300, int(file_size_mb * 5)))  # 60s to 300s based on size
            
            try:
                # Upload the file (Mistral client doesn't support timeout parameter for upload)
                uploaded_file = self.client.files.upload(
                    file={
                        "file_name": file_path.stem,
                        "content": file_path.read_bytes(),
                    },
                    purpose="ocr"
                )
                
                logger.info(f"PDF uploaded in {time.time() - upload_start:.2f}s")
                
                # Get a signed URL for the uploaded file
                signed_url = self.client.files.get_signed_url(file_id=uploaded_file.id, expiry=1)
                
                # Process the PDF with OCR - use adaptive timeout based on file size
                logger.info(f"Processing PDF with OCR using {OCR_MODEL}")
                
                # Adaptive retry strategy based on file size
                max_retries = 3 if file_size_mb < 20 else 2  # Fewer retries for large files
                base_retry_delay = 1 if file_size_mb < 10 else 2  # Longer delays for large files
                
                # Adaptive timeout based on file size
                ocr_timeout_ms = min(180000, max(60000, int(file_size_mb * 3000)))  # 60s to 180s
                
                # Try processing with retries
                for retry in range(max_retries):
                    try:
                        ocr_start = time.time()
                        pdf_response = self.client.ocr.process(
                            document=DocumentURLChunk(document_url=signed_url.url), 
                            model=OCR_MODEL, 
                            include_image_base64=True,
                            timeout_ms=ocr_timeout_ms
                        )
                        logger.info(f"PDF OCR processing completed in {time.time() - ocr_start:.2f}s")
                        break  # Success, exit retry loop
                    except Exception as e:
                        error_msg = str(e)
                        logger.warning(f"API error on attempt {retry+1}/{max_retries}: {error_msg}")
                        
                        # Handle errors with optimized retry logic
                        error_lower = error_msg.lower()
                        
                        # Authentication errors - no point in retrying
                        if any(term in error_lower for term in ["unauthorized", "401", "403", "authentication"]):
                            logger.error("API authentication failed. Check your API key.")
                            raise ValueError(f"Authentication failed. Please verify your Mistral API key: {error_msg}")
                        
                        # Connection or server errors - worth retrying
                        elif any(term in error_lower for term in ["connection", "timeout", "520", "server error", "502", "503", "504"]):
                            if retry < max_retries - 1:
                                # Exponential backoff with jitter for better retry behavior
                                wait_time = base_retry_delay * (2 ** retry) * (0.8 + 0.4 * random.random())
                                logger.info(f"Connection issue detected. Waiting {wait_time:.1f}s before retry...")
                                time.sleep(wait_time)
                            else:
                                # Last retry failed
                                logger.error("Maximum retries reached, API connection error persists.")
                                raise ValueError(f"Could not connect to Mistral API after {max_retries} attempts: {error_msg}")
                        
                        # Rate limit errors - much longer wait
                        elif any(term in error_lower for term in ["rate limit", "429", "too many requests", "requests rate limit exceeded"]):
                            # Check specifically for token exhaustion vs temporary rate limit
                            if "quota" in error_lower or "credit" in error_lower or "subscription" in error_lower:
                                logger.error("API quota or credit limit reached. No retry will help.")
                                raise ValueError(f"Mistral API quota or credit limit reached. Please check your subscription: {error_msg}")
                            elif retry < max_retries - 1:
                                wait_time = base_retry_delay * (2 ** retry) * 6.0  # Significantly longer wait for rate limits
                                logger.info(f"Rate limit exceeded. Waiting {wait_time:.1f}s before retry...")
                                time.sleep(wait_time)
                            else:
                                logger.error("Maximum retries reached, rate limit error persists.")
                                raise ValueError(f"API rate limit exceeded. Please try again later: {error_msg}")
                        
                        # Misc errors - typically no retry will help
                        else:
                            if retry < max_retries - 1 and any(term in error_lower for term in ["transient", "temporary"]):
                                # Only retry for errors explicitly marked as transient
                                wait_time = base_retry_delay * (2 ** retry)
                                logger.info(f"Transient error detected. Waiting {wait_time:.1f}s before retry...")
                                time.sleep(wait_time)
                            else:
                                logger.error(f"Unrecoverable API error: {error_msg}")
                                raise
                
                # Calculate the number of pages to process
                pages_to_process = pdf_response.pages
                total_pages = len(pdf_response.pages)
                limited_pages = False
                
                logger.info(f"API returned {total_pages} total PDF pages")
                
                # Smart page selection logic for better performance
                if custom_pages:
                    # Convert to 0-based indexing and filter valid page numbers
                    valid_indices = [i-1 for i in custom_pages if 0 < i <= total_pages]
                    if valid_indices:
                        pages_to_process = [pdf_response.pages[i] for i in valid_indices]
                        limited_pages = True
                        logger.info(f"Processing {len(valid_indices)} custom-selected pages")
                # Max pages limit with smart sampling
                elif max_pages and total_pages > max_pages:
                    if max_pages == 1:
                        # Just first page
                        pages_to_process = pages_to_process[:1]
                    elif max_pages < 5 and total_pages > 10:
                        # For small max_pages on large docs, include first, last, and middle
                        indices = [0]  # First page
                        if max_pages > 1:
                            indices.append(total_pages - 1)  # Last page
                        if max_pages > 2:
                            indices.append(total_pages // 2)  # Middle page
                        # Add more pages up to max_pages if needed
                        if max_pages > 3:
                            remaining = max_pages - len(indices)
                            step = total_pages // (remaining + 1)
                            for i in range(1, remaining + 1):
                                idx = i * step
                                if idx not in indices and 0 <= idx < total_pages:
                                    indices.append(idx)
                        indices.sort()
                        pages_to_process = [pdf_response.pages[i] for i in indices]
                    else:
                        # Default: first max_pages
                        pages_to_process = pages_to_process[:max_pages]
                    
                    limited_pages = True
                    logger.info(f"Processing {len(pages_to_process)} pages out of {total_pages} total")
                
                # Directly extract any language information from the OCR response
                detected_languages = set()
                
                # Check if the response has a 'languages' attribute in any form
                # First check direct attributes on the response object
                if hasattr(pdf_response, 'languages') and pdf_response.languages:
                    for lang in pdf_response.languages:
                        detected_languages.add(str(lang))
                        logger.info(f"Found language in OCR response: {lang}")
                
                # Then check if it's in the response as a dictionary format
                elif hasattr(pdf_response, '__dict__'):
                    response_dict = pdf_response.__dict__
                    if 'languages' in response_dict and response_dict['languages']:
                        for lang in response_dict['languages']:
                            detected_languages.add(str(lang))
                            logger.info(f"Found language in OCR response dict: {lang}")
                
                # Calculate confidence score if available
                try:
                    confidence_values = [page.confidence for page in pages_to_process if hasattr(page, 'confidence')]
                    confidence_score = sum(confidence_values) / len(confidence_values) if confidence_values else 0.89
                except Exception:
                    confidence_score = 0.89  # Improved default
                
                # Merge page content intelligently - include page numbers for better context
                all_markdown = []
                for idx, page in enumerate(pages_to_process):
                    # Try to determine actual page number
                    if custom_pages and len(custom_pages) == len(pages_to_process):
                        page_num = custom_pages[idx]
                    else:
                        # Estimate page number - may not be accurate with sampling
                        page_num = idx + 1
                        
                    page_markdown = page.markdown if hasattr(page, 'markdown') else ""
                    # Add page content without obvious page markers
                    if page_markdown.strip():
                        all_markdown.append(f"{page_markdown}")
                    
                    # Collect language information from individual pages if available
                    if hasattr(page, 'languages') and page.languages:
                        for lang in page.languages:
                            detected_languages.add(str(lang))
                            logger.info(f"Found language in page {page_num}: {lang}")
                    
                # Join all pages with separation
                combined_markdown = "\n\n".join(all_markdown)
                
                # Extract structured data with the appropriate model
                if use_vision:
                    # Try to get a good image for vision model
                    vision_image = None
                    
                    # Try first page with images
                    for page in pages_to_process:
                        if hasattr(page, 'images') and page.images:
                            vision_image = page.images[0].image_base64
                            break
                    
                    if vision_image:
                        # Use vision model with enhanced prompt
                        logger.info(f"Using vision model: {VISION_MODEL}")
                        result = self._extract_structured_data_with_vision(
                            vision_image, combined_markdown, file_path.name, custom_prompt
                        )
                    else:
                        # Fall back to text-only if no images available
                        logger.info(f"No images in PDF, falling back to text model: {TEXT_MODEL}")
                        result = self._extract_structured_data_text_only(
                            combined_markdown, file_path.name, custom_prompt
                        )
                else:
                    # Use text-only model as requested
                    logger.info(f"Using text-only model as specified: {TEXT_MODEL}")
                    result = self._extract_structured_data_text_only(
                        combined_markdown, file_path.name, custom_prompt
                    )
                
                # If we have detected languages directly from the OCR model, use them
                if detected_languages:
                    logger.info(f"Using languages detected by OCR model: {', '.join(detected_languages)}")
                    result['languages'] = list(detected_languages)
                    # Add flag to indicate source of language detection
                    result['language_detection_source'] = 'mistral-ocr-latest'
                
                # Add metadata about pages
                if limited_pages:
                    result['limited_pages'] = {
                        'processed': len(pages_to_process),
                        'total': total_pages
                    }
                    
                # Set confidence score from OCR
                result['confidence_score'] = confidence_score
                
                # Add processing method info
                result['pdf_processing_method'] = 'api'
                result['total_pages'] = total_pages
                result['processed_pages'] = len(pages_to_process)
                
                # Store serialized OCR response for rendering
                serialized_response = serialize_ocr_response(pdf_response)
                result['raw_response_data'] = serialized_response
                
                # Check if there are images to include
                has_images = hasattr(pdf_response, 'pages') and any(
                    hasattr(page, 'images') and page.images for page in pdf_response.pages
                )
                result['has_images'] = has_images
                
                # Include image data for rendering if available
                if has_images:
                    # Prepare pages data with image references
                    result['pages_data'] = []
                    
                    # Get serialized pages - handle different formats
                    serialized_pages = None
                    try:
                        if hasattr(serialized_response, 'pages'):
                            serialized_pages = serialized_response.pages
                        elif isinstance(serialized_response, dict) and 'pages' in serialized_response:
                            serialized_pages = serialized_response.get('pages', [])
                        else:
                            # No pages found in response
                            logger.warning("No pages found in OCR response")
                            serialized_pages = []
                    except Exception as pages_err:
                        logger.warning(f"Error extracting pages from OCR response: {str(pages_err)}")
                        serialized_pages = []
                    
                    # Process each page to extract images
                    for page_idx, page in enumerate(serialized_pages):
                        try:
                            # Skip processing pages not in our selection
                            if limited_pages and page_idx >= len(pages_to_process):
                                continue
                                
                            # Extract page data with careful error handling
                            markdown = ""
                            images = []
                            
                            # Handle different page formats safely
                            if isinstance(page, dict):
                                markdown = page.get('markdown', '')
                                images = page.get('images', [])
                            else:
                                # Try attribute access
                                if hasattr(page, 'markdown'):
                                    markdown = page.markdown
                                if hasattr(page, 'images'):
                                    images = page.images
                            
                            # Create page data record
                            page_data = {
                                'page_number': page_idx + 1,
                                'markdown': markdown,
                                'images': []
                            }
                            
                            # Process images with careful error handling
                            for img_idx, img in enumerate(images):
                                try:
                                    # Extract image ID and base64 data
                                    img_id = None
                                    img_base64 = None
                                    
                                    if isinstance(img, dict):
                                        img_id = img.get('id')
                                        img_base64 = img.get('image_base64')
                                    else:
                                        # Try attribute access
                                        if hasattr(img, 'id'):
                                            img_id = img.id
                                        if hasattr(img, 'image_base64'):
                                            img_base64 = img.image_base64
                                    
                                    # Only add if we have valid image data
                                    if img_base64 and isinstance(img_base64, str):
                                        # Ensure ID exists
                                        safe_id = img_id if img_id else f"img_{page_idx}_{img_idx}"
                                        page_data['images'].append({
                                            'id': safe_id,
                                            'image_base64': img_base64
                                        })
                                except Exception as img_err:
                                    logger.warning(f"Error processing image {img_idx} on page {page_idx+1}: {str(img_err)}")
                                    continue  # Skip this image
                            
                            # Add page data if it has content
                            if page_data['markdown'] or page_data['images']:
                                result['pages_data'].append(page_data)
                                
                        except Exception as page_err:
                            logger.warning(f"Error processing page {page_idx+1}: {str(page_err)}")
                            continue  # Skip this page
                
                # Record final processing time
                total_time = time.time() - start_time
                result['processing_time'] = total_time
                logger.info(f"PDF API processing completed in {total_time:.2f}s")
                
                return result
                
            except Exception as api_e:
                logger.error(f"Error in API-based PDF processing: {str(api_e)}")
                # Re-raise to be caught by outer exception handler
                raise
                
        except Exception as e:
            # Log the error and return a helpful error result
            logger.error(f"Error processing PDF: {str(e)}")
            
            # Return basic result on error
            return {
                "file_name": file_path.name,
                "topics": ["Document"],
                "languages": ["English"],
                "confidence_score": 0.0,
                "error": str(e),
                "ocr_contents": {
                    "error": f"Failed to process PDF: {str(e)}",
                    "partial_text": "Document could not be fully processed."
                },
                "processing_time": time.time() - start_time
            }
    
    def _process_image(self, file_path, use_vision=True, custom_prompt=None):
        """Process an image file with OCR"""
        logger = logging.getLogger("image_processor")
        logger.info(f"Processing image: {file_path}")
        
        # Check if we're in test mode
        if self.test_mode:
            # Return a placeholder document response
            return {
                "file_name": file_path.name,
                "topics": ["Document"],
                "languages": ["English"],
                "ocr_contents": {
                    "title": "Document",
                    "content": "Please set up API key to process documents."
                },
                "processing_time": 0.5,
                "confidence_score": 0.0
            }
        
        # No automatic document type detection - rely on the document type specified in the custom prompt
        # The document type is passed from the UI through the custom prompt in ocr_processing.py
        
        try:
            # Check file size
            file_size_mb = file_path.stat().st_size / (1024 * 1024)
            logger.info(f"Original image size: {file_size_mb:.2f} MB")
            
            # Use enhanced preprocessing functions from ocr_utils
            try:
                from preprocessing import preprocess_image
                from utils.file_utils import get_base64_from_bytes
                
                logger.info(f"Applying image preprocessing for OCR")
                
                # Get preprocessing settings from config
                max_size_mb = IMAGE_PREPROCESSING.get("max_size_mb", 8.0)
                
                if file_size_mb > max_size_mb:
                    logger.info(f"Image is large ({file_size_mb:.2f} MB), optimizing for API submission")
                
                # Use standard preprocessing - document type will be handled by preprocessing.py
                # based on the options passed from the UI
                base64_data_url = get_base64_from_bytes(
                    preprocess_image(file_path.read_bytes(),
                                   {"document_type": "standard",
                                    "grayscale": True,
                                    "denoise": True,
                                    "contrast": 0})
                )
                
                logger.info(f"Image preprocessing completed successfully")
                
            except (ImportError, AttributeError) as e:
                # Fallback to basic processing if advanced functions not available
                logger.warning(f"Advanced preprocessing not available: {str(e)}. Using basic image processing.")
                
                # If image is larger than 8MB, resize it to reduce API payload size
                if file_size_mb > 8:
                    logger.info("Image is large, resizing before API submission")
                    try:
                        from PIL import Image
                        import io
                        
                        # Open and process the image
                        with Image.open(file_path) as img:
                            # Convert to RGB if not already (prevents mode errors)
                            if img.mode != 'RGB':
                                img = img.convert('RGB')
                            
                            # Calculate new dimensions (maintain aspect ratio)
                            # Target around 2000-2500 pixels on longest side for better OCR quality
                            width, height = img.size
                            max_dimension = max(width, height)
                            target_dimension = 2000  # Restored to 2000 for better image quality
                            
                            if max_dimension > target_dimension:
                                scale_factor = target_dimension / max_dimension
                                resized_width = int(width * scale_factor)
                                resized_height = int(height * scale_factor)
                                # Use LANCZOS instead of BILINEAR for better quality
                                img = img.resize((resized_width, resized_height), Image.LANCZOS)
                            
                            # Enhance contrast for better text recognition
                            from PIL import ImageEnhance
                            enhancer = ImageEnhance.Contrast(img)
                            img = enhancer.enhance(1.3)
                            
                            # Save to bytes with compression
                            buffer = io.BytesIO()
                            img.save(buffer, format="JPEG", quality=92, optimize=True)  # Higher quality for better OCR
                            buffer.seek(0)
                            
                            # Get the base64
                            encoded_image = base64.b64encode(buffer.getvalue()).decode()
                            base64_data_url = f"data:image/jpeg;base64,{encoded_image}"
                            
                            # Log the new size
                            new_size_mb = len(buffer.getvalue()) / (1024 * 1024)
                            logger.info(f"Resized image to {new_size_mb:.2f} MB")
                    except ImportError:
                        logger.warning("PIL not available for resizing. Using original image.")
                        # Use enhanced encoder with proper MIME type detection
                        from utils.image_utils import encode_image_for_api
                        base64_data_url = encode_image_for_api(file_path)
                    except Exception as e:
                        logger.warning(f"Image resize failed: {str(e)}. Using original image.")
                        # Use enhanced encoder with proper MIME type detection
                        from utils.image_utils import encode_image_for_api
                        base64_data_url = encode_image_for_api(file_path)
                else:
                    # For smaller images, use as-is with proper MIME type
                    from utils.image_utils import encode_image_for_api
                    base64_data_url = encode_image_for_api(file_path)
            except Exception as e:
                # Fallback to original image if any preprocessing fails
                logger.warning(f"Image preprocessing failed: {str(e)}. Using original image.")
                # Use enhanced encoder with proper MIME type detection
                from utils.image_utils import encode_image_for_api
                base64_data_url = encode_image_for_api(file_path)
            
            # Process the image with OCR
            logger.info(f"Processing image with OCR using {OCR_MODEL}")
            
            # Add retry logic with more retries and longer backoff periods for rate limit issues
            max_retries = 2  # Reduced to prevent rate limiting
            retry_delay = 1  # Shorter delay between retries
            
            for retry in range(max_retries):
                try:
                    image_response = self.client.ocr.process(
                        document=ImageURLChunk(image_url=base64_data_url), 
                        model=OCR_MODEL,
                        include_image_base64=True,
                        timeout_ms=45000  # 45 second timeout for better performance
                    )
                    break  # Success, exit retry loop
                except Exception as e:
                    error_msg = str(e)
                    logger.warning(f"API error on attempt {retry+1}/{max_retries}: {error_msg}")
                    
                    # Check specific error types to handle them appropriately
                    error_lower = error_msg.lower()
                    
                    # Authentication errors - no point in retrying
                    if "unauthorized" in error_lower or "401" in error_lower:
                        logger.error("API authentication failed. Check your API key.")
                        raise ValueError(f"Authentication failed with API key. Please verify your Mistral API key is correct and active: {error_msg}")
                    
                    # Connection errors - worth retrying
                    elif "connection" in error_lower or "timeout" in error_lower or "520" in error_msg or "server error" in error_lower:
                        if retry < max_retries - 1:
                            # Wait with shorter delay before retrying
                            wait_time = retry_delay * (2 ** retry)
                            logger.info(f"Connection issue detected. Waiting {wait_time}s before retry...")
                            time.sleep(wait_time)
                        else:
                            # Last retry failed
                            logger.error("Maximum retries reached, API connection error persists.")
                            raise ValueError(f"Could not connect to Mistral API after {max_retries} attempts: {error_msg}")
                    
                    # Rate limit errors
                    elif "rate limit" in error_lower or "429" in error_lower or "requests rate limit exceeded" in error_lower:
                        # Check specifically for token exhaustion vs temporary rate limit
                        if "quota" in error_lower or "credit" in error_lower or "subscription" in error_lower:
                            logger.error("API quota or credit limit reached. No retry will help.")
                            raise ValueError(f"Mistral API quota or credit limit reached. Please check your subscription: {error_msg}")
                        elif retry < max_retries - 1:
                            # More aggressive backoff for rate limits
                            wait_time = retry_delay * (2 ** retry) * 5  # 5x longer wait for rate limits
                            logger.info(f"Rate limit exceeded. Waiting {wait_time}s before retry...")
                            time.sleep(wait_time)
                        else:
                            # Last retry failed, try local OCR as fallback
                            logger.error("Maximum retries reached, rate limit error persists.")
                            try:
                                # Try to import the local OCR fallback function
                                from utils.image_utils import try_local_ocr_fallback
                                
                                # Attempt local OCR fallback
                                ocr_text = try_local_ocr_fallback(file_path, base64_data_url)
                                
                                if ocr_text:
                                    logger.info("Successfully used local OCR fallback")
                                    # Return a basic result with the local OCR text
                                    return {
                                        "file_name": file_path.name,
                                        "topics": ["Document"],
                                        "languages": ["English"],
                                        "ocr_contents": {
                                            "title": "Document (Local OCR)",
                                            "content": "This document was processed with local OCR due to API rate limiting.",
                                            "raw_text": ocr_text
                                        },
                                        "processing_method": "local_fallback",
                                        "processing_note": "Used local OCR due to API rate limit"
                                    }
                            except (ImportError, Exception) as local_err:
                                logger.warning(f"Local OCR fallback failed: {str(local_err)}")
                            
                            # If we get here, both API and local OCR failed
                            raise ValueError(f"Mistral API rate limit exceeded. Please try again later: {error_msg}")
                    
                    # Other errors - no retry
                    else:
                        logger.error(f"Unrecoverable API error: {error_msg}")
                        raise
            
            # Get the OCR markdown from the first page
            image_ocr_markdown = image_response.pages[0].markdown if image_response.pages else ""
            
            # Check if the OCR response has images
            has_images = hasattr(image_response, 'pages') and image_response.pages and hasattr(image_response.pages[0], 'images') and image_response.pages[0].images
            
            # Check for language information directly from the OCR model
            detected_languages = set()
            
            # Check if the response has a 'languages' attribute in any form
            # First check direct attributes on the response object
            if hasattr(image_response, 'languages') and image_response.languages:
                for lang in image_response.languages:
                    detected_languages.add(str(lang))
                    logger.info(f"Found language in OCR response: {lang}")
            
            # Then check if it's in the response as a dictionary format
            elif hasattr(image_response, '__dict__'):
                response_dict = image_response.__dict__
                if 'languages' in response_dict and response_dict['languages']:
                    for lang in response_dict['languages']:
                        detected_languages.add(str(lang))
                        logger.info(f"Found language in OCR response dict: {lang}")
                        
            # Check for languages in individual pages
            if hasattr(image_response, 'pages') and image_response.pages:
                for page in image_response.pages:
                    if hasattr(page, 'languages') and page.languages:
                        for lang in page.languages:
                            detected_languages.add(str(lang))
                            logger.info(f"Found language in page: {lang}")
            
            # Optimize: Skip vision model step if ocr_markdown is very small or empty
            # BUT make an exception if custom_prompt is provided
            # OR if the image has visual content worth preserving
            if (not custom_prompt and not has_images) and (not image_ocr_markdown or len(image_ocr_markdown) < 50):
                logger.warning("OCR produced minimal text with no images. Returning basic result.")
                return {
                    "file_name": file_path.name,
                    "topics": ["Document"],
                    "languages": ["English"],
                    "ocr_contents": {
                        "raw_text": image_ocr_markdown if image_ocr_markdown else "No text could be extracted from the image."
                    },
                    "processing_note": "OCR produced minimal text content",
                    # Include raw response data for images
                    "raw_response_data": serialize_ocr_response(image_response)
                }
            
            # For images with minimal text but visual content, enhance the prompt
            elif has_images and (not image_ocr_markdown or len(image_ocr_markdown) < 100):
                logger.info("Document with images but minimal text detected. Using enhanced prompt for mixed media.")
                if not custom_prompt:
                    custom_prompt = "This is a mixed media document with both text and important visual elements. Please carefully describe the image content and extract all visible text, preserving the relationship between text and visuals."
                elif "visual" not in custom_prompt.lower() and "image" not in custom_prompt.lower():
                    custom_prompt += " The document contains important visual elements that should be described along with the text content."
                
            # Extract structured data using the appropriate model, with a single API call
            if use_vision:
                logger.info(f"Using vision model: {VISION_MODEL}")
                result = self._extract_structured_data_with_vision(base64_data_url, image_ocr_markdown, file_path.name, custom_prompt)
            else:
                logger.info(f"Using text-only model: {TEXT_MODEL}")
                result = self._extract_structured_data_text_only(image_ocr_markdown, file_path.name, custom_prompt)
                
            # If we have detected languages directly from the OCR model, use them
            if detected_languages:
                logger.info(f"Using languages detected by OCR model: {', '.join(detected_languages)}")
                result['languages'] = list(detected_languages)
                # Add flag to indicate source of language detection
                result['language_detection_source'] = 'mistral-ocr-latest'
                
            # Store the serialized OCR response for image rendering (for compatibility with original version)
            # Don't store raw_response directly as it's not JSON serializable
            serialized_response = serialize_ocr_response(image_response)
            result['raw_response_data'] = serialized_response
            
            # Store key parts of the OCR response for image rendering
            # With serialized format that can be stored in JSON
            result['has_images'] = has_images
            
            if has_images:
                # Serialize the entire response to ensure it's JSON serializable
                serialized_response = serialize_ocr_response(image_response)
                
                # Create a structured representation of images that can be serialized
                result['pages_data'] = []
                
                if hasattr(serialized_response, 'pages'):
                    serialized_pages = serialized_response.pages
                else:
                    # Handle case where serialization returns a dict instead of an object
                    serialized_pages = serialized_response.get('pages', [])
                    
                for page_idx, page in enumerate(serialized_pages):
                    # Handle both object and dict forms
                    if isinstance(page, dict):
                        markdown = page.get('markdown', '')
                        images = page.get('images', [])
                    else:
                        markdown = page.markdown if hasattr(page, 'markdown') else ''
                        images = page.images if hasattr(page, 'images') else []
                    
                    page_data = {
                        'page_number': page_idx + 1,
                        'markdown': markdown,
                        'images': []
                    }
                    
                    # Extract images if present
                    for img_idx, img in enumerate(images):
                        img_id = None
                        img_base64 = None
                        
                        if isinstance(img, dict):
                            img_id = img.get('id')
                            img_base64 = img.get('image_base64')
                        else:
                            img_id = img.id if hasattr(img, 'id') else None
                            img_base64 = img.image_base64 if hasattr(img, 'image_base64') else None
                        
                        if img_base64:
                            page_data['images'].append({
                                'id': img_id if img_id else f"img_{page_idx}_{img_idx}",
                                'image_base64': img_base64
                            })
                    
                    result['pages_data'].append(page_data)
            
            logger.info("Image processing completed successfully")
            return result
            
        except Exception as e:
            logger.error(f"Error processing image: {str(e)}")
            # Return basic result on error
            return {
                "file_name": file_path.name,
                "topics": ["Document"],
                "languages": ["English"],
                "error": str(e),
                "ocr_contents": {
                    "error": f"Failed to process image: {str(e)}",
                    "partial_text": "Image could not be processed."
                }
            }
    
    def _extract_structured_data_with_vision(self, image_base64, ocr_markdown, filename, custom_prompt=None):
        """
        Extract structured data using vision model with detailed historical context prompting
        Optimized for speed, accuracy, and resilience
        """
        logger = logging.getLogger("vision_processor")
        
        try:
            # Check if this is a newspaper or document with columns by filename
            is_likely_newspaper = False
            newspaper_keywords = ["newspaper", "gazette", "herald", "times", "journal", 
                                "chronicle", "post", "tribune", "news", "press", "gender"]
            
            # Check filename for newspaper indicators
            filename_lower = filename.lower()
            for keyword in newspaper_keywords:
                if keyword in filename_lower:
                    is_likely_newspaper = True
                    logger.info(f"Likely newspaper document detected in vision processing: {filename}")
                    break
            
            # Fast path: Skip vision API if OCR already produced reasonable text
            # We'll define "reasonable" as having at least 300 characters
            if len(ocr_markdown.strip()) > 300:
                logger.info("Sufficient OCR text detected, analyzing language before using OCR text directly")
                
                # Perform language detection on the OCR text before returning
                if LANG_DETECTOR_AVAILABLE and self.language_detector:
                    detected_languages = self.language_detector.detect_languages(
                        ocr_markdown, 
                        filename=getattr(self, 'current_filename', None)
                    )
                else:
                    # If language detector is not available, use default English
                    detected_languages = ["English"]
                
                return {
                    "file_name": filename,
                    "topics": ["Document"],
                    "languages": detected_languages,
                    "ocr_contents": {
                        "raw_text": ocr_markdown
                    }
                }
                
            # Only use vision model for minimal OCR text or when document has columns
            if is_likely_newspaper and (not ocr_markdown or len(ocr_markdown.strip()) < 300):
                logger.info("Using vision model for newspaper with minimal OCR text")
                if not custom_prompt:
                    custom_prompt = "Document has columns. Extract text by reading each column top to bottom."
            
            # Fast path: Skip if in test mode or no API key
            if self.test_mode or not self.api_key:
                logger.info("Test mode or no API key, using text-only processing")
                return self._extract_structured_data_text_only(ocr_markdown, filename)
            
            # Use only the first part of OCR text to keep prompts small and processing fast
            if len(ocr_markdown) > 1000:
                truncated_ocr = ocr_markdown[:1000]
                logger.info(f"Truncated OCR text from {len(ocr_markdown)} to 1000 chars for faster processing")
            else:
                truncated_ocr = ocr_markdown
            
            # Build a comprehensive prompt with OCR text and detailed instructions for title detection and language handling
            enhanced_prompt = f"This is a document's OCR text:\n<BEGIN_OCR>\n{truncated_ocr}\n<END_OCR>\n\n"
            
            # Add custom prompt if provided
            if custom_prompt:
                enhanced_prompt += f"User instructions: {custom_prompt}\n\n"
                
            # Primary focus on document structure and title detection
            enhanced_prompt += "You are analyzing a historical document. Follow these extraction priorities:\n"
            enhanced_prompt += "1. FIRST PRIORITY: Identify and extract the TITLE of the document. Look for large text at the top, decorative typography, or centered text that appears to be a title. The title is often one of the first elements in historical documents.\n"
            enhanced_prompt += "2. SECOND: Extract all text content accurately from this document, including any text visible in the image that may not have been captured by OCR.\n\n"
            enhanced_prompt += "Document Title Guidelines:\n"
            enhanced_prompt += "- For printed historical works: Look for primary heading at top of the document, all-caps text, or larger font size text\n"
            enhanced_prompt += "- For newspapers/periodicals: Extract both newspaper name and article title if present\n"
            enhanced_prompt += "- For handwritten documents: Look for centered text at the top or underlined headings\n"
            enhanced_prompt += "- For engravings/illustrations: Include the title or caption, which often appears below the image\n\n"
            
            # Language detection guidance
            enhanced_prompt += "IMPORTANT: After extracting the title and text content, determine the languages present.\n"
            enhanced_prompt += "Precisely identify and list ALL languages present in the document separately. Look closely for multiple languages that might appear together.\n"
            enhanced_prompt += "For language detection, examine these specific indicators:\n"
            enhanced_prompt += "- French: accents (é, è, ê, à, ç, â, î, ô, û), words like 'le', 'la', 'les', 'et', 'en', 'de', 'du', 'des', 'dans', 'ce', 'cette', 'ces', 'par', 'pour', 'qui', 'que', 'où', 'avec'\n"
            enhanced_prompt += "- Portuguese: accents (ã, õ, á, é, ê, ó, ç), words like 'e', 'o', 'de', 'da', 'do', 'em', 'para', 'que', 'não', 'com'\n"
            enhanced_prompt += "- Spanish: ñ, inverted punctuation (¿, ¡), accents (á, é, í, ó, ú), words like 'el', 'la', 'los', 'las', 'y', 'en', 'por', 'que', 'con'\n"
            enhanced_prompt += "- German: umlauts (ä, ö, ü), sharp s (ß), words like 'und', 'der', 'die', 'das', 'in', 'mit'\n"
            enhanced_prompt += "- Italian: accents (à, è, é, ì, ò, ù), words like 'il', 'la', 'e', 'di', 'che', 'per', 'con'\n"
            enhanced_prompt += "- Chinese: hanzi characters (汉字), lack of spaces between words, markers like 的, 是, 了, 在, 和, 有\n"
            enhanced_prompt += "- Latin: words like 'et', 'in', 'ad', 'est', 'sunt', 'non', 'cum', 'sed'\n\n"
            enhanced_prompt += "If the document contains multiple columns or sections, process each section independently and then combine them logically.\n"
            enhanced_prompt += "Return ALL detected languages as separate entries in the languages array, never combine them.\n"
            enhanced_prompt += "CRITICAL: Do NOT default to English unless absolutely certain. If you see French characteristics like 'é', 'è', 'ê', 'ç' or French words, prioritize French in your language detection."
            
            # Measure API call time for optimization feedback
            start_time = time.time()
            
            try:
                # Use a fixed, shorter timeout for single-page documents
                timeout_ms = 45000  # 45 seconds is optimal for most single-page documents
                
                logger.info(f"Calling vision model with {timeout_ms}ms timeout")
                chat_response = self.client.chat.parse(
                    model=VISION_MODEL,
                    messages=[
                        {
                            "role": "user",
                            "content": [
                                ImageURLChunk(image_url=image_base64),
                                TextChunk(text=enhanced_prompt)
                            ],
                        },
                    ],
                    response_format=StructuredOCRModel,
                    temperature=0,
                    timeout_ms=timeout_ms
                )
                
                api_time = time.time() - start_time
                logger.info(f"Vision model completed in {api_time:.2f}s")
                
            except Exception as e:
                # If there's an error with the enhanced prompt, try progressively simpler approaches
                logger.warning(f"Enhanced prompt failed after {time.time() - start_time:.2f}s: {str(e)}")
                
                # Try a very simplified approach with minimal context
                try:
                    # Ultra-short prompt for faster processing
                    simplified_prompt = (
                        f"Extract text from this document image. "
                        f"<BEGIN_OCR>\n{truncated_ocr[:500]}\n<END_OCR>\n"
                        f"Return a JSON with file_name, topics, languages, and ocr_contents fields."
                    )
                    
                    # Only add minimal custom prompt if provided
                    if custom_prompt and len(custom_prompt) < 100:
                        simplified_prompt += f"\n{custom_prompt}"
                    
                    logger.info(f"Trying simplified prompt approach")
                    chat_response = self.client.chat.parse(
                        model=VISION_MODEL,
                        messages=[
                            {
                                "role": "user",
                                "content": [
                                    ImageURLChunk(image_url=image_base64),
                                    TextChunk(text=simplified_prompt)
                                ],
                            },
                        ],
                        response_format=StructuredOCRModel,
                        temperature=0,
                        timeout_ms=30000  # Very short timeout for simplified approach (30 seconds)
                    )
                    
                    logger.info(f"Simplified prompt approach succeeded")
                    
                except Exception as second_e:
                    # If that fails, try with minimal prompt and just image analysis
                    logger.warning(f"Simplified prompt failed: {str(second_e)}. Trying minimal prompt.")
                    
                    try:
                        # Minimal prompt focusing only on OCR task
                        minimal_prompt = (
                            f"Extract the text from this image. "
                            f"Return JSON with file_name, topics, languages, and ocr_contents.raw_text fields."
                        )
                        
                        logger.info(f"Trying minimal prompt with image-only focus")
                        chat_response = self.client.chat.parse(
                            model=VISION_MODEL,
                            messages=[
                                {
                                    "role": "user",
                                    "content": [
                                        ImageURLChunk(image_url=image_base64),
                                        TextChunk(text=minimal_prompt)
                                    ],
                                },
                            ],
                            response_format=StructuredOCRModel,
                            temperature=0,
                            timeout_ms=25000  # Minimal timeout for last attempt (25 seconds)
                        )
                        
                        logger.info(f"Minimal prompt approach succeeded")
                        
                    except Exception as third_e:
                        # If all vision attempts fail, fall back to text-only model
                        logger.warning(f"All vision model attempts failed, falling back to text-only model: {str(third_e)}")
                        return self._extract_structured_data_text_only(ocr_markdown, filename)
            
            # Convert the response to a dictionary
            result = json.loads(chat_response.choices[0].message.parsed.json())
            
            # Ensure languages is a list of strings, not Language enum objects
            if 'languages' in result:
                result['languages'] = [str(lang) for lang in result.get('languages', [])]
                
            # Add simplified metadata about processing
            result['processing_info'] = {
                'method': 'vision_model',
                'ocr_text_length': len(ocr_markdown),
                'api_response_time': time.time() - start_time
            }
            
            # Note if custom prompt was applied
            if custom_prompt:
                result['custom_prompt_applied'] = 'vision_model'
            
            # Add confidence score if not present
            if 'confidence_score' not in result:
                result['confidence_score'] = 0.92  # Vision model typically has higher confidence
                
            # If OCR text has clear French patterns but language is English or missing, fix it
            if ocr_markdown and 'languages' in result:
                if LANG_DETECTOR_AVAILABLE and self.language_detector:
                    result['languages'] = self.language_detector.detect_languages(
                        ocr_markdown, 
                        filename=getattr(self, 'current_filename', None),
                        current_languages=result['languages']
                    )
                
        except Exception as e:
            # Fall back to text-only model if vision model fails
            logger.warning(f"Vision model processing failed, falling back to text-only model: {str(e)}")
            result = self._extract_structured_data_text_only(ocr_markdown, filename)
            
        return result
        
    # We've removed document type detection entirely for simplicity

        
        # Create a prompt with enhanced language detection instructions
        generic_section = (
            f"You are an OCR specialist processing historical documents. "
            f"Focus on accurately extracting text content and image chunks while preserving structure and formatting. "
            f"Pay attention to any historical features and document characteristics.\n\n"
            f"Create a structured JSON response with the following fields:\n"
            f"- file_name: The document's name\n"
            f"- topics: An array of topics covered in the document\n"
            f"- languages: An array of languages used in the document (be precise and specific about language detection)\n"
            f"- ocr_contents: A comprehensive dictionary with the document's contents including:\n"
            f"  * title: The title or heading (if present)\n"
            f"  * transcript: The full text of the document\n"
            f"  * text: The main text content (if different from transcript)\n"
            f"  * content: The body content (if different than transcript)\n"
            f"  * images: An array of image objects with their base64 data\n"
            f"  * alt_text: The alt text or description of the images\n"
            f"  * caption: The caption or title of the images\n"
            f"  * raw_text: The complete OCR text\n"
        )
        
        # Add custom prompt if provided
        custom_section = ""
        if custom_prompt:
            custom_section = f"\n\nUser-provided instructions: {custom_prompt}\n"
        
        # Return the enhanced prompt
        return generic_section + custom_section
            
    def _extract_structured_data_text_only(self, ocr_markdown, filename, custom_prompt=None):
        """
        Extract structured data using text-only model with detailed historical context prompting
        and improved error handling with enhanced language detection
        """
        logger = logging.getLogger("text_processor")
        start_time = time.time()
        
        try:
            # Fast path: Skip for minimal OCR text
            if not ocr_markdown or len(ocr_markdown.strip()) < 50:
                logger.info("Minimal OCR text - returning basic result")
                
                # Attempt comprehensive language detection even for minimal text
                detected_languages = []
                
                # Simple language detection based on character frequency
                if ocr_markdown and len(ocr_markdown.strip()) > 10:
                    # Define indicators for all supported languages
                    language_indicators = {
                        "Portuguese": {
                            "chars": ['ã', 'õ', 'á', 'é', 'ê', 'í', 'ó', 'ú', 'ç'],
                            "words": ['e', 'o', 'de', 'da', 'do', 'em', 'para', 'que', 'não', 'com']
                        },
                        "Spanish": {
                            "chars": ['ñ', 'á', 'é', 'í', 'ó', 'ú', '¿', '¡'],
                            "words": ['el', 'la', 'los', 'las', 'y', 'en', 'por', 'que', 'con', 'del']
                        },
                        "French": {
                            "chars": ['é', 'è', 'ê', 'à', 'ç', 'ù', 'â', 'î', 'ô', 'û'],
                            "words": ['le', 'la', 'les', 'et', 'en', 'de', 'du', 'des', 'un', 'une', 'ce', 'cette', 'qui', 'que', 'pour', 'dans', 'par', 'sur']
                        },
                        "German": {
                            "chars": ['ä', 'ö', 'ü', 'ß'],
                            "words": ['der', 'die', 'das', 'und', 'ist', 'von', 'mit', 'für', 'sich']
                        },
                        "Italian": {
                            "chars": ['à', 'è', 'é', 'ì', 'ò', 'ù'],
                            "words": ['il', 'la', 'e', 'di', 'che', 'per', 'con', 'sono', 'non']
                        },
                        "Latin": {
                            "chars": [],
                            "words": ['et', 'in', 'ad', 'est', 'sunt', 'non', 'cum', 'sed', 'qui', 'quod']
                        }
                    }
                    
                    words = ocr_markdown.lower().split()
                    
                    # Check for indicators of each language
                    for language, indicators in language_indicators.items():
                        chars = indicators["chars"]
                        lang_words = indicators["words"]
                        
                        has_chars = any(char in ocr_markdown for char in chars) if chars else False
                        word_count = sum(1 for word in words if word in lang_words)
                        
                        # Add language if strong enough indicators are present
                        if has_chars or word_count >= 2:
                            detected_languages.append(language)
                    
                    # Check for English separately
                    english_words = ['the', 'and', 'of', 'to', 'in', 'a', 'is', 'that', 'for', 'it']
                    english_count = sum(1 for word in words if word in english_words)
                    if english_count >= 2:
                        detected_languages.append("English")
                
                # If no languages detected, default to English
                if not detected_languages:
                    detected_languages = ["English"]
                
                return {
                    "file_name": filename,
                    "topics": ["Document"],
                    "languages": detected_languages,
                    "ocr_contents": {
                        "raw_text": ocr_markdown if ocr_markdown else "No text could be extracted"
                    },
                    "processing_method": "minimal_text"
                }
            
            # Check for API key to avoid unnecessary processing
            if self.test_mode or not self.api_key:
                logger.info("Test mode or no API key - returning basic result")
                return {
                    "file_name": filename,
                    "topics": ["Document"],
                    "languages": ["English"],
                    "ocr_contents": {
                        "raw_text": ocr_markdown[:10000] if ocr_markdown else "No text could be extracted",
                        "note": "API key not provided - showing raw OCR text only"
                    },
                    "processing_method": "test_mode"
                }
                
            # If OCR text is very large, truncate it to avoid API limits
            truncated_text = ocr_markdown
            if len(ocr_markdown) > 25000:
                # Keep first 15000 chars and last 5000 chars
                truncated_text = ocr_markdown[:15000] + "\n...[content truncated]...\n" + ocr_markdown[-5000:]
                logger.info(f"OCR text truncated from {len(ocr_markdown)} to {len(truncated_text)} chars")
                
            # Build a prompt with enhanced title detection and language detection instructions
            enhanced_prompt = f"This is a document's OCR text:\n<BEGIN_OCR>\n{truncated_text}\n<END_OCR>\n\n"
            
            # Add custom prompt if provided
            if custom_prompt:
                enhanced_prompt += f"User instructions: {custom_prompt}\n\n"
            
            # Add title detection focus
            enhanced_prompt += "You are analyzing a historical document. Please follow these extraction priorities:\n"
            enhanced_prompt += "1. FIRST PRIORITY: Identify and extract the TITLE of the document. Look for prominent text at the top, decorative typography, or centered text that appears to be a title.\n"
            enhanced_prompt += "   - For historical documents with prominent headings at the top\n"
            enhanced_prompt += "   - For newspapers or periodicals, extract both the publication name and article title\n"
            enhanced_prompt += "   - For manuscripts or letters, identify any heading or subject line\n"
            enhanced_prompt += "2. SECOND PRIORITY: Extract all text content accurately and return structured data with the document's contents.\n\n"
            enhanced_prompt += "IMPORTANT: Precisely identify and list ALL languages present in the document separately. Look closely for multiple languages that might appear together.\n"
            enhanced_prompt += "For language detection, examine these specific indicators:\n"
            enhanced_prompt += "- French: accents (é, è, ê, à, ç), words like 'le', 'la', 'les', 'et', 'en', 'de', 'du'\n"
            enhanced_prompt += "- German: umlauts (ä, ö, ü), sharp s (ß), words like 'und', 'der', 'die', 'das', 'in', 'mit'\n"
            enhanced_prompt += "- Spanish: ñ, inverted punctuation (¿, ¡), accents (á, é, í, ó, ú), words like 'el', 'la', 'los', 'las', 'y', 'en'\n"
            enhanced_prompt += "- Italian: words like 'il', 'la', 'e', 'di', 'che', 'per', 'con'\n"
            enhanced_prompt += "- Chinese: hanzi characters (汉字), lack of spaces between words, markers like 的, 是, 了, 在, 和, 有\n"
            enhanced_prompt += "- Latin: words like 'et', 'in', 'ad', 'est', 'sunt', 'non', 'cum', 'sed'\n"
            enhanced_prompt += "Do NOT classify text as English unless you can positively confirm it contains specifically English words and phrases.\n\n"
            enhanced_prompt += "Return ALL detected languages as separate entries in the languages array. If multiple languages are present, list them ALL separately."
            
            # Use enhanced prompt with text-only model - with retry logic
            max_retries = 2
            retry_delay = 1
            
            for retry in range(max_retries):
                try:
                    logger.info(f"Calling text model ({TEXT_MODEL})")
                    api_start = time.time()
                    
                    # Set appropriate timeout based on text length
                    timeout_ms = min(120000, max(30000, len(truncated_text) * 5))  # 30-120s based on length
                    
                    # Make API call with appropriate timeout
                    chat_response = self.client.chat.parse(
                        model=TEXT_MODEL,
                        messages=[
                            {
                                "role": "user",
                                "content": enhanced_prompt
                            },
                        ],
                        response_format=StructuredOCRModel,
                        temperature=0,
                        timeout_ms=timeout_ms
                    )
                    
                    api_time = time.time() - api_start
                    logger.info(f"Text model API call completed in {api_time:.2f}s")
                    
                    # Convert the response to a dictionary
                    result = json.loads(chat_response.choices[0].message.parsed.json())
                    
                    # Ensure languages is a list of strings, not Language enum objects
                    if 'languages' in result:
                        result['languages'] = [str(lang) for lang in result.get('languages', [])]
                    
                    # Add simplified processing metadata
                    result['processing_method'] = 'text_model'
                    result['model_used'] = TEXT_MODEL
                    result['processing_time'] = time.time() - start_time
                    
                    # Flag when custom prompt has been successfully applied
                    if custom_prompt:
                        result['custom_prompt_applied'] = 'text_model'
                    
                    # Add raw text for reference if not already present
                    if 'ocr_contents' in result and 'raw_text' not in result['ocr_contents']:
                        # Add truncated raw text if very large
                        if len(ocr_markdown) > 50000:
                            result['ocr_contents']['raw_text'] = ocr_markdown[:50000] + "\n...[content truncated]..."
                        else:
                            result['ocr_contents']['raw_text'] = ocr_markdown
                            
                    return result
                
                except Exception as api_error:
                    error_msg = str(api_error).lower()
                    logger.warning(f"API error on attempt {retry+1}/{max_retries}: {str(api_error)}")
                    
                    # Check if retry would help
                    if retry < max_retries - 1:
                        # Rate limit errors - special handling with longer wait
                        if any(term in error_msg for term in ["rate limit", "429", "too many requests", "requests rate limit exceeded"]):
                            # Check specifically for token exhaustion vs temporary rate limit
                            if any(term in error_msg for term in ["quota", "credit", "subscription"]):
                                logger.error("API quota or credit limit reached. No retry will help.")
                                raise ValueError(f"Mistral API quota or credit limit reached. Please check your subscription: {error_msg}")
                            # Longer backoff for rate limit errors
                            wait_time = retry_delay * (2 ** retry) * 6.0  # 6x longer wait for rate limits
                            logger.info(f"Rate limit exceeded. Waiting {wait_time:.1f}s before retry...")
                            time.sleep(wait_time)
                        # Other transient errors
                        elif any(term in error_msg for term in ["timeout", "connection", "500", "503", "504"]):
                            # Wait before retrying
                            wait_time = retry_delay * (2 ** retry)
                            logger.info(f"Transient error, retrying in {wait_time}s")
                            time.sleep(wait_time)
                        else:
                            # Non-retryable error
                            raise
                    else:
                        # Last retry failed
                        raise
            
            # This shouldn't be reached due to raise in the loop, but just in case
            raise Exception("All retries failed for text model")
            
        except Exception as e:
            logger.error(f"Text model failed: {str(e)}. Creating basic result.")
            
            # Create a basic result with available OCR text
            try:
                # Create a more informative fallback result
                result = {
                    "file_name": filename,
                    "topics": ["Document"],
                    "languages": ["English"],
                    "ocr_contents": {
                        "raw_text": ocr_markdown[:50000] if ocr_markdown else "No text could be extracted",
                        "error": "AI processing failed: " + str(e).replace('"', '\\"')
                    },
                    "processing_method": "fallback",
                    "processing_error": str(e),
                    "processing_time": time.time() - start_time
                }
                
                # No topic detection to avoid issue with document misclassification
                        
            except Exception as inner_e:
                logger.error(f"Error creating basic result: {str(inner_e)}")
                result = {
                    "file_name": str(filename) if filename else "unknown",
                    "topics": ["Document"],
                    "languages": ["English"],
                    "ocr_contents": {
                        "error": "Processing failed completely",
                        "partial_text": ocr_markdown[:1000] if ocr_markdown else "Document could not be processed."
                    }
                }
            
        return result

# For testing directly
if __name__ == "__main__":
    import sys
    
    if len(sys.argv) < 2:
        print("Usage: python structured_ocr.py <file_path>")
        sys.exit(1)
        
    file_path = sys.argv[1]
    processor = StructuredOCR()
    result = processor.process_file(file_path)
    
    print(json.dumps(result, indent=2))