Spaces:
Sleeping
Sleeping
File size: 1,111 Bytes
5f096cc |
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 |
from pathlib import Path
from langchain_community.document_loaders import (
PyPDFLoader,
TextLoader,
PythonLoader,
NotebookLoader,
)
import pickle
DATA_DIR = Path("E:/courses/LangChain Project/main root/data/")
OUTPUT_DIR = Path("E:/courses/LangChain Project/main root/output/")
OUTPUT_PATH = OUTPUT_DIR / "all_docs.pkl"
# ✅ Create output folder if it doesn't exist
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
loaders = {
".pdf": PyPDFLoader,
".txt": lambda path: TextLoader(path, encoding="utf-8"),
".py": PythonLoader,
".ipynb": NotebookLoader,
}
documents = []
for file in DATA_DIR.rglob("*"):
loader_class = loaders.get(file.suffix.lower())
if loader_class:
try:
docs = loader_class(str(file)).load()
documents.extend(docs)
print(f"[✓] Loaded: {file.name}")
except Exception as e:
print(f"[!] Failed to load {file.name}: {e}")
with open(OUTPUT_PATH, "wb") as f:
pickle.dump(documents, f)
print(f"📦 Saved {len(documents)} documents to {OUTPUT_PATH}") |