-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_process.py
More file actions
42 lines (36 loc) · 1.23 KB
/
data_process.py
File metadata and controls
42 lines (36 loc) · 1.23 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
41
42
from utils import *
import os
from glob import glob
from langchain.vectorstores.chroma import Chroma
from langchain_community.document_loaders import CSVLoader, PyMuPDFLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
def doc2vec():
# Define a text splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size = 300,
chunk_overlap = 50
)
# Read and split files
dir_path = os.path.join(os.path.dirname(__file__), './inputs/')
documents = []
for file_path in glob(dir_path + '*.*'):
loader = None
if '.csv' in file_path:
loader = CSVLoader(file_path)
if '.pdf' in file_path:
loader = PyMuPDFLoader(file_path)
if '.txt' in file_path:
loader = TextLoader(file_path)
if loader:
documents += loader.load_and_split(text_splitter)
# vectorize and store
if documents:
vdb = Chroma.from_documents(
documents = documents,
embedding = get_embeddings_model(),
persist_directory = os.path.join(os.path.dirname(__file__), './data/db/')
)
vdb.persist()
print(documents)
if __name__ == '__main__':
doc2vec()