-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest.py
More file actions
40 lines (33 loc) · 1.25 KB
/
Copy pathingest.py
File metadata and controls
40 lines (33 loc) · 1.25 KB
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
import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# Definir rutas (opcional, para mantener orden)
pdf_path = "data/Saga Dune - Frank Herbert.pdf"
index_path = "faiss_index"
# 1. Cargar documento PDF
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"El archivo no se encuentra en: {pdf_path}")
print("Cargando PDF...")
loader = PyPDFLoader(pdf_path)
documents = loader.load()
print(f"PDF cargado. Total de páginas: {len(documents)}")
# 2. Dividir texto
print("Dividiendo texto...")
splitter = RecursiveCharacterTextSplitter(
chunk_size=400,
chunk_overlap=80,
)
docs = splitter.split_documents(documents)
print(f"Texto dividido en {len(docs)} fragmentos (chunks).")
# 3. Embeddings
print("Generando embeddings (esto puede tardar un poco)...")
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
# 4. Crear y guardar índice FAISS
print("Creando índice FAISS...")
db = FAISS.from_documents(docs, embeddings)
db.save_local(index_path)
print(f"¡Listo! Documentos indexados correctamente en la carpeta '{index_path}'.")