forked from microsoft/TaskWeaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_indexer.py
323 lines (288 loc) · 10.4 KB
/
document_indexer.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import argparse
import csv
import json
import os
import pickle
import re
import traceback
from typing import Dict, List, Literal, Tuple
try:
import tiktoken
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
except ImportError:
raise ImportError("Please install the dependencies first.")
def chunk_str_overlap(
s: str,
separator: chr = "\n",
num_tokens: int = 64,
step_tokens: int = 64,
encoding: tiktoken.Encoding = None,
) -> List[str]:
"""
Split a string into chunks with overlap
:param s: the input string
:param separator: the separator to split the string
:param num_tokens: the number of tokens in each chunk
:param step_tokens: the number of tokens to step forward
:param encoding: the encoding to encode the string
"""
assert step_tokens <= num_tokens, (
f"The number of tokens {num_tokens} in each chunk " f"should be larger than the step size {step_tokens}."
)
lines = s.split(separator)
chunks = dict()
final_chunks = []
if len(lines) == 0:
return []
first_line = lines[0]
first_line_size = len(encoding.encode(first_line))
chunks[0] = [first_line, first_line_size]
this_step_size = first_line_size
for i in range(1, len(lines)):
line = lines[i]
line_size = len(encoding.encode(line))
to_pop = []
for key in chunks:
if chunks[key][1] + line_size > num_tokens:
to_pop.append(key)
else:
chunks[key][0] += f"{separator}{line}"
chunks[key][1] += line_size
final_chunks += [chunks.pop(key)[0] for key in to_pop]
if this_step_size + line_size > step_tokens:
chunks[i] = [line, line_size]
this_step_size = 0
this_step_size += line_size
max_remained_chunk = ""
max_remained_chunk_size = 0
for key in chunks:
if chunks[key][1] > max_remained_chunk_size:
max_remained_chunk_size = chunks[key][1]
max_remained_chunk = chunks[key][0]
if max_remained_chunk_size > 0:
final_chunks.append(max_remained_chunk)
return final_chunks
def get_title(
file_name: str,
prop="title: ",
) -> str:
"""
Get the title of a file
:param file_name: the file name
:param prop: the property to get the title
"""
with open(file_name, encoding="utf-8", errors="ignore") as f_in:
for line in f_in:
line = line.strip()
if line and (line.startswith(prop) or any([c.isalnum() for c in line])):
return line
return ""
def extract_text_from_file(
file: str,
file_type: Literal["pdf", "docx", "csv", "pptx"],
) -> Tuple[str, str]:
"""
Extract text from a file in pdf, docx, csv or pptx format
:param file: the file path
:param file_type: the extension of the file
"""
if file_type == "pdf":
try:
from pypdf import PdfReader
except ImportError:
raise ImportError("Please install pypdf first.")
# Extract text from pdf using PyPDF2
reader = PdfReader(file)
extracted_text = " ".join([page.extract_text() for page in reader.pages])
title = extracted_text.split("\n")[0]
elif file_type == "docx":
try:
import docx2txt
except ImportError:
raise ImportError("Please install docx2txt first.")
# Extract text from docx using docx2txt
extracted_text = docx2txt.process(file)
title = extracted_text.split("\n")[0]
elif file_type == "csv":
# Extract text from csv using csv module
extracted_text = ""
title = ""
reader = csv.reader(file)
for row in reader:
extracted_text += " ".join(row) + "\n"
elif file_type == "pptx":
try:
import pptx
except ImportError:
raise ImportError("Please install python-pptx first.")
extracted_text = ""
no_title = True
title = ""
presentation = pptx.Presentation(file)
for slide in presentation.slides:
for shape in slide.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
extracted_text += run.text + " "
if no_title and len(run.text) > 10:
title = run.text
no_title = False
extracted_text += "\n"
else:
# Unsupported file type
raise ValueError(f"Unsupported file type: {file_type}")
return title[:100], extracted_text
def text_parser(
read_file: str,
) -> Tuple[str, str]:
"""
Returns the title, parsed text and a BeautifulSoup object with different file extension
: param read_file: the input file with a given extension
: return: the title, parsed text and a BeautifulSoup object, the BeautifulSoup object is used to get the document
link from the html files
"""
filename, extension = os.path.splitext(read_file)
extension = extension.lstrip(".")
title = filename
soup = None
supported_extensions = ["md", "markdown", "html", "htm", "txt", "json", "jsonl"]
other_extensions = ["docx", "pptx", "pdf", "csv"]
# utf-8-sig will treat BOM header as a metadata of a file not a part of the file content
default_encoding = "utf-8-sig"
if extension in ("md", "markdown", "txt"):
title = get_title(read_file)
with open(read_file, "r", encoding=default_encoding, errors="ignore") as f:
text = f.read()
elif extension in ("html", "htm"):
from bs4 import BeautifulSoup
with open(read_file, "r", encoding=default_encoding, errors="ignore") as f:
soup = BeautifulSoup(f, "html.parser")
title = next(soup.stripped_strings)[:100]
text = soup.get_text("\n")
# read json/jsonl file in and convert each json to a row of string
elif extension in ("json", "jsonl"):
try:
with open(read_file, "r", encoding=default_encoding, errors="ignore") as f:
data = json.load(f) if extension == "json" else [json.loads(line) for line in f]
except:
# json file encoding issue, skip this file
return title, ""
if isinstance(data, dict):
text = json.dumps(data)
elif isinstance(data, list):
content_list = [json.dumps(each_json) for each_json in data]
text = "\n".join(content_list)
title = filename
elif extension in other_extensions:
title, text = extract_text_from_file(read_file, extension)
else: # no support for other format
print(
f"Not support for file with extension: {extension}. "
f"The supported extensions are {supported_extensions}",
)
return title, ""
output_text = re.sub(r"\n{3,}", "\n\n", text)
# keep whitespaces for formatting
output_text = re.sub(r"-{3,}", "---", output_text)
output_text = re.sub(r"\*{3,}", "***", output_text)
output_text = re.sub(r"_{3,}", "___", output_text)
return title, output_text
def chunk_document(
doc_path: str,
chunk_size: int,
chunk_step: int,
) -> Tuple[int, List[str], List[Dict[str, str]], Dict[str, int]]:
"""
Split documents into chunks
:param doc_path: the path of the documents
:param chunk_size: the size of the chunk
:param chunk_step: the step size of the chunk
"""
texts = []
metadata_list = []
file_count = 0
chunk_id_to_index = dict()
enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
# traverse all files under dir
print("Split documents into chunks...")
for root, dirs, files in os.walk(doc_path):
for name in files:
f = os.path.join(root, name)
print(f"Reading {f}")
try:
title, content = text_parser(f)
file_count += 1
if file_count % 100 == 0:
print(f"{file_count} files read.")
if len(content) == 0:
continue
chunks = chunk_str_overlap(
content.strip(),
num_tokens=chunk_size,
step_tokens=chunk_step,
separator="\n",
encoding=enc,
)
source = os.path.sep.join(f.split(os.path.sep)[4:])
for i in range(len(chunks)):
# custom metadata if needed
metadata = {
"source": source,
"title": title,
"chunk_id": i,
}
chunk_id_to_index[f"{source}_{i}"] = len(texts) + i
metadata_list.append(metadata)
texts.extend(chunks)
except Exception as e:
print(f"Error encountered when reading {f}: {traceback.format_exc()} {e}")
return file_count, texts, metadata_list, chunk_id_to_index
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--doc_path",
help="the path of the documents",
type=str,
default="",
)
parser.add_argument(
"-c",
"--chunk_size",
help="the size of the chunk",
type=int,
default=64,
)
parser.add_argument(
"-s",
"--chunk_step",
help="the step size of the chunk",
type=int,
default=64,
)
parser.add_argument(
"-o",
"--output_path",
help="the path of the output",
type=str,
default="",
)
args = parser.parse_args()
file_count, texts, metadata_list, chunk_id_to_index = chunk_document(
doc_path=args.doc_path,
chunk_size=args.chunk_size,
chunk_step=args.chunk_step,
)
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = FAISS.from_texts(
texts=texts,
metadatas=metadata_list,
embedding=embeddings,
)
vectorstore.save_local(folder_path=args.output_path)
with open(os.path.join(args.output_path, "chunk_id_to_index.pkl"), "wb") as f:
pickle.dump(chunk_id_to_index, f)
print(f"Saved vectorstore to {args.output_path}")