File size: 43,497 Bytes
36db703
549d638
188f4e4
260a555
188f4e4
31341ac
549d638
 
 
 
 
 
 
 
 
 
 
 
36db703
 
 
 
188f4e4
4b89f17
ce4796a
31341ac
260a555
36db703
4b89f17
188f4e4
 
260a555
86a23c0
36db703
 
 
 
ce4796a
86a23c0
188f4e4
 
ce4796a
36db703
188f4e4
 
 
 
4b6c56e
549d638
4b6c56e
 
549d638
 
188f4e4
ce4796a
188f4e4
260a555
188f4e4
 
 
 
 
 
 
36db703
c3194eb
188f4e4
 
 
 
c3194eb
 
86a23c0
 
 
31341ac
 
 
36db703
188f4e4
 
 
 
86a23c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36db703
 
188f4e4
549d638
188f4e4
 
36db703
 
 
 
188f4e4
260a555
ce4796a
36db703
 
 
 
188f4e4
36db703
 
 
 
 
 
 
549d638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260a555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188f4e4
 
36db703
260a555
188f4e4
 
 
 
 
 
 
 
 
 
260a555
 
 
 
 
 
 
 
 
 
 
 
ce4796a
188f4e4
 
 
 
 
 
 
 
 
ce4796a
 
188f4e4
ce4796a
 
260a555
ce4796a
36db703
 
260a555
188f4e4
 
260a555
 
 
 
 
 
 
 
 
188f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31341ac
188f4e4
 
 
 
 
 
 
 
 
 
31341ac
 
 
 
188f4e4
31341ac
 
 
 
188f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31341ac
 
188f4e4
31341ac
 
 
 
 
 
188f4e4
 
 
 
 
 
 
 
31341ac
 
 
 
 
 
 
 
 
 
 
 
188f4e4
 
86a23c0
 
 
31341ac
188f4e4
86a23c0
 
260a555
 
 
86a23c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31341ac
86a23c0
 
 
31341ac
188f4e4
 
31341ac
 
 
86a23c0
 
 
31341ac
188f4e4
 
86a23c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260a555
86a23c0
 
 
 
 
 
 
260a555
86a23c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260a555
86a23c0
 
 
 
 
 
260a555
86a23c0
 
 
 
 
 
 
 
 
 
 
 
31341ac
188f4e4
 
86a23c0
31341ac
 
 
 
 
 
 
 
 
 
 
 
260a555
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
31341ac
 
 
 
 
 
86a23c0
 
 
 
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
31341ac
 
 
 
86a23c0
 
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
31341ac
 
 
 
86a23c0
31341ac
86a23c0
31341ac
86a23c0
 
 
 
 
31341ac
 
 
 
 
 
86a23c0
 
 
 
31341ac
 
 
 
86a23c0
 
 
 
 
31341ac
 
 
 
 
86a23c0
 
 
 
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
31341ac
 
 
 
 
 
 
 
 
 
 
 
 
86a23c0
 
 
 
188f4e4
260a555
 
 
188f4e4
 
 
260a555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36db703
 
 
188f4e4
 
36db703
260a555
9efdf3f
4b6c56e
188f4e4
 
 
ce4796a
 
549d638
 
 
 
 
 
260a555
ce4796a
 
549d638
ce4796a
549d638
 
 
 
 
 
 
 
ce4796a
260a555
188f4e4
ce4796a
188f4e4
ce4796a
188f4e4
 
ce4796a
 
188f4e4
 
ce4796a
260a555
ce4796a
36db703
 
188f4e4
 
ce4796a
c3194eb
 
 
0a05ea2
188f4e4
ce4796a
c3194eb
36db703
260a555
 
 
 
 
 
 
 
 
 
 
86a23c0
 
 
260a555
86a23c0
260a555
86a23c0
31341ac
260a555
 
 
 
31341ac
260a555
 
 
 
 
 
 
 
 
 
 
188f4e4
746cdfd
188f4e4
260a555
188f4e4
 
260a555
 
188f4e4
 
6a019e1
4b89f17
188f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3194eb
188f4e4
c3194eb
 
4b89f17
0a05ea2
4b89f17
188f4e4
 
 
 
 
 
 
c3194eb
 
4b89f17
6a019e1
188f4e4
4b89f17
ce4796a
4b89f17
188f4e4
 
c3194eb
 
188f4e4
c3194eb
 
188f4e4
4b6c56e
188f4e4
 
 
c3194eb
 
 
188f4e4
 
 
6a019e1
188f4e4
6a019e1
 
 
 
188f4e4
 
6a019e1
188f4e4
 
 
 
6a019e1
 
 
188f4e4
fa05cd2
 
188f4e4
9efdf3f
188f4e4
36db703
188f4e4
 
 
86a23c0
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
"""
rag_engine.py — Multimodal RAG Engine with Multi-File Support, Reranking & Memory
Supports: PDF, TXT, DOCX, CSV, XLSX, Images (JPG/PNG/WEBP)
Features: Up to 5 simultaneous files, per-file removal, additive indexing
Memory: sliding window of last 6 exchanges

KEY CHANGES (v5 — Cross-Encoder Reranking):
  1. Cross-encoder reranker (ms-marco-MiniLM-L-6-v2) scores every retrieved
     chunk for true semantic relevance to the query — not just embedding distance.
  2. Over-fetches 12+ candidates from the vectorstore, then reranks to pick
     the top-k most relevant chunks for the LLM context.
  3. Graceful fallback — if the reranker fails to load, uses original order.

Previous features preserved:
  - Additive indexing, per-file removal, MAX_FILES=5
  - Multi-file aware generation, cross-doc coverage
  - OCR, color analysis, BLIP raw bytes, VLM descriptions for images
  - Conversation memory (6-exchange sliding window)
"""

import os
import re
import io
import json
import time
import base64
import hashlib
import tempfile
import requests
import logging
from pathlib import Path
from typing import Tuple, List, Optional, Dict
from collections import Counter

from chromadb.config import Settings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.document_loaders import PyPDFLoader
from langchain.schema import Document

import monitor

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ── Constants ────────────────────────────────────────────────────────────────
EMBED_MODEL     = "all-MiniLM-L6-v2"
RERANK_MODEL    = "cross-encoder/ms-marco-MiniLM-L-6-v2"  # ~80MB, CPU-friendly
CHUNK_SIZE      = 600
CHUNK_OVERLAP   = 100
TOP_K           = 4       # final chunks sent to LLM after reranking
RERANK_FETCH_K  = 12      # over-fetch this many candidates for reranking
COLLECTION_NAME = "docmind_multimodal"
HF_API_URL      = "https://router.huggingface.co/v1/chat/completions"
MEMORY_WINDOW   = 6   # number of past Q&A pairs to keep
MAX_FILES        = 5   # maximum simultaneous documents

SUPPORTED_EXTENSIONS = {
    ".pdf", ".txt",
    ".docx", ".doc",
    ".csv", ".xlsx", ".xls",
    ".jpg", ".jpeg", ".png", ".webp",
}

CANDIDATE_MODELS = [
    "meta-llama/Llama-3.1-8B-Instruct:cerebras",
    "meta-llama/Llama-3.3-70B-Instruct:cerebras",
    "mistralai/Mistral-7B-Instruct-v0.3:fireworks-ai",
    "HuggingFaceTB/SmolLM3-3B:hf-inference",
]

# Vision-language models for detailed image descriptions (order matters)
VLM_MODELS = [
    "Qwen/Qwen2.5-VL-7B-Instruct",
    "meta-llama/Llama-3.2-11B-Vision-Instruct",
]


def get_suffix(name: str) -> str:
    return Path(name).suffix.lower() or ".txt"


def _classify_color(r: int, g: int, b: int) -> str:
    """Classify an RGB pixel into a human-readable color name."""
    if r > 220 and g > 220 and b > 220:
        return "white"
    if r < 35 and g < 35 and b < 35:
        return "black"
    if max(r, g, b) - min(r, g, b) < 30:
        if r > 170:
            return "light gray"
        if r > 100:
            return "gray"
        return "dark gray"
    if r > 180 and g > 180 and b < 100:
        return "yellow"
    if r > 180 and g > 100 and g < 180 and b < 80:
        return "orange"
    if r > 150 and g < 80 and b < 80:
        return "red"
    if r > 150 and g < 100 and b > 100:
        return "pink" if r > 200 else "purple"
    if g > 150 and r < 100 and b < 100:
        return "green"
    if g > 120 and r < 80 and b < 80:
        return "dark green"
    if b > 150 and r < 100 and g < 100:
        return "blue"
    if b > 150 and g > 100 and r < 100:
        return "cyan" if g > 150 else "teal"
    if r > 100 and g > 100 and b < 80:
        return "olive"
    if r > 150 and g < 80 and b > 150:
        return "magenta"
    if g >= r and g >= b:
        return "green"
    if r >= g and r >= b:
        return "red"
    return "blue"


class RAGEngine:
    def __init__(self):
        self._embeddings:  Optional[HuggingFaceEmbeddings] = None
        self._reranker = None  # lazy-loaded cross-encoder
        self._vectorstore: Optional[Chroma] = None
        self._splitter = RecursiveCharacterTextSplitter(
            chunk_size=CHUNK_SIZE,
            chunk_overlap=CHUNK_OVERLAP,
            separators=["\n\n", "\n", ". ", " ", ""],
        )
        self._memory: List[dict] = []
        self._documents: Dict[str, dict] = {}  # {filename: {chunk_count, chunk_ids, type}}
        monitor.log_startup()

    @property
    def embeddings(self):
        if self._embeddings is None:
            logger.info("Loading embedding model...")
            self._embeddings = HuggingFaceEmbeddings(
                model_name=EMBED_MODEL,
                model_kwargs={"device": "cpu"},
                encode_kwargs={"normalize_embeddings": True},
            )
        return self._embeddings

    @property
    def reranker(self):
        """Lazy-load the cross-encoder reranker (~80MB, CPU-friendly)."""
        if self._reranker is None:
            try:
                from sentence_transformers import CrossEncoder
                logger.info(f"Loading reranker model: {RERANK_MODEL}...")
                self._reranker = CrossEncoder(RERANK_MODEL, max_length=512)
                logger.info("Reranker loaded successfully.")
            except Exception as e:
                logger.warning(f"Failed to load reranker: {e}. Will skip reranking.")
                self._reranker = False  # sentinel: don't retry
        return self._reranker if self._reranker is not False else None

    def _rerank_documents(self, question: str, docs: List[Document], top_k: int) -> List[Document]:
        """Score and reorder documents using the cross-encoder reranker."""
        if not docs:
            return docs

        ranker = self.reranker
        if ranker is None:
            # Reranker unavailable — fall back to original order
            logger.info("Reranker not available, using original retrieval order.")
            return docs[:top_k]

        # Build query-document pairs for the cross-encoder
        pairs = [(question, doc.page_content) for doc in docs]

        try:
            scores = ranker.predict(pairs)

            # Pair each doc with its rerank score
            scored = list(zip(docs, scores))
            scored.sort(key=lambda x: x[1], reverse=True)

            reranked = [doc for doc, score in scored[:top_k]]

            # Log the reranking effect
            original_sources = [d.metadata.get("source", "?")[:30] for d in docs[:top_k]]
            reranked_sources = [d.metadata.get("source", "?")[:30] for d in reranked]
            top_scores = [f"{s:.3f}" for _, s in scored[:top_k]]
            logger.info(
                f"Reranked {len(docs)} candidates → top {top_k}. "
                f"Scores: {top_scores}. "
                f"Before: {original_sources}, After: {reranked_sources}"
            )

            return reranked
        except Exception as e:
            logger.warning(f"Reranking failed: {e}. Using original order.")
            return docs[:top_k]

    # ── Memory ───────────────────────────────────────────────────────────────

    def clear_memory(self):
        self._memory = []

    def add_to_memory(self, question: str, answer: str):
        self._memory.append({"role": "user",      "content": question})
        self._memory.append({"role": "assistant",  "content": answer})
        max_msgs = MEMORY_WINDOW * 2
        if len(self._memory) > max_msgs:
            self._memory = self._memory[-max_msgs:]

    def get_memory_messages(self) -> List[dict]:
        return self._memory.copy()

    def get_memory_count(self) -> int:
        return len(self._memory) // 2

    # ── Document Management ──────────────────────────────────────────────────

    def get_documents(self) -> List[dict]:
        """Return list of all loaded documents with their info."""
        return [
            {
                "name": name,
                "type": info["type"],
                "chunk_count": info["chunk_count"],
            }
            for name, info in self._documents.items()
        ]

    def get_total_chunks(self) -> int:
        """Total chunks across all loaded files."""
        return sum(info["chunk_count"] for info in self._documents.values())

    def get_file_count(self) -> int:
        return len(self._documents)

    def remove_file(self, filename: str) -> bool:
        """Remove a specific file's chunks from the vectorstore."""
        if filename not in self._documents:
            logger.warning(f"Cannot remove '{filename}' — not found in loaded documents.")
            return False

        chunk_ids = self._documents[filename]["chunk_ids"]

        if self._vectorstore and chunk_ids:
            try:
                self._vectorstore._collection.delete(ids=chunk_ids)
                logger.info(f"Removed {len(chunk_ids)} chunks for '{filename}'")
            except Exception as e:
                logger.warning(f"Failed to delete chunks for '{filename}': {e}")

        del self._documents[filename]

        # If no documents left, clean up the vectorstore entirely
        if not self._documents:
            self._vectorstore = None
            logger.info("All documents removed — vectorstore cleared.")

        return True

    def reset(self):
        """Reset everything — all documents, vectorstore, and memory."""
        self._documents = {}
        if self._vectorstore:
            try:
                self._vectorstore._client.reset()
            except Exception:
                pass
        self._vectorstore = None
        self._memory = []
        logger.info("Full reset: all documents, vectorstore, and memory cleared.")

    # ── Ingestion ────────────────────────────────────────────────────────────

    def ingest_file(self, uploaded_file) -> int:
        """Accept FastAPI UploadFile or Streamlit UploadedFile. Additive indexing."""
        t0 = time.time()
        filename = getattr(uploaded_file, "name", None) or getattr(uploaded_file, "filename", "file")
        suffix   = get_suffix(filename)
        error    = ""
        chunks   = 0

        if suffix not in SUPPORTED_EXTENSIONS:
            raise ValueError(
                f"Unsupported: {suffix}. Supported: {', '.join(sorted(SUPPORTED_EXTENSIONS))}"
            )

        # Enforce file limit (replacement of same name doesn't count as new)
        if filename not in self._documents and len(self._documents) >= MAX_FILES:
            raise ValueError(
                f"Maximum {MAX_FILES} files reached. Remove a file before uploading more."
            )

        # If same filename exists, remove old version first (replacement)
        if filename in self._documents:
            logger.info(f"Replacing existing file: {filename}")
            self.remove_file(filename)

        try:
            if hasattr(uploaded_file, "read"):
                data = uploaded_file.read()
                if hasattr(uploaded_file, "seek"):
                    uploaded_file.seek(0)
            else:
                data = uploaded_file.file.read()

            docs   = self._route(data, filename, suffix)
            chunks = self._index(docs, filename)
        except Exception as e:
            error = str(e)
            logger.error(f"Ingestion error: {e}")
            raise
        finally:
            monitor.log_ingestion(filename, chunks, (time.time() - t0) * 1000, error)
        return chunks

    def ingest_path(self, path: str, name: str = "") -> int:
        """Ingest a file from a local path. Also additive."""
        filename = name or Path(path).name
        suffix   = get_suffix(filename)

        if filename not in self._documents and len(self._documents) >= MAX_FILES:
            raise ValueError(
                f"Maximum {MAX_FILES} files reached. Remove a file before uploading more."
            )

        if filename in self._documents:
            self.remove_file(filename)

        with open(path, "rb") as f:
            data = f.read()
        docs   = self._route(data, filename, suffix)
        chunks = self._index(docs, filename)
        return chunks

    def _route(self, data: bytes, filename: str, suffix: str) -> List[Document]:
        if suffix == ".pdf":
            return self._load_pdf(data, filename)
        elif suffix == ".txt":
            return self._load_text(data, filename)
        elif suffix in {".docx", ".doc"}:
            return self._load_docx(data, filename)
        elif suffix == ".csv":
            return self._load_csv(data, filename)
        elif suffix in {".xlsx", ".xls"}:
            return self._load_excel(data, filename)
        elif suffix in {".jpg", ".jpeg", ".png", ".webp"}:
            return self._load_image(data, filename)
        raise ValueError(f"No loader for {suffix}")

    # ── Loaders ──────────────────────────────────────────────────────────────

    def _load_pdf(self, data: bytes, filename: str) -> List[Document]:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            tmp.write(data)
            tmp_path = tmp.name
        try:
            docs = PyPDFLoader(tmp_path).load()
            for doc in docs:
                doc.metadata.update({"source": filename, "type": "pdf"})
            return docs
        finally:
            os.unlink(tmp_path)

    def _load_text(self, data: bytes, filename: str) -> List[Document]:
        return [Document(
            page_content=data.decode("utf-8", errors="replace"),
            metadata={"source": filename, "type": "text"}
        )]

    def _load_docx(self, data: bytes, filename: str) -> List[Document]:
        text = ""
        try:
            import docx2txt
            with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
                tmp.write(data)
                tmp_path = tmp.name
            try:
                text = docx2txt.process(tmp_path)
            finally:
                os.unlink(tmp_path)
        except ImportError:
            logger.warning("docx2txt not installed — falling back to raw text extraction")
            text = data.decode("utf-8", errors="replace")
        except Exception as e:
            logger.warning(f"docx2txt failed ({e}) — falling back to raw text extraction")
            text = data.decode("utf-8", errors="replace")

        if not text or not text.strip():
            text = f"[Document: {filename}] — Could not extract text content."

        return [Document(page_content=text, metadata={"source": filename, "type": "docx"})]

    def _load_csv(self, data: bytes, filename: str) -> List[Document]:
        import pandas as pd
        df   = pd.read_csv(io.BytesIO(data))
        docs = []

        summary = (
            f"File: {filename}\n"
            f"Shape: {df.shape[0]} rows × {df.shape[1]} columns\n"
            f"Columns: {', '.join(df.columns.tolist())}\n\n"
            f"First 10 rows:\n{df.head(10).to_string(index=False)}"
        )
        docs.append(Document(page_content=summary, metadata={"source": filename, "type": "csv_summary"}))

        try:
            stats = "Statistical summary:\n" + df.describe(include="all").to_string()
            docs.append(Document(page_content=stats, metadata={"source": filename, "type": "csv_stats"}))
        except Exception as e:
            logger.warning(f"CSV stats failed: {e}")

        try:
            for i in range(0, min(len(df), 500), 50):
                chunk = f"Rows {i}{i+50}:\n{df.iloc[i:i+50].to_string(index=False)}"
                docs.append(Document(page_content=chunk, metadata={"source": filename, "type": "csv_rows"}))
        except Exception as e:
            logger.warning(f"CSV row chunking failed: {e}")

        return docs

    def _load_excel(self, data: bytes, filename: str) -> List[Document]:
        import pandas as pd
        xl   = pd.ExcelFile(io.BytesIO(data))
        docs = []
        for sheet in xl.sheet_names:
            try:
                df = xl.parse(sheet)
                text = (
                    f"Sheet: {sheet} | {df.shape[0]} rows × {df.shape[1]} cols\n"
                    f"Columns: {', '.join(str(c) for c in df.columns)}\n\n"
                    f"{df.head(10).to_string(index=False)}"
                )
                docs.append(Document(page_content=text, metadata={
                    "source": filename, "type": "excel", "sheet": sheet
                }))
            except Exception as e:
                logger.warning(f"Excel sheet '{sheet}' failed: {e}")
        return docs

    # ══════════════════════════════════════════════════════════════════════════
    # IMAGE LOADING — v3: OCR + Color Analysis + BLIP + VLM
    # ══════════════════════════════════════════════════════════════════════════

    def _load_image(self, data: bytes, filename: str) -> List[Document]:
        logger.info(f"Processing image: {filename}")

        ocr_text        = self._ocr_image(data, filename)
        color_info      = self._analyze_colors(data, filename)
        blip_caption    = self._caption_image_blip(data, filename)
        vlm_description = self._describe_image_with_vlm(data, filename, blip_caption, ocr_text)

        sections = [f"Image file: {filename}", ""]

        if ocr_text:
            sections.append("=== TEXT FOUND IN IMAGE (OCR) ===")
            sections.append(ocr_text)
            sections.append("")

        if color_info:
            sections.append("=== COLOR ANALYSIS ===")
            sections.append(color_info)
            sections.append("")

        sections.append("=== SHORT CAPTION ===")
        sections.append(blip_caption)
        sections.append("")

        sections.append("=== DETAILED VISUAL DESCRIPTION ===")
        sections.append(vlm_description)
        sections.append("")

        summary_parts = [f"This image ({filename})"]
        if ocr_text:
            summary_parts.append(f'contains the text: "{ocr_text}"')
        if color_info:
            summary_parts.append(f"has {color_info.lower()}")
        summary_parts.append(f"and shows: {blip_caption}")

        sections.append("=== SUMMARY ===")
        sections.append(". ".join(summary_parts) + ".")
        sections.append(f"Detailed: {vlm_description}")

        text = "\n".join(sections)
        logger.info(f"Image document length: {len(text)} chars "
                     f"(OCR: {len(ocr_text)} chars, colors: {bool(color_info)}, "
                     f"BLIP: {len(blip_caption)} chars, VLM: {len(vlm_description)} chars)")

        return [Document(
            page_content=text,
            metadata={
                "source": filename,
                "type": "image",
                "ocr_text": ocr_text[:500] if ocr_text else "",
                "caption": blip_caption,
                "colors": color_info,
            }
        )]

    # ── OCR ──────────────────────────────────────────────────────────────────

    def _ocr_image(self, data: bytes, filename: str) -> str:
        try:
            import pytesseract
            from PIL import Image

            img = Image.open(io.BytesIO(data))
            if img.mode not in ("RGB", "L"):
                img = img.convert("RGB")

            text = pytesseract.image_to_string(img).strip()

            if not text or len(text) < 2:
                gray = img.convert("L")
                w, h = gray.size
                if w < 1000 or h < 1000:
                    scale = max(1000 / w, 1000 / h, 1)
                    gray = gray.resize((int(w * scale), int(h * scale)), Image.LANCZOS)
                text = pytesseract.image_to_string(gray).strip()

            if text:
                text = re.sub(r'\n{3,}', '\n\n', text).strip()
                logger.info(f"OCR extracted text from {filename}: '{text[:100]}...'")
                return text
            else:
                logger.info(f"OCR found no text in {filename}")
                return ""

        except ImportError:
            logger.warning("pytesseract not installed — skipping OCR.")
            return self._ocr_image_api(data, filename)
        except Exception as e:
            logger.warning(f"OCR failed for {filename}: {e}")
            return self._ocr_image_api(data, filename)

    def _ocr_image_api(self, data: bytes, filename: str) -> str:
        hf_token = os.environ.get("HF_TOKEN", "")
        if not hf_token:
            return ""

        ocr_models = [
            "microsoft/trocr-large-printed",
            "microsoft/trocr-base-printed",
        ]

        for model_id in ocr_models:
            try:
                resp = requests.post(
                    f"https://api-inference.huggingface.co/models/{model_id}",
                    headers={"Authorization": f"Bearer {hf_token}"},
                    data=data,
                    timeout=30,
                )
                if resp.status_code == 200:
                    result = resp.json()
                    if isinstance(result, list) and result:
                        text = result[0].get("generated_text", "").strip()
                        if text:
                            return text
            except Exception as e:
                logger.warning(f"OCR API failed ({model_id}): {e}")
        return ""

    # ── Color Analysis ───────────────────────────────────────────────────────

    def _analyze_colors(self, data: bytes, filename: str) -> str:
        try:
            from PIL import Image

            img = Image.open(io.BytesIO(data)).convert("RGB")
            img_small = img.resize((80, 80), Image.LANCZOS)
            pixels = list(img_small.getdata())

            color_names = [_classify_color(r, g, b) for r, g, b in pixels]
            counter = Counter(color_names)
            total = len(pixels)

            dominant = [
                (name, count / total * 100)
                for name, count in counter.most_common(5)
                if count / total * 100 >= 3
            ]

            if not dominant:
                return ""

            bg_color = dominant[0][0]
            result = "dominant colors: " + ", ".join(
                f"{name} ({pct:.0f}%)" for name, pct in dominant
            )
            result += f". The background appears to be {bg_color}."
            logger.info(f"Color analysis for {filename}: {result}")
            return result

        except Exception as e:
            logger.warning(f"Color analysis failed for {filename}: {e}")
            return ""

    # ── BLIP Caption ─────────────────────────────────────────────────────────

    def _caption_image_blip(self, data: bytes, filename: str) -> str:
        hf_token = os.environ.get("HF_TOKEN", "")
        if not hf_token:
            return f"[Image: {filename}] — Add HF_TOKEN to enable captioning."

        caption_models = [
            "Salesforce/blip-image-captioning-large",
            "Salesforce/blip-image-captioning-base",
            "nlpconnect/vit-gpt2-image-captioning",
        ]

        for model_id in caption_models:
            try:
                resp = requests.post(
                    f"https://api-inference.huggingface.co/models/{model_id}",
                    headers={"Authorization": f"Bearer {hf_token}"},
                    data=data,  # raw bytes, NOT json
                    timeout=30,
                )
                if resp.status_code == 200:
                    result = resp.json()
                    if isinstance(result, list) and result:
                        caption = result[0].get("generated_text", "")
                        if caption:
                            logger.info(f"BLIP caption ({model_id}): {caption[:80]}")
                            return caption
                elif resp.status_code == 503:
                    logger.info(f"{model_id} is loading, waiting 10s...")
                    time.sleep(10)
                    resp2 = requests.post(
                        f"https://api-inference.huggingface.co/models/{model_id}",
                        headers={"Authorization": f"Bearer {hf_token}"},
                        data=data,
                        timeout=45,
                    )
                    if resp2.status_code == 200:
                        result = resp2.json()
                        if isinstance(result, list) and result:
                            caption = result[0].get("generated_text", "")
                            if caption:
                                return caption
                else:
                    logger.warning(f"BLIP {model_id}: {resp.status_code}: {resp.text[:100]}")
            except Exception as e:
                logger.warning(f"BLIP caption failed ({model_id}): {e}")
                continue

        return f"An image named {filename} was uploaded."

    # ── VLM Detailed Description ─────────────────────────────────────────────

    def _describe_image_with_vlm(self, data: bytes, filename: str,
                                  short_caption: str, ocr_text: str) -> str:
        hf_token = os.environ.get("HF_TOKEN", "")
        if not hf_token:
            return short_caption

        mime = "image/jpeg"
        if data[:8] == b'\x89PNG\r\n\x1a\n':
            mime = "image/png"
        elif data[:4] == b'RIFF' and data[8:12] == b'WEBP':
            mime = "image/webp"

        b64_image = base64.b64encode(data).decode("utf-8")
        image_url = f"data:{mime};base64,{b64_image}"

        headers = {
            "Authorization": f"Bearer {hf_token}",
            "Content-Type": "application/json",
        }

        ocr_hint = ""
        if ocr_text:
            ocr_hint = (
                f"\n\nNote: An OCR system already detected this text in the image: "
                f'"{ocr_text}". Please confirm or correct this text reading.'
            )

        prompt_text = (
            "Analyze this image thoroughly and provide a detailed description. "
            "You MUST address ALL of the following:\n\n"
            "1. TEXT: Read and transcribe ALL text visible in the image, "
            "character by character, word by word. Include any titles, labels, "
            "captions, watermarks, or writing of any kind. If there is text, "
            "quote it exactly.\n\n"
            "2. COLORS: What are the exact colors visible? What is the "
            "background color? What color is the text (if any)? List all "
            "significant colors.\n\n"
            "3. OBJECTS & LAYOUT: What objects, shapes, people, or elements "
            "are in the image? Where are they positioned?\n\n"
            "4. CONTEXT: What type of image is this (photo, screenshot, "
            "diagram, logo, meme, sign, document, etc.)?\n\n"
            "Be specific and factual. Do not guess or make assumptions about "
            "things you cannot see."
            f"{ocr_hint}"
        )

        for model_id in VLM_MODELS:
            try:
                logger.info(f"Trying VLM description with {model_id}...")
                payload = {
                    "model": model_id,
                    "messages": [
                        {
                            "role": "user",
                            "content": [
                                {
                                    "type": "image_url",
                                    "image_url": {"url": image_url},
                                },
                                {
                                    "type": "text",
                                    "text": prompt_text,
                                },
                            ],
                        }
                    ],
                    "max_tokens": 700,
                    "temperature": 0.1,
                    "stream": False,
                }

                resp = requests.post(
                    HF_API_URL,
                    headers=headers,
                    data=json.dumps(payload),
                    timeout=60,
                )

                if resp.status_code == 200:
                    raw = resp.json()["choices"][0]["message"]["content"].strip()
                    description = _strip_thinking(raw)
                    if description and len(description) > 20:
                        logger.info(f"VLM description ({model_id}): {description[:100]}...")
                        return description
                else:
                    logger.warning(f"VLM {model_id}: {resp.status_code}: {resp.text[:150]}")
            except Exception as e:
                logger.warning(f"VLM description failed ({model_id}): {e}")
                continue

        return self._expand_caption_with_llm(short_caption, ocr_text, filename)

    def _expand_caption_with_llm(self, caption: str, ocr_text: str, filename: str) -> str:
        hf_token = os.environ.get("HF_TOKEN", "")
        if not hf_token:
            parts = [caption]
            if ocr_text:
                parts.append(f'Text found in image: "{ocr_text}"')
            return " ".join(parts)

        headers = {
            "Authorization": f"Bearer {hf_token}",
            "Content-Type": "application/json",
        }

        ocr_section = ""
        if ocr_text:
            ocr_section = f'\nOCR text extracted from the image: "{ocr_text}"'

        messages = [
            {
                "role": "system",
                "content": (
                    "You are an image description assistant. You are given information "
                    "extracted from an image (a short AI caption and OCR text). "
                    "Combine this information into a clear, factual description. "
                    "If OCR text was found, make sure to include it prominently. "
                    "Do NOT invent details that aren't supported by the provided info."
                ),
            },
            {
                "role": "user",
                "content": (
                    f"Image file: '{filename}'\n"
                    f"AI caption: \"{caption}\"\n"
                    f"{ocr_section}\n\n"
                    f"Please provide a consolidated description of this image."
                ),
            },
        ]

        for model_id in CANDIDATE_MODELS:
            try:
                resp = requests.post(
                    HF_API_URL,
                    headers=headers,
                    data=json.dumps({
                        "model": model_id,
                        "messages": messages,
                        "max_tokens": 400,
                        "temperature": 0.2,
                        "stream": False,
                    }),
                    timeout=45,
                )
                if resp.status_code == 200:
                    raw = resp.json()["choices"][0]["message"]["content"].strip()
                    expanded = _strip_thinking(raw)
                    if expanded and len(expanded) > 30:
                        return expanded
            except Exception as e:
                logger.warning(f"Caption expansion failed ({model_id}): {e}")
                continue

        parts = [caption]
        if ocr_text:
            parts.append(f'Text visible in image: "{ocr_text}"')
        return " ".join(parts)

    # ══════════════════════════════════════════════════════════════════════════
    # INDEXING — ADDITIVE (does NOT destroy existing data)
    # ══════════════════════════════════════════════════════════════════════════

    def _index(self, docs: List[Document], filename: str) -> int:
        chunks = self._splitter.split_documents(docs)

        if not chunks:
            logger.warning(f"No chunks produced from {filename}")
            return 0

        # Generate unique, stable chunk IDs for this file
        safe_name = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
        name_hash = hashlib.md5(filename.encode()).hexdigest()[:8]
        chunk_ids = [f"{safe_name}_{name_hash}__chunk__{i}" for i in range(len(chunks))]

        # Create vectorstore if this is the first file
        if self._vectorstore is None:
            self._vectorstore = Chroma(
                collection_name=COLLECTION_NAME,
                embedding_function=self.embeddings,
                client_settings=Settings(anonymized_telemetry=False),
            )

        # Add chunks to the existing vectorstore (additive!)
        texts     = [c.page_content for c in chunks]
        metadatas = [c.metadata for c in chunks]
        self._vectorstore.add_texts(texts=texts, metadatas=metadatas, ids=chunk_ids)

        # Track this file
        self._documents[filename] = {
            "chunk_count": len(chunks),
            "chunk_ids":   chunk_ids,
            "type":        get_suffix(filename),
        }

        logger.info(
            f"Indexed {len(chunks)} chunks from '{filename}' "
            f"(total files: {len(self._documents)}, total chunks: {self.get_total_chunks()})"
        )
        return len(chunks)

    # ── Query ────────────────────────────────────────────────────────────────

    def query(self, question: str) -> Tuple[str, List[str]]:
        if not self._documents or self._vectorstore is None:
            return "Please upload a document first.", []

        t0 = time.time()
        error = answer = model_used = ""
        sources = []

        try:
            # ── Step 1: Over-fetch candidates ────────────────────────────────
            # Retrieve more candidates than needed so the reranker can pick
            # the truly relevant ones. Scale with number of loaded files.
            num_files = len(self._documents)
            fetch_k   = max(RERANK_FETCH_K, RERANK_FETCH_K + (num_files - 1) * 2)
            initial_k = fetch_k  # MMR will return this many diverse candidates

            retriever = self._vectorstore.as_retriever(
                search_type="mmr",
                search_kwargs={"k": initial_k, "fetch_k": fetch_k * 2},
            )
            candidate_docs = retriever.invoke(question)

            # ── Step 2: Rerank with cross-encoder ────────────────────────────
            # The cross-encoder scores each (query, chunk) pair for true
            # semantic relevance — much more accurate than embedding distance.
            final_k = min(TOP_K + num_files - 1, 6)
            docs = self._rerank_documents(question, candidate_docs, top_k=final_k)

            context = "\n\n---\n\n".join(
                f"[Chunk {i+1} | source: {d.metadata.get('source', '?')} | type: {d.metadata.get('type','text')}]\n{d.page_content}"
                for i, d in enumerate(docs)
            )
            sources = list({d.metadata.get("source", "Document") for d in docs})
            answer, model_used = self._generate(question, context)
            self.add_to_memory(question, answer)

        except Exception as e:
            error  = str(e)
            answer = f"Error: {error}"
            logger.error(f"Query error: {e}")
        finally:
            monitor.log_query(question, answer, sources, (time.time() - t0) * 1000, model_used, TOP_K, error)

        return answer, sources

    # ── LLM ──────────────────────────────────────────────────────────────────

    def _generate(self, question: str, context: str) -> Tuple[str, str]:
        hf_token = os.environ.get("HF_TOKEN", "")
        if not hf_token:
            return (
                "HF_TOKEN not set. Add it as a Secret in Space Settings.\n\n"
                "Best matching excerpt:\n\n" + _extract_best(question, context),
                "none"
            )

        # ── Build doc-type hints from ALL loaded files ────────────────────────
        loaded_types = set(info["type"] for info in self._documents.values())
        all_names    = list(self._documents.keys())

        hints = []
        image_types = {".jpg", ".jpeg", ".png", ".webp"}
        table_types = {".csv", ".xlsx", ".xls"}

        if loaded_types & image_types:
            hints.append(
                "Some documents are IMAGES. Their context contains:\n"
                "  - OCR-extracted text (actual text visible in the image)\n"
                "  - Color analysis (dominant colors detected)\n"
                "  - AI-generated visual descriptions\n"
                "When asked about text in an image, refer to the OCR section. "
                "When asked about colors, refer to the color analysis. "
                "When asked what an image shows, use the descriptions. "
                "Be specific and quote the actual text/colors from the context."
            )
        if loaded_types & table_types:
            hints.append(
                "Some documents are tabular data (spreadsheet/CSV). "
                "Refer to column names and values precisely."
            )
        if loaded_types & {".docx", ".doc"}:
            hints.append("Some documents are Word documents.")

        doc_type_hint = "\n".join(hints)

        # ── File list for the system prompt ───────────────────────────────────
        if len(all_names) == 1:
            files_str = f"You are analyzing: '{all_names[0]}'."
        else:
            files_list = ", ".join(f"'{n}'" for n in all_names)
            files_str = f"You have {len(all_names)} documents loaded: {files_list}."

        system_prompt = (
            f"You are DocMind AI, an expert document analyst built by Ryan Farahani.\n"
            f"{files_str}\n"
            f"{doc_type_hint}\n"
            "Answer using ONLY the provided document context. "
            "When the context contains chunks from multiple files, indicate which file "
            "the information comes from if relevant.\n"
            "Be concise and precise. No preamble. No reasoning out loud. Just answer.\n"
            "If asked a follow-up question, use the conversation history for context."
        )

        messages = [{"role": "system", "content": system_prompt}]
        memory   = self.get_memory_messages()

        if memory:
            messages.append({
                "role":    "system",
                "content": f"Current document context:\n{context}"
            })
            messages.extend(memory)
            messages.append({"role": "user", "content": question})
        else:
            messages.append({
                "role":    "user",
                "content": f"Document context:\n{context}\n\n---\nQuestion: {question}"
            })

        headers    = {"Authorization": f"Bearer {hf_token}", "Content-Type": "application/json"}
        last_error = ""

        for model_id in CANDIDATE_MODELS:
            try:
                resp = requests.post(
                    HF_API_URL,
                    headers=headers,
                    data=json.dumps({
                        "model":       model_id,
                        "messages":    messages,
                        "max_tokens":  500,
                        "temperature": 0.1,
                        "stream":      False,
                    }),
                    timeout=60,
                )
                if resp.status_code == 200:
                    raw    = resp.json()["choices"][0]["message"]["content"].strip()
                    answer = _strip_thinking(raw)
                    if answer:
                        return answer, model_id
                else:
                    last_error = f"{model_id}{resp.status_code}: {resp.text[:150]}"
                    logger.warning(last_error)
            except Exception as e:
                last_error = str(e)
                logger.warning(f"Exception on {model_id}: {e}")
                continue

        return (
            "AI unavailable. Most relevant excerpt:\n\n"
            + _extract_best(question, context)
            + f"\n\n(Error: {last_error})",
            "fallback"
        )


# ── Helpers ──────────────────────────────────────────────────────────────────

def _strip_thinking(text: str) -> str:
    text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL).strip()
    starters = [
        "okay", "ok,", "alright", "let me", "let's", "i need", "i will",
        "i'll", "first,", "so,", "the user", "looking at", "going through",
        "based on the chunk", "parsing", "to answer", "in order to",
    ]
    lines = text.split("\n")
    clean, found = [], False
    for line in lines:
        lower = line.strip().lower()
        if not found:
            if line.strip() and not any(lower.startswith(p) for p in starters):
                found = True
                clean.append(line)
        else:
            clean.append(line)
    return "\n".join(clean).strip() or text


def _extract_best(question: str, context: str) -> str:
    keywords   = set(re.findall(r'\b\w{4,}\b', question.lower()))
    best, score = "", 0
    for chunk in context.split("---"):
        s = len(keywords & set(re.findall(r'\b\w{4,}\b', chunk.lower())))
        if s > score:
            score, best = s, chunk.strip()
    return (best[:600] + "...") if len(best) > 600 else best or "No relevant content found."