-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocessing.py
49 lines (41 loc) · 1.51 KB
/
data_preprocessing.py
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
43
44
45
46
47
48
49
# !pip install langchain
# !pip install torch
# !pip install -U langchain-community
# !pip install instructorembedding
# !pip install -U sentence-transformers==2.2.2
# !pip install chromadb
from langchain.schema.document import Document
import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
import torch
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
from langchain_community.vectorstores import Chroma
class data_preprocessing:
def __init__(self) -> None:
pass
def create_document(self):
directory_path = 'data/'
docs = []
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
with open(file_path, 'r') as file:
content = file.read()
docs.append(Document(page_content = content))
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=5000,
chunk_overlap=64
)
self.texts = text_splitter.split_documents(docs)
return self
def create_embedding(self,
model_name="hkunlp/instructor-large"):
DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
self.embeddings = HuggingFaceInstructEmbeddings(
model_name=model_name, model_kwargs={"device": DEVICE}
)
db = Chroma.from_documents(
self.texts,
self.embeddings,
persist_directory="vector_db"
)
return db