File size: 48,165 Bytes
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
8f25c31
cc9e035
50fcf88
 
 
 
 
c23a6c5
50fcf88
cc9e035
8f25c31
ba6e1a9
8f25c31
cc9e035
8f25c31
 
cc9e035
8f25c31
 
c23a6c5
cc9e035
 
 
 
 
c23a6c5
cc9e035
 
 
c23a6c5
cc9e035
8f25c31
 
50fcf88
 
 
 
 
 
 
ba6e1a9
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
c23a6c5
50fcf88
c23a6c5
50fcf88
 
 
 
 
c23a6c5
50fcf88
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a7fd26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50fcf88
 
 
 
 
 
 
 
 
 
 
 
2a7fd26
 
50fcf88
c23a6c5
50fcf88
 
 
c23a6c5
50fcf88
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c60a4f
25c4d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c60a4f
25c4d23
 
 
1c60a4f
8f25c31
 
 
c23a6c5
80f44a7
50fcf88
 
 
 
8f25c31
50fcf88
 
 
 
 
 
 
 
 
 
a30d34a
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f25c31
 
2a7fd26
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
7b1cb2d
 
50fcf88
 
 
 
 
 
 
 
 
 
 
7b1cb2d
c23a6c5
7b1cb2d
 
50fcf88
 
 
 
 
 
 
 
 
 
 
 
2a7fd26
 
50fcf88
2a7fd26
50fcf88
 
 
2a7fd26
 
 
 
 
 
 
 
 
 
 
 
 
 
50fcf88
 
 
 
2a7fd26
50fcf88
 
 
 
 
 
 
2a7fd26
50fcf88
 
 
 
 
 
 
 
a30d34a
 
 
 
50fcf88
2a7fd26
50fcf88
 
 
 
 
 
 
 
a30d34a
 
 
 
50fcf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a30d34a
 
 
 
50fcf88
2a7fd26
50fcf88
 
 
 
 
 
 
 
 
 
a30d34a
 
 
 
50fcf88
 
 
a30d34a
c23a6c5
7b1cb2d
 
50fcf88
 
 
 
 
 
 
 
 
a30d34a
 
 
 
2a7fd26
50fcf88
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
 
7b1cb2d
 
50fcf88
 
 
 
 
 
 
 
 
 
 
a30d34a
 
 
 
 
 
 
 
 
50fcf88
 
a30d34a
 
 
50fcf88
 
 
 
 
a30d34a
50fcf88
 
a30d34a
 
 
 
 
 
8f25c31
c23a6c5
8f25c31
 
 
c23a6c5
 
 
8f25c31
 
cc9e035
c6f51a5
cc9e035
 
c6f51a5
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
 
 
 
 
 
 
 
 
 
 
 
 
9d7baa4
 
 
 
 
ca6762d
 
 
 
 
 
9d7baa4
 
ca6762d
 
c23a6c5
 
 
 
 
9d7baa4
 
c23a6c5
9d7baa4
 
 
 
 
 
 
 
ca6762d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d7baa4
ca6762d
9d7baa4
 
 
 
 
c23a6c5
9d7baa4
 
c23a6c5
9d7baa4
c23a6c5
9d7baa4
ca6762d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d7baa4
94d209d
9d7baa4
ca6762d
 
9d7baa4
 
ca6762d
 
 
 
9d7baa4
 
 
ca6762d
 
 
9d7baa4
 
ca6762d
9d7baa4
ca6762d
 
 
 
 
 
 
 
 
 
9d7baa4
 
 
c23a6c5
 
9d7baa4
ca6762d
9d7baa4
 
ca6762d
 
9d7baa4
c23a6c5
9d7baa4
c23a6c5
 
 
c6f51a5
c23a6c5
 
 
 
c6f51a5
c23a6c5
c6f51a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
 
 
 
8f25c31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a6c5
a30d34a
 
 
 
 
c23a6c5
cc9e035
 
 
c23a6c5
cc9e035
 
 
 
 
50fcf88
 
 
c23a6c5
2a7fd26
50fcf88
 
 
9d7baa4
50fcf88
 
 
 
 
 
9d7baa4
a30d34a
50fcf88
 
 
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
import configuration.logger_setup
import logging

logger = logging.getLogger(__name__)

import hashlib
import socket
from typing import List, Dict
import os
import shutil
from pathlib import Path
from datetime import datetime
import time
import random
from collections import defaultdict, deque
import threading

from content_analyzer.document_parser import DocumentProcessor
from search_engine.indexer import RetrieverBuilder
from intelligence.orchestrator import AgentWorkflow
from configuration import definitions, parameters
                             

# Rate limiting configuration - 3 requests per hour per IP
WINDOW_S = 3600
MAX_CALLS = 5
_calls = defaultdict(deque)  # ip -> timestamps
_calls_lock = threading.Lock()  # Thread-safe access to rate limit state

def rate_limit(request):
    """Thread-safe rate limiting per IP address."""
    ip = getattr(request.client, "host", "unknown")
    now = time.time()

    with _calls_lock:
        q = _calls[ip]
        # Remove expired entries
        while q and (now - q[0]) > WINDOW_S:
            q.popleft()
    
        if len(q) >= MAX_CALLS:
            import gradio as gr
            raise gr.Error(f"Rate limit: {MAX_CALLS} requests per {WINDOW_S//60} minutes. Please wait.")
    
        q.append(now)


# Example data for demo
EXAMPLES = {
    "Generative AI and Jobs": {
        "question": "Which occupations are most likely to be automated by AI?",
        "file_paths": ["samples/OIT-NASK-IAGen_WP140_web.pdf"]  
    },
    "Energy and AI": {
        "question": "Explain electricity consumption across different AI model types?",
        "file_paths": ["samples/EnergyandAI.pdf"]
    },
     "Digital Progress and Trends Report 2025": {
        "question": "which country has most Gen Ai patents and which country has most total funding raised by AI start-ups?",
        "file_paths": ["samples/Digital Progress and Trends Report 2025, Strengthening AI Foundations.pdf"]
    }
}


def format_chat_history(history: List[Dict]) -> str:
    """Format chat history as markdown for display."""
    if not history:
        return "*No conversation history yet. Ask a question to get started!*"

    formatted = []
    for i, entry in enumerate(history, 1):
        timestamp = entry.get("timestamp", "")
        question = entry.get("question", "")
        answer = entry.get("answer", "")
        confidence = entry.get("confidence", "N/A")
    
        formatted.append(f"""
---
### πŸ’¬ Q{i} ({timestamp})
**Question:** {question}

**Answer:** {answer}

*Confidence: {confidence}*
""")

    return "\n".join(formatted)


def format_document_context(documents: List, question: str = "") -> str:
    """Format retrieved documents with annotation highlighting."""
    if not documents:
        return "*No documents retrieved yet.*"

    formatted = [f"### πŸ“š Retrieved Context ({len(documents)} chunks)\n"]

    # Extract key terms from question for highlighting
    key_terms = []
    if question:
        stopwords = {'the', 'a', 'an', 'is', 'are', 'was', 'were', 'in', 'on', 'at', 'to', 'for', 'of', 'and', 'or', 'what', 'how', 'why', 'when', 'where', 'which'}
        key_terms = [word.lower() for word in question.split() if word.lower() not in stopwords and len(word) > 2]

    for i, doc in enumerate(documents[:5], 1):
        content = doc.page_content if hasattr(doc, 'page_content') else str(doc)
        source = doc.metadata.get('source', 'Unknown') if hasattr(doc, 'metadata') else 'Unknown'
    
        # Truncate long content
        if len(content) > 500:
            content = content[:500] + "..."

        # Highlight key terms
        highlighted_content = content
        for term in key_terms[:5]:
            import re
            pattern = re.compile(re.escape(term), re.IGNORECASE)
            highlighted_content = pattern.sub(f"**{term}**", highlighted_content)
    
        formatted.append(f"""
<details>
<summary>πŸ“„ Chunk {i} - {os.path.basename(source)}</summary>

{highlighted_content}

</details>
""")

    if len(documents) > 5:
        formatted.append(f"\n*... and {len(documents) - 5} more chunks*")

    return "\n".join(formatted)


def _get_file_hashes(uploaded_files: List) -> frozenset:
    """Generate SHA-256 hashes for uploaded files."""
    hashes = set()
    for file in uploaded_files:
        with open(file.name, "rb") as f:
            hashes.add(hashlib.sha256(f.read()).hexdigest())
    return frozenset(hashes)


def _find_open_port(start_port: int, max_attempts: int = 20) -> int:
    """Find an available TCP port starting from start_port."""
    port = start_port
    for _ in range(max_attempts):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            try:
                sock.bind(("127.0.0.1", port))
                return port
            except OSError:
                port += 1
    raise RuntimeError(f"Could not find an open port starting at {start_port}")


def _ensure_hfhub_hffolder_compat():
    """
    Shim for Gradio <5.7.1 with huggingface_hub >=1.0.
    """
    import huggingface_hub
    if hasattr(huggingface_hub, "HfFolder"):
        return
    try:
        from huggingface_hub.utils import get_token
    except Exception:
        return
    class HfFolder:
        @staticmethod
        def get_token():
            return get_token()
    huggingface_hub.HfFolder = HfFolder


def _setup_gradio_shim():
    """Shim Gradio's JSON schema conversion to tolerate boolean additionalProperties values."""
    from gradio_client import utils as grc_utils
    _orig_json_schema_to_python_type = grc_utils._json_schema_to_python_type
    def _json_schema_to_python_type_safe(schema, defs=None):
        if isinstance(schema, bool):
            return "Any" if schema else "Never"
        return _orig_json_schema_to_python_type(schema, defs)
    grc_utils._json_schema_to_python_type = _json_schema_to_python_type_safe


def main():
    _ensure_hfhub_hffolder_compat()  # must run before importing gradio
    import gradio as gr
    _setup_gradio_shim()

    logger.info("=" * 60)
    logger.info("Starting SmartDoc AI application...")
    logger.info("=" * 60)

    # Initialize components
    processor = DocumentProcessor()
    retriever_indexer = RetrieverBuilder()
    orchestrator = AgentWorkflow()

    logger.info("All components initialized successfully")

    # CSS styling - Clean, accessible light theme with professional colors
    css = """
    /* Global styling - Light, clean background */
    .gradio-container {
        background: linear-gradient(180deg, #f8fafc 0%, #e2e8f0 100%) !important;
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
    }

    /* Title styles - Dark text for readability */
    .app-title { 
        font-size: 2.2em !important; 
        text-align: center !important; 
        color: #1e293b !important;
        font-weight: 700 !important;
        margin-bottom: 8px !important;
    }
    .app-subtitle { 
        font-size: 1.1em !important; 
        text-align: center !important; 
        color: #0369a1 !important;
        font-weight: 500 !important;
    }
    .app-description { 
        text-align: center; 
        color: #475569 !important;
        font-size: 0.95em !important;
        line-height: 1.6 !important;
    }

    /* Section headers */
    .section-header {
        color: #1e293b !important;
        font-weight: 600 !important;
        border-bottom: 2px solid #0ea5e9 !important;
        padding-bottom: 8px !important;
        margin-bottom: 16px !important;
    }

    /* Chat history panel - Clean white card with more height */
    .chat-history { 
        min-height: 500px;
        max-height: 600px; 
        overflow-y: auto; 
        border: 1px solid #cbd5e1; 
        border-radius: 12px; 
        padding: 20px; 
        background: #ffffff;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        color: #334155 !important;
    }
    #chat-history {
        min-height: 120px !important;
        max-height: none !important;
        height: auto !important;
    }
    .chat-history h3 {
        color: #0f172a !important;
    }
    .chat-history strong {
        color: #1e293b !important;
    }

    /* Document context panel */
    .doc-context { 
        max-height: 380px; 
        overflow-y: auto; 
        border: 1px solid #cbd5e1; 
        border-radius: 12px; 
        padding: 20px; 
        background: #ffffff;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        color: #334155 !important;
    }
    .doc-context details { 
        margin-bottom: 12px; 
        padding: 14px; 
        background: #f1f5f9; 
        border-radius: 8px; 
        border-left: 4px solid #0ea5e9; 
    }
    .doc-context summary { 
        cursor: pointer; 
        font-weight: 600; 
        color: #0369a1 !important;
    }
    .doc-context p, .doc-context span {
        color: #475569 !important;
    }

    /* Answer box - Success green accent, auto-height */
    .answer-box > div:nth-child(2) { 
        border-left: 4px solid #10b981 !important; 
        padding: 16px 16px 16px 20px !important; 
        background: #f0fdf4 !important;
        border-radius: 8px !important;
        min-height: 100px;
        color: #166534 !important;
    }
    .answer-box p, .answer-box li, .answer-box span {
        color: #166534 !important;
    }
    .answer-box strong {
        color: #14532d !important;
    }
    .answer-box h1, .answer-box h2, .answer-box h3, .answer-box h4 {
        color: #15803d !important;
    }
    .answer-box code {
        background: #dcfce7 !important;
        color: #166534 !important;
        padding: 2px 6px !important;
        border-radius: 4px !important;
    }
    .answer-box pre {
        background: #dcfce7 !important;
        padding: 12px !important;
        border-radius: 6px !important;
        overflow-x: auto !important;
    }

    /* Verification box - Blue accent */
    .verification-box >  div:nth-child(2) {
        border-left: 4px solid #0ea5e9 !important;
        padding: 16px 16px 16px 20px !important;
        background: #f0f9ff !important;
        border-radius: 8px !important;
        min-height: 80px;
        color: #0369a1 !important;
    }
    .verification-box p, .verification-box li, .verification-box span {
        color: #0c4a6e !important;
    }
    .verification-box strong {
        color: #075985 !important;
    }

    /* Stats panel - Professional blue gradient */
    .stats-panel { 
        background: linear-gradient(135deg, #0369a1 0%, #0284c7 50%, #0ea5e9 100%) !important; 
        color: #ffffff !important; 
        padding: 20px !important; 
        border-radius: 12px !important; 
        text-align: center;
        box-shadow: 0 4px 14px rgba(3, 105, 161, 0.3);
    }
    .stats-panel strong {
        color: #ffffff !important;
    }

    /* Info panel */
    .info-panel {
        background: #eff6ff !important;
        border: 1px solid #bfdbfe !important;
        border-radius: 8px !important;
        padding: 12px !important;
        color: #1e40af !important;
    }

    /* Form elements */
    .gr-input, .gr-textbox textarea {
        background: #ffffff !important;
        border: 1px solid #cbd5e1 !important;
        border-radius: 8px !important;
        color: #1e293b !important;
    }
    .gr-input:focus, .gr-textbox textarea:focus {
        border-color: #0ea5e9 !important;
        box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1) !important;
    }

    /* Labels */
    label {
        color: #374151 !important;
        font-weight: 500 !important;
    }   

    /* Dropdown - High contrast with darker background for visibility */
    .gr-dropdown, 
    [data-testid="dropdown"],
    .svelte-dropdown,dropdownExample
    div[class*="dropdown"] {
        background: #e0e7ff !important;
        color: #1e293b !important;
        border: 2px solid #1e40af !important;
        border-radius: 8px !important;
        box-shadow: 0 2px 8px rgba(30, 64, 175, 0.2) !important;
    }
    .gr-dropdown:hover,
    [data-testid="dropdown"]:hover {
        background: #c7d2fe !important;
        border-color: #1d4ed8 !important;
        box-shadow: 0 4px 12px rgba(30, 64, 175, 0.3) !important;
    }
    .gr-dropdown select, 
    .gr-dropdown input,
    [data-testid="dropdown"] input {
        color: #1e293b !important;
        background: transparent !important;
        font-weight: 500 !important;
    }

    /* Dropdown container and options */
    [data-testid="dropdown"] span, 
    .dropdown-container span,
    div[class*="dropdown"] span {
        color: #1e293b !important;
        font-weight: 500 !important;
    }

    /* Dropdown list options */
    .gr-dropdown ul, 
    .dropdown-options,
    ul[class*="dropdown"] {
        background: #ffffff !important;
        border: 2px solid #1e40af !important;
        border-radius: 8px !important;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15) !important;
    }
    .gr-dropdown li, 
    .dropdown-options li,
    ul[class*="dropdown"] li {
        color: #1e293b !important;
        padding: 10px 14px !important;
    }
    .gr-dropdown li:hover,
    ul[class*="dropdown"] li:hover {
        background: #c7d2fe !important;
        color: #1e40af !important;
    }

    /* Dropdown label */
    .gr-dropdown label,
    [data-testid="dropdown"] label {
        color: #1e40af !important;
        font-weight: 600 !important;
    }

    /* Tabs - Clean styling */
    .tab-nav {
        border-bottom: 2px solid #e2e8f0 !important;
    }
    .tab-nav button {
        color: #64748b !important;
        font-weight: 500 !important;
        padding: 12px 20px !important;
        border: none !important;
        background: transparent !important;
    }
    .tab-nav button.selected {
        color: #0369a1 !important;
        border-bottom: 3px solid #0369a1 !important;
        font-weight: 600 !important;
    }

    /* Markdown text */
    .prose, .markdown-text {
        color: #334155 !important;
    }
    .prose h1, .prose h2, .prose h3, 
    .markdown-text h1, .markdown-text h2, .markdown-text h3 {
        color: #1e293b !important;
    }
    .prose strong, .markdown-text strong {
        color: #0f172a !important;
    }

    /* Scrollbar styling */
    ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
    }
    ::-webkit-scrollbar-track {
        background: #f1f5f9;
        border-radius: 4px;
    }
    ::-webkit-scrollbar-thumb {
        background: #94a3b8;
        border-radius: 4px;
    }
    ::-webkit-scrollbar-thumb:hover {
        background: #64748b;
    }

    button.secondary {
        background: #1e40af !important;
        color: #ffffff !important;
        border: none !important;
        border-radius: 8px !important;
        font-weight: 600 !important;
        box-shadow: 0 2px 6px rgba(30, 64, 175, 0.3) !important;
        padding: 12px 20px !important;
        min-height: 44px !important;
    }
    button.secondary:hover {
        background: #1d4ed8 !important;
        box-shadow: 0 4px 10px rgba(30, 64, 175, 0.4) !important;
    }         

    /* Left side input boxes with borders */
    .left-panel-box {
        background: #fafafa !important;
        border: 2px solid #94a3b8 !important;
        border-radius: 10px !important;
        padding: 14px !important;
        margin-bottom: 8px !important;
    }
    .left-panel-box:hover {
        border-color: #64748b !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
    }

    /* File upload box with border */
    .file-upload-box {
        background: #f8fafc !important;
        border: 2px dashed #64748b !important;
        border-radius: 10px !important;
        padding: 14px !important;
    }
    .file-upload-box:hover {
        border-color: #0369a1 !important;
        border-style: solid !important;
        background: #f0f9ff !important;
    }

    /* Question input box with border */
    .question-box {
        background: #fffbeb !important;
        border: 2px solid #f59e0b !important;
        border-radius: 10px !important;
        padding: 14px !important;
    }
    .question-box:hover {
        border-color: #d97706 !important;
        box-shadow: 0 2px 8px rgba(245, 158, 11, 0.2) !important;
    }

    /* Dropdown Example - Beige background on 3rd parent container */
    .dropdownExample {
        background: #f5f5dc !important;
        padding: 16px !important;
        border-radius: 8px !important;
        border: 2px solid #d1d5db !important;
        margin-bottom: 16px !important;
    }
    """
    js = r'''
const uploadMessages = [
  "Crunching your documents...",
  "Warming up the AI...",
  "Extracting knowledge...",
  "Scanning for insights...",
  "Preparing your data...",
  "Looking for answers...",
  "Analyzing file structure...",
  "Reading your files...",
  "Indexing content...",
  "Almost ready..."
];

let msgInterval = null;
let timerInterval = null;
let startMs = 0;
let lastMsg = null;

function root() {
  return document.getElementById("processing-message");
}
function isVisible(el) {
  return !!(el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length));
}
function pickMsg() {
  if (uploadMessages.length === 0) return "";
  if (uploadMessages.length === 1) return uploadMessages[0];
  let m;
  do { m = uploadMessages[Math.floor(Math.random() * uploadMessages.length)]; }
  while (m === lastMsg);
  lastMsg = m;
  return m;
}
function getMsgSpan() {
  const r = root();
  return r ? r.querySelector("#processing-msg") : null;
}
function getTimerSpan() {
  const r = root();
  return r ? r.querySelector("#processing-timer") : null;
}
function setMsg(t) {
  const s = getMsgSpan();
  if (s) s.textContent = t;
}
function fmtElapsed() {
  return ((Date.now() - startMs) / 1000).toFixed(1) + "s elapsed";
}

function start() {
  if (msgInterval || timerInterval) return;
  startMs = Date.now();
  setMsg(pickMsg());

  msgInterval = setInterval(() => setMsg(pickMsg()), 2000);

  const t = getTimerSpan();
  if (t) {
    t.textContent = fmtElapsed();
    timerInterval = setInterval(() => { t.textContent = fmtElapsed(); }, 200);
  }
}

function stop() {
  if (msgInterval) { clearInterval(msgInterval); msgInterval = null; }
  if (timerInterval) { clearInterval(timerInterval); timerInterval = null; }
  const t = getTimerSpan();
  if (t) t.textContent = "";
}

function tick() {
  const r = root();
  if (isVisible(r)) start();
  else stop();
}

// Observe rerenders / visibility changes
const obs = new MutationObserver(tick);
obs.observe(document.body, { subtree: true, childList: true, attributes: true });

window.addEventListener("load", tick);
setInterval(tick, 500);
'''

    # Launch server - Compatible with both local and Hugging Face Spaces
    # HF Spaces sets SPACE_ID environment variable
    is_hf_space = os.environ.get("SPACE_ID") is not None

    with gr.Blocks(title="SmartDoc AI") as demo:
        gr.Markdown("### SmartDoc AI - Document Q&A", elem_classes="app-title")
        gr.Markdown("Upload your documents and ask questions. Answers will appear below, just like a chat.", elem_classes="app-description")
        gr.Markdown("---")

        # Examples dropdown - visible for both local and HF Spaces
        example_dropdown = gr.Dropdown(
            label="Quick Start - Choose an Example",
            choices=list(EXAMPLES.keys()),
            value=None,
            info="Select a pre-loaded example to try"
        )
        loaded_file_info = gr.Markdown("", elem_classes="info-panel", visible=False)

        files = gr.Files(label="Upload your files", file_types=definitions.ALLOWED_TYPES)
        question = gr.Textbox(label="Ask a question", lines=2, placeholder="Type your question here...")
        chat = gr.Chatbot(label="Answers", elem_id="chat-history")
        submit_btn = gr.Button("Get Answer", variant="primary")
        processing_message = gr.HTML("", elem_id="processing-message", visible=False)
        doc_context_display = gr.Markdown("*Submit a question to see which document sections were referenced*", elem_classes="doc-context", visible=False)
        refresh_context_btn = gr.Button("Refresh Sources", variant="secondary", visible=False)
        with gr.Tab("Context"):
            pass  # No .render() calls here; components are already defined and used in outputs

        session_state = gr.State({
            "file_hashes": frozenset(),
            "retriever": None,
            "chat_history": [],
            "last_documents": [],
            "total_questions": 0,
            "session_start": datetime.now().strftime("%Y-%m-%d %H:%M")
        })

        def process_question(question_text, uploaded_files, chat_history, request: gr.Request):
            rate_limit(request)
            chat_history = chat_history or []            
            yield (
                chat_history,
                gr.update(visible=False),
                gr.update(visible=False),
                gr.update(interactive=False),
                gr.update(interactive=False),
                gr.update(interactive=False),
                gr.update(interactive=False),
                gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
            )
            try:
                if not question_text.strip():
               
                    chat_history.append({"role": "user", "content": question_text})
                    chat_history.append({"role": "assistant", "content": "Please enter a question."})
                    yield (
                        chat_history,
                        gr.update(visible=False),
                        gr.update(visible=False),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(value="", visible=False)
                    )
                    return
                if not uploaded_files:                         
              
                    chat_history.append({"role": "user", "content": question_text})
                    chat_history.append({"role": "assistant", "content": "Please upload at least one document."})
                    yield (
                        chat_history,
                        gr.update(visible=False),
                        gr.update(visible=False),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(interactive=True),
                        gr.update(value="", visible=False)
                    )
                    return
                # Stage 2: Chunking with per-chunk progress and rotating status
                def load_or_process(file):
                    with open(file.name, "rb") as f:
                        file_content = f.read()
                    file_hash = processor._generate_hash(file_content)
                    cache_path = processor.cache_dir / f"{file_hash}.pkl"
                    if processor._is_cache_valid(cache_path):
                        chunks = processor._load_from_cache(cache_path)
                        if chunks:
                            logger.info(f"Using cached chunks for {file.name}")
                            return chunks
                    chunks = processor._process_file(file)
                    processor._save_to_cache(chunks, cache_path)
                    return chunks

                all_chunks = []
                seen_hashes = set()
                chunks_by_file = []
                total_chunks = 0
                for file in uploaded_files:
                    chunks = load_or_process(file)
                    chunks_by_file.append(chunks)
                    total_chunks += len(chunks)
                if total_chunks == 0:
                    total_chunks = 1
                chunk_idx = 0
                for chunks in chunks_by_file:
                    for chunk in chunks:
                        chunk_hash = processor._generate_hash(chunk.page_content.encode())
                        if chunk_hash not in seen_hashes:
                            seen_hashes.add(chunk_hash)
                            all_chunks.append(chunk)
                        # else: skip duplicate chunk
                        chunk_idx += 1
                        # yield progress here if needed
                        yield (
                            chat_history,
                            gr.update(visible=False),
                            gr.update(visible=False),
                            gr.update(interactive=False),
                            gr.update(interactive=False),
                            gr.update(interactive=False),
                            gr.update(interactive=False),
                            gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
                        )
                # After all chunks, show 100%                    
                yield (
                    chat_history,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
                )
                # Stage 3: Building Retriever
                yield (
                    chat_history,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(value=(
                        '<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04); display:flex; align-items:center;">'
                        '<img src="https://media.giphy.com/media/26ufnwz3wDUli7GU0/giphy.gif" alt="AI working" style="height:40px; margin-right:16px;">'
                        '<span id="processing-msg"></span>'
                        '</div>'
                    ), visible=True)
                )
                retriever = retriever_indexer.build_hybrid_retriever(all_chunks)
                # Stage 4: Generating Answer
                yield (
                    chat_history,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
                )
                result = orchestrator.run_workflow(question=question_text, retriever=retriever)
                answer = result["draft_answer"]
                # Stage 5: Verifying Answer
                yield (
                    chat_history,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
                )
                verification = result.get("verification_report", "No verification details available.")
                logger.info(f"Verification (internal):\n{verification}")
                # Do not display verification to user, only use internally
           
                chat_history.append({"role": "user", "content": question_text})
                chat_history.append({"role": "assistant", "content": f"**Answer:**\n{answer}"})
                session_state.value["last_documents"] = retriever.invoke(question_text)
                yield (
                    chat_history,
                    gr.update(visible=True),  # doc_context_display
                    gr.update(visible=True),  # refresh_context_btn
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(value='''<div style="background:#fff; border-radius:8px; padding:18px 24px; margin-top:32px; color:#1e293b; font-size:1.2em; font-weight:600; box-shadow:0 2px 8px rgba(0,0,0,0.04);">
  <span id="processing-msg"></span>
  <span id="processing-timer" style="opacity:0.8; margin-left:8px;"></span>
</div>''', visible=True)
                )      
                yield (
                    chat_history,
                    gr.update(visible=True),
                    gr.update(visible=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(value="", visible=False)
                )
            except Exception as e:
                logger.error(f"Processing error: {e}", exc_info=True)
           
           
                chat_history.append({"role": "user", "content": question_text})
                chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
                yield (
                    chat_history,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(interactive=True),
                    gr.update(value="", visible=False)
                )

        submit_btn.click(
            fn=process_question,
            inputs=[question, files, chat],
            outputs=[chat, doc_context_display, refresh_context_btn, submit_btn, question, files, example_dropdown, processing_message],
            queue=True,
            show_progress=True
        )

        def refresh_context():
            docs = session_state.value.get("last_documents", [])
            last_question = ""
            for msg in reversed(chat.value or []):
                if msg["role"] == "user":
                    last_question = msg["content"]
                    break
            return format_document_context(docs, last_question)

        refresh_context_btn.click(
            fn=refresh_context,
            inputs=[],
            outputs=[doc_context_display]
        )

        def load_example(example_key):
            if not example_key or example_key not in EXAMPLES:
                return [], "", "Select a valid example from the dropdown above"
            ex_data = EXAMPLES[example_key]
            question_text = ex_data["question"]
            file_names = ex_data["file_paths"]
        
            # Try to download from HF dataset if on Spaces
            if is_hf_space:
                try:
                    from datasets import load_dataset
                    import tempfile
                    
                    copied_files = []
                    file_info_text = f"βœ… Loaded: {example_key}\n\n"
                    
                    # Get HF token - REQUIRED for gated datasets
                    hf_token = os.environ.get("HF_TOKEN", None)
                    
                    if not hf_token:
                        logger.warning("HF_TOKEN not set - required for gated datasets")
                        return [], "", (
                            "❌ **Authentication Required**\n\n"
                            "The example dataset is gated and requires authentication.\n\n"
                            "**To fix:**\n"
                            "1. Go to Space Settings β†’ Repository secrets\n"
                            "2. Add secret: `HF_TOKEN` = your Hugging Face token\n"
                            "3. Restart the Space\n\n"
                            "Or make your dataset public at:\n"
                            "https://huggingface.co/datasets/TilanB/smartdoc-samples/settings\n\n"
                            "For now, please **upload files manually**."
                        )
                    
                    try:
                        # Load dataset - uses row-based structure
                        logger.info(f"Loading dataset from HuggingFace: TilanB/smartdoc-samples")
                        ds = load_dataset(
                            "TilanB/smartdoc-samples",
                            split="train",
                            token=hf_token
                        )
                        logger.info(f"Dataset loaded with {len(ds)} rows")
                        
                        # Create temp directory for files
                        temp_dir = tempfile.mkdtemp(prefix='hf_examples_')
                        
                        # Debug: Log first row structure
                        if len(ds) > 0:
                            first_row = ds[0]
                            pdf_data = first_row.get('pdf', None)
                            logger.info(f"Dataset first row 'pdf' type: {type(pdf_data)}")
                            
                            # Handle different types
                            if hasattr(pdf_data, 'stream') and hasattr(pdf_data.stream, 'name'):
                                # pdfplumber PDF object
                                logger.info(f"PDF is pdfplumber object, stream path: {pdf_data.stream.name}")
                            elif isinstance(pdf_data, dict):
                                logger.info(f"PDF dict keys: {list(pdf_data.keys())}")
                                if 'path' in pdf_data:
                                    logger.info(f"PDF path: {pdf_data.get('path', 'N/A')}")
            
                        # Extract requested files from dataset rows
                        for file_path in file_names:
                            filename = os.path.basename(file_path)
                            file_found = False
                            
                            logger.info(f"Looking for file: {filename}")
                            
                            # Search through dataset rows
                            for row_idx, row in enumerate(ds):
                                # The 'pdf' column contains file objects from HF datasets
                                pdf_data = row.get('pdf', None)
                                
                                if pdf_data is None:
                                    continue
                                
                                # Extract the actual filename from the pdf data
                                # HF datasets with PDF files can return different types:
                                # 1. pdfplumber.pdf.PDF objects (when using pdf feature type)
                                # 2. dict with 'path' and 'bytes' keys
                                # 3. str path
                                # 4. bytes directly
                                
                                row_filename = ""
                                
                                # Check for pdfplumber PDF object (has .stream.name attribute)
                                if hasattr(pdf_data, 'stream') and hasattr(pdf_data.stream, 'name'):
                                    row_filename = pdf_data.stream.name
                                    logger.debug(f"Got filename from pdfplumber stream: {row_filename}")
                                # Check for pdfplumber PDF object with path attribute
                                elif hasattr(pdf_data, 'path'):
                                    row_filename = pdf_data.path
                                # Check for dict format
                                elif isinstance(pdf_data, dict):
                                    row_filename = pdf_data.get('path', '')
                                # Check for string path
                                elif isinstance(pdf_data, str):
                                    row_filename = pdf_data
                                
                                row_basename = os.path.basename(str(row_filename))
                                logger.debug(f"Row {row_idx}: checking '{row_basename}' vs '{filename}'")
                                
                                # Match by filename
                                if row_basename == filename:
                                    temp_file_path = os.path.join(temp_dir, filename)
                                    logger.info(f"Found match! Extracting {filename}...")
                                    
                                    try:
                                        extracted = False
                                        
                                        # Handle pdfplumber PDF object
                                        if hasattr(pdf_data, 'stream'):
                                            # Get the file path from pdfplumber's stream
                                            source_path = pdf_data.stream.name
                                            if source_path and os.path.exists(source_path):
                                                shutil.copy2(source_path, temp_file_path)
                                                logger.info(f"Copied from pdfplumber stream: {source_path}")
                                                extracted = True
                                            else:
                                                # Try to read bytes from stream
                                                try:
                                                    pdf_data.stream.seek(0)
                                                    pdf_bytes = pdf_data.stream.read()
                                                    with open(temp_file_path, 'wb') as f:
                                                        f.write(pdf_bytes)
                                                    logger.info(f"Wrote {len(pdf_bytes)} bytes from pdfplumber stream")
                                                    extracted = True
                                                except Exception as stream_err:
                                                    logger.warning(f"Could not read stream: {stream_err}")
                                        
                                        # Handle dict format
                                        elif isinstance(pdf_data, dict):
                                            if 'bytes' in pdf_data and pdf_data['bytes']:
                                                with open(temp_file_path, 'wb') as f:
                                                    f.write(pdf_data['bytes'])
                                                logger.info(f"Wrote {len(pdf_data['bytes'])} bytes")
                                                extracted = True
                                            elif 'path' in pdf_data and pdf_data['path'] and os.path.exists(pdf_data['path']):
                                                shutil.copy2(pdf_data['path'], temp_file_path)
                                                logger.info(f"Copied from dict path: {pdf_data['path']}")
                                                extracted = True
                                        
                                        # Handle bytes directly
                                        elif isinstance(pdf_data, bytes):
                                            with open(temp_file_path, 'wb') as f:
                                                f.write(pdf_data)
                                            extracted = True
                                        
                                        # Handle string path
                                        elif isinstance(pdf_data, str) and os.path.exists(pdf_data):
                                            shutil.copy2(pdf_data, temp_file_path)
                                            extracted = True
                                        
                                        if extracted and os.path.exists(temp_file_path):
                                            copied_files.append(temp_file_path)
                                            file_size_mb = os.path.getsize(temp_file_path) / (1024 * 1024)
                                            file_info_text += f"πŸ“„ {filename} ({file_size_mb:.2f} MB)\n"
                                            file_found = True
                                            logger.info(f"βœ… Successfully extracted {filename}")
                                            break
                                        else:
                                            logger.error(f"Could not extract file: {type(pdf_data)}")
                                            
                                    except Exception as ex:
                                        logger.error(f"Failed to extract {filename}: {ex}", exc_info=True)
                                        continue
                            
                            if not file_found:
                                logger.warning(f"❌ File {filename} not found in dataset rows")
                                # Debug: show what's available
                                for idx, row in enumerate(ds):
                                    pdf_data = row.get('pdf', None)
                                    if pdf_data and hasattr(pdf_data, 'stream') and hasattr(pdf_data.stream, 'name'):
                                        available_name = os.path.basename(str(pdf_data.stream.name))
                                        logger.info(f"  Available file in row {idx}: '{available_name}'")
                                file_info_text += f"⚠️ {filename} - Not found in dataset\n"

                        if not copied_files:
                            if len(ds) > 0:
                                logger.error(f"Dataset structure: {list(ds[0].keys())}")
                            return [], "", f"❌ Could not find example files in dataset.\n\nDataset has {len(ds)} rows. Please upload files manually."
                        
                        return copied_files, question_text, file_info_text
                        
                    except Exception as e:
                        error_msg = str(e)
                        logger.error(f"Failed to load dataset: {e}", exc_info=True)
                        
                        # Check for gated dataset error
                        if "gated" in error_msg.lower() or "authenticated" in error_msg.lower():
                            return [], "", (
                                "❌ **Dataset Access Denied**\n\n"
                                "The dataset is gated and your token doesn't have access.\n\n"
                                "**To fix:**\n"
                                "1. Visit: https://huggingface.co/datasets/TilanB/smartdoc-samples\n"
                                "2. Accept the access terms (if any)\n"
                                "3. Make sure HF_TOKEN is set in Space secrets\n\n"
                                "Or make your dataset public.\n\n"
                                "For now, please **upload files manually**."
                            )
                        
                        return [], "", f"❌ Failed to load dataset: {error_msg}\n\nPlease upload files manually."
                
                except ImportError as e:
                    logger.error(f"datasets package not installed: {e}")
                    return [], "", "❌ 'datasets' package not installed"
            else:
                # Local mode - use files from samples directory
                import tempfile
                temp_dir = tempfile.mkdtemp()
                copied_files = []
                file_info_text = f"Loaded: {example_key}\n\n"
                for source_file_path in file_names:
                    abs_source = os.path.abspath(source_file_path)
                    if os.path.exists(abs_source):
                        filename = os.path.basename(abs_source)
                        temp_file_path = os.path.join(temp_dir, filename)
                        shutil.copy2(abs_source, temp_file_path)
                        copied_files.append(temp_file_path)
                        file_size_mb = os.path.getsize(temp_file_path) / (1024 * 1024)
                        file_info_text += f"{filename} ({file_size_mb:.2f} MB)\n"
                    else:
                        file_info_text += f"{source_file_path} not found\n"
                if not copied_files:
                    return [], "", "Could not load example files"
                return copied_files, question_text, file_info_text
    
        example_dropdown.change(
            fn=load_example,
            inputs=[example_dropdown],
            outputs=[files, question, loaded_file_info]
        )
    
        # Show loaded_file_info when example is selected
        def show_info(example_key):
            return gr.update(visible=bool(example_key))
    
        example_dropdown.change(
            fn=show_info,
            inputs=[example_dropdown],
            outputs=[loaded_file_info]
        )
    # Launch server - Compatible with both local and Hugging Face Spaces
    # HF Spaces sets SPACE_ID environment variable
    is_hf_space = os.environ.get("SPACE_ID") is not None

    demo.queue()
    if is_hf_space:
        # Hugging Face Spaces configuration
        logger.info("Running on Hugging Face Spaces")
        demo.launch(theme=gr.themes.Soft(), server_name="0.0.0.0", server_port=7860, css=css, js=js)
    else:
        # Local development configuration
        configured_port = int(os.environ.get("GRADIO_SERVER_PORT", "7860"))
        server_port = _find_open_port(configured_port)
        logger.info(f"Launching Gradio on port {server_port}")
        logger.info(f"Access the app at: http://127.0.0.1:{server_port}")
        demo.launch(theme=gr.themes.Soft(), server_name="127.0.0.1", server_port=server_port, share=False, css=css, js=js)


if __name__ == "__main__":
    main()