Spaces:
Runtime error
Runtime error
Oviya commited on
Commit Β·
99efaaa
1
Parent(s): 6113f7e
fix
Browse files4 unmodified lines56789101112131415161718192021222324252627282930313233342 unmodified lines3738394041424344452 unmodified lines4849505152534 unmodified lines58596061624 unmodified lines# Allow running both as a module and as a scriptif __package__ in (None, ""):# When you run: python ragg/ingest_all.pysys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))from ragg.rag_backend import ingest_pdfs_from_folder, get_embeddingsimport ragg.rag_backend as rag_backendelse:# When you run: python -m ragg.ingest_all (recommended)from ragg.rag_backend import ingest_pdfs_from_folder, get_embeddingsimport ragg.rag_backend as rag_backendIS_PROD = os.getenv("ENV", "dev").lower() == "prod"HERE = os.path.dirname(__file__)PDFS_ROOT = os.path.abspath(os.path.join(HERE, "..", "pdfs"))# Hugging Face shows cwd as /app β persist under /app/chromaCHROMA_BASE = "/app/chroma" if IS_PROD else "chroma"def ingest_all_levels():"""Ingest PDFs from ../pdfs/{low,mid,high} into separate Chroma DBs."""print("\nπ Starting ingestion\n")print(f"π PDFs root: {PDFS_ROOT}")print(f"πΎ Chroma base: {CHROMA_BASE} (ENV={'prod' if IS_PROD else 'dev'})\n")for name in ["low", "mid", "high"]:folder_path = os.path.join(PDFS_ROOT, name)if not os.path.isdir(folder_path):print(f"β οΈ Skip {name}: {folder_path} not found")2 unmodified lineschroma_dir = os.path.join(CHROMA_BASE, name)os.makedirs(chroma_dir, exist_ok=True)# Monkey-patch vectorstore factory used by your backend for this rundef get_vectorstore_for_level():print(f"πΉ Chroma at: {chroma_dir}")return Chroma(persist_directory=chroma_dir,embedding_function=get_embeddings()2 unmodified linesrag_backend.get_vectorstore = get_vectorstore_for_leveltry:res = ingest_pdfs_from_folder(folder_path, subject="English", grade="5", chapter=name)4 unmodified linesprint(f"β {name}: {e}")print(traceback.format_exc())if __name__ == "__main__":ingest_all_levels()4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940412 unmodified lines4445464748495051522 unmodified lines555657585960614 unmodified lines666768697071724 unmodified lines# Allow running both as a module and as a scriptif __package__ in (None, ""):sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))from ragg.rag_backend import ingest_pdfs_from_folder, get_embeddingsimport ragg.rag_backend as rag_backendelse:from ragg.rag_backend import ingest_pdfs_from_folder, get_embeddingsimport ragg.rag_backend as rag_backendIS_PROD = os.getenv("ENV", "dev").lower() == "prod"HERE = os.path.dirname(__file__)# β Point to the actual PDF location inside the SpacePDFS_ROOT = os.path.abspath(os.path.join(HERE, "pdfs")) # /app/ragg/pdfs (fallback)if not os.path.isdir(PDFS_ROOT):# Hugging Face uses /app as working dir β /app/pdfs/*PDFS_ROOT = os.path.abspath(os.path.join(HERE, "..", "pdfs"))# β Always persist Chroma under /data/chroma (mounted persistent dir)CHROMA_BASE = os.getenv("CHROMA_ROOT", "/data/chroma")def ingest_all_levels():"""Ingest PDFs from ./pdfs/{low,mid,high} into separate Chroma DBs.Works both locally and on Hugging Face."""print("\nπ Starting ingestion\n")print(f"π PDFs root: {PDFS_ROOT}")print(f"πΎ Chroma base: {CHROMA_BASE} (ENV={'prod' if IS_PROD else 'dev'})\n")levels = ["low", "mid", "high"]for name in levels:folder_path = os.path.join(PDFS_ROOT, name)if not os.path.isdir(folder_path):print(f"β οΈ Skip {name}: {folder_path} not found")2 unmodified lineschroma_dir = os.path.join(CHROMA_BASE, name)os.makedirs(chroma_dir, exist_ok=True)# Rebind vectorstore factory used by your backend for this rundef get_vectorstore_for_level():print(f"πΉ Using Chroma at: {chroma_dir}")return Chroma(persist_directory=chroma_dir,embedding_function=get_embeddings()2 unmodified linesrag_backend.get_vectorstore = get_vectorstore_for_leveltry:print(f"β‘οΈ Ingesting {folder_path}")res = ingest_pdfs_from_folder(folder_path, subject="English", grade="5", chapter=name)4 unmodified linesprint(f"β {name}: {e}")print(traceback.format_exc())print("π― Ingestion complete.\n")if __name__ == "__main__":ingest_all_levels()
ragg/ingest_all.py+19-9