File size: 50,790 Bytes
5f30414
 
 
 
 
 
 
 
 
 
 
 
0d2793f
 
 
7478cb5
 
5f30414
 
 
114ad9c
0d2793f
 
 
 
 
 
 
 
 
5f30414
0d2793f
 
 
 
 
 
 
 
5f30414
0d2793f
5f30414
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
0d2793f
 
5f30414
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
0d2793f
 
5f30414
 
 
 
 
 
 
0d2793f
 
 
 
 
5f30414
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
 
5f30414
0d2793f
5f30414
 
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
 
 
 
 
 
 
 
 
 
 
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
 
 
 
 
 
 
 
 
114ad9c
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
 
5f30414
 
 
0d2793f
5f30414
 
 
0d2793f
5f30414
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
 
114ad9c
5f30414
 
0d2793f
5f30414
0d2793f
5f30414
 
0d2793f
5f30414
 
0d2793f
5f30414
0d2793f
 
5f30414
 
 
 
 
 
0d2793f
 
 
 
 
5f30414
 
 
0d2793f
 
 
 
 
 
 
5f30414
0d2793f
 
 
 
 
 
5f30414
0d2793f
 
 
5f30414
0d2793f
 
 
 
 
 
 
5f30414
 
 
0d2793f
 
5f30414
 
 
0d2793f
 
5f30414
 
0d2793f
5f30414
 
 
 
 
0d2793f
 
 
 
 
 
 
114ad9c
0d2793f
5f30414
 
0d2793f
5f30414
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
0d2793f
 
 
 
 
 
 
5f30414
bc7e8e9
0d2793f
5f30414
 
0d2793f
 
 
5f30414
 
 
0d2793f
 
 
5f30414
 
 
 
0d2793f
5f30414
 
0d2793f
5f30414
 
 
0d2793f
 
 
 
 
 
 
5f30414
0d2793f
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
0d2793f
 
 
 
 
 
 
5f30414
bc7e8e9
0d2793f
5f30414
 
0d2793f
 
 
5f30414
 
 
0d2793f
 
 
5f30414
 
 
 
0d2793f
5f30414
0d2793f
 
 
 
5f30414
 
 
 
0d2793f
5f30414
 
0d2793f
 
5f30414
 
 
 
 
0d2793f
 
5f30414
 
 
 
 
 
 
 
0d2793f
5f30414
 
0d2793f
5f30414
0d2793f
5f30414
 
 
 
 
 
 
 
 
 
0d2793f
 
 
 
 
 
 
5f30414
 
0d2793f
5f30414
 
 
 
0d2793f
 
 
 
5f30414
0d2793f
 
 
 
5f30414
0d2793f
5f30414
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
5f30414
 
 
 
0d2793f
 
 
 
 
 
 
 
 
5f30414
 
0d2793f
 
 
5f30414
 
 
0d2793f
 
 
5f30414
 
 
 
 
0d2793f
a76c6b6
5f30414
 
0d2793f
 
 
 
5f30414
 
a76c6b6
 
 
0d2793f
 
 
 
 
a76c6b6
 
0d2793f
5f30414
 
0d2793f
 
 
5f30414
 
 
a76c6b6
5f30414
a76c6b6
5f30414
 
a76c6b6
5f30414
 
114ad9c
a76c6b6
 
 
 
 
0d2793f
a76c6b6
0d2793f
 
a76c6b6
0d2793f
 
a76c6b6
0d2793f
 
 
a76c6b6
0d2793f
 
 
 
 
 
 
a76c6b6
 
114ad9c
0d2793f
5f30414
 
a76c6b6
114ad9c
0d2793f
114ad9c
a76c6b6
 
5f30414
114ad9c
5f30414
a76c6b6
5f30414
0d2793f
 
5f30414
 
a76c6b6
 
0d2793f
 
5f30414
 
0d2793f
5f30414
0d2793f
 
 
5f30414
a76c6b6
 
0d2793f
a76c6b6
 
 
0d2793f
 
a76c6b6
5f30414
 
0d2793f
a76c6b6
5f30414
a76c6b6
5f30414
0d2793f
 
 
 
a76c6b6
 
 
 
 
 
0d2793f
 
 
 
a76c6b6
 
0d2793f
a76c6b6
0d2793f
5f30414
0d2793f
 
 
a76c6b6
5f30414
0d2793f
 
5f30414
 
 
0d2793f
 
 
5f30414
 
 
a76c6b6
5f30414
0d2793f
a76c6b6
114ad9c
0d2793f
a76c6b6
0d2793f
 
 
a76c6b6
0d2793f
 
 
 
 
 
 
 
 
 
 
a76c6b6
0d2793f
 
a76c6b6
0d2793f
 
a76c6b6
 
114ad9c
a76c6b6
 
 
 
0d2793f
 
a76c6b6
0d2793f
 
 
 
 
 
 
 
 
a76c6b6
0d2793f
 
a76c6b6
0d2793f
 
 
 
a76c6b6
0d2793f
 
 
 
 
 
 
a76c6b6
 
 
114ad9c
0d2793f
5f30414
 
a76c6b6
114ad9c
 
a76c6b6
 
5f30414
114ad9c
5f30414
0d2793f
5f30414
0d2793f
 
5f30414
 
a76c6b6
 
0d2793f
 
 
5f30414
 
0d2793f
 
 
 
 
5f30414
a76c6b6
 
 
 
 
0d2793f
a76c6b6
 
5f30414
 
0d2793f
a76c6b6
5f30414
a76c6b6
5f30414
0d2793f
 
 
 
 
a76c6b6
 
0d2793f
 
 
 
 
 
 
 
 
 
5f30414
0d2793f
 
a76c6b6
 
5f30414
0d2793f
 
5f30414
a76c6b6
5f30414
 
0d2793f
 
 
5f30414
 
 
 
a76c6b6
5f30414
0d2793f
a76c6b6
0d2793f
 
5f30414
0d2793f
 
 
 
a76c6b6
0d2793f
5f30414
0d2793f
 
 
a76c6b6
5f30414
114ad9c
0d2793f
 
 
 
5f30414
114ad9c
a76c6b6
 
0d2793f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a76c6b6
0d2793f
 
 
 
 
a76c6b6
0d2793f
 
 
 
a76c6b6
114ad9c
 
a76c6b6
114ad9c
5f30414
 
a76c6b6
0d2793f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f30414
 
a76c6b6
5f30414
 
0d2793f
 
 
5f30414
 
0d2793f
a76c6b6
5f30414
0d2793f
 
 
a76c6b6
0d2793f
a76c6b6
0d2793f
 
a76c6b6
0d2793f
 
 
 
 
 
 
 
a76c6b6
0d2793f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f30414
a76c6b6
 
0d2793f
a76c6b6
5f30414
0d2793f
 
5f30414
a76c6b6
5f30414
 
 
a76c6b6
 
0d2793f
 
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
from flask import Flask, request, jsonify, render_template_string
from flask_cors import CORS
from google import genai
from google.genai import types
import os
import io
import httpx
import uuid
from datetime import datetime, timezone, timedelta
from dotenv import load_dotenv
import json

# Load environment variables from a .env file
# This is useful for local development. In production on platforms like Hugging Face,
# you'll set these as environment variables directly in the settings.
load_dotenv()

app = Flask(__name__)
CORS(app)

# Initialize Gemini client
# The API key should be loaded from environment variables
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
    print("Error: GOOGLE_API_KEY environment variable not set.")
    # In a real app, you might exit or raise an exception here.
    # For this example, we'll print an error but allow the app to start;
    # API calls will fail if the key is missing.
    # If running locally, make sure you have a .env file with GOOGLE_API_KEY=YOUR_API_KEY
    pass # Allows the app to run without a key for debugging non-API parts

try:
    client = genai.Client(api_key=api_key)
except Exception as e:
    print(f"Failed to initialize Gemini client: {e}")
    client = None # Set client to None if initialization fails

# In-memory storage for demo (in production, use a database like Redis or PostgreSQL)
# Maps our internal cache_id (UUID) to Gemini's cache_name and other info
document_caches = {}
user_sessions = {} # Not used in this version, but kept from template

# HTML template for the web interface
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Smart Document Analysis Platform</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            color: white;
        }

        .header h1 {
            font-size: 2.8em;
            font-weight: 700;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.2em;
            opacity: 0.9;
            font-weight: 300;
        }

        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            flex-grow: 1;
        }

        .left-panel, .right-panel {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
        }

        .left-panel {
             overflow-y: auto; /* Allow scrolling if content is tall */
        }

        .panel-title {
            font-size: 1.5em;
            font-weight: 600;
            margin-bottom: 20px;
            color: #2d3748;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .upload-section {
            margin-bottom: 30px;
        }

        .upload-area {
            border: 2px dashed #667eea;
            border-radius: 15px;
            padding: 40px;
            text-align: center;
            background: #f8fafc;
            transition: all 0.3s ease;
            margin-bottom: 20px;
            cursor: pointer; /* Indicate clickable area */
        }

        .upload-area:hover {
            border-color: #764ba2;
            background: #f0f2ff;
            transform: translateY(-2px);
        }

        .upload-area.dragover {
            border-color: #764ba2;
            background: #e8f0ff;
            transform: scale(1.02);
        }

        .upload-icon {
            font-size: 3em;
            color: #667eea;
            margin-bottom: 15px;
        }

        .file-input {
            display: none;
        }

        .upload-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 1.1em;
            font-weight: 500;
            transition: all 0.3s ease;
            margin: 10px;
        }

        .upload-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .url-input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            font-size: 1em;
            margin-bottom: 15px;
            transition: border-color 0.3s ease;
        }

        .url-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 20px;
            cursor: pointer;
            font-size: 1em;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .chat-container {
            flex: 1;
            border: 1px solid #e2e8f0;
            border-radius: 15px;
            overflow-y: auto;
            padding: 20px;
            background: #f8fafc;
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
        }

        .message {
            margin-bottom: 15px;
            padding: 15px;
            border-radius: 12px;
            max-width: 85%;
            animation: fadeIn 0.3s ease;
            word-wrap: break-word; /* Ensure long words wrap */
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .user-message {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            margin-left: auto;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        }

        .ai-message {
            background: white;
            color: #333;
            border: 1px solid #e2e8f0;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-right: auto; /* Align AI messages to the left */
        }

        .input-group {
            display: flex;
            gap: 10px;
        }

        .question-input {
            flex: 1;
            padding: 15px;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            font-size: 1em;
            transition: border-color 0.3s ease;
        }

        .question-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .cache-info {
            background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            color: white;
            box-shadow: 0 4px 12px rgba(72, 187, 120, 0.3);
        }

        .cache-info h3 {
            margin-bottom: 10px;
            font-weight: 600;
        }

         .cache-info p {
             font-size: 0.9em;
             margin-bottom: 5px;
         }

         .cache-info p:last-child {
             margin-bottom: 0;
         }


        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .loading-spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }

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

        .error {
            background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
            border-radius: 12px;
            padding: 15px;
            color: white;
            margin-bottom: 20px;
            box-shadow: 0 4px 12px rgba(245, 101, 101, 0.3);
        }

        .success {
            background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
            border-radius: 12px;
            padding: 15px;
            color: white;
            margin-bottom: 20px;
            box-shadow: 0 4px 12px rgba(72, 187, 120, 0.3);
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
                gap: 20px;
            }

            .header h1 {
                font-size: 2em;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📚 Smart Document Analysis Platform</h1>
            <p>Upload PDF documents once, ask questions forever with Gemini API caching</p>
             <p style="font-size:0.9em; margin-top: 5px; opacity: 0.8;">Powered by Google Gemini API - Explicit Caching</p>
        </div>

        <div class="main-content">
            <!-- Left Panel - Upload Section -->
            <div class="left-panel">
                <div class="panel-title">
                    📤 Upload PDF Document
                </div>

                <div class="upload-section">
                    <div class="upload-area" id="uploadArea">
                        <div class="upload-icon">📄</div>
                        <p>Drag and drop your PDF file here, or click to select</p>
                        <input type="file" id="fileInput" class="file-input" accept=".pdf">
                         <!-- The button triggers the hidden file input -->
                        <button type="button" class="upload-btn" onclick="document.getElementById('fileInput').click()">
                            Choose PDF File
                        </button>
                    </div>

                    <div style="margin-top: 20px;">
                        <h3>Or provide a URL:</h3>
                        <input type="url" id="urlInput" class="url-input" placeholder="https://example.com/document.pdf">
                        <button type="button" class="btn" onclick="uploadFromUrl()">Upload from URL</button>
                    </div>
                </div>

                <div id="loading" class="loading" style="display: none;">
                    <div class="loading-spinner"></div>
                    <p id="loadingText">Processing your PDF... This may take a moment.</p>
                </div>

                <div id="error" class="error" style="display: none;"></div>
                <div id="success" class="success" style="display: none;"></div>
            </div>

            <!-- Right Panel - Chat Section -->
            <div class="right-panel">
                <div class="panel-title">
                    💬 Ask Questions
                </div>

                <div id="cacheInfo" class="cache-info" style="display: none;">
                    <h3>✅ Document Cached Successfully!</h3>
                    <p>Your PDF has been cached using Gemini API. You can now ask multiple questions without re-uploading.</p>
                    <p><strong>Cache ID:</strong> <span id="cacheId"></span></p>
                    <p><strong>Tokens Cached:</strong> <span id="tokenCount"></span></p>
                    <p>Note: Caching is ideal for larger documents (typically 1024+ tokens required).</p>
                </div>

                <div class="chat-container" id="chatContainer">
                    <div class="message ai-message">
                        👋 Hello! Upload a PDF document using the panel on the left, and I'll help you analyze it using Gemini API caching!
                    </div>
                </div>

                <div class="input-group">
                    <input type="text" id="questionInput" class="question-input" placeholder="Ask a question about your document..." disabled>
                    <button type="button" class="btn" onclick="askQuestion()" id="askBtn" disabled>Ask</button>
                </div>
            </div>
        </div>
    </div>
    <script>
        let currentCacheId = null;

        // Disable input/button initially
        document.getElementById('questionInput').disabled = true;
        document.getElementById('askBtn').disabled = true;

        // File upload handling
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');

        // Prevent default drag behaviors
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            uploadArea.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults (e) {
            e.preventDefault();
            e.stopPropagation();
        }

        // Highlight drop area when item is dragged over
        ['dragenter', 'dragover'].forEach(eventName => {
            uploadArea.addEventListener(eventName, () => uploadArea.classList.add('dragover'), false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            uploadArea.addEventListener(eventName, () => uploadArea.classList.remove('dragover'), false);
        });

        // Handle dropped files
        uploadArea.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;
            if (files.length > 0) {
                uploadFile(files[0]);
            }
        }

        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                uploadFile(e.target.files[0]);
                // Clear the input so the same file can be selected again if needed
                e.target.value = '';
            }
        });

        async function uploadFile(file) {
            if (!file.type.includes('pdf')) {
                showError('Please select a PDF file.');
                return;
            }

            // Clear previous status messages
            hideError();
            hideSuccess();
            document.getElementById('cacheInfo').style.display = 'none'; // Hide old cache info
            currentCacheId = null; // Clear old cache ID

            showLoading('Uploading PDF...');

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                if (result.success) {
                    currentCacheId = result.cache_id;
                    document.getElementById('cacheId').textContent = result.cache_id;
                    document.getElementById('tokenCount').textContent = result.token_count;
                    document.getElementById('cacheInfo').style.display = 'block';
                    showSuccess('PDF uploaded and cached successfully! You can now ask questions.');

                    // Enable chat input and button
                    document.getElementById('questionInput').disabled = false;
                    document.getElementById('askBtn').disabled = false;
                     document.getElementById('questionInput').focus(); // Focus input

                    // Add initial message
                    addMessage("I've analyzed your PDF document. What would you like to know about it?", 'ai');

                } else {
                    showError(result.error);
                     // Disable chat input/button if upload/cache failed
                    document.getElementById('questionInput').disabled = true;
                    document.getElementById('askBtn').disabled = true;
                }
            } catch (error) {
                showError('Error uploading file: ' + error.message);
                 // Disable chat input/button on network/server error
                document.getElementById('questionInput').disabled = true;
                document.getElementById('askBtn').disabled = true;
            } finally {
                hideLoading();
            }
        }

        async function uploadFromUrl() {
            const url = document.getElementById('urlInput').value;
            if (!url.trim()) {
                showError('Please enter a valid URL.');
                return;
            }

            // Clear previous status messages
            hideError();
            hideSuccess();
            document.getElementById('cacheInfo').style.display = 'none'; // Hide old cache info
            currentCacheId = null; // Clear old cache ID

            showLoading('Uploading PDF from URL...');

            try {
                const response = await fetch('/upload-url', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ url: url })
                });

                const result = await response.json();

                if (result.success) {
                    currentCacheId = result.cache_id;
                    document.getElementById('cacheId').textContent = result.cache_id;
                    document.getElementById('tokenCount').textContent = result.token_count;
                    document.getElementById('cacheInfo').style.display = 'block';
                    showSuccess('PDF uploaded and cached successfully! You can now ask questions.');

                    // Enable chat input and button
                    document.getElementById('questionInput').disabled = false;
                    document.getElementById('askBtn').disabled = false;
                     document.getElementById('questionInput').focus(); // Focus input

                    // Add initial message
                    addMessage("I've analyzed your PDF document. What would you like to know about it?", 'ai');

                } else {
                    showError(result.error);
                     // Disable chat input/button if upload/cache failed
                    document.getElementById('questionInput').disabled = true;
                    document.getElementById('askBtn').disabled = true;
                }
            } catch (error) {
                showError('Error uploading from URL: ' + error.message);
                 // Disable chat input/button on network/server error
                document.getElementById('questionInput').disabled = true;
                document.getElementById('askBtn').disabled = false; // Should be false? Fix: should be true
            } finally {
                hideLoading();
            }
        }

        async function askQuestion() {
            const questionInput = document.getElementById('questionInput');
            const question = questionInput.value.trim();
            if (!question) return; // Don't send empty questions

            if (!currentCacheId) {
                showError('Please upload a PDF document first.');
                return;
            }

            // Add user message to chat
            addMessage(question, 'user');
            questionInput.value = ''; // Clear input immediately

            // Show loading state
            const askBtn = document.getElementById('askBtn');
            const originalText = askBtn.textContent;
            askBtn.textContent = 'Generating...';
            askBtn.disabled = true;
             questionInput.disabled = true; // Disable input while generating

            try {
                const response = await fetch('/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        question: question,
                        cache_id: currentCacheId // Use our internal cache_id
                    })
                });

                const result = await response.json();

                if (result.success) {
                    addMessage(result.answer, 'ai');
                } else {
                    addMessage('Error: ' + result.error, 'ai');
                }
            } catch (error) {
                addMessage('Error: ' + error.message, 'ai');
            } finally {
                askBtn.textContent = originalText;
                askBtn.disabled = false;
                questionInput.disabled = false; // Re-enable input
                questionInput.focus(); // Put focus back on input
                // Ensure button is disabled only if no cache is active
                if (!currentCacheId) {
                    askBtn.disabled = true;
                    questionInput.disabled = true;
                }
            }
        }

        function addMessage(text, sender) {
            const chatContainer = document.getElementById('chatContainer');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${sender}-message`;

            // Use innerHTML to handle potential formatting like newlines or markdown
            // (Basic textContent might be sufficient depending on expected AI output)
            // For simplicity here, sticking to textContent as AI might output plain text
            messageDiv.textContent = text;

            // Basic handling for newlines
            messageDiv.style.whiteSpace = 'pre-wrap';

            chatContainer.appendChild(messageDiv);
            chatContainer.scrollTop = chatContainer.scrollHeight; // Auto-scroll to latest message
        }

        function showLoading(text = 'Processing...') {
            document.getElementById('loadingText').textContent = text;
            document.getElementById('loading').style.display = 'block';
        }

        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }

        function showError(message) {
            const errorDiv = document.getElementById('error');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
            // Auto-hide after 5 seconds
            setTimeout(() => {
                errorDiv.style.display = 'none';
            }, 5000);
        }

        function showSuccess(message) {
            const successDiv = document.getElementById('success');
            successDiv.textContent = message;
            successDiv.style.display = 'block';
             // Auto-hide after 5 seconds
            setTimeout(() => {
                successDiv.style.display = 'none';
            }, 5000);
        }

        function hideError() {
             document.getElementById('error').style.display = 'none';
        }

        function hideSuccess() {
             document.getElementById('success').style.display = 'none';
        }

        // Enter key to ask question
        document.getElementById('questionInput').addEventListener('keypress', (e) => {
            // Check if the input is not disabled and the key is Enter
            if (!document.getElementById('questionInput').disabled && e.key === 'Enter') {
                e.preventDefault(); // Prevent default form submission if input is part of a form
                askQuestion();
            }
        });

        // Initial message visibility
        // addMessage("👋 Hello! Upload a PDF document using the panel on the left, and I'll help you analyze it using Gemini API caching!", 'ai'); // Added this directly in HTML
    </script>
</body>
</html>
"""

# --- Flask Routes ---

@app.route('/')
def index():
    # Ensure API key is set before rendering, or add a warning to the template
    if not api_key:
        # You could modify the template or pass a variable to indicate error state
        print("Warning: API key not set. API calls will fail.")
    return render_template_string(HTML_TEMPLATE)

@app.route('/health', methods=['GET'])
def health_check():
    # A simple endpoint to check if the application is running
    # Can optionally check API client status if needed, but basic 200 is common.
    if client is None and api_key is not None: # Client failed to initialize despite key being present
        return jsonify({"status": "unhealthy", "reason": "Gemini client failed to initialize"}), 500
    # Note: This doesn't check if the API key is *valid* or if the API is reachable,
    # just if the Flask app is running and the client object was created.
    return jsonify({"status": "healthy"}), 200


@app.route('/upload', methods=['POST'])
def upload_file():
    if client is None or api_key is None:
         return jsonify({'success': False, 'error': 'API key not configured or Gemini client failed to initialize.'}), 500

    try:
        if 'file' not in request.files:
            return jsonify({'success': False, 'error': 'No file provided'})

        file = request.files['file']

        if file.filename == '':
            return jsonify({'success': False, 'error': 'No file selected'})

        # Read file content
        file_content = file.read()
        file_io = io.BytesIO(file_content)

        # --- CORRECTED FILE UPLOAD CALL ---
        # Upload to Gemini File API using the correct method client.upload_file
        # Pass the file content as a tuple (filename, file-like object, mime_type)
        # This replaces the incorrect client.files.upload call
        document = None # Initialize document variable
        try:
            # The mime_type is crucial for the API to correctly process the file.
            # The filename is used as the display_name by default if not provided.
            document = client.upload_file(
                file=(file.filename, file_io, 'application/pdf'), # Use the 'file' argument with tuple format
                # display_name=file.filename # Optional: explicitly provide a display name
            )
            print(f"File uploaded successfully to Gemini File API: {document.name}") # Log for debugging
            # Note: client.upload_file returns a google.generativeai.types.File object
            # which contains the resource name (e.g., 'files/xyz123').
        except Exception as upload_error:
             # Attempt to provide more specific feedback if possible
             error_msg = str(upload_error)
             print(f"Error uploading file to Gemini API: {error_msg}")
             # Check for common upload errors like exceeding file size limits
             if "file content size exceeds limit" in error_msg.lower():
                  return jsonify({'success': False, 'error': f'Error uploading file: File size exceeds API limit. {error_msg}'}), 413 # 413 Payload Too Large
             return jsonify({'success': False, 'error': f'Error uploading file to Gemini API: {error_msg}'}), 500
        # --- END CORRECTED FILE UPLOAD CALL ---

        # Create cache with system instruction
        cache = None # Initialize cache variable
        try:
            system_instruction = "You are an expert document analyzer. Provide detailed, accurate answers based on the uploaded document content. Always be helpful and thorough in your responses."

            # Use the correct model format as per documentation
            # Using a specific stable version is recommended for production
            model = 'models/gemini-2.0-flash-001'

            print(f"Attempting to create cache for file: {document.name}") # Log
            cache = client.caches.create(
                model=model,
                config=types.CreateCachedContentConfig(
                    display_name=f'pdf document cache: {file.filename}', # Use filename in display_name
                    system_instruction=system_instruction,
                    contents=[document], # contents should be a list of content parts. document is already a File object, which is a valid content part type.
                    ttl="3600s",  # 1 hour TTL. Use string format like "300s" or "1h".
                )
            )
            print(f"Cache created successfully: {cache.name}") # Log

            # Store cache info in our in-memory dictionary
            # We map our internal UUID cache_id to the Gemini API's cache.name (resource name)
            cache_id = str(uuid.uuid4())
            document_caches[cache_id] = {
                'gemini_cache_name': cache.name, # Store the Gemini API resource name
                'document_name': file.filename,
                'gemini_file_name': document.name, # Also store the Gemini File API resource name for cleanup
                'created_at': datetime.now().isoformat(),
                'expires_at': (datetime.now(timezone.utc) + timedelta(seconds=3600)).isoformat(), # Store expiry time for reference
            }

            # Get token count from cache metadata if available
            # Note: cached_token_count might be available on the cache object after creation
            token_count = 'Unknown'
            if hasattr(cache, 'usage_metadata') and cache.usage_metadata:
                 token_count = getattr(cache.usage_metadata, 'cached_token_count', 'Unknown')
                 print(f"Cached token count: {token_count}")


            return jsonify({
                'success': True,
                'cache_id': cache_id, # Return our internal ID
                'token_count': token_count
            })

        except Exception as cache_error:
            error_msg = str(cache_error)
            print(f"Cache creation failed: {error_msg}") # Log the cache error
            # If caching fails, attempt to delete the uploaded file to clean up.
            if document and hasattr(document, 'name'):
                 try:
                     client.files.delete(document.name)
                     print(f"Cleaned up uploaded file {document.name} after caching failure.")
                 except Exception as cleanup_error:
                      print(f"Failed to clean up file {document.name}: {cleanup_error}")

            # Handle specific cache creation errors
            # Note: The exact error message for content size can vary or might not be specific
            # The documentation mentions minimum tokens for caching.
            if "Cached content is too small" in error_msg or "minimum size" in error_msg.lower() or "tokens required" in error_msg.lower():
                 return jsonify({
                     'success': False,
                     'error': f'PDF content is too small for caching. Minimum token count varies by model, but is typically 1024+ for Flash. {error_msg}',
                     'suggestion': 'Try uploading a longer document or combine multiple documents.'
                 }), 400 # 400 Bad Request - client error
            else:
                # Re-raise other unexpected errors or return a generic error
                 return jsonify({'success': False, 'error': f'Error creating cache with Gemini API: {error_msg}'}), 500


    except Exception as e:
        print(f"An unexpected error occurred during upload process: {str(e)}") # Log general errors
        return jsonify({'success': False, 'error': str(e)}), 500

@app.route('/upload-url', methods=['POST'])
def upload_from_url():
    if client is None or api_key is None:
         return jsonify({'success': False, 'error': 'API key not configured or Gemini client failed to initialize.'}), 500

    try:
        data = request.get_json()
        url = data.get('url')

        if not url:
            return jsonify({'success': False, 'error': 'No URL provided'}), 400 # 400 Bad Request

        # Download file from URL
        response = None
        try:
            # Use stream=True for potentially large files, although httpx handles it well.
            # Add a timeout to prevent hanging on unresponsive URLs.
            response = httpx.get(url, follow_redirects=True, timeout=30.0)
            response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)

             # Basic check for PDF mime type (optional but good practice)
            content_type = response.headers.get('Content-Type', '').lower()
            if 'application/pdf' not in content_type:
                 print(f"Warning: URL content type is not application/pdf: {content_type}")
                 # Decide if you want to block non-PDFs or try to upload anyway
                 # For now, we'll proceed but log a warning. API might reject it.
                 # If strictly PDF required, return an error here:
                 # return jsonify({'success': False, 'error': f'URL does not point to a PDF document (Content-Type: {content_type})'}), 415 # 415 Unsupported Media Type


        except httpx.HTTPStatusError as e:
             print(f"HTTP error downloading file from URL {url}: {e.response.status_code} - {e.response.text}")
             return jsonify({'success': False, 'error': f'HTTP error downloading file from URL: {e.response.status_code} - {e.response.text}'}), e.response.status_code
        except httpx.RequestError as e:
             print(f"Error downloading file from URL {url}: {e}")
             return jsonify({'success': False, 'error': f'Error downloading file from URL: {e}'}), 500


        file_io = io.BytesIO(response.content)

        # --- CORRECTED FILE UPLOAD CALL ---
        # Upload to Gemini File API using the correct method client.upload_file
        # Pass the file content as a tuple (filename, file-like object, mime_type)
        # Use a generic filename for the file-like object if none derived from URL
        document = None # Initialize document variable
        try:
            # Attempt to get filename from URL or headers, otherwise use generic
            filename = os.path.basename(url)
            if not filename or '.' not in filename:
                 filename = 'downloaded_document.pdf' # Default generic name

            # Use the mime type from the response headers if available and looks right
            mime_type = content_type if 'application/pdf' in content_type else 'application/pdf'


            document = client.upload_file(
                file=(filename, file_io, mime_type), # Use parsed filename and mime_type
                display_name=url # Use the URL as display name in Gemini API
            )
            print(f"File from URL uploaded successfully to Gemini File API: {document.name}") # Log
             # Note: client.upload_file returns a google.generativeai.types.File object
             # which contains the resource name (e.g., 'files/xyz123').

        except Exception as upload_error:
             # Attempt to provide more specific feedback if possible
             error_msg = str(upload_error)
             print(f"Error uploading file from URL to Gemini API: {error_msg}")
             # Check for common upload errors like exceeding file size limits
             if "file content size exceeds limit" in error_msg.lower():
                  return jsonify({'success': False, 'error': f'Error uploading file: File size exceeds API limit. {error_msg}'}), 413 # 413 Payload Too Large
             return jsonify({'success': False, 'error': f'Error uploading file from URL to Gemini API: {error_msg}'}), 500
        # --- END CORRECTED FILE UPLOAD CALL ---


        # Create cache with system instruction
        cache = None # Initialize cache variable
        try:
            system_instruction = "You are an expert document analyzer. Provide detailed, accurate answers based on the uploaded document content. Always be helpful and thorough in your responses."

            # Use the correct model format as per documentation
            model = 'models/gemini-2.0-flash-001'

            print(f"Attempting to create cache for file: {document.name}") # Log
            cache = client.caches.create(
                model=model,
                config=types.CreateCachedContentConfig(
                    display_name=f'pdf document cache: {url}', # Use URL in display_name for cache
                    system_instruction=system_instruction,
                    contents=[document], # contents should be a list containing the File object
                    ttl="3600s",  # 1 hour TTL. Use string format like "300s" or "1h".
                )
            )
            print(f"Cache created successfully: {cache.name}") # Log


            # Store cache info in our in-memory dictionary
            # We map our internal UUID cache_id to the Gemini API's cache.name (resource name)
            cache_id = str(uuid.uuid4())
            document_caches[cache_id] = {
                'gemini_cache_name': cache.name, # Store the Gemini API resource name
                'document_name': url, # Store the URL as the document name
                'gemini_file_name': document.name, # Also store the Gemini File API resource name for cleanup
                'created_at': datetime.now().isoformat(),
                 'expires_at': (datetime.now(timezone.utc) + timedelta(seconds=3600)).isoformat(), # Store expiry time for reference
            }

            # Get token count from cache metadata if available
            token_count = 'Unknown'
            if hasattr(cache, 'usage_metadata') and cache.usage_metadata:
                 token_count = getattr(cache.usage_metadata, 'cached_token_count', 'Unknown')
                 print(f"Cached token count: {token_count}")


            return jsonify({
                'success': True,
                'cache_id': cache_id, # Return our internal ID
                'token_count': token_count
            })

        except Exception as cache_error:
            error_msg = str(cache_error)
            print(f"Cache creation failed: {error_msg}") # Log the cache error
            # If caching fails, attempt to delete the uploaded file to clean up.
            if document and hasattr(document, 'name'):
                 try:
                     client.files.delete(document.name)
                     print(f"Cleaned up uploaded file {document.name} after caching failure.")
                 except Exception as cleanup_error:
                      print(f"Failed to clean up file {document.name}: {cleanup_error}")

            # Handle specific cache creation errors
            if "Cached content is too small" in error_msg or "minimum size" in error_msg.lower() or "tokens required" in error_msg.lower():
                 return jsonify({
                     'success': False,
                     'error': f'PDF content is too small for caching. Minimum token count varies by model, but is typically 1024+ for Flash. {error_msg}',
                     'suggestion': 'Try uploading a longer document or combine multiple documents.'
                 }), 400 # 400 Bad Request - client error
            else:
                # Re-raise other unexpected errors or return a generic error
                 return jsonify({'success': False, 'error': f'Error creating cache with Gemini API: {error_msg}'}), 500


    except Exception as e:
        print(f"An unexpected error occurred during URL upload process: {str(e)}") # Log general errors
        return jsonify({'success': False, 'error': str(e)}), 500


@app.route('/ask', methods=['POST'])
def ask_question():
    if client is None or api_key is None:
         return jsonify({'success': False, 'error': 'API key not configured or Gemini client failed to initialize.'}), 500

    try:
        data = request.get_json()
        question = data.get('question')
        cache_id = data.get('cache_id')

        if not question or not cache_id:
            return jsonify({'success': False, 'error': 'Missing question or cache_id'}), 400 # 400 Bad Request

        # --- CORRECTED CACHE LOOKUP ---
        # Check if our internal cache_id exists in the in-memory dictionary
        if cache_id not in document_caches:
            # If not found, it's either an invalid ID, expired, or the server restarted.
            # For this simple demo, we treat it as unavailable.
            print(f"Cache ID {cache_id} not found in local storage.")
            return jsonify({'success': False, 'error': 'Cache not found or expired. Please upload the document again.'}), 404 # 404 Not Found

        # If found, retrieve the Gemini API cache name
        cache_info = document_caches[cache_id]
        gemini_cache_name = cache_info['gemini_cache_name']
        print(f"Using Gemini cache name: {gemini_cache_name} for question.")
        # --- END CORRECTED CACHE LOOKUP ---

        # Generate response using cached content with correct model format
        response = client.models.generate_content(
            model='models/gemini-2.0-flash-001', # Ensure using the model the cache was created with
            contents=[{'text': question}], # User's question as text content part
            generation_config=types.GenerateContentConfig(
                cached_content=gemini_cache_name # Use the retrieved Gemini cache name
            )
        )

        # Check if response has parts before accessing .text
        answer = "Could not generate response from the model."
        if response and response.candidates:
            # Handle potential tool_code or other non-text parts if necessary
            answer_parts = []
            for candidate in response.candidates:
                if candidate.content and candidate.content.parts:
                    for part in candidate.content.parts:
                        if hasattr(part, 'text'):
                             answer_parts.append(part.text)
                        # Add handling for other part types if needed (e.g., tool_code, function_response)
                        # elif hasattr(part, 'tool_code'):
                        #      answer_parts.append(f"\n```tool_code\n{part.tool_code.code}\n```\n")
                        # elif hasattr(part, 'function_response'):
                        #      answer_parts.append(f"\n```function_response\n{json.dumps(part.function_response, indent=2)}\n```\n")
            if answer_parts:
                 answer = "".join(answer_parts)
            else:
                 # Handle cases where candidates exist but have no text parts (e.g., tool calls)
                 answer = "Model returned content without text parts (e.g., tool calls)."
                 print(f"Model returned non-text parts: {response.candidates}") # Log for debugging

        elif response and response.prompt_feedback and response.prompt_feedback.block_reason:
             # Handle cases where the prompt was blocked
             block_reason = response.prompt_feedback.block_reason.name
             block_message = getattr(response.prompt_feedback, 'block_reason_message', 'No message provided')
             answer = f"Request blocked by safety filters. Reason: {block_reason}. Message: {block_message}"
             print(f"Request blocked: {block_reason} - {block_message}")

        else:
             # Handle other unexpected response structures
             print(f"Unexpected response structure from API: {response}")
             # answer stays as the initial "Could not generate response..." message

        return jsonify({
            'success': True,
            'answer': answer
        })

    except Exception as e:
        print(f"An error occurred during question asking: {str(e)}") # Log errors
        # Attempt to provide more specific API error messages
        error_msg = str(e)
        if "Resource has been exhausted" in error_msg:
             error_msg = "API rate limit or quota exceeded. Please try again later."
        elif "cached_content refers to a resource that has been deleted" in error_msg:
             error_msg = "The cached document has expired or was deleted from Gemini API. Please upload the document again."
             # Clean up local entry if API confirms deletion/expiry
             if cache_id in document_caches:
                  print(f"Removing local entry for cache_id {cache_id} as API confirmed deletion.")
                  del document_caches[cache_id]
        elif "invalid cached_content value" in error_msg:
             error_msg = "Invalid cache reference. The cached document might have expired or been deleted. Please upload the document again."
             # Clean up local entry if API confirms deletion/expiry
             if cache_id in document_caches:
                  print(f"Removing local entry for cache_id {cache_id} as API confirmed deletion (invalid reference).")
                  del document_caches[cache_id]
        elif "model does not exist" in error_msg:
             error_msg = "The specified model is not available."


        return jsonify({'success': False, 'error': f'Error from Gemini API: {error_msg}'}), 500 # 500 Internal Server Error


@app.route('/caches', methods=['GET'])
def list_caches():
    # Lists caches stored *in this application's memory*.
    # It does NOT list caches directly from the Gemini API unless you add that logic.
    try:
        caches = []
        for cache_id, cache_info in list(document_caches.items()): # Use list() to iterate safely if modification occurs during iteration
             # Optional: Check if the cache still exists in Gemini API before listing
             # This adds complexity and potential API calls, so skipping for simple demo
             try:
                  # Attempt to get cache metadata from API to confirm existence/details
                  api_cache_info = client.caches.get(name=cache_info['gemini_cache_name'])
                  # If successful, add to list
                  caches.append({
                       'cache_id': cache_id, # Our internal ID
                       'document_name': cache_info['document_name'],
                       'gemini_cache_name': cache_info['gemini_cache_name'], # Include Gemini name
                       'created_at': cache_info['created_at'],
                       'expires_at': getattr(api_cache_info, 'expire_time', 'Unknown'), # Get actual expiry from API
                       'cached_token_count': getattr(api_cache_info.usage_metadata, 'cached_token_count', 'Unknown') if hasattr(api_cache_info, 'usage_metadata') else 'Unknown'
                  })
             except Exception as e:
                  # If API lookup fails (e.g., cache expired/deleted), remove from our local map
                  print(f"Gemini cache {cache_info['gemini_cache_name']} for local ID {cache_id} not found via API. Removing from local storage. Error: {e}")
                  del document_caches[cache_id]
                  # Don't add it to the list of active caches

        return jsonify({'success': True, 'caches': caches})

    except Exception as e:
        print(f"An error occurred listing caches: {str(e)}")
        return jsonify({'success': False, 'error': str(e)})


@app.route('/cache/<cache_id>', methods=['DELETE'])
def delete_cache(cache_id):
    if client is None or api_key is None:
         return jsonify({'success': False, 'error': 'API key not configured or Gemini client failed to initialize.'}), 500

    try:
        if cache_id not in document_caches:
            return jsonify({'success': False, 'error': 'Cache not found'}), 404 # 404 Not Found

        cache_info = document_caches[cache_id]
        gemini_cache_name_to_delete = cache_info['gemini_cache_name']
        gemini_file_name_to_delete = cache_info['gemini_file_name']


        # Delete from Gemini API Cache Service
        try:
            client.caches.delete(gemini_cache_name_to_delete)
            print(f"Gemini cache deleted: {gemini_cache_name_to_delete}") # Log
        except Exception as delete_error:
             error_msg = str(delete_error)
             print(f"Error deleting Gemini cache {gemini_cache_name_to_delete}: {error_msg}") # Log
             # Handle case where the cache was already gone (e.g. expired)
             if "Resource not found" in error_msg:
                  print(f"Gemini cache {gemini_cache_name_to_delete} already gone from API.")
             else:
                  # For other errors, you might want to stop and return the error
                  return jsonify({'success': False, 'error': f'Failed to delete cache from API: {error_msg}'}), 500


        # Also delete the associated file from Gemini File API to free up storage
        if gemini_file_name_to_delete:
             try:
                 client.files.delete(gemini_file_name_to_delete)
                 print(f"Associated Gemini file deleted: {gemini_file_name_to_delete}") # Log
             except Exception as file_delete_error:
                 error_msg = str(file_delete_error)
                 print(f"Error deleting Gemini file {gemini_file_name_to_delete}: {error_msg}") # Log
                 if "Resource not found" in error_msg:
                      print(f"Gemini file {gemini_file_name_to_delete} already gone from API.")
                 else:
                      # Log but continue, deleting the cache is the primary goal
                      pass


        # Remove from local storage *after* attempting API deletion
        del document_caches[cache_id]
        print(f"Local cache entry deleted for ID: {cache_id}") # Log

        return jsonify({'success': True, 'message': 'Cache and associated file deleted successfully'})

    except Exception as e:
        print(f"An unexpected error occurred during cache deletion process: {str(e)}") # Log
        return jsonify({'success': False, 'error': str(e)}), 500


if __name__ == '__main__':
    import os
    port = int(os.environ.get("PORT", 7860))
    print(f"Starting Flask app on port {port}") # Log start
    # In production, set debug=False
    # Use threaded=True or a production WSGI server (like Gunicorn) for concurrent requests
    app.run(debug=True, host='0.0.0.0', port=port, threaded=True)