Spaces:
Sleeping
Sleeping
Quincy Hsieh commited on
Commit ·
48798a9
1
Parent(s): dc06bcc
Change sample data to train data folder
Browse files
app.py
CHANGED
|
@@ -51,7 +51,7 @@ logging.getLogger("chromadb.telemetry.product.posthog").setLevel(logging.CRITICA
|
|
| 51 |
DATA_DIR = Path("/data") if Path("/data").is_dir() else Path("./data")
|
| 52 |
|
| 53 |
CHROMA_PERSIST_DIR = str(DATA_DIR / "chroma_db")
|
| 54 |
-
|
| 55 |
COLLECTION_NAME = "rag_documents"
|
| 56 |
CHUNK_SIZE = 512
|
| 57 |
CHUNK_OVERLAP = 50
|
|
@@ -375,30 +375,29 @@ def rag_query(query: str, top_k: int = TOP_K_RESULTS) -> dict:
|
|
| 375 |
|
| 376 |
|
| 377 |
# ---------------------------------------------------------------------------
|
| 378 |
-
# Step 4: Ingest
|
| 379 |
# ---------------------------------------------------------------------------
|
| 380 |
-
# Load sample documents so the demo works out of the box.
|
| 381 |
|
| 382 |
|
| 383 |
-
def
|
| 384 |
-
"""Load and embed
|
| 385 |
if collection.count() > 0:
|
| 386 |
logger.info("Vector store already has documents, skipping ingestion.")
|
| 387 |
return
|
| 388 |
|
| 389 |
-
if not
|
| 390 |
-
logger.warning("No
|
| 391 |
return
|
| 392 |
|
| 393 |
-
# Ingest plain text files
|
| 394 |
-
for file_path in
|
| 395 |
logger.info(f"Ingesting text file: {file_path.name}")
|
| 396 |
text = file_path.read_text(encoding="utf-8")
|
| 397 |
chunks = chunk_text(text, source=file_path.name)
|
| 398 |
add_documents_to_vectorstore(chunks)
|
| 399 |
|
| 400 |
-
# Ingest PDF files (
|
| 401 |
-
for file_path in
|
| 402 |
logger.info(f"Ingesting PDF file: {file_path.name}")
|
| 403 |
text = extract_text_from_pdf(file_path)
|
| 404 |
if text.strip():
|
|
@@ -407,10 +406,7 @@ def ingest_sample_documents():
|
|
| 407 |
else:
|
| 408 |
logger.warning(f"No extractable text found in: {file_path.name}")
|
| 409 |
|
| 410 |
-
logger.info(f"
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
ingest_sample_documents()
|
| 414 |
|
| 415 |
|
| 416 |
# ---------------------------------------------------------------------------
|
|
|
|
| 51 |
DATA_DIR = Path("/data") if Path("/data").is_dir() else Path("./data")
|
| 52 |
|
| 53 |
CHROMA_PERSIST_DIR = str(DATA_DIR / "chroma_db")
|
| 54 |
+
TRAIN_DOCS_DIR = Path("./train_data")
|
| 55 |
COLLECTION_NAME = "rag_documents"
|
| 56 |
CHUNK_SIZE = 512
|
| 57 |
CHUNK_OVERLAP = 50
|
|
|
|
| 375 |
|
| 376 |
|
| 377 |
# ---------------------------------------------------------------------------
|
| 378 |
+
# Step 4: Ingest Train Documents (on-demand)
|
| 379 |
# ---------------------------------------------------------------------------
|
|
|
|
| 380 |
|
| 381 |
|
| 382 |
+
def ingest_train_documents():
|
| 383 |
+
"""Load and embed training documents into the vector store."""
|
| 384 |
if collection.count() > 0:
|
| 385 |
logger.info("Vector store already has documents, skipping ingestion.")
|
| 386 |
return
|
| 387 |
|
| 388 |
+
if not TRAIN_DOCS_DIR.exists():
|
| 389 |
+
logger.warning(f"No train_data directory found at: {TRAIN_DOCS_DIR}")
|
| 390 |
return
|
| 391 |
|
| 392 |
+
# Ingest plain text files (recursively in subdirectories)
|
| 393 |
+
for file_path in TRAIN_DOCS_DIR.rglob("*.txt"):
|
| 394 |
logger.info(f"Ingesting text file: {file_path.name}")
|
| 395 |
text = file_path.read_text(encoding="utf-8")
|
| 396 |
chunks = chunk_text(text, source=file_path.name)
|
| 397 |
add_documents_to_vectorstore(chunks)
|
| 398 |
|
| 399 |
+
# Ingest PDF files (recursively in subdirectories)
|
| 400 |
+
for file_path in TRAIN_DOCS_DIR.rglob("*.pdf"):
|
| 401 |
logger.info(f"Ingesting PDF file: {file_path.name}")
|
| 402 |
text = extract_text_from_pdf(file_path)
|
| 403 |
if text.strip():
|
|
|
|
| 406 |
else:
|
| 407 |
logger.warning(f"No extractable text found in: {file_path.name}")
|
| 408 |
|
| 409 |
+
logger.info(f"Train document ingestion complete. Total chunks: {collection.count()}")
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
|
| 412 |
# ---------------------------------------------------------------------------
|