Spaces:
Sleeping
Sleeping
File size: 27,905 Bytes
d1646a9 6ad61bb |
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 |
"""
RAG Engine Module
=================
Handles all RAG pipeline operations:
- PDF text extraction
- Text chunking with overlap
- Embedding generation using SentenceTransformers
- FAISS vector storage and retrieval
- Metadata and document registry management
- Persistence of embeddings and metadata
"""
import os
import json
import hashlib
from datetime import datetime
from typing import List, Dict, Tuple, Optional
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
import PyPDF2
import google.generativeai as genai
from PIL import Image
import io
# OCR imports (optional)
try:
import pytesseract
OCR_AVAILABLE = True
except ImportError:
OCR_AVAILABLE = False
print("Warning: pytesseract not installed. OCR functionality will be disabled.")
# ============================================
# CONFIGURATION
# ============================================
# Chunking parameters
DEFAULT_CHUNK_SIZE = 200 # words per chunk
DEFAULT_OVERLAP_SIZE = 50 # overlapping words
# Retrieval parameters
DEFAULT_TOP_K = 5 # number of chunks to retrieve
# Embedding model
EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
EMBEDDING_DIMENSION = 384
class RAGEngine:
"""
Main RAG Engine class that handles:
- Document processing and embedding
- FAISS index management
- Query processing and answer generation
- Persistence of all data
"""
def __init__(self, gemini_api_key: str, storage_dir: Optional[str] = None):
"""
Initialize the RAG Engine.
Args:
gemini_api_key: API key for Google Gemini
storage_dir: Optional custom storage directory for per-user isolation
"""
# Set storage paths
if storage_dir is None:
storage_dir = os.path.join(os.path.dirname(__file__), "storage")
self.storage_dir = storage_dir
self.faiss_index_path = os.path.join(storage_dir, "faiss.index")
self.metadata_path = os.path.join(storage_dir, "metadata.json")
self.documents_path = os.path.join(storage_dir, "documents.json")
# Ensure storage directory exists
os.makedirs(storage_dir, exist_ok=True)
# Initialize embedding model
print("Loading embedding model...")
self.embed_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
# Initialize Gemini
genai.configure(api_key=gemini_api_key)
self.gemini_model = genai.GenerativeModel("gemini-2.5-flash")
# Initialize or load FAISS index
self.index: Optional[faiss.IndexFlatL2] = None
self.metadata: List[Dict] = [] # Stores chunk text, source, page
self.documents: Dict[str, Dict] = {} # Document registry
# Load existing data if available
self._load_persistent_data()
print(f"RAG Engine initialized. Documents: {len(self.documents)}, Chunks: {len(self.metadata)}")
# ============================================
# PERSISTENCE METHODS
# ============================================
def _load_persistent_data(self):
"""Load FAISS index, metadata, and document registry from disk."""
# Load document registry
if os.path.exists(self.documents_path):
with open(self.documents_path, "r", encoding="utf-8") as f:
self.documents = json.load(f)
print(f"Loaded {len(self.documents)} documents from registry")
# Load metadata
if os.path.exists(self.metadata_path):
with open(self.metadata_path, "r", encoding="utf-8") as f:
self.metadata = json.load(f)
print(f"Loaded {len(self.metadata)} chunks metadata")
# Load FAISS index
if os.path.exists(self.faiss_index_path) and len(self.metadata) > 0:
self.index = faiss.read_index(self.faiss_index_path)
print(f"Loaded FAISS index with {self.index.ntotal} vectors")
else:
# Create new empty index
self.index = faiss.IndexFlatL2(EMBEDDING_DIMENSION)
print("Created new FAISS index")
def _save_persistent_data(self):
"""Save FAISS index, metadata, and document registry to disk."""
# Save document registry
with open(self.documents_path, "w", encoding="utf-8") as f:
json.dump(self.documents, f, indent=2, ensure_ascii=False)
# Save metadata
with open(self.metadata_path, "w", encoding="utf-8") as f:
json.dump(self.metadata, f, indent=2, ensure_ascii=False)
# Save FAISS index
if self.index is not None and self.index.ntotal > 0:
faiss.write_index(self.index, self.faiss_index_path)
print("Persistent data saved successfully")
# ============================================
# DOCUMENT PROCESSING METHODS
# ============================================
@staticmethod
def compute_file_hash(file_content: bytes) -> str:
"""
Compute SHA-256 hash of file content.
Args:
file_content: Raw bytes of the file
Returns:
Hexadecimal hash string
"""
return hashlib.sha256(file_content).hexdigest()
@staticmethod
def chunk_text_with_overlap(text: str, chunk_size: int = DEFAULT_CHUNK_SIZE,
overlap_size: int = DEFAULT_OVERLAP_SIZE) -> List[str]:
"""
Split text into overlapping chunks.
Args:
text: Input text to chunk
chunk_size: Number of words per chunk
overlap_size: Number of overlapping words between chunks
Returns:
List of text chunks
"""
words = text.split()
chunks = []
start = 0
while start < len(words):
end = start + chunk_size
chunk = " ".join(words[start:end])
if chunk.strip(): # Only add non-empty chunks
chunks.append(chunk)
start += chunk_size - overlap_size
return chunks
@staticmethod
def extract_text_from_image(image: Image.Image) -> str:
"""
Extract text from an image using OCR.
Args:
image: PIL Image object
Returns:
Extracted text string
"""
if not OCR_AVAILABLE:
return ""
try:
# Convert to RGB if needed
if image.mode != 'RGB':
image = image.convert('RGB')
# Run OCR
text = pytesseract.image_to_string(image, lang='eng')
return text.strip()
except Exception as e:
print(f"OCR error: {e}")
return ""
def extract_text_from_pdf(self, pdf_content: bytes) -> List[Dict]:
"""
Extract text from PDF page by page, including OCR for images.
Args:
pdf_content: Raw bytes of PDF file
Returns:
List of dicts with page_num, text, and ocr_text
"""
pages = []
try:
reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
for page_num, page in enumerate(reader.pages):
# Extract regular text
text = page.extract_text()
ocr_text = ""
# Extract images and apply OCR
if OCR_AVAILABLE:
try:
# Get images from page
if '/XObject' in page['/Resources']:
xObject = page['/Resources']['/XObject'].get_object()
for obj in xObject:
if xObject[obj]['/Subtype'] == '/Image':
try:
# Extract image data
size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
data = xObject[obj].get_data()
# Try to create image
if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
mode = "RGB"
elif xObject[obj]['/ColorSpace'] == '/DeviceGray':
mode = "L"
else:
mode = "RGB" # Default
try:
image = Image.frombytes(mode, size, data)
# Apply OCR
img_text = self.extract_text_from_image(image)
if img_text:
ocr_text += img_text + "\n"
except Exception as img_error:
# Try with PIL's open if frombytes fails
try:
image = Image.open(io.BytesIO(data))
img_text = self.extract_text_from_image(image)
if img_text:
ocr_text += img_text + "\n"
except:
pass
except Exception as e:
# Skip this image if extraction fails
continue
except Exception as e:
print(f"Error extracting images from page {page_num + 1}: {e}")
# Combine regular text and OCR text
combined_text = ""
if text and text.strip():
combined_text += text.strip()
if ocr_text.strip():
if combined_text:
combined_text += "\n\n[Text from images:]\n" + ocr_text.strip()
else:
combined_text = ocr_text.strip()
if combined_text:
pages.append({
"page_num": page_num + 1,
"text": combined_text,
"has_ocr": bool(ocr_text.strip())
})
except Exception as e:
print(f"Error extracting PDF text: {e}")
raise
return pages
def process_pdf(self, filename: str, file_content: bytes,
chunk_size: int = DEFAULT_CHUNK_SIZE,
overlap_size: int = DEFAULT_OVERLAP_SIZE) -> List[Dict]:
"""
Process a PDF: extract text (including OCR), chunk it, and prepare metadata.
Args:
filename: Original filename
file_content: Raw bytes of PDF
chunk_size: Words per chunk
overlap_size: Overlap between chunks
Returns:
List of chunk metadata dicts
"""
# Extract pages
pages = self.extract_text_from_pdf(file_content)
# Chunk each page
chunks_metadata = []
for page_info in pages:
page_chunks = self.chunk_text_with_overlap(
page_info["text"],
chunk_size,
overlap_size
)
for chunk_text in page_chunks:
chunks_metadata.append({
"text": chunk_text,
"source": filename,
"page": page_info["page_num"],
"has_ocr": page_info.get("has_ocr", False)
})
return chunks_metadata
# ============================================
# DUPLICATE DETECTION METHODS
# ============================================
def check_duplicate(self, file_hash: str) -> Optional[Dict]:
"""
Check if a document with the same hash already exists.
Args:
file_hash: SHA-256 hash of the file
Returns:
Document info if duplicate found, None otherwise
"""
for doc_id, doc_info in self.documents.items():
if doc_info.get("hash") == file_hash:
return {"doc_id": doc_id, **doc_info}
return None
def get_document_by_filename(self, filename: str) -> Optional[Dict]:
"""
Get document info by filename.
Args:
filename: Original filename
Returns:
Document info if found, None otherwise
"""
for doc_id, doc_info in self.documents.items():
if doc_info.get("filename") == filename:
return {"doc_id": doc_id, **doc_info}
return None
# ============================================
# EMBEDDING AND INDEXING METHODS
# ============================================
def generate_embeddings(self, texts: List[str]) -> np.ndarray:
"""
Generate embeddings for a list of texts.
Args:
texts: List of text strings
Returns:
Numpy array of embeddings
"""
embeddings = self.embed_model.encode(texts)
return np.array(embeddings).astype("float32")
def add_to_index(self, chunks_metadata: List[Dict]) -> int:
"""
Add new chunks to FAISS index and metadata.
Args:
chunks_metadata: List of chunk dicts with text, source, page
Returns:
Number of chunks added
"""
if not chunks_metadata:
return 0
# Extract texts for embedding
texts = [c["text"] for c in chunks_metadata]
# Generate embeddings
embeddings = self.generate_embeddings(texts)
# Add to FAISS index
self.index.add(embeddings)
# Add to metadata
self.metadata.extend(chunks_metadata)
return len(chunks_metadata)
def remove_document_from_index(self, filename: str):
"""
Remove all chunks of a document from the index.
Note: FAISS IndexFlatL2 doesn't support removal, so we rebuild.
Args:
filename: Filename of document to remove
"""
# Filter out chunks from this document
remaining_metadata = [
m for m in self.metadata if m["source"] != filename
]
if len(remaining_metadata) == len(self.metadata):
return # Nothing to remove
# Rebuild index with remaining chunks
self.metadata = remaining_metadata
if self.metadata:
texts = [m["text"] for m in self.metadata]
embeddings = self.generate_embeddings(texts)
self.index = faiss.IndexFlatL2(EMBEDDING_DIMENSION)
self.index.add(embeddings)
else:
self.index = faiss.IndexFlatL2(EMBEDDING_DIMENSION)
print(f"Removed document '{filename}' from index")
# ============================================
# DOCUMENT UPLOAD METHODS
# ============================================
def upload_document(self, filename: str, file_content: bytes,
action: str = "auto") -> Dict:
"""
Upload and process a document.
Args:
filename: Original filename
file_content: Raw bytes of PDF
action: "auto", "use_existing", "replace", or "cancel"
Returns:
Result dict with status and info
"""
# Compute hash
file_hash = self.compute_file_hash(file_content)
# Check for duplicate
existing_doc = self.check_duplicate(file_hash)
if existing_doc:
if action == "auto":
# Return duplicate warning
return {
"status": "duplicate",
"filename": filename,
"existing_filename": existing_doc["filename"],
"hash": file_hash,
"message": f"Document already exists as '{existing_doc['filename']}'",
"options": ["use_existing", "replace", "cancel"]
}
elif action == "use_existing":
return {
"status": "success",
"filename": existing_doc["filename"],
"message": "Using existing document embeddings",
"chunks": 0,
"reused": True
}
elif action == "cancel":
return {
"status": "cancelled",
"filename": filename,
"message": "Upload cancelled"
}
elif action == "replace":
# Remove old document and continue with upload
self.remove_document_from_index(existing_doc["filename"])
del self.documents[existing_doc["doc_id"]]
# Process new document
try:
chunks_metadata = self.process_pdf(filename, file_content)
if not chunks_metadata:
return {
"status": "error",
"filename": filename,
"message": "No text could be extracted from PDF"
}
# Add to index
num_chunks = self.add_to_index(chunks_metadata)
# Register document
doc_id = f"doc_{len(self.documents) + 1}_{int(datetime.now().timestamp())}"
self.documents[doc_id] = {
"filename": filename,
"hash": file_hash,
"upload_timestamp": datetime.now().isoformat(),
"num_chunks": num_chunks,
"num_pages": max(c["page"] for c in chunks_metadata)
}
# Persist changes
self._save_persistent_data()
return {
"status": "success",
"filename": filename,
"message": f"Document processed successfully",
"chunks": num_chunks,
"pages": self.documents[doc_id]["num_pages"]
}
except Exception as e:
return {
"status": "error",
"filename": filename,
"message": f"Error processing document: {str(e)}"
}
# ============================================
# QUERY AND RETRIEVAL METHODS
# ============================================
def retrieve_relevant_chunks(self, query: str, top_k: int = DEFAULT_TOP_K, doc_id: str = None) -> List[Dict]:
"""
Retrieve most relevant chunks for a query.
Args:
query: User's question
top_k: Number of chunks to retrieve
Returns:
List of relevant chunks with metadata
"""
if self.index is None or self.index.ntotal == 0:
return []
# Limit top_k to available chunks
top_k = min(top_k, self.index.ntotal)
# Embed query
query_embedding = self.embed_model.encode([query]).astype("float32")
# Search FAISS (request more if scoping to a single doc might filter results)
k_search = max(top_k, min(50, self.index.ntotal))
distances, indices = self.index.search(query_embedding, k=k_search)
# If doc_id provided, determine filename to filter by
filename_filter = None
if doc_id and doc_id in self.documents:
filename_filter = self.documents[doc_id].get('filename')
# Gather results and apply optional filename filter
results = []
for i, idx in enumerate(indices[0]):
if idx < len(self.metadata):
meta = self.metadata[idx]
if filename_filter and meta.get('source') != filename_filter:
continue
results.append({
**meta,
"distance": float(distances[0][i]),
"relevance_rank": len(results) + 1
})
if len(results) >= top_k:
break
return results
def generate_answer(self, query: str, context_chunks: List[Dict]) -> str:
"""
Generate answer using Gemini with retrieved context.
Args:
query: User's question
context_chunks: Retrieved relevant chunks
Returns:
Generated answer string
"""
if not context_chunks:
return "I don't have enough information to answer this question. Please upload relevant documents first."
# Build context string
context_parts = []
for chunk in context_chunks:
context_parts.append(
f"[Source: {chunk['source']}, Page {chunk['page']}]\n{chunk['text']}"
)
context = "\n\n".join(context_parts)
# Create prompt
prompt = f"""You are a helpful assistant that answers questions based ONLY on the provided context.
Do NOT make up information that is not in the context.
If the context doesn't contain enough information to answer, say so clearly.
You may summarize, combine, or rephrase information from the context to make your answer clear and helpful.
CONTEXT:
{context}
QUESTION:
{query}
ANSWER:"""
try:
response = self.gemini_model.generate_content(prompt)
return response.text
except Exception as e:
return f"Error generating answer: {str(e)}"
def verify_sources(self, query: str, answer: str, context_chunks: List[Dict]) -> List[int]:
"""
Verify which chunks actually support the generated answer.
Args:
query: User's question
answer: Generated answer
context_chunks: All retrieved chunks
Returns:
List of indices of chunks that support the answer
"""
if not context_chunks:
return []
# Build context with numbered chunks
context_parts = []
for i, chunk in enumerate(context_chunks):
context_parts.append(
f"[{i}] Source: {chunk['source']}, Page {chunk['page']}\n{chunk['text']}"
)
context = "\n\n".join(context_parts)
# Create verification prompt
prompt = f"""You are a citation verification assistant. Given a question, an answer, and numbered source chunks, identify which chunks were actually used to generate the answer.
Return ONLY a comma-separated list of chunk numbers that directly support the answer (e.g., "0,2,3").
If no chunks support the answer, return "NONE".
Do not include explanations or any other text.
QUESTION:
{query}
ANSWER:
{answer}
NUMBERED CHUNKS:
{context}
CHUNK NUMBERS THAT SUPPORT THE ANSWER:"""
try:
response = self.gemini_model.generate_content(prompt)
result = response.text.strip()
# Parse the response
if result.upper() == "NONE":
return []
# Extract numbers
used_indices = []
for part in result.split(","):
try:
idx = int(part.strip())
if 0 <= idx < len(context_chunks):
used_indices.append(idx)
except ValueError:
continue
return used_indices
except Exception as e:
print(f"Error verifying sources: {e}")
# Fallback: return all chunks if verification fails
return list(range(len(context_chunks)))
def ask(self, query: str, top_k: int = DEFAULT_TOP_K, doc_id: str = None) -> Dict:
"""
Main query method: retrieve context, generate answer, and filter sources.
Args:
query: User's question
top_k: Number of chunks to retrieve
Returns:
Dict with answer and verified sources
"""
# Retrieve relevant chunks (optionally scoped to a document)
relevant_chunks = self.retrieve_relevant_chunks(query, top_k, doc_id=doc_id)
# Generate answer
answer = self.generate_answer(query, relevant_chunks)
# Verify which chunks actually support the answer
used_indices = self.verify_sources(query, answer, relevant_chunks)
# Filter sources to only those that support the answer
sources = []
seen = set()
for idx in used_indices:
if idx < len(relevant_chunks):
chunk = relevant_chunks[idx]
source_key = f"{chunk['source']}_{chunk['page']}"
if source_key not in seen:
sources.append({
"file": chunk["source"],
"page": chunk["page"]
})
seen.add(source_key)
return {
"answer": answer,
"sources": sources,
"num_chunks_used": len(sources),
"num_chunks_retrieved": len(relevant_chunks)
}
# ============================================
# DOCUMENT MANAGEMENT METHODS
# ============================================
def get_all_documents(self) -> List[Dict]:
"""
Get list of all uploaded documents.
Returns:
List of document info dicts
"""
return [
{"doc_id": doc_id, **info}
for doc_id, info in self.documents.items()
]
def delete_document(self, doc_id: str) -> Dict:
"""
Delete a document and its embeddings.
Args:
doc_id: Document ID to delete
Returns:
Result dict
"""
if doc_id not in self.documents:
return {
"status": "error",
"message": f"Document {doc_id} not found"
}
filename = self.documents[doc_id]["filename"]
# Remove from index
self.remove_document_from_index(filename)
# Remove from registry
del self.documents[doc_id]
# Persist changes
self._save_persistent_data()
return {
"status": "success",
"message": f"Document '{filename}' deleted successfully"
}
def get_stats(self) -> Dict:
"""
Get system statistics.
Returns:
Dict with stats
"""
return {
"total_documents": len(self.documents),
"total_chunks": len(self.metadata),
"index_size": self.index.ntotal if self.index else 0,
"embedding_model": EMBEDDING_MODEL_NAME,
"embedding_dimension": EMBEDDING_DIMENSION
} |