File size: 42,397 Bytes
93d997f
 
 
c41ebc7
93d997f
 
 
 
 
 
 
 
 
 
 
b62495b
 
 
 
 
 
 
93d997f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ff7ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd1c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ff7ad
dcd1c9e
c4ff7ad
 
 
 
 
 
 
 
 
 
 
 
 
dcd1c9e
c4ff7ad
 
 
 
 
 
 
 
 
 
 
ab9c9c5
c4ff7ad
ab9c9c5
 
c4ff7ad
ab9c9c5
c4ff7ad
 
 
ab9c9c5
 
dcd1c9e
 
 
c4ff7ad
ab9c9c5
dcd1c9e
 
 
 
 
 
 
 
 
 
 
 
c4ff7ad
 
ab9c9c5
c4ff7ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
 
 
 
 
 
 
 
c41ebc7
93d997f
 
c41ebc7
93d997f
 
 
 
 
 
c41ebc7
 
 
 
 
 
93d997f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c41ebc7
93d997f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd1c9e
 
 
 
93d997f
 
 
 
 
 
 
 
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
b62495b
93d997f
b62495b
 
 
93d997f
b62495b
93d997f
b62495b
 
 
 
 
 
 
93d997f
b62495b
 
 
 
 
 
93d997f
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd1c9e
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd1c9e
b62495b
 
 
 
 
 
 
 
 
dcd1c9e
 
b62495b
 
 
 
 
 
dcd1c9e
b62495b
 
 
 
 
 
 
dcd1c9e
b62495b
dcd1c9e
b62495b
 
 
dcd1c9e
b62495b
 
 
 
dcd1c9e
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
b62495b
93d997f
b62495b
 
 
 
 
93d997f
b62495b
 
 
 
 
93d997f
b62495b
 
 
 
 
c4ff7ad
b62495b
 
 
 
93d997f
b62495b
 
 
 
93d997f
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
b62495b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d997f
 
b62495b
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
import gradio as gr
import os
from pathlib import Path
from pinecone import Pinecone
from typing import List, Tuple
import tempfile
import shutil
from dotenv import load_dotenv
import time
from datetime import datetime
import json

# Load environment variables
load_dotenv()

# Authentication configuration
AUTH_PASSWORD = "gst_magic@##56$$"

def authenticate(password):
    """Simple authentication function"""
    return password == AUTH_PASSWORD

# Validate required environment variables
required_env_vars = ["PINECONE_API_KEY"]
missing_vars = [var for var in required_env_vars if not os.getenv(var)]

if missing_vars:
    raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")

# Initialize Pinecone
pinecone_api_key = os.getenv("PINECONE_API_KEY")
pc = Pinecone(api_key=pinecone_api_key)

# Create uploads directory
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

def parse_pinecone_timestamp(iso_string):
    """
    Parses an ISO 8601 string from Pinecone, handling nanosecond precision.
    
    Args:
        iso_string (str): The ISO-formatted timestamp string.
        
    Returns:
        datetime: The parsed datetime object.
    """
    if not isinstance(iso_string, str) or not iso_string:
        return datetime.min # Return a very old date for sorting purposes

    # Handle ISO strings ending with 'Z' (convert to +00:00)
    if iso_string.endswith('Z'):
        iso_string = iso_string[:-1] + '+00:00'
    
    # Find the decimal point for fractional seconds
    decimal_point = iso_string.find('.')
    
    if decimal_point != -1:
        # Find where the timezone info starts (+ or -)
        tz_start = max(iso_string.rfind('+'), iso_string.rfind('-'))
        
        if tz_start > decimal_point:
            # Extract the fractional seconds part
            fractional_part = iso_string[decimal_point+1:tz_start]
            
            # Truncate to 6 digits (microseconds) if longer
            if len(fractional_part) > 6:
                fractional_part = fractional_part[:6]
            
            # Reconstruct the ISO string with truncated fractional seconds
            iso_string = iso_string[:decimal_point+1] + fractional_part + iso_string[tz_start:]
    
    return datetime.fromisoformat(iso_string)

def get_all_files():
    """Get all files from Pinecone Assistant and sort them"""
    try:
        assistant_name = os.getenv("PINECONE_ASSISTANT_NAME", "gstminutes")
        assistant = pc.assistant.Assistant(assistant_name=assistant_name)
        
        # List all files in the assistant
        files_response = assistant.list_files()
        
        # Check if files_response is directly a list or has a files attribute
        if hasattr(files_response, 'files'):
            files_list = files_response.files
        else:
            files_list = files_response
        
        if not files_list:
            return []
        
        # Sort files by creation time (most recent first) using robust timestamp parsing
        sorted_files = sorted(
            files_list, 
            key=lambda x: parse_pinecone_timestamp(getattr(x, 'created_on', '')), 
            reverse=True
        )
        
        return sorted_files
        
    except Exception as e:
        return []

def get_file_choices():
    """Get file choices for the dropdown - returns list of (title, file_id) tuples"""
    try:
        all_files = get_all_files()
        if not all_files:
            return []
        
        choices = []
        for file_obj in all_files:
            file_name = getattr(file_obj, 'name', 'Unknown File')
            file_id = getattr(file_obj, 'id', 'unknown')
            created_on = getattr(file_obj, 'created_on', '')
            
            # Format timestamp for display
            try:
                if created_on:
                    created_formatted = parse_pinecone_timestamp(created_on).strftime('%Y-%m-%d %H:%M')
                    display_name = f"{file_name} (uploaded: {created_formatted})"
                else:
                    display_name = file_name
            except:
                display_name = file_name
            
            choices.append((display_name, file_id))
        
        return choices
    except Exception as e:
        return []

def refresh_delete_dropdown():
    """Refresh the dropdown with current files"""
    choices = get_file_choices()
    if not choices:
        return gr.update(choices=[], value=None, interactive=False)
    return gr.update(choices=choices, value=None, interactive=True)

def delete_selected_files(selected_file_ids, progress=gr.Progress()):
    """Delete multiple selected files by their IDs"""
    if not selected_file_ids:
        return "❌ **Error:** No files selected for deletion", ""
    
    try:
        progress(0.1, desc="πŸ”§ Initializing Pinecone Assistant...")
        assistant_name = os.getenv("PINECONE_ASSISTANT_NAME", "gstminutes")
        assistant = pc.assistant.Assistant(assistant_name=assistant_name)
        
        # Get current files to map IDs to names
        all_files = get_all_files()
        file_id_to_name = {getattr(f, 'id', ''): getattr(f, 'name', 'Unknown') for f in all_files}
        
        total_files = len(selected_file_ids)
        deleted_files = []
        failed_files = []
        
        progress(0.2, desc=f"πŸ—‘οΈ Starting deletion of {total_files} files...")
        
        for i, file_id in enumerate(selected_file_ids):
            try:
                file_name = file_id_to_name.get(file_id, f"File ID: {file_id}")
                progress((0.2 + (i / total_files) * 0.7), desc=f"πŸ—‘οΈ Deleting: {file_name}...")
                
                # Delete the file
                response = assistant.delete_file(file_id=file_id)
                deleted_files.append({
                    'name': file_name,
                    'id': file_id,
                    'status': 'success'
                })
                
                time.sleep(0.2)  # Small delay between deletions
                
            except Exception as delete_error:
                failed_files.append({
                    'name': file_id_to_name.get(file_id, f"File ID: {file_id}"),
                    'id': file_id,
                    'error': str(delete_error)
                })
        
        progress(1.0, desc="βœ… Deletion process completed!")
        
        # Format results
        success_count = len(deleted_files)
        error_count = len(failed_files)
        
        status_message = f"πŸ“Š **Deletion Complete**\n\n"
        status_message += f"βœ… **Successfully deleted:** {success_count} files\n"
        status_message += f"❌ **Failed to delete:** {error_count} files\n"
        status_message += f"πŸ“ **Total processed:** {total_files} files\n\n"
        
        # Detailed results
        detailed_results = "## πŸ—‘οΈ **Deletion Results**\n\n"
        
        if deleted_files:
            detailed_results += "### βœ… **Successfully Deleted Files:**\n"
            for file_info in deleted_files:
                detailed_results += f"- **{file_info['name']}** (`{file_info['id']}`)\n"
            detailed_results += "\n"
        
        if failed_files:
            detailed_results += "### ❌ **Failed Deletions:**\n"
            for file_info in failed_files:
                detailed_results += f"- **{file_info['name']}** (`{file_info['id']}`)\n"
                detailed_results += f"  - Error: {file_info['error']}\n"
            detailed_results += "\n"
        
        return status_message, detailed_results
        
    except Exception as e:
        error_msg = f"❌ **Critical Error during deletion:** {str(e)}"
        return error_msg, ""

def list_uploaded_files_paginated(page_num=0, progress=gr.Progress()):
    """List files with pagination - 100 files per page"""
    try:
        progress(0.1, desc="πŸ” Getting files...")
        
        # Get all files
        all_files = get_all_files()
        
        if not all_files:
            progress(1.0, desc="βœ… Complete - No files found")
            return "πŸ“‹ **No files found in Pinecone Assistant**", "", "No files available", gr.update(visible=False), gr.update(visible=False)
        
        progress(0.5, desc="πŸ“Š Processing page...")
        
        # Pagination settings
        files_per_page = 100
        start_idx = page_num * files_per_page
        end_idx = start_idx + files_per_page
        
        # Get files for current page
        page_files = all_files[start_idx:end_idx]
        total_files = len(all_files)
        total_pages = (total_files + files_per_page - 1) // files_per_page
        
        # Create summary
        summary = f"πŸ“Š **Files Summary (Page {page_num + 1} of {total_pages})**\n\n"
        summary += f"πŸ“ **Total files:** {total_files}\n"
        summary += f"πŸ“‹ **Showing:** {start_idx + 1}-{min(end_idx, total_files)} of {total_files}\n\n"
        
        # Create file list - ONLY TITLES, VERTICALLY
        detailed_info = f"## πŸ“‹ **Latest Uploaded Files - Page {page_num + 1}**\n\n"
        
        progress(0.8, desc="πŸ“ Formatting file titles...")
        
        for i, file_obj in enumerate(page_files, 1):
            try:
                # Get only the file name/title
                file_name = getattr(file_obj, 'name', 'Unknown File')
                file_id = getattr(file_obj, 'id', 'Unknown ID')
                created_on = getattr(file_obj, 'created_on', '')
                
                global_index = start_idx + i
                
                # Format timestamp
                try:
                    if created_on:
                        created_formatted = parse_pinecone_timestamp(created_on).strftime('%Y-%m-%d %H:%M')
                    else:
                        created_formatted = 'Unknown'
                except:
                    created_formatted = 'Unknown'
                
                # Display file info
                detailed_info += f"{global_index}. **{file_name}**\n"
                detailed_info += f"   πŸ“… Uploaded: {created_formatted} | πŸ†” ID: `{file_id}`\n\n"
                
            except Exception as file_error:
                detailed_info += f"{start_idx + i}. ❌ **Error loading file**\n\n"
        
        # Pagination info
        pagination_info = f"πŸ“„ Page {page_num + 1} of {total_pages} | Total: {total_files} files"
        
        # Show/hide next/prev buttons
        show_prev = page_num > 0
        show_next = page_num < total_pages - 1
        
        progress(1.0, desc="βœ… Page loaded successfully!")
        
        return summary, detailed_info, pagination_info, gr.update(visible=show_prev), gr.update(visible=show_next)
        
    except Exception as e:
        error_msg = f"❌ **Error retrieving file list:** {str(e)}"
        return error_msg, "", "Error", gr.update(visible=False), gr.update(visible=False)

def load_next_page(current_page_info):
    """Load next page of files"""
    try:
        # Extract current page number from pagination info
        current_page = int(current_page_info.split("Page ")[1].split(" of")[0]) - 1
        return list_uploaded_files_paginated(current_page + 1)
    except:
        return list_uploaded_files_paginated(0)

def load_prev_page(current_page_info):
    """Load previous page of files"""
    try:
        # Extract current page number from pagination info
        current_page = int(current_page_info.split("Page ")[1].split(" of")[0]) - 1
        return list_uploaded_files_paginated(max(0, current_page - 1))
    except:
        return list_uploaded_files_paginated(0)

def refresh_file_list():
    """Refresh the file list"""
    return "πŸ”„ **Refreshing file list... Please wait**"

def process_files_with_progress(files, *metadata_inputs, progress=gr.Progress()):
    """Process multiple files with individual metadata and show progress"""
    if not files:
        return "❌ Error: No files selected", ""
    
    if len(files) > 10:
        return "❌ Error: Maximum 10 files allowed at a time", ""
    
    try:
        results = []
        errors = []
        total_files = len(files)
        
        # Initialize Pinecone Assistant
        progress(0, desc="πŸ”§ Initializing Pinecone Assistant...")
        time.sleep(0.5)  # Small delay to show the progress
        assistant_name = os.getenv("PINECONE_ASSISTANT_NAME", "gstminutes")
        assistant = pc.assistant.Assistant(assistant_name=assistant_name)

        # Process each file with its individual metadata
        for i, file_path in enumerate(files):
            try:
                filename = os.path.basename(file_path)
                progress((i / total_files), desc=f"πŸ“„ Processing {filename}... ({i+1}/{total_files})")
                
                # Get metadata for this specific file (3 fields per file: sections, keywords, description)
                sections_idx = i * 3
                keywords_idx = i * 3 + 1
                description_idx = i * 3 + 2
                
                if sections_idx < len(metadata_inputs):
                    sections = metadata_inputs[sections_idx] or ""
                    keywords = metadata_inputs[keywords_idx] or ""
                    description = metadata_inputs[description_idx] or ""
                else:
                    sections = keywords = description = ""
                
                # Skip if no metadata provided for this file
                if not sections.strip() and not keywords.strip() and not description.strip():
                    errors.append({
                        "filename": filename,
                        "error": "❌ Error: No metadata provided"
                    })
                    continue
                
                # Prepare metadata for this file
                progress((i / total_files), desc=f"🏷️ Preparing metadata for {filename}...")
                metadata = {
                    "sections": [s.strip() for s in sections.split(",") if s.strip()],
                    "keywords": [k.strip() for k in keywords.split(",") if k.strip()],
                    "description": description.strip()
                }
                
                # Copy to uploads directory
                progress((i / total_files), desc=f"πŸ“ Copying {filename} to uploads...")
                destination_path = os.path.join(UPLOAD_FOLDER, filename)
                shutil.copy2(file_path, destination_path)

                # Upload to Pinecone Assistant
                progress((i / total_files), desc=f"☁️ Uploading {filename} to Pinecone...")
                response = assistant.upload_file(
                    file_path=destination_path, 
                    metadata=metadata, 
                    timeout=None
                )

                results.append({
                    "filename": filename,
                    "status": "βœ… Success",
                    "metadata": metadata,
                    "response": str(response)
                })

            except Exception as file_error:
                errors.append({
                    "filename": os.path.basename(file_path),
                    "error": f"❌ Error: {str(file_error)}"
                })

        # Final progress update
        progress(1.0, desc="βœ… Processing complete!")
        time.sleep(0.5)

        # Format results for display
        success_count = len(results)
        error_count = len(errors)
        
        status_message = f"πŸ“Š **Processing Complete**\n\n"
        status_message += f"βœ… **Successful uploads:** {success_count}\n"
        status_message += f"❌ **Failed uploads:** {error_count}\n"
        status_message += f"πŸ“ **Total files processed:** {len(files)}\n\n"
        
        # Detailed results
        detailed_results = "## πŸ“‹ **Detailed Results**\n\n"
        
        if results:
            detailed_results += "### βœ… **Successful Uploads:**\n"
            for result in results:
                detailed_results += f"- **{result['filename']}**\n"
                detailed_results += f"  - Sections: {', '.join(result['metadata']['sections'])}\n"
                detailed_results += f"  - Keywords: {', '.join(result['metadata']['keywords'])}\n"
                detailed_results += f"  - Description: {result['metadata']['description']}\n\n"
        
        if errors:
            detailed_results += "### ❌ **Failed Uploads:**\n"
            for error in errors:
                detailed_results += f"- **{error['filename']}** - {error['error']}\n"
        
        return status_message, detailed_results, "βœ… **Processing completed successfully!**"
    
    except Exception as e:
        error_msg = f"❌ **Critical Error:** {str(e)}"
        return error_msg, "", "❌ **Processing failed with error**"

def update_metadata_fields(files):
    """Update metadata fields based on uploaded files"""
    if not files:
        return [gr.update(visible=False)] * 30  # Hide all fields
    
    if len(files) > 10:
        # Show error and hide all fields
        return [gr.update(visible=False)] * 30
    
    updates = []
    for i in range(len(files)):
        if i < len(files):
            filename = os.path.basename(files[i])
            # Show 3 fields per file (sections, keywords, description)
            updates.extend([
                gr.update(visible=True, label=f"πŸ“‘ Sections for {filename}", placeholder="e.g., Introduction, Financial Data, Compliance"),
                gr.update(visible=True, label=f"πŸ” Keywords for {filename}", placeholder="e.g., GST, tax, compliance, revenue"),
                gr.update(visible=True, label=f"πŸ“ Description for {filename}", placeholder="Brief description of this document")
            ])
        
    # Hide remaining fields
    while len(updates) < 30:
        updates.append(gr.update(visible=False))
    
    return updates[:30]

def clear_form():
    """Clear all form fields"""
    return [None] + [""] * 30 + ["", "", "🟒 **Ready to process documents**"]

def clear_delete_form():
    """Clear delete form"""
    return gr.update(value=[]), "", ""

def start_processing():
    """Show processing started status"""
    return "πŸ”„ **Processing documents... Please wait**"

def finish_processing():
    """Show processing finished status"""
    return "βœ… **Processing completed successfully!**"

def create_main_interface():
    """Create the main application interface"""
    with gr.Blocks(
        title="πŸ“„ Tax Document Ingestion System",
        theme=gr.themes.Soft(),
        css="""
        .gradio-container {
            max-width: 1400px !important;
            margin: auto;
        }
        .upload-container {
            border: 2px dashed #4CAF50;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            background-color: #f8f9fa;
        }
        .delete-container {
            border: 2px dashed #f44336;
            border-radius: 10px;
            padding: 20px;
            background-color: #ffebee;
        }
        .tab-nav {
            margin-bottom: 20px;
        }
        """
    ) as app:
        
        gr.Markdown(
            """
            # πŸ“„ Tax Document Ingestion System
            
            Upload, manage, and delete documents in the Pinecone Assistant for GST Minutes processing.
            
            ## πŸš€ Features:
            - βœ… **Multiple file upload** - Select and upload multiple documents at once
            - 🏷️ **Metadata tagging** - Add sections, keywords, and descriptions
            - πŸ”„ **Batch processing** - All files processed with individual metadata
            - πŸ—‘οΈ **File deletion** - Delete multiple files by selecting from dropdown
            - πŸ“Š **File management** - View uploaded files with timestamps and metadata
            - πŸ“‹ **Detailed reporting** - See success/failure status for each operation
            
            ---
            """
        )
        
        # Create tabs for different functionalities
        with gr.Tabs() as tabs:
            
            # Tab 1: Upload Documents
            with gr.TabItem("πŸ“€ Upload Documents", id="upload_tab"):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.Markdown("### πŸ“ **File Upload**")
                        files_input = gr.File(
                            label="Select Documents (Max 10 files)",
                            file_count="multiple",
                            file_types=[".pdf", ".doc", ".docx", ".txt"],
                            elem_classes=["upload-container"]
                        )
                        
                    with gr.Column(scale=1):
                        gr.Markdown("### 🏷️ **Document Metadata (Individual for Each File)**")
                        gr.Markdown("*Upload files first, then metadata fields will appear for each document*")
                
                # Dynamic metadata fields container
                with gr.Column() as metadata_container:
                    # Create 30 text fields (enough for 10 files with 3 fields each)
                    metadata_fields = []
                    for i in range(30):
                        field = gr.Textbox(
                            label=f"Field {i}",
                            placeholder="",
                            visible=False,
                            lines=2
                        )
                        metadata_fields.append(field)
                
                with gr.Row():
                    with gr.Column(scale=1):
                        upload_btn = gr.Button(
                            "πŸš€ Upload Documents to Pinecone Assistant",
                            variant="primary",
                            size="lg"
                        )
                        
                    with gr.Column(scale=1):
                        clear_btn = gr.Button(
                            "πŸ—‘οΈ Clear Form",
                            variant="secondary",
                            size="lg"
                        )
                
                # Processing status indicator
                with gr.Row():
                    processing_status = gr.Markdown(
                        value="🟒 **Ready to process documents**",
                        visible=True
                    )
                
                gr.Markdown("---")
                
                # Results section
                with gr.Row():
                    with gr.Column():
                        status_output = gr.Markdown(
                            label="πŸ“Š Upload Status",
                            value="*Ready to upload documents...*"
                        )
                
                with gr.Row():
                    with gr.Column():
                        results_output = gr.Markdown(
                            label="πŸ“‹ Detailed Results",
                            value="",
                            max_height=400  # Add height limit for scrolling
                        )

            # Tab 2: Delete Documents
            with gr.TabItem("πŸ—‘οΈ Delete Documents", id="delete_tab"):
                gr.Markdown("### πŸ—‘οΈ **Delete Multiple Documents**")
                gr.Markdown("Select multiple files from the dropdown to delete them from the Pinecone Assistant.")
                
                with gr.Row():
                    with gr.Column(scale=2):
                        file_dropdown = gr.Dropdown(
                            label="πŸ“‹ Select Files to Delete (Multiple Selection)",
                            choices=[],
                            multiselect=True,
                            interactive=False,
                            elem_classes=["delete-container"]
                        )
                        
                    with gr.Column(scale=1):
                        refresh_dropdown_btn = gr.Button(
                            "πŸ”„ Refresh File List",
                            variant="secondary",
                            size="lg"
                        )
                
                with gr.Row():
                    with gr.Column(scale=1):
                        delete_btn = gr.Button(
                            "πŸ—‘οΈ Delete Selected Files",
                            variant="stop",
                            size="lg"
                        )
                        
                    with gr.Column(scale=1):
                        clear_delete_btn = gr.Button(
                            "β†Ί Clear Selection",
                            variant="secondary",
                            size="lg"
                        )
                
                gr.Markdown("---")
                
                # Delete results section
                with gr.Row():
                    with gr.Column():
                        delete_status_output = gr.Markdown(
                            label="πŸ“Š Deletion Status",
                            value="*Select files to delete...*"
                        )
                
                with gr.Row():
                    with gr.Column():
                        delete_results_output = gr.Markdown(
                            label="πŸ—‘οΈ Deletion Results",
                            value="",
                            max_height=400
                        )
            
            # Tab 3: View Uploaded Files
            with gr.TabItem("πŸ“‹ View Uploaded Files", id="view_tab"):
                gr.Markdown("### πŸ“‹ **Uploaded Files Management**")
                gr.Markdown("View all files currently uploaded to the Pinecone Assistant with their metadata and timestamps.")
                
                with gr.Row():
                    refresh_btn = gr.Button(
                        "πŸ”„ Fetch Files",
                        variant="primary",
                        size="lg"
                    )
                
                # File list status
                with gr.Row():
                    file_list_status = gr.Markdown(
                        value="🟑 **Click 'Fetch Files' to load uploaded files**",
                        visible=True
                    )
                
                gr.Markdown("---")
                
                # Pagination controls
                with gr.Row():
                    prev_btn = gr.Button(
                        "⬅️ Previous 100",
                        variant="secondary",
                        visible=False
                    )
                    pagination_info = gr.Markdown(
                        value="πŸ“„ Page 1 of 1 | Total: 0 files",
                        elem_classes=["pagination-info"]
                    )
                    next_btn = gr.Button(
                        "Next 100 ➑️",
                        variant="secondary", 
                        visible=False
                    )
                
                # File list results
                with gr.Row():
                    with gr.Column(scale=1):
                        file_summary = gr.Markdown(
                            label="πŸ“Š Files Summary",
                            value="*Click refresh to load file summary...*"
                        )
                
                with gr.Row():
                    with gr.Column():
                        file_details = gr.Markdown(
                            label="πŸ“‹ File Details",
                            value="*Click refresh to load file details...*",
                            max_height=600  # Add height limit for scrolling
                        )
        
        # Event handlers for Upload tab
        
        # Update metadata fields when files are uploaded
        files_input.change(
            fn=update_metadata_fields,
            inputs=[files_input],
            outputs=metadata_fields
        )
        
        # Show processing status when upload starts
        upload_btn.click(
            fn=start_processing,
            outputs=[processing_status]
        ).then(
            fn=process_files_with_progress,
            inputs=[files_input] + metadata_fields,
            outputs=[status_output, results_output, processing_status]
        )
        
        clear_btn.click(
            fn=clear_form,
            outputs=[files_input] + metadata_fields + [status_output, results_output, processing_status]
        )
        
        # Event handlers for Delete tab
        
        # Refresh dropdown with current files
        refresh_dropdown_btn.click(
            fn=refresh_delete_dropdown,
            outputs=[file_dropdown]
        )
        
        # Delete selected files
        delete_btn.click(
            fn=delete_selected_files,
            inputs=[file_dropdown],
            outputs=[delete_status_output, delete_results_output]
        )
        
        # Clear delete form
        clear_delete_btn.click(
            fn=clear_delete_form,
            outputs=[file_dropdown, delete_status_output, delete_results_output]
        )
        
        # Event handlers for View Files tab
        
        # Fetch files - load page 1
        refresh_btn.click(
            fn=refresh_file_list,
            outputs=[file_list_status]
        ).then(
            fn=list_uploaded_files_paginated,
            inputs=[],
            outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn]
        )
        
        # Next page button
        next_btn.click(
            fn=load_next_page,
            inputs=[pagination_info],
            outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn]
        )
        
        # Previous page button  
        prev_btn.click(
            fn=load_prev_page,
            inputs=[pagination_info],
            outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn]
        )
        
        # Footer
        gr.Markdown(
            """
            ---
            
            ### πŸ’‘ **Usage Tips:**
            
            **Upload Documents:**
            - Select up to 10 PDF, DOC, DOCX, or TXT files at once
            - Upload files first, then fill individual metadata for each document
            - Each file gets its own sections, keywords, and description
            - Check the results section for upload status
            
            **Delete Documents:**
            - Click 'Refresh File List' to load current files in dropdown
            - Select multiple files using the dropdown (supports multi-select)
            - Click 'Delete Selected Files' to remove them permanently
            - View deletion results for success/failure status
            
            **View Uploaded Files:**
            - Click 'Fetch Files' to see all uploaded files
            - View file details including upload timestamps and metadata
            - Files are sorted by most recent first
            - Use pagination to navigate through large file lists
            
            ### ⚠️ **Important Notes:**
            - File deletion is **permanent** and cannot be undone
            - Always verify your selection before deleting files
            - The system maps file titles to IDs internally for deletion
            
            ### πŸ“ž **Support:**
            For issues or questions, contact the development team.
            """
        )
    
    return app

def create_login_interface():
    """Create the login interface"""
    with gr.Blocks(
        title="πŸ” Authentication Required",
        theme=gr.themes.Soft(),
        css="""
        .gradio-container {
            max-width: 500px !important;
            margin: auto;
            padding-top: 100px;
        }
        .login-container {
            border: 2px solid #2196F3;
            border-radius: 15px;
            padding: 30px;
            text-align: center;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .password-input {
            margin: 20px 0;
        }
        .login-button {
            margin-top: 15px;
        }
        .error-message {
            color: #f44336;
            font-weight: bold;
            margin-top: 10px;
        }
        """
    ) as login_app:
        
        with gr.Column(elem_classes=["login-container"]):
            gr.Markdown(
                """
                # πŸ” **Tax Document System**
                ## **Authentication Required**
                
                Please enter the password to access the application.
                
                ---
                """
            )
            
            password_input = gr.Textbox(
                label="πŸ”‘ Password",
                type="password",
                placeholder="Enter password to access the system",
                elem_classes=["password-input"]
            )
            
            login_btn = gr.Button(
                "πŸš€ Login",
                variant="primary",
                size="lg",
                elem_classes=["login-button"]
            )
            
            error_message = gr.Markdown(
                value="",
                visible=False,
                elem_classes=["error-message"]
            )
            
            gr.Markdown(
                """
                ---
                
                ### πŸ›‘οΈ **Security Notice:**
                - This system contains sensitive tax documentation
                - Authorized access only
                - All activities are logged
                
                ### πŸ“ž **Need Access?**
                Contact your system administrator for credentials.
                """
            )
    
    return login_app, password_input, login_btn, error_message
def main():
    """Main application with authentication β€” single Gradio Blocks app.

    This builds both the login UI and the main application UI inside one
    `gr.Blocks` so we only call `.launch()` once. The main UI is hidden
    until authentication succeeds. This avoids launching two separate
    Gradio servers which was causing the server to close unexpectedly.
    """

    # Reuse same theme/CSS as main app for consistency
    app_css = """
        .gradio-container {
            max-width: 1400px !important;
            margin: auto;
        }
        .upload-container {
            border: 2px dashed #4CAF50;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            background-color: #f8f9fa;
        }
        .delete-container {
            border: 2px dashed #f44336;
            border-radius: 10px;
            padding: 20px;
            background-color: #ffebee;
        }
        .login-container {
            border: 2px solid #2196F3;
            border-radius: 15px;
            padding: 30px;
            text-align: center;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
    """

    with gr.Blocks(title="Tax Document Ingestion System", theme=gr.themes.Soft(), css=app_css) as app:

        # --- LOGIN CONTAINER (visible initially) ---
        with gr.Column(elem_classes=["login-container"]) as login_container:
            gr.Markdown("""
            # Tax Document System
            Authentication required
            """)

            password_input = gr.Textbox(
                label="Password",
                type="password",
                placeholder="Enter password"
            )

            login_btn = gr.Button("Login", variant="primary", size="lg")

            error_message = gr.Markdown(value="", visible=False)

        # --- MAIN APP CONTAINER (hidden until auth) ---
        with gr.Column(visible=False) as main_container:
            # Insert main UI contents (slimmed text, no emojis)
            gr.Markdown(
                """
                # Tax Document Ingestion System

                Upload, manage, and delete documents in the Pinecone Assistant.
                """
            )

            with gr.Tabs() as tabs:
                # Upload Tab
                with gr.TabItem("Upload Documents", id="upload_tab"):
                    with gr.Row():
                        with gr.Column(scale=1):
                            gr.Markdown("### File Upload")
                            files_input = gr.File(
                                label="Select Documents (Max 10 files)",
                                file_count="multiple",
                                file_types=[".pdf", ".doc", ".docx", ".txt"],
                                elem_classes=["upload-container"]
                            )
                        with gr.Column(scale=1):
                            gr.Markdown("### Document Metadata")

                    with gr.Column() as metadata_container:
                        metadata_fields = []
                        for i in range(30):
                            field = gr.Textbox(label=f"Field {i}", placeholder="", visible=False, lines=2)
                            metadata_fields.append(field)

                    with gr.Row():
                        with gr.Column(scale=1):
                            upload_btn = gr.Button("Upload Documents to Pinecone Assistant", variant="primary")
                        with gr.Column(scale=1):
                            clear_btn = gr.Button("Clear Form", variant="secondary")

                    processing_status = gr.Markdown(value="Ready to process documents", visible=True)

                    status_output = gr.Markdown(label="Upload Status", value="Ready to upload documents...")
                    results_output = gr.Markdown(label="Detailed Results", value="", max_height=400)

                # Delete Tab
                with gr.TabItem("Delete Documents", id="delete_tab"):
                    gr.Markdown("### Delete Documents")
                    with gr.Row():
                        with gr.Column(scale=2):
                            file_dropdown = gr.Dropdown(label="Select Files to Delete", choices=[], multiselect=True, interactive=False, elem_classes=["delete-container"])
                        with gr.Column(scale=1):
                            refresh_dropdown_btn = gr.Button("Refresh File List", variant="secondary")

                    with gr.Row():
                        with gr.Column(scale=1):
                            delete_btn = gr.Button("Delete Selected Files", variant="stop")
                        with gr.Column(scale=1):
                            clear_delete_btn = gr.Button("Clear Selection", variant="secondary")

                    delete_status_output = gr.Markdown(label="Deletion Status", value="Select files to delete...")
                    delete_results_output = gr.Markdown(label="Deletion Results", value="", max_height=400)

                # View Tab
                with gr.TabItem("View Uploaded Files", id="view_tab"):
                    gr.Markdown("### Uploaded Files Management")
                    with gr.Row():
                        refresh_btn = gr.Button("Fetch Files", variant="primary")

                    file_list_status = gr.Markdown(value="Click 'Fetch Files' to load uploaded files", visible=True)

                    with gr.Row():
                        prev_btn = gr.Button("Previous 100", variant="secondary", visible=False)
                        pagination_info = gr.Markdown(value="Page 1 of 1 | Total: 0 files")
                        next_btn = gr.Button("Next 100", variant="secondary", visible=False)

                    file_summary = gr.Markdown(label="Files Summary", value="Click refresh to load file summary...")
                    file_details = gr.Markdown(label="File Details", value="Click refresh to load file details...", max_height=600)

        # --- EVENT HANDLERS ---
        # Login handlers: show main_container on success, show error on failure
        def handle_login(password):
            if authenticate(password):
                return gr.update(visible=False), gr.update(value="", visible=False), gr.update(visible=True)
            else:
                return gr.update(visible=True), gr.update(value="Invalid password. Please try again.", visible=True), gr.update(visible=False)

        login_btn.click(fn=handle_login, inputs=[password_input], outputs=[login_container, error_message, main_container])
        password_input.submit(fn=handle_login, inputs=[password_input], outputs=[login_container, error_message, main_container])

        # Wiring existing handlers from earlier in the file
        files_input.change(fn=update_metadata_fields, inputs=[files_input], outputs=metadata_fields)

        upload_btn.click(fn=start_processing, outputs=[processing_status]).then(
            fn=process_files_with_progress,
            inputs=[files_input] + metadata_fields,
            outputs=[status_output, results_output, processing_status]
        )

        clear_btn.click(fn=clear_form, outputs=[files_input] + metadata_fields + [status_output, results_output, processing_status])

        refresh_dropdown_btn.click(fn=refresh_delete_dropdown, outputs=[file_dropdown])
        delete_btn.click(fn=delete_selected_files, inputs=[file_dropdown], outputs=[delete_status_output, delete_results_output])
        clear_delete_btn.click(fn=clear_delete_form, outputs=[file_dropdown, delete_status_output, delete_results_output])

        refresh_btn.click(fn=refresh_file_list, outputs=[file_list_status]).then(
            fn=list_uploaded_files_paginated,
            inputs=[],
            outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn]
        )

        next_btn.click(fn=load_next_page, inputs=[pagination_info], outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn])
        prev_btn.click(fn=load_prev_page, inputs=[pagination_info], outputs=[file_summary, file_details, pagination_info, prev_btn, next_btn])

    # Launch single app
    app.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True, show_error=True)

if __name__ == "__main__":
    main()