Spaces:
Sleeping
Sleeping
File size: 21,089 Bytes
b4971bd |
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 |
#!/usr/bin/env python3
"""
Automated Vector Store Builder for VedaMD
==========================================
This script automates the complete vector store creation process:
1. Scans directory for PDF documents
2. Extracts text using best available method (PyMuPDF โ PDFPlumber โ OCR)
3. Smart chunking with medical section awareness
4. Batch embedding generation
5. FAISS index creation
6. Metadata generation (citations, sources, quality scores)
7. Automatic Hugging Face Hub upload
8. Configuration file generation
Usage:
python scripts/build_vector_store.py \\
--input-dir ./Obs \\
--output-dir ./data/vector_store \\
--repo-id sniro23/VedaMD-Vector-Store \\
--upload
Author: VedaMD Team
Date: October 22, 2025
Version: 1.0.0
"""
import os
import sys
import json
import hashlib
import logging
import argparse
from pathlib import Path
from typing import List, Dict, Tuple, Optional
from datetime import datetime
import warnings
# PDF processing
try:
import fitz # PyMuPDF
HAS_PYMUPDF = True
except ImportError:
HAS_PYMUPDF = False
warnings.warn("PyMuPDF not available. Install with: pip install PyMuPDF")
try:
import pdfplumber
HAS_PDFPLUMBER = True
except ImportError:
HAS_PDFPLUMBER = False
warnings.warn("pdfplumber not available. Install with: pip install pdfplumber")
# Embeddings and vector store
try:
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
HAS_EMBEDDINGS = True
except ImportError:
HAS_EMBEDDINGS = False
raise ImportError("Required packages not installed. Run: pip install sentence-transformers faiss-cpu numpy")
# Hugging Face Hub
try:
from huggingface_hub import HfApi, create_repo
HAS_HF = True
except ImportError:
HAS_HF = False
warnings.warn("Hugging Face Hub not available. Install with: pip install huggingface-hub")
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('vector_store_build.log')
]
)
logger = logging.getLogger(__name__)
class PDFExtractor:
"""Handles PDF text extraction with multiple fallback methods"""
@staticmethod
def extract_with_pymupdf(pdf_path: str) -> Tuple[str, Dict]:
"""Extract text using PyMuPDF (fastest, most reliable)"""
if not HAS_PYMUPDF:
raise ImportError("PyMuPDF not available")
logger.info(f"๐ Extracting with PyMuPDF: {pdf_path}")
text = ""
metadata = {"method": "pymupdf", "pages": 0}
try:
doc = fitz.open(pdf_path)
metadata["pages"] = len(doc)
metadata["title"] = doc.metadata.get("title", "")
metadata["author"] = doc.metadata.get("author", "")
for page_num, page in enumerate(doc, 1):
page_text = page.get_text()
text += f"\n--- Page {page_num} ---\n{page_text}"
doc.close()
logger.info(f"โ
Extracted {len(text)} characters from {metadata['pages']} pages")
return text, metadata
except Exception as e:
logger.error(f"โ PyMuPDF extraction failed: {e}")
raise
@staticmethod
def extract_with_pdfplumber(pdf_path: str) -> Tuple[str, Dict]:
"""Extract text using pdfplumber (better table handling)"""
if not HAS_PDFPLUMBER:
raise ImportError("pdfplumber not available")
logger.info(f"๐ Extracting with pdfplumber: {pdf_path}")
text = ""
metadata = {"method": "pdfplumber", "pages": 0}
try:
with pdfplumber.open(pdf_path) as pdf:
metadata["pages"] = len(pdf.pages)
for page_num, page in enumerate(pdf.pages, 1):
page_text = page.extract_text() or ""
text += f"\n--- Page {page_num} ---\n{page_text}"
logger.info(f"โ
Extracted {len(text)} characters from {metadata['pages']} pages")
return text, metadata
except Exception as e:
logger.error(f"โ pdfplumber extraction failed: {e}")
raise
@staticmethod
def extract_text(pdf_path: str) -> Tuple[str, Dict]:
"""Extract text using best available method with fallbacks"""
errors = []
# Try PyMuPDF first (fastest)
if HAS_PYMUPDF:
try:
return PDFExtractor.extract_with_pymupdf(pdf_path)
except Exception as e:
errors.append(f"PyMuPDF: {e}")
logger.warning(f"โ ๏ธ PyMuPDF failed, trying pdfplumber...")
# Fallback to pdfplumber
if HAS_PDFPLUMBER:
try:
return PDFExtractor.extract_with_pdfplumber(pdf_path)
except Exception as e:
errors.append(f"pdfplumber: {e}")
logger.warning(f"โ ๏ธ pdfplumber failed")
# If all methods fail
raise Exception(f"All extraction methods failed: {'; '.join(errors)}")
class MedicalChunker:
"""Smart chunking with medical section awareness"""
def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 100):
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
# Medical section headers to preserve
self.section_markers = [
"INTRODUCTION", "BACKGROUND", "DEFINITION", "EPIDEMIOLOGY",
"PATHOPHYSIOLOGY", "CLINICAL FEATURES", "DIAGNOSIS", "MANAGEMENT",
"TREATMENT", "PREVENTION", "COMPLICATIONS", "PROGNOSIS",
"REFERENCES", "GUIDELINES", "PROTOCOL", "RECOMMENDATIONS"
]
def chunk_text(self, text: str, source: str) -> List[Dict]:
"""Split text into chunks while preserving medical sections"""
logger.info(f"๐ Chunking text from {source}")
# Clean text
text = text.strip()
if not text:
logger.warning(f"โ ๏ธ Empty text from {source}")
return []
chunks = []
current_chunk = ""
current_section = "General"
# Split by paragraphs
paragraphs = text.split('\n\n')
for para in paragraphs:
para = para.strip()
if not para:
continue
# Check if paragraph is a section header
para_upper = para.upper()
for marker in self.section_markers:
if marker in para_upper and len(para) < 100:
current_section = para
break
# Add paragraph to current chunk
if len(current_chunk) + len(para) + 2 <= self.chunk_size:
current_chunk += f"\n\n{para}"
else:
# Save current chunk
if current_chunk.strip():
chunks.append({
"content": current_chunk.strip(),
"source": source,
"section": current_section,
"size": len(current_chunk)
})
# Start new chunk with overlap
if self.chunk_overlap > 0:
# Keep last few sentences for context
sentences = current_chunk.split('. ')
overlap_text = '. '.join(sentences[-2:]) if len(sentences) > 1 else ""
current_chunk = f"{overlap_text}\n\n{para}"
else:
current_chunk = para
# Add final chunk
if current_chunk.strip():
chunks.append({
"content": current_chunk.strip(),
"source": source,
"section": current_section,
"size": len(current_chunk)
})
logger.info(f"โ
Created {len(chunks)} chunks from {source}")
return chunks
class VectorStoreBuilder:
"""Main vector store builder class"""
def __init__(
self,
input_dir: str,
output_dir: str,
embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2",
chunk_size: int = 1000,
chunk_overlap: int = 100
):
self.input_dir = Path(input_dir)
self.output_dir = Path(output_dir)
self.embedding_model_name = embedding_model
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
# Create output directory
self.output_dir.mkdir(parents=True, exist_ok=True)
# Initialize components
logger.info(f"๐ง Initializing vector store builder...")
logger.info(f"๐ Input directory: {self.input_dir}")
logger.info(f"๐ Output directory: {self.output_dir}")
# Load embedding model
logger.info(f"๐ค Loading embedding model: {self.embedding_model_name}")
self.embedding_model = SentenceTransformer(self.embedding_model_name)
self.embedding_dim = self.embedding_model.get_sentence_embedding_dimension()
logger.info(f"โ
Embedding dimension: {self.embedding_dim}")
# Initialize chunker
self.chunker = MedicalChunker(chunk_size, chunk_overlap)
# Storage
self.documents = []
self.embeddings = []
self.metadata = []
def scan_pdfs(self) -> List[Path]:
"""Scan input directory for PDF files"""
logger.info(f"๐ Scanning for PDFs in {self.input_dir}")
if not self.input_dir.exists():
raise FileNotFoundError(f"Input directory not found: {self.input_dir}")
pdf_files = list(self.input_dir.glob("**/*.pdf"))
logger.info(f"โ
Found {len(pdf_files)} PDF files")
for pdf in pdf_files:
logger.info(f" ๐ {pdf.name}")
return pdf_files
def process_pdf(self, pdf_path: Path) -> int:
"""Process a single PDF file"""
logger.info(f"\n{'='*60}")
logger.info(f"๐ Processing: {pdf_path.name}")
logger.info(f"{'='*60}")
try:
# Extract text
text, extraction_metadata = PDFExtractor.extract_text(str(pdf_path))
if not text or len(text) < 100:
logger.warning(f"โ ๏ธ Extracted text too short ({len(text)} chars), skipping")
return 0
# Generate file hash for duplicate detection
file_hash = hashlib.md5(text.encode()).hexdigest()
# Chunk text
chunks = self.chunker.chunk_text(text, pdf_path.name)
if not chunks:
logger.warning(f"โ ๏ธ No chunks created from {pdf_path.name}")
return 0
# Generate embeddings
logger.info(f"๐งฎ Generating embeddings for {len(chunks)} chunks...")
chunk_texts = [chunk["content"] for chunk in chunks]
chunk_embeddings = self.embedding_model.encode(
chunk_texts,
show_progress_bar=True,
batch_size=32
)
# Store documents and embeddings
for i, (chunk, embedding) in enumerate(zip(chunks, chunk_embeddings)):
self.documents.append(chunk["content"])
self.embeddings.append(embedding)
self.metadata.append({
"source": pdf_path.name,
"section": chunk["section"],
"chunk_id": i,
"chunk_size": chunk["size"],
"file_hash": file_hash,
"extraction_method": extraction_metadata["method"],
"total_pages": extraction_metadata["pages"],
"processed_at": datetime.now().isoformat()
})
logger.info(f"โ
Processed {pdf_path.name}: {len(chunks)} chunks added")
return len(chunks)
except Exception as e:
logger.error(f"โ Error processing {pdf_path.name}: {e}")
return 0
def build_faiss_index(self):
"""Build FAISS index from embeddings"""
logger.info(f"\n{'='*60}")
logger.info(f"๐๏ธ Building FAISS index...")
logger.info(f"{'='*60}")
if not self.embeddings:
raise ValueError("No embeddings to index")
# Convert to numpy array
embeddings_array = np.array(self.embeddings).astype('float32')
logger.info(f"๐ Embeddings shape: {embeddings_array.shape}")
# Create FAISS index (L2 distance)
index = faiss.IndexFlatL2(self.embedding_dim)
# Add embeddings
index.add(embeddings_array)
logger.info(f"โ
FAISS index created with {index.ntotal} vectors")
return index
def save_vector_store(self, index):
"""Save vector store to disk"""
logger.info(f"\n{'='*60}")
logger.info(f"๐พ Saving vector store...")
logger.info(f"{'='*60}")
# Save FAISS index
index_path = self.output_dir / "faiss_index.bin"
faiss.write_index(index, str(index_path))
logger.info(f"โ
Saved FAISS index: {index_path}")
# Save documents
docs_path = self.output_dir / "documents.json"
with open(docs_path, 'w', encoding='utf-8') as f:
json.dump(self.documents, f, ensure_ascii=False, indent=2)
logger.info(f"โ
Saved documents: {docs_path}")
# Save metadata
metadata_path = self.output_dir / "metadata.json"
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(self.metadata, f, ensure_ascii=False, indent=2)
logger.info(f"โ
Saved metadata: {metadata_path}")
# Save configuration
config = {
"embedding_model": self.embedding_model_name,
"embedding_dim": self.embedding_dim,
"chunk_size": self.chunk_size,
"chunk_overlap": self.chunk_overlap,
"total_documents": len(self.documents),
"total_chunks": len(self.documents),
"build_date": datetime.now().isoformat(),
"version": "1.0.0"
}
config_path = self.output_dir / "config.json"
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2)
logger.info(f"โ
Saved config: {config_path}")
# Save build log
log_data = {
"build_date": datetime.now().isoformat(),
"input_dir": str(self.input_dir),
"output_dir": str(self.output_dir),
"total_pdfs": len(set(m["source"] for m in self.metadata)),
"total_chunks": len(self.documents),
"sources": list(set(m["source"] for m in self.metadata)),
"config": config
}
log_path = self.output_dir / "build_log.json"
with open(log_path, 'w', encoding='utf-8') as f:
json.dump(log_data, f, indent=2)
logger.info(f"โ
Saved build log: {log_path}")
def upload_to_hf(self, repo_id: str, token: Optional[str] = None):
"""Upload vector store to Hugging Face Hub"""
if not HAS_HF:
logger.warning("โ ๏ธ Hugging Face Hub not available, skipping upload")
return
logger.info(f"\n{'='*60}")
logger.info(f"โ๏ธ Uploading to Hugging Face Hub...")
logger.info(f"๐ฆ Repository: {repo_id}")
logger.info(f"{'='*60}")
try:
api = HfApi(token=token)
# Create repo if it doesn't exist
try:
create_repo(repo_id, repo_type="dataset", exist_ok=True, token=token)
logger.info(f"โ
Repository ready: {repo_id}")
except Exception as e:
logger.warning(f"โ ๏ธ Repo creation: {e}")
# Upload all files
files_to_upload = [
"faiss_index.bin",
"documents.json",
"metadata.json",
"config.json",
"build_log.json"
]
for filename in files_to_upload:
file_path = self.output_dir / filename
if file_path.exists():
logger.info(f"๐ค Uploading {filename}...")
api.upload_file(
path_or_fileobj=str(file_path),
path_in_repo=filename,
repo_id=repo_id,
repo_type="dataset",
token=token
)
logger.info(f"โ
Uploaded {filename}")
logger.info(f"๐ Upload complete! View at: https://huggingface.co/datasets/{repo_id}")
except Exception as e:
logger.error(f"โ Upload failed: {e}")
raise
def build(self, upload: bool = False, repo_id: Optional[str] = None, hf_token: Optional[str] = None):
"""Main build process"""
start_time = datetime.now()
logger.info(f"\n{'='*60}")
logger.info(f"๐ STARTING VECTOR STORE BUILD")
logger.info(f"{'='*60}\n")
try:
# Scan for PDFs
pdf_files = self.scan_pdfs()
if not pdf_files:
raise ValueError("No PDF files found in input directory")
# Process each PDF
total_chunks = 0
for pdf_path in pdf_files:
chunks_added = self.process_pdf(pdf_path)
total_chunks += chunks_added
if total_chunks == 0:
raise ValueError("No chunks created from any PDF")
# Build FAISS index
index = self.build_faiss_index()
# Save to disk
self.save_vector_store(index)
# Upload to HF if requested
if upload and repo_id:
self.upload_to_hf(repo_id, hf_token)
# Summary
duration = (datetime.now() - start_time).total_seconds()
logger.info(f"\n{'='*60}")
logger.info(f"โ
BUILD COMPLETE!")
logger.info(f"{'='*60}")
logger.info(f"๐ Summary:")
logger.info(f" โข PDFs processed: {len(pdf_files)}")
logger.info(f" โข Total chunks: {total_chunks}")
logger.info(f" โข Embedding dimension: {self.embedding_dim}")
logger.info(f" โข Output directory: {self.output_dir}")
logger.info(f" โข Build time: {duration:.2f} seconds")
logger.info(f"{'='*60}\n")
return True
except Exception as e:
logger.error(f"\n{'='*60}")
logger.error(f"โ BUILD FAILED: {e}")
logger.error(f"{'='*60}\n")
raise
def main():
parser = argparse.ArgumentParser(
description="Build VedaMD Vector Store from PDF documents",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Build locally
python scripts/build_vector_store.py --input-dir ./Obs --output-dir ./data/vector_store
# Build and upload to HF
python scripts/build_vector_store.py \\
--input-dir ./Obs \\
--output-dir ./data/vector_store \\
--repo-id sniro23/VedaMD-Vector-Store \\
--upload
"""
)
parser.add_argument(
"--input-dir",
type=str,
required=True,
help="Directory containing PDF files"
)
parser.add_argument(
"--output-dir",
type=str,
default="./data/vector_store",
help="Output directory for vector store files"
)
parser.add_argument(
"--embedding-model",
type=str,
default="sentence-transformers/all-MiniLM-L6-v2",
help="Sentence transformer model for embeddings"
)
parser.add_argument(
"--chunk-size",
type=int,
default=1000,
help="Maximum chunk size in characters"
)
parser.add_argument(
"--chunk-overlap",
type=int,
default=100,
help="Overlap between chunks in characters"
)
parser.add_argument(
"--upload",
action="store_true",
help="Upload to Hugging Face Hub after building"
)
parser.add_argument(
"--repo-id",
type=str,
help="Hugging Face repository ID (e.g., username/repo-name)"
)
parser.add_argument(
"--hf-token",
type=str,
help="Hugging Face API token (or set HF_TOKEN env var)"
)
args = parser.parse_args()
# Get HF token from env if not provided
hf_token = args.hf_token or os.getenv("HF_TOKEN")
# Validate upload arguments
if args.upload and not args.repo_id:
parser.error("--repo-id is required when --upload is specified")
# Build vector store
builder = VectorStoreBuilder(
input_dir=args.input_dir,
output_dir=args.output_dir,
embedding_model=args.embedding_model,
chunk_size=args.chunk_size,
chunk_overlap=args.chunk_overlap
)
builder.build(
upload=args.upload,
repo_id=args.repo_id,
hf_token=hf_token
)
if __name__ == "__main__":
main()
|