forked from The-OpenROAD-Project/ORAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_html.py
More file actions
74 lines (58 loc) · 2.06 KB
/
Copy pathprocess_html.py
File metadata and controls
74 lines (58 loc) · 2.06 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
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
import os
import glob
import json
import logging
from tqdm import tqdm
from typing import Optional
from langchain_core.documents import Document
from langchain_community.document_loaders import BSHTMLLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from .chunk_documents import chunk_documents
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO").upper())
chunk_size: int = int(os.getenv("CHUNK_SIZE", 4000))
chunk_overlap: int = int(os.getenv("CHUNK_OVERLAP", 400))
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
length_function=len,
is_separator_regex=False,
)
def process_html(
folder_path: str,
split_text: bool = True,
chunk_size: Optional[int] = None,
) -> list[Document]:
"""
For processing OR/ORFS docs
"""
if not os.path.exists(folder_path) or not os.listdir(folder_path):
logging.error(f"{folder_path} is not populated, returning empty list.")
return []
with open("data/source_list.json") as f:
src_dict = json.loads(f.read())
html_files = glob.glob(os.path.join(folder_path, "**/*.html"), recursive=True)
documents = []
for file_path in tqdm(html_files, desc="Loading HTML files"):
content = BSHTMLLoader(file_path=file_path).load()
for doc in content:
doc.metadata["source"] = file_path.split("./")[-1]
documents.extend(content)
for doc in documents:
try:
url = src_dict[doc.metadata["source"]]
except KeyError:
logging.warning(f"Could not find source for {doc.metadata['source']}")
url = ""
new_metadata = {
"url": url,
"source": doc.metadata["source"],
}
doc.metadata = new_metadata
if split_text:
if not chunk_size:
raise ValueError("Chunk size not set.")
documents = text_splitter.split_documents(documents)
docs_chunked = chunk_documents(chunk_size, documents)
return docs_chunked
else:
return documents