Spaces:
Sleeping
Sleeping
| """ | |
| PDF ingestion. | |
| WHY: Local embeddings remove paid API dependency entirely. | |
| """ | |
| import os | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from app.config import PDF_DIR, FAISS_DIR, CHUNK_SIZE, CHUNK_OVERLAP, EMBEDDING_MODEL | |
| def ingest_pdfs(): | |
| documents = [] | |
| for file in os.listdir(PDF_DIR): | |
| if file.endswith(".pdf"): | |
| loader = PyPDFLoader(os.path.join(PDF_DIR, file)) | |
| documents.extend(loader.load()) | |
| splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=CHUNK_SIZE, | |
| chunk_overlap=CHUNK_OVERLAP | |
| ) | |
| chunks = splitter.split_documents(documents) | |
| embeddings = HuggingFaceEmbeddings( | |
| model_name=EMBEDDING_MODEL | |
| ) | |
| vectorstore = FAISS.from_documents(chunks, embeddings) | |
| vectorstore.save_local(FAISS_DIR) | |