File size: 28,114 Bytes
9b56ad1
1dedfac
c8716d2
6705397
1dedfac
 
 
 
 
 
d7bf74b
3406461
 
253bfed
c8716d2
 
6705397
3406461
1dedfac
253bfed
1dedfac
253bfed
c8716d2
 
1dedfac
c8716d2
1dedfac
 
 
 
253bfed
1dedfac
 
3406461
253bfed
 
c8716d2
 
1dedfac
 
c8716d2
a8283c8
c8716d2
 
 
 
 
 
 
3406461
c8716d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253bfed
 
c8716d2
 
 
 
253bfed
 
 
c8716d2
 
253bfed
c8716d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253bfed
 
 
 
 
c8716d2
 
253bfed
 
c8716d2
253bfed
 
c8716d2
 
253bfed
c8716d2
 
 
 
 
 
 
 
a8283c8
c8716d2
 
a8283c8
 
c8716d2
 
a8283c8
c8716d2
 
1dedfac
c8716d2
 
 
 
1dedfac
253bfed
c8716d2
 
611ac83
3406461
253bfed
3406461
 
253bfed
 
 
c8716d2
253bfed
 
 
 
 
c8716d2
253bfed
 
 
 
 
c8716d2
253bfed
 
 
 
 
c8716d2
253bfed
 
 
 
 
c8716d2
 
253bfed
3406461
 
253bfed
 
 
 
d7bf74b
 
253bfed
c8716d2
253bfed
 
d7bf74b
3406461
253bfed
3406461
253bfed
 
c8716d2
253bfed
 
 
 
c8716d2
253bfed
c8716d2
253bfed
 
 
 
 
 
3406461
 
253bfed
3406461
6705397
c8716d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253bfed
 
 
c8716d2
 
 
 
 
 
253bfed
 
 
c8716d2
 
 
253bfed
c8716d2
253bfed
 
 
c8716d2
253bfed
1dedfac
c8716d2
1dedfac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8716d2
1dedfac
 
 
 
c8716d2
3406461
 
c8716d2
253bfed
c8716d2
253bfed
1dedfac
 
253bfed
1dedfac
 
3406461
1dedfac
 
 
 
3406461
253bfed
 
1dedfac
 
 
 
253bfed
3406461
 
 
1dedfac
3406461
c8716d2
3406461
 
 
 
 
c8716d2
1dedfac
dcc21e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dedfac
dcc21e5
1dedfac
dcc21e5
 
 
 
 
 
 
 
 
 
 
1dedfac
dcc21e5
1dedfac
 
253bfed
1dedfac
 
 
 
 
 
 
 
 
 
 
 
 
253bfed
1dedfac
 
 
 
 
 
 
253bfed
d7bf74b
3406461
 
1dedfac
253bfed
 
3406461
 
1dedfac
 
3406461
 
 
c8716d2
1dedfac
253bfed
 
1dedfac
 
 
 
253bfed
1dedfac
 
 
 
 
 
 
3406461
c8716d2
253bfed
c8716d2
1dedfac
 
 
 
c8716d2
dcc21e5
1dedfac
c8716d2
1dedfac
 
c8716d2
1dedfac
 
 
253bfed
1dedfac
c8716d2
253bfed
dcc21e5
 
c8716d2
3406461
c8716d2
dcc21e5
1dedfac
253bfed
c8716d2
dcc21e5
611ac83
1dedfac
dcc21e5
1dedfac
 
 
 
 
 
 
3406461
c8716d2
dcc21e5
c8716d2
253bfed
3406461
dcc21e5
c8716d2
1dedfac
 
c8716d2
1dedfac
dcc21e5
c8716d2
 
1dedfac
c8716d2
dcc21e5
 
c8716d2
dcc21e5
 
c8716d2
dcc21e5
c8716d2
 
 
dcc21e5
c8716d2
dcc21e5
 
 
 
 
 
 
 
 
 
 
c8716d2
dcc21e5
 
 
 
 
c8716d2
dcc21e5
c8716d2
dcc21e5
c8716d2
 
 
1dedfac
 
3406461
dcc21e5
c8716d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea914de
dcc21e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8716d2
253bfed
3406461
9b56ad1
253bfed
1dedfac
253bfed
1dedfac
253bfed
1dedfac
c8716d2
1dedfac
c8716d2
 
 
253bfed
c8716d2
1dedfac
 
3406461
1dedfac
 
 
3406461
1dedfac
3406461
 
1dedfac
3406461
1dedfac
 
 
c8716d2
3406461
1dedfac
 
 
 
 
 
 
 
 
c8716d2
1dedfac
 
 
253bfed
c8716d2
1dedfac
 
3406461
 
253bfed
3406461
1dedfac
 
 
c8716d2
3406461
1dedfac
 
 
 
 
 
 
 
 
3406461
 
 
 
 
1dedfac
 
9b56ad1
253bfed
6705397
1dedfac
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import PyPDF2
import docx
import io
import os
import re
from typing import List, Optional, Dict, Tuple
import json
from collections import Counter
import warnings
warnings.filterwarnings("ignore")

class SmartDocumentRAG:
    def __init__(self):
        print("πŸš€ Initializing Enhanced Smart RAG System...")
        
        # Initialize better embedding model
        self.embedder = SentenceTransformer('all-MiniLM-L6-v2')  # Faster and good quality
        print("βœ… Embedding model loaded")
        
        # Initialize optimized LLM with better quantization
        self.setup_llm()
        
        # Document storage
        self.documents = []
        self.document_metadata = []
        self.index = None
        self.is_indexed = False
        self.raw_text = ""
        self.document_type = "general"
        self.document_summary = ""
        self.sentence_embeddings = []
        self.sentences = []
        
    def setup_llm(self):
        """Setup optimized model with better quantization"""
        try:
            # Check CUDA availability
            device = "cuda" if torch.cuda.is_available() else "cpu"
            print(f"πŸ”§ Using device: {device}")
            
            if device == "cuda":
                self.setup_gpu_model()
            else:
                self.setup_cpu_model()
                    
        except Exception as e:
            print(f"❌ Error loading models: {e}")
            self.setup_fallback_model()

    def setup_gpu_model(self):
        """Setup GPU model with proper quantization"""
        try:
            # Use Phi-2 - excellent for Q&A and reasoning
            model_name = "microsoft/DialoGPT-medium"
            
            # Better quantization config
            quantization_config = BitsAndBytesConfig(
                load_in_4bit=True,
                bnb_4bit_compute_dtype=torch.float16,
                bnb_4bit_use_double_quant=True,
                bnb_4bit_quant_type="nf4",
                bnb_4bit_quant_storage=torch.uint8
            )
            
            try:
                # Try Flan-T5 first - excellent for Q&A
                model_name = "google/flan-t5-base"
                print(f"πŸ€– Loading {model_name}...")
                
                self.tokenizer = AutoTokenizer.from_pretrained(model_name)
                self.model = AutoModelForCausalLM.from_pretrained(
                    model_name,
                    quantization_config=quantization_config,
                    device_map="auto",
                    torch_dtype=torch.float16,
                    trust_remote_code=True
                )
                
                # Create pipeline for easier use
                self.qa_pipeline = pipeline(
                    "text2text-generation",
                    model=self.model,
                    tokenizer=self.tokenizer,
                    max_length=512,
                    do_sample=True,
                    temperature=0.3,
                    top_p=0.9
                )
                
                print("βœ… Flan-T5 model loaded successfully")
                self.model_type = "flan-t5"
                
            except Exception as e:
                print(f"Flan-T5 failed, trying Phi-2: {e}")
                # Try Phi-2 as backup
                model_name = "microsoft/phi-2"
                print(f"πŸ€– Loading {model_name}...")
                
                self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
                self.model = AutoModelForCausalLM.from_pretrained(
                    model_name,
                    quantization_config=quantization_config,
                    device_map="auto",
                    torch_dtype=torch.float16,
                    trust_remote_code=True
                )
                
                if self.tokenizer.pad_token is None:
                    self.tokenizer.pad_token = self.tokenizer.eos_token
                    
                print("βœ… Phi-2 model loaded successfully")
                self.model_type = "phi-2"
                
        except Exception as e:
            print(f"❌ GPU models failed: {e}")
            self.setup_cpu_model()

    def setup_cpu_model(self):
        """Setup CPU-optimized model"""
        try:
            # Use DistilBERT for Q&A - much better than DialoGPT for this task
            model_name = "distilbert-base-cased-distilled-squad"
            print(f"πŸ€– Loading CPU model: {model_name}")
            
            self.qa_pipeline = pipeline(
                "question-answering",
                model=model_name,
                tokenizer=model_name
            )
            self.model_type = "distilbert-qa"
            print("βœ… DistilBERT Q&A model loaded successfully")
            
        except Exception as e:
            print(f"❌ CPU model failed: {e}")
            self.setup_fallback_model()

    def setup_fallback_model(self):
        """Fallback to basic model"""
        try:
            print("πŸ€– Loading fallback model...")
            self.qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
            self.model_type = "fallback"
            print("βœ… Fallback model loaded")
        except Exception as e:
            print(f"❌ All models failed: {e}")
            self.qa_pipeline = None
            self.model_type = "none"

    def detect_document_type(self, text: str) -> str:
        """Enhanced document type detection"""
        text_lower = text.lower()
        
        resume_patterns = [
            'experience', 'skills', 'education', 'linkedin', 'email', 'phone', 
            'work experience', 'employment', 'resume', 'cv', 'curriculum vitae',
            'internship', 'projects', 'achievements', 'career', 'profile', 'objective'
        ]
        
        research_patterns = [
            'abstract', 'introduction', 'methodology', 'conclusion', 'references',
            'literature review', 'hypothesis', 'study', 'research', 'findings',
            'data analysis', 'results', 'discussion', 'bibliography', 'journal'
        ]
        
        business_patterns = [
            'company', 'revenue', 'market', 'strategy', 'business', 'financial',
            'quarter', 'profit', 'sales', 'growth', 'investment', 'stakeholder',
            'operations', 'management', 'corporate', 'enterprise', 'budget'
        ]
        
        technical_patterns = [
            'implementation', 'algorithm', 'system', 'technical', 'specification',
            'architecture', 'development', 'software', 'programming', 'api',
            'database', 'framework', 'deployment', 'infrastructure', 'code'
        ]
        
        def count_matches(patterns, text):
            score = 0
            for pattern in patterns:
                count = text.count(pattern)
                score += count * (2 if len(pattern.split()) > 1 else 1)  # Weight phrases higher
            return score
        
        scores = {
            'resume': count_matches(resume_patterns, text_lower),
            'research': count_matches(research_patterns, text_lower),
            'business': count_matches(business_patterns, text_lower),
            'technical': count_matches(technical_patterns, text_lower)
        }
        
        max_score = max(scores.values())
        if max_score > 5:  # Higher threshold
            return max(scores, key=scores.get)
        return 'general'

    def create_document_summary(self, text: str) -> str:
        """Enhanced document summary creation"""
        try:
            clean_text = re.sub(r'\s+', ' ', text).strip()
            sentences = re.split(r'[.!?]+', clean_text)
            sentences = [s.strip() for s in sentences if len(s.strip()) > 30]
            
            if not sentences:
                return "Document contains basic information."
            
            # Use first few sentences and key information
            if self.document_type == 'resume':
                return self.extract_resume_summary(sentences, clean_text)
            elif self.document_type == 'research':
                return self.extract_research_summary(sentences)
            elif self.document_type == 'business':
                return self.extract_business_summary(sentences)
            else:
                return self.extract_general_summary(sentences)
                
        except Exception as e:
            print(f"Summary creation error: {e}")
            return "Document summary not available."

    def extract_resume_summary(self, sentences: List[str], full_text: str) -> str:
        """Extract resume-specific summary with better name detection"""
        summary_parts = []
        
        # Extract name using multiple patterns
        name = self.extract_name(full_text)
        if name:
            summary_parts.append(f"Resume of {name}")
        
        # Extract role/title
        role_patterns = [
            r'(?:software|senior|junior|lead|principal)?\s*(?:engineer|developer|analyst|manager|designer|architect|consultant)',
            r'(?:full stack|frontend|backend|data|ml|ai)\s*(?:engineer|developer)',
            r'(?:product|project|technical)\s*manager'
        ]
        
        for sentence in sentences[:5]:
            for pattern in role_patterns:
                matches = re.findall(pattern, sentence.lower())
                if matches:
                    summary_parts.append(f"working as {matches[0].title()}")
                    break
        
        # Extract experience
        exp_match = re.search(r'(\d+)[\+\-\s]*(?:years?|yrs?)\s*(?:of\s*)?(?:experience|exp)', full_text.lower())
        if exp_match:
            summary_parts.append(f"with {exp_match.group(1)}+ years of experience")
        
        return '. '.join(summary_parts) + '.' if summary_parts else "Professional resume with career details."

    def extract_name(self, text: str) -> str:
        """Extract name from document using multiple strategies"""
        # Strategy 1: Look for name patterns at the beginning
        lines = text.split('\n')[:10]  # First 10 lines
        
        for line in lines:
            line = line.strip()
            if len(line) < 50 and len(line) > 3:  # Likely a header line
                # Check if it looks like a name
                name_match = re.match(r'^([A-Z][a-z]+\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)(?:\s|$)', line)
                if name_match:
                    return name_match.group(1)
        
        # Strategy 2: Look for "Name:" pattern
        name_patterns = [
            r'(?:name|full name):\s*([A-Z][a-z]+\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)',
            r'^([A-Z][a-z]+\s+[A-Z][a-z]+)(?:\s*\n|\s*email|\s*phone|\s*linkedin)',
        ]
        
        for pattern in name_patterns:
            match = re.search(pattern, text, re.MULTILINE | re.IGNORECASE)
            if match:
                return match.group(1)
        
        return ""

    def extract_research_summary(self, sentences: List[str]) -> str:
        """Extract research paper summary"""
        # Look for abstract or introduction
        for sentence in sentences[:5]:
            if any(word in sentence.lower() for word in ['abstract', 'study', 'research', 'paper']):
                return sentence[:200] + ('...' if len(sentence) > 200 else '')
        
        return "Research document with academic content."

    def extract_business_summary(self, sentences: List[str]) -> str:
        """Extract business document summary"""
        for sentence in sentences[:3]:
            if any(word in sentence.lower() for word in ['company', 'business', 'organization']):
                return sentence[:200] + ('...' if len(sentence) > 200 else '')
        
        return "Business document with organizational information."

    def extract_general_summary(self, sentences: List[str]) -> str:
        """Extract general document summary"""
        return sentences[0][:200] + ('...' if len(sentences[0]) > 200 else '') if sentences else "General document."

    def extract_text_from_file(self, file_path: str) -> str:
        """Enhanced text extraction"""
        try:
            file_extension = os.path.splitext(file_path)[1].lower()
            
            if file_extension == '.pdf':
                return self.extract_from_pdf(file_path)
            elif file_extension == '.docx':
                return self.extract_from_docx(file_path)
            elif file_extension == '.txt':
                return self.extract_from_txt(file_path)
            else:
                return f"Unsupported file format: {file_extension}"
                
        except Exception as e:
            return f"Error reading file: {str(e)}"
    
    def extract_from_pdf(self, file_path: str) -> str:
        """Enhanced PDF extraction"""
        text = ""
        try:
            with open(file_path, 'rb') as file:
                pdf_reader = PyPDF2.PdfReader(file)
                for page in pdf_reader.pages:
                    page_text = page.extract_text()
                    if page_text.strip():
                        # Better text cleaning
                        page_text = re.sub(r'\s+', ' ', page_text)
                        page_text = re.sub(r'([a-z])([A-Z])', r'\1 \2', page_text)  # Fix merged words
                        text += f"{page_text}\n"
        except Exception as e:
            text = f"Error reading PDF: {str(e)}"
        return text.strip()
    
    def extract_from_docx(self, file_path: str) -> str:
        """Enhanced DOCX extraction"""
        try:
            doc = docx.Document(file_path)
            text = ""
            for paragraph in doc.paragraphs:
                if paragraph.text.strip():
                    text += paragraph.text.strip() + "\n"
            return text.strip()
        except Exception as e:
            return f"Error reading DOCX: {str(e)}"
    
    def extract_from_txt(self, file_path: str) -> str:
        """Enhanced TXT extraction"""
        encodings = ['utf-8', 'latin-1', 'cp1252', 'iso-8859-1']
        
        for encoding in encodings:
            try:
                with open(file_path, 'r', encoding=encoding) as file:
                    return file.read().strip()
            except UnicodeDecodeError:
                continue
            except Exception as e:
                return f"Error reading TXT: {str(e)}"
        
        return "Error: Could not decode file"
    
    def enhanced_chunk_text(self, text: str, max_chunk_size: int = 300, overlap: int = 50) -> list[str]:
        """
        Splits text into smaller overlapping chunks for better semantic search.
        
        Args:
            text (str): The full text to chunk.
            max_chunk_size (int): Maximum tokens/words per chunk.
            overlap (int): Number of words overlapping between consecutive chunks.
    
        Returns:
            list[str]: List of text chunks.
        """
        import re
    
        # Clean and normalize whitespace
        text = re.sub(r'\s+', ' ', text).strip()
        
        words = text.split()
        chunks = []
        start = 0
        text_len = len(words)
    
        while start < text_len:
            end = min(start + max_chunk_size, text_len)
            chunk_words = words[start:end]
            chunk = ' '.join(chunk_words)
            chunks.append(chunk)
            # Move start forward by chunk size minus overlap to create overlap
            start += max_chunk_size - overlap
    
        return chunks

    
    def process_documents(self, files) -> str:
        """Enhanced document processing"""
        if not files:
            return "❌ No files uploaded!"
        
        try:
            all_text = ""
            processed_files = []
            
            for file in files:
                if file is None:
                    continue
                    
                file_text = self.extract_text_from_file(file.name)
                if not file_text.startswith("Error") and not file_text.startswith("Unsupported"):
                    all_text += f"\n{file_text}"
                    processed_files.append(os.path.basename(file.name))
                else:
                    return f"❌ {file_text}"
            
            if not all_text.strip():
                return "❌ No text extracted from files!"
            
            # Store and analyze
            self.raw_text = all_text
            self.document_type = self.detect_document_type(all_text)
            self.document_summary = self.create_document_summary(all_text)
            
            # Enhanced chunking
            chunk_data = self.enhanced_chunk_text(all_text)
            
            if not chunk_data:
                return "❌ No valid text chunks created!"
            
            self.documents = [chunk['text'] for chunk in chunk_data]
            self.document_metadata = chunk_data
            
            # Create embeddings
            print(f"πŸ“„ Creating embeddings for {len(self.documents)} chunks...")
            embeddings = self.embedder.encode(self.documents, show_progress_bar=False)
            
            # Build FAISS index
            dimension = embeddings.shape[1]
            self.index = faiss.IndexFlatIP(dimension)
            
            # Normalize for cosine similarity
            faiss.normalize_L2(embeddings)
            self.index.add(embeddings.astype('float32'))
            
            self.is_indexed = True
            
            return f"βœ… Successfully processed {len(processed_files)} files:\n" + \
                   f"πŸ“„ Files: {', '.join(processed_files)}\n" + \
                   f"πŸ“Š Document Type: {self.document_type.title()}\n" + \
                   f"πŸ” Created {len(self.documents)} chunks\n" + \
                   f"πŸ“ Summary: {self.document_summary}\n" + \
                   f"πŸš€ Ready for Q&A!"
            
        except Exception as e:
            return f"❌ Error processing documents: {str(e)}"
    
    def find_relevant_content(self, query: str, k: int = 3) -> str:
        """Improved content retrieval with stricter relevance filter"""
        if not self.is_indexed:
            return ""
        
        try:
            # Semantic search
            query_embedding = self.embedder.encode([query])
            faiss.normalize_L2(query_embedding)
            
            scores, indices = self.index.search(query_embedding.astype('float32'), min(k, len(self.documents)))
            
            relevant_chunks = []
            for i, idx in enumerate(indices[0]):
                score = scores[0][i]
                if idx < len(self.documents) and score > 0.4:  # βœ… stricter similarity filter
                    relevant_chunks.append(self.documents[idx])
            
            return ' '.join(relevant_chunks)
        
        except Exception as e:
            print(f"Error in content retrieval: {e}")
            return ""

    
    def answer_question(self, query: str) -> str:
        """Enhanced question answering with better model usage and hallucination reduction."""
        if not query.strip():
            return "❓ Please ask a question!"
        
        if not self.is_indexed:
            return "πŸ“ Please upload and process documents first!"
        
        try:
            query_lower = query.lower()
            
            # Handle summary requests explicitly
            if any(word in query_lower for word in ['summary', 'summarize', 'about', 'overview']):
                return f"πŸ“„ **Document Summary:**\n\n{self.document_summary}"
            
            # Retrieve relevant content chunks via semantic search
            context = self.find_relevant_content(query, k=3)
            
            if not context:
                return "πŸ” No relevant information found. Try rephrasing your question."
            
            # If no QA pipeline, fall back to direct extraction
            if self.qa_pipeline is None:
                return self.extract_direct_answer(query, context)
            
            try:
                if self.model_type in ["distilbert-qa", "fallback"]:
                    # Use extractive Q&A pipeline
                    result = self.qa_pipeline(question=query, context=context)
                    answer = result.get('answer', '').strip()
                    confidence = result.get('score', 0)
                    
                    if confidence > 0.1 and answer:
                        return f"**Answer:** {answer}\n\n**Context:** {context[:200]}..."
                    else:
                        return self.extract_direct_answer(query, context)
                
                elif self.model_type == "flan-t5":
                    # Use generative model with improved prompt to reduce hallucination
                    prompt = (
                        f"Answer concisely and strictly based on the following context.\n\n"
                        f"Context:\n{context}\n\n"
                        f"Question:\n{query}\n\n"
                        f"If the answer is not contained in the context, reply with 'Not found in document.'\n"
                        f"Answer:"
                    )
                    result = self.qa_pipeline(prompt, max_length=256, num_return_sequences=1)
                    generated_text = result[0].get('generated_text', '')
                    answer = generated_text.replace(prompt, '').strip()
                    
                    if answer.lower() in ["not found in document.", "no answer", "unknown", ""]:
                        return "πŸ” Sorry, the answer was not found in the documents."
                    else:
                        return f"**Answer:** {answer}"
                
                else:
                    # Default fallback extraction
                    return self.extract_direct_answer(query, context)
                
            except Exception as e:
                print(f"Model inference error: {e}")
                return self.extract_direct_answer(query, context)
            
        except Exception as e:
            return f"❌ Error processing question: {str(e)}"

    
    def extract_direct_answer(self, query: str, context: str) -> str:
        """Direct answer extraction as fallback"""
        query_lower = query.lower()
        
        # Name extraction
        if any(word in query_lower for word in ['name', 'who is', 'who']):
            names = re.findall(r'\b[A-Z][a-z]+ [A-Z][a-z]+\b', context)
            if names:
                return f"**Name:** {names[0]}"
        
        # Experience extraction
        if any(word in query_lower for word in ['experience', 'years']):
            exp_matches = re.findall(r'(\d+)[\+\-\s]*(?:years?|yrs?)', context.lower())
            if exp_matches:
                return f"**Experience:** {exp_matches[0]} years"
        
        # Skills extraction
        if any(word in query_lower for word in ['skill', 'technology', 'tech']):
            # Common tech skills
            tech_patterns = [
                r'\b(?:Python|Java|JavaScript|React|Node|SQL|AWS|Docker|Kubernetes|Git)\b',
                r'\b(?:HTML|CSS|Angular|Vue|Spring|Django|Flask|MongoDB|PostgreSQL)\b'
            ]
            skills = []
            for pattern in tech_patterns:
                skills.extend(re.findall(pattern, context, re.IGNORECASE))
            
            if skills:
                return f"**Skills mentioned:** {', '.join(set(skills))}"
        
        # Education extraction
        if any(word in query_lower for word in ['education', 'degree', 'university']):
            edu_matches = re.findall(r'(?:Bachelor|Master|PhD|B\.?S\.?|M\.?S\.?|B\.?A\.?|M\.?A\.?).*?(?:in|of)\s+([^.]+)', context)
            if edu_matches:
                return f"**Education:** {edu_matches[0]}"
        
        # Return first relevant sentence
        sentences = [s.strip() for s in context.split('.') if s.strip()]
        if sentences:
            return f"**Answer:** {sentences[0]}"
        
        return "I found relevant content but couldn't extract a specific answer."

    def clean_text(self, text: str) -> str:
        """
        Clean and normalize raw text by:
        - Removing excessive whitespace
        - Fixing merged words (camel case separation)
        - Removing unwanted characters (optional)
        - Lowercasing or preserving case (optional)
        """
        import re
    
        # Replace multiple whitespace/newlines/tabs with single space
        text = re.sub(r'\s+', ' ', text).strip()
        
        # Fix merged words like 'wordAnotherWord' -> 'word Another Word'
        text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
        
        # Optional: remove special characters except basic punctuation
        # text = re.sub(r'[^a-zA-Z0-9,.!?;:\'\"()\-\s]', '', text)
        
        return text





    

# Initialize the system
print("Initializing Enhanced Smart RAG System...")
rag_system = SmartDocumentRAG()

# Create the interface
def create_interface():
    with gr.Blocks(title="🧠 Enhanced Document Q&A", theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # 🧠 Enhanced Document Q&A System
        
        **Optimized with Better Models & Quantization!**
        
        **Features:**
        - 🎯 Flan-T5 or DistilBERT for accurate Q&A
        - ⚑ 4-bit quantization for GPU efficiency
        - πŸ“Š Direct answer extraction
        - πŸ” Enhanced semantic search
        """)
        
        with gr.Tab("πŸ“€ Upload & Process"):
            with gr.Row():
                with gr.Column():
                    file_upload = gr.File(
                        label="πŸ“ Upload Documents",
                        file_count="multiple",
                        file_types=[".pdf", ".docx", ".txt"],
                        height=150
                    )
                    process_btn = gr.Button("πŸ”„ Process Documents", variant="primary", size="lg")
                
                with gr.Column():
                    process_status = gr.Textbox(
                        label="πŸ“‹ Processing Status",
                        lines=10,
                        interactive=False
                    )
            
            process_btn.click(
                fn=rag_system.process_documents,
                inputs=[file_upload],
                outputs=[process_status]
            )
        
        with gr.Tab("❓ Q&A"):
            with gr.Row():
                with gr.Column():
                    question_input = gr.Textbox(
                        label="πŸ€” Ask Your Question",
                        placeholder="What is the person's name? / How many years of experience? / What skills do they have?",
                        lines=3
                    )
                    
                    with gr.Row():
                        ask_btn = gr.Button("🧠 Get Answer", variant="primary")
                        summary_btn = gr.Button("πŸ“Š Get Summary", variant="secondary")
                
                with gr.Column():
                    answer_output = gr.Textbox(
                        label="πŸ’‘ Answer",
                        lines=8,
                        interactive=False
                    )
            
            ask_btn.click(
                fn=rag_system.answer_question,
                inputs=[question_input],
                outputs=[answer_output]
            )
            
            summary_btn.click(
                fn=lambda: rag_system.answer_question("summary"),
                inputs=[],
                outputs=[answer_output]
            )
    
    return demo

# Launch the app
if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True
    )