Update ingest.py
Browse files
ingest.py
CHANGED
@@ -6,72 +6,67 @@ from langchain.document_loaders import PyPDFLoader
|
|
6 |
import shutil
|
7 |
import time
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Obtén la ruta completa del directorio actual del script
|
20 |
-
script_directory = os.path.dirname(os.path.abspath(__file__))
|
21 |
-
md_folder_path = os.path.join(script_directory,
|
22 |
-
mdToIngest_path = os.path.join(script_directory,
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
for filename in os.listdir(mdToIngest_path):
|
26 |
try:
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
with open(file_path, "r", encoding="utf-8") as archivo:
|
32 |
-
contenido = archivo.read()
|
33 |
-
print(f"Se leyó el archivo '{file_path}'.")
|
34 |
-
|
35 |
-
headersToSplitOn = [("#", "Header"), ("##", "Title")]
|
36 |
-
|
37 |
-
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headersToSplitOn)
|
38 |
-
md_header_splits = markdown_splitter.split_text(contenido)
|
39 |
-
|
40 |
-
for document in md_header_splits:
|
41 |
-
lista = []
|
42 |
-
|
43 |
-
# Extraer y mostrar los metadatos
|
44 |
-
metadata = document.metadata
|
45 |
-
page_content = document.page_content
|
46 |
-
for key, value in metadata.items():
|
47 |
-
lista.append(f"{value}{page_content}")
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
try:
|
52 |
-
shutil.move(file_path, ruta_destino)
|
53 |
-
print(f'Archivo movido a {ruta_destino} exitosamente.')
|
54 |
-
except shutil.Error as e:
|
55 |
-
print(f'Ocurrió un error al mover el archivo: {e}')
|
56 |
|
|
|
57 |
|
58 |
except Exception as e:
|
59 |
-
print(f'Ocurrió un error
|
60 |
-
print('Pasando al siguiente archivo...')
|
61 |
-
continue
|
62 |
-
|
63 |
-
try:
|
64 |
-
time.sleep(5)
|
65 |
-
# Eliminar la carpeta y su contenido
|
66 |
-
shutil.rmtree(mdToIngest_path)
|
67 |
-
|
68 |
-
# Crear la carpeta nuevamente
|
69 |
-
os.mkdir("mdToIngest")
|
70 |
-
|
71 |
-
print(f'Carpeta {mdToIngest_path} eliminada y recreada exitosamente.')
|
72 |
-
|
73 |
-
except Exception as e:
|
74 |
-
print(f'Ocurrió un error: {e}')
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
6 |
import shutil
|
7 |
import time
|
8 |
|
9 |
+
def procesar_archivos(mdToIngest_path="mdToIngest", md_folder_path="md_folder"):
|
10 |
+
model_name = "BAAI/bge-large-en"
|
11 |
+
model_kwargs = {'device': 'cpu'}
|
12 |
+
encode_kwargs = {'normalize_embeddings': False}
|
13 |
+
embeddings = HuggingFaceBgeEmbeddings(
|
14 |
+
model_name=model_name,
|
15 |
+
model_kwargs=model_kwargs,
|
16 |
+
encode_kwargs=encode_kwargs
|
17 |
+
)
|
18 |
+
|
19 |
+
# Obtén la ruta completa del directorio actual del script
|
20 |
+
script_directory = os.path.dirname(os.path.abspath(__file__))
|
21 |
+
md_folder_path = os.path.join(script_directory, md_folder_path)
|
22 |
+
mdToIngest_path = os.path.join(script_directory, mdToIngest_path)
|
23 |
+
|
24 |
+
for filename in os.listdir(mdToIngest_path):
|
25 |
+
try:
|
26 |
+
# Construye la ruta completa del archivo
|
27 |
+
file_path = os.path.join(mdToIngest_path, filename)
|
28 |
+
ruta_destino = os.path.join(md_folder_path, filename)
|
29 |
+
|
30 |
+
with open(file_path, "r", encoding="utf-8") as archivo:
|
31 |
+
contenido = archivo.read()
|
32 |
+
print(f"Se leyó el archivo '{file_path}'.")
|
33 |
+
|
34 |
+
headersToSplitOn = [("#", "Header"), ("##", "Title")]
|
35 |
+
|
36 |
+
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headersToSplitOn)
|
37 |
+
md_header_splits = markdown_splitter.split_text(contenido)
|
38 |
+
|
39 |
+
for document in md_header_splits:
|
40 |
+
lista = []
|
41 |
+
|
42 |
+
# Extraer y mostrar los metadatos
|
43 |
+
metadata = document.metadata
|
44 |
+
page_content = document.page_content
|
45 |
+
for key, value in metadata.items():
|
46 |
+
lista.append(f"{value}{page_content}")
|
47 |
+
|
48 |
+
vector_store = Chroma.from_documents(md_header_splits, embeddings, collection_metadata={"hnsw:space": "cosine"}, persist_directory="stores/ConserGPT")
|
49 |
+
|
50 |
+
try:
|
51 |
+
shutil.move(file_path, ruta_destino)
|
52 |
+
print(f'Archivo movido a {ruta_destino} exitosamente.')
|
53 |
+
except shutil.Error as e:
|
54 |
+
print(f'Ocurrió un error al mover el archivo: {e}')
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
print(f'Ocurrió un error al leer el archivo: {e}')
|
58 |
+
print('Pasando al siguiente archivo...')
|
59 |
+
continue
|
60 |
|
|
|
61 |
try:
|
62 |
+
time.sleep(5)
|
63 |
+
# Eliminar la carpeta y su contenido
|
64 |
+
shutil.rmtree(mdToIngest_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
# Crear la carpeta nuevamente
|
67 |
+
os.mkdir("mdToIngest")
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
print(f'Carpeta {mdToIngest_path} eliminada y recreada exitosamente.')
|
70 |
|
71 |
except Exception as e:
|
72 |
+
print(f'Ocurrió un error: {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|