Spaces:
Sleeping
Sleeping
File size: 12,498 Bytes
fb503bc d3ba43b 7fff458 fb503bc d3ba43b fb503bc d3ba43b fb503bc d3ba43b fb503bc d3ba43b fb503bc d3ba43b fb503bc 518d665 fb503bc 518d665 7fff458 fb503bc d3ba43b fb503bc d3ba43b fb503bc d3ba43b fb503bc | 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 | """
Ingestion pipeline untuk memproses semua buku dari metadata, melakukan:
- parsing PDF → teks bersih
- chunking untuk fulltext
- summarization
- embedding (summary & chunks)
- build FAISS indexes (summary & fulltext)
Hasil akhir disimpan di data/faiss/
"""
import json
from pathlib import Path
import sys
from api.routes.books import reload_static_cache
from scripts.generate_static_data import generate_static_files
import traceback
from typing import Any, Dict, List, Tuple
# Tambahkan root proyek ke path jika perlu
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from chunking.text_chunker import chunk_document
from config.settings import FULLTEXT_INDEX_PATH, RAW_DATA_FILE, SUMMARY_INDEX_PATH
from embedding.embedder import GeminiEmbedder
from embedding.vector_store import FulltextVectorStore, SummaryVectorStore
from parsing.pdf_cleaner import clean_text
from parsing.pdf_parser import parse_pdf_to_text
from summarization.book_summarizer import summarize_book
from utils.logger import configure_root_logger, get_logger
# Konfigurasi logging
configure_root_logger(log_file=Path("logs/ingest.log"), level=20) # logging.INFO == 20
logger = get_logger(__name__)
def load_books_metadata() -> List[Dict[str, Any]]:
"""Load list of books from metadata file."""
if not RAW_DATA_FILE.exists():
raise FileNotFoundError(f"Metadata file not found: {RAW_DATA_FILE}")
with open(RAW_DATA_FILE, 'r', encoding='utf-8') as f:
books = json.load(f)
logger.info(f"Loaded {len(books)} books from metadata")
return books
def process_book(
book: Dict[str, Any],
embedder: GeminiEmbedder,
interactive: bool = False,
override_summary: str = None
) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:
"""
Process a single book: parse, clean, chunk, summarize, embed.
Args:
book: Metadata of the book.
embedder: Instance of GeminiEmbedder.
interactive: If True, asks user to confirm or edit the summary.
override_summary: If provided, skips LLM summarization and uses this text.
Returns:
Tuple of (summary_vector_metadata, list_of_chunk_metadatas_with_vectors)
Each metadata dict includes the generated vector.
"""
book_id = book.get("book_id")
title = book.get("judul_buku", book.get("title", "Unknown"))
pdf_path = book.get("pdf_path", book.get("link_pdf"))
if not pdf_path or not Path(pdf_path).exists():
logger.error(f"Book {book_id} - PDF not found: {pdf_path}")
raise FileNotFoundError(f"PDF missing for {book_id}")
logger.info(f"Processing book: {title} ({book_id})")
# 1. Parse PDF
raw_text = parse_pdf_to_text(pdf_path)
if not raw_text:
raise ValueError("Parsed text is empty")
# 2. Clean text
clean_text_content = clean_text(raw_text)
if not clean_text_content:
raise ValueError("Cleaned text is empty")
# 3. Summarize (API call or override)
if override_summary:
summary_text = override_summary
logger.info("Using overridden summary.")
else:
summary_text = summarize_book(metadata=book, combined_text=clean_text_content)
if not summary_text:
raise ValueError("Summary generation failed")
# Admin Interactive review
if interactive and not override_summary:
print("\n" + "="*50)
print("HASIL RINGKASAN MODEL:")
print("="*50)
print(summary_text)
print("="*50)
user_input = input("Apakah Anda ingin memakai ringkasan ini? (y/n): ")
if user_input.lower() != 'y':
print("Ketikkan ringkasan baru Anda (tekan Enter 2x untuk selesai):")
lines = []
while True:
line = input()
if line == "":
break
lines.append(line)
new_summary = "\n".join(lines).strip()
if new_summary:
summary_text = new_summary
logger.info("Summary manually updated by admin.")
# 4. Chunk full text
chunks = chunk_document(
text=clean_text_content,
method="character",
book_id=book_id,
title=title
)
if not chunks:
raise ValueError("No chunks created")
logger.info(f"Created {len(chunks)} chunks for book {book_id}")
# 5. Generate embeddings
# Summary embedding (gabungan string)
format_doc = {"metadata": book, "summary_text": summary_text}
enhanced_summary_text = embedder._build_enhanced_text(format_doc)
summary_vector = embedder.embed_text(enhanced_summary_text, task_type="RETRIEVAL_DOCUMENT")
# Chunk embeddings (batch for efficiency)
chunk_texts = [chunk["text"] for chunk in chunks]
chunk_vectors = embedder.embed_texts(chunk_texts, task_type="RETRIEVAL_DOCUMENT")
# 6. Prepare metadata for vector stores
# Summary metadata
summary_meta = {
"book_id": book_id,
"judul_buku": title,
"title": title,
"text": summary_text,
"type": "summary",
"jenjang": book.get("jenjang"),
"kelas": book.get("kelas"),
"mata_pelajaran": book.get("mata_pelajaran"),
}
# Save the updated summary_text back to book metadata
book["summary_text"] = summary_text
# Add vector to metadata for later collection
summary_meta["vector"] = summary_vector
# Chunks metadata (each with its vector)
chunk_metas = []
for i, (chunk, vec) in enumerate(zip(chunks, chunk_vectors)):
meta = {
"book_id": book_id,
"judul_buku": title,
"title": title,
"chunk_text": chunk["text"],
"chunk_index": chunk.get("index", i),
"start_char": chunk.get("start"),
"end_char": chunk.get("end"),
"type": "fulltext",
}
# Include page if exists in chunk metadata
if "page" in chunk:
meta["page"] = chunk["page"]
meta["vector"] = vec
chunk_metas.append(meta)
logger.info(f"Completed book {book_id}: summary + {len(chunk_metas)} chunks")
return summary_meta, chunk_metas
def build_and_save_indexes(
all_summary_metas: List[Dict[str, Any]],
all_chunk_metas: List[Dict[str, Any]],
dimension: int
):
"""
Build FAISS indexes from collected vectors/metadata and save to disk.
"""
logger.info(f"Building summary index from {len(all_summary_metas)} vectors")
summary_vectors = [m["vector"] for m in all_summary_metas]
summary_metadatas = [{k: v for k, v in m.items() if k != "vector"} for m in all_summary_metas]
summary_store = SummaryVectorStore(SUMMARY_INDEX_PATH, dimension=dimension)
# Use internal method to add all at once (to avoid multiple saves)
summary_store.add_vectors(summary_vectors, summary_metadatas)
summary_store._save() # ensure save
logger.info(f"Summary index saved to {SUMMARY_INDEX_PATH}")
logger.info(f"Building fulltext index from {len(all_chunk_metas)} vectors")
chunk_vectors = [m["vector"] for m in all_chunk_metas]
chunk_metadatas = [{k: v for k, v in m.items() if k != "vector"} for m in all_chunk_metas]
fulltext_store = FulltextVectorStore(FULLTEXT_INDEX_PATH, dimension=dimension)
fulltext_store.add_vectors(chunk_vectors, chunk_metadatas)
fulltext_store._save()
logger.info(f"Fulltext index saved to {FULLTEXT_INDEX_PATH}")
def add_new_book(book_data: Dict[str, Any], interactive: bool = True, override_summary: str = None) -> tuple[bool, dict]:
"""
Public admin API for adding a single new book.
Saves to RAW_DATA_FILE and updates both vector stores.
"""
if "book_id" not in book_data:
import uuid
book_data["book_id"] = str(uuid.uuid4())[:16]
# Phase 1 Alignment: Must inject embedding key manager explicitly
from utils.api_key_manager import create_gemini_embedding_key_manager
embedding_key_manager = create_gemini_embedding_key_manager()
embedder = GeminiEmbedder(key_manager=embedding_key_manager)
# Dummy to initialize client/dimension
dimension = len(embedder.embed_text("test"))
try:
summary_meta, chunk_metas = process_book(book_data, embedder, interactive=interactive, override_summary=override_summary)
# Build and save single book indices
summary_vectors = [summary_meta.pop("vector")]
summary_store = SummaryVectorStore(SUMMARY_INDEX_PATH, dimension=dimension)
summary_store.add_vectors(summary_vectors, [summary_meta])
summary_store._save()
chunk_vectors = [m.pop("vector") for m in chunk_metas]
fulltext_store = FulltextVectorStore(FULLTEXT_INDEX_PATH, dimension=dimension)
fulltext_store.add_vectors(chunk_vectors, chunk_metas)
fulltext_store._save()
# Append to metadata JSONL
with open(RAW_DATA_FILE, 'a', encoding='utf-8') as f:
f.write(json.dumps(book_data, ensure_ascii=False) + "\n")
generate_static_files()
reload_static_cache()
logger.info(f"Successfully added new book {book_data['book_id']} to system.")
# Prepare sample chunks (up to 5)
sample_chunks = [m.get("chunk_text") for m in chunk_metas[:5]]
result_data = {"summary": summary_meta.get("text", ""), "chunks": sample_chunks}
return True, result_data
except Exception as e:
logger.error(f"Failed to add new book: {e}\n{traceback.format_exc()}")
return False, {}
def main():
"""Main ingestion pipeline."""
import argparse
parser = argparse.ArgumentParser(description="Add books to RAG pipeline.")
parser.add_argument("--interactive", action="store_true", help="Interactive admin mode")
parser.add_argument("--id", type=str, help="Add single book ID (must be in JSON)")
parser.add_argument("--bulk", action="store_true", help="Bulk process all missing from books.json")
args = parser.parse_args()
if not args.bulk and not args.id:
logger.info("Use --bulk to run all from JSON or --id <book_id> to add a specific book.")
return
logger.info("Starting ingestion pipeline")
# Load metadata
try:
books = load_books_metadata()
except Exception as e:
logger.error(f"Failed to load metadata: {e}")
return
if args.id:
book = next((b for b in books if b.get('book_id') == args.id), None)
if not book:
logger.error(f"Book {args.id} not found in metadata file.")
return
success, _ = add_new_book(book, interactive=args.interactive)
return
# Phase 1 Alignment: Initialize embedder strictly from embedding pool
from utils.api_key_manager import create_gemini_embedding_key_manager
embedding_key_manager = create_gemini_embedding_key_manager()
embedder = GeminiEmbedder(key_manager=embedding_key_manager)
# Get embedding dimension from first embed (or from model)
# We'll determine dimension by embedding a dummy text
dummy_vector = embedder.embed_text("test")
dimension = len(dummy_vector)
logger.info(f"Embedding dimension: {dimension}")
all_summary_metas = []
all_chunk_metas = []
failed_books = []
for book in books:
try:
summary_meta, chunk_metas = process_book(book, embedder)
all_summary_metas.append(summary_meta)
all_chunk_metas.extend(chunk_metas)
except Exception as e:
book_id = book.get("book_id", "unknown")
logger.error(f"Failed to process book {book_id}: {e}\n{traceback.format_exc()}")
failed_books.append(book_id)
continue # proceed with next book
if not all_summary_metas:
logger.error("No books successfully processed. Exiting.")
return
# Build and save indexes
try:
build_and_save_indexes(all_summary_metas, all_chunk_metas, dimension)
logger.info(f"Ingestion completed. Success: {len(all_summary_metas)} books, chunks: {len(all_chunk_metas)}. Failed: {len(failed_books)}")
if failed_books:
logger.warning(f"Failed books: {failed_books}")
except Exception as e:
logger.error(f"Failed to build/save indexes: {e}\n{traceback.format_exc()}")
if __name__ == "__main__":
main() |