File size: 1,800 Bytes
c07fa76
 
 
 
 
314c1d9
 
c07fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314c1d9
c07fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a5dc2
c07fa76
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
import os
import tempfile
import requests

from fastapi import HTTPException
# from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader
from langchain_community.document_loaders import PyMuPDFLoader, Docx2txtLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document


def load_and_chunk(url: str) -> list[Document]:
    resp = requests.get(url)
    if resp.status_code != 200:
        raise HTTPException(400, "Could not download document")

    content_type = resp.headers.get("Content-Type", "").lower()
    url_lower = url.lower()

    if "application/pdf" in content_type or ".pdf" in url_lower:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            tmp.write(resp.content)
            tmp_path = tmp.name
        try:
            loader = PyMuPDFLoader(tmp_path)
            docs = loader.load_and_split()
        finally:
            os.remove(tmp_path)

    elif (
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document" in content_type
        or ".docx" in url_lower
    ):
        with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
            tmp.write(resp.content)
            tmp_path = tmp.name
        try:
            loader = Docx2txtLoader(tmp_path)
            docs = loader.load_and_split()
        finally:
            os.remove(tmp_path)

    elif "text/plain" in content_type or ".txt" in url_lower:
        text = resp.content.decode("utf-8", errors="ignore")
        docs = [Document(page_content=text)]

    else:
        raise HTTPException(400, f"Unsupported document type: {content_type}")

    splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
    return splitter.split_documents(docs)