Spaces:
Sleeping
Sleeping
Update utils/DocsLoader.py
Browse files- utils/DocsLoader.py +58 -47
utils/DocsLoader.py
CHANGED
|
@@ -19,55 +19,66 @@ nltk.download("punkt_tab", download_dir=NLTK_PATH, quiet=True)
|
|
| 19 |
|
| 20 |
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
def load_and_chunk(url: str) -> list[Document]:
|
| 23 |
print(url)
|
| 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 |
-
text = resp.content.decode("utf-8", errors="ignore")
|
| 56 |
-
docs = [Document(page_content=text)]
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# sentence_docs = []
|
| 62 |
-
# for doc in docs:
|
| 63 |
-
# sentences = nltk.sent_tokenize(doc.page_content)
|
| 64 |
-
# for sent in sentences:
|
| 65 |
-
# if sent.strip():
|
| 66 |
-
# sentence_docs.append(Document(page_content=sent, metadata=doc.metadata))
|
| 67 |
-
full_text = "\n".join([doc.page_content for doc in docs])
|
| 68 |
|
| 69 |
-
# splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
| 70 |
-
splitter = SentenceTransformersTokenTextSplitter(model_name = MODEL_DIR,tokens_per_chunk=512, chunk_overlap=80)
|
| 71 |
-
# return splitter.split_documents(docs)
|
| 72 |
-
# return splitter.split_documents([Document(page_content=full_text)])
|
| 73 |
-
return splitter.create_documents([full_text])
|
|
|
|
| 19 |
|
| 20 |
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
|
| 21 |
|
| 22 |
+
chunk_dict= {}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
def load_and_chunk(url: str) -> list[Document]:
|
| 26 |
print(url)
|
| 27 |
+
if(url not in chunk_dict):
|
| 28 |
+
print("processing new url")
|
| 29 |
+
resp = requests.get(url)
|
| 30 |
+
if resp.status_code != 200:
|
| 31 |
+
raise HTTPException(400, "Could not download document")
|
| 32 |
+
|
| 33 |
+
content_type = resp.headers.get("Content-Type", "").lower()
|
| 34 |
+
url_lower = url.lower()
|
| 35 |
+
|
| 36 |
+
if "application/pdf" in content_type or ".pdf" in url_lower:
|
| 37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 38 |
+
tmp.write(resp.content)
|
| 39 |
+
tmp_path = tmp.name
|
| 40 |
+
try:
|
| 41 |
+
loader = PyMuPDFLoader(tmp_path)
|
| 42 |
+
docs = loader.load_and_split()
|
| 43 |
+
finally:
|
| 44 |
+
os.remove(tmp_path)
|
| 45 |
+
|
| 46 |
+
elif (
|
| 47 |
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" in content_type
|
| 48 |
+
or ".docx" in url_lower
|
| 49 |
+
):
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
|
| 51 |
+
tmp.write(resp.content)
|
| 52 |
+
tmp_path = tmp.name
|
| 53 |
+
try:
|
| 54 |
+
loader = Docx2txtLoader(tmp_path)
|
| 55 |
+
docs = loader.load_and_split()
|
| 56 |
+
finally:
|
| 57 |
+
os.remove(tmp_path)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
elif "text/plain" in content_type or ".txt" in url_lower:
|
| 60 |
+
text = resp.content.decode("utf-8", errors="ignore")
|
| 61 |
+
docs = [Document(page_content=text)]
|
| 62 |
+
|
| 63 |
+
else:
|
| 64 |
+
raise HTTPException(400, f"Unsupported document type: {content_type}")
|
| 65 |
+
# # --- Step 1: Sentence split ---
|
| 66 |
+
# sentence_docs = []
|
| 67 |
+
# for doc in docs:
|
| 68 |
+
# sentences = nltk.sent_tokenize(doc.page_content)
|
| 69 |
+
# for sent in sentences:
|
| 70 |
+
# if sent.strip():
|
| 71 |
+
# sentence_docs.append(Document(page_content=sent, metadata=doc.metadata))
|
| 72 |
+
full_text = "\n".join([doc.page_content for doc in docs])
|
| 73 |
+
|
| 74 |
+
# splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
| 75 |
+
splitter = SentenceTransformersTokenTextSplitter(model_name = MODEL_DIR,tokens_per_chunk=512, chunk_overlap=90)
|
| 76 |
+
# return splitter.split_documents(docs)
|
| 77 |
+
# return splitter.split_documents([Document(page_content=full_text)])
|
| 78 |
+
chunk_dict[url] = splitter.create_documents([full_text])
|
| 79 |
+
# return splitter.create_documents([full_text])
|
| 80 |
+
return chunk_dict[url]
|
| 81 |
else:
|
| 82 |
+
print("stored chunk")
|
| 83 |
+
return chunk_dict[url]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|