forked from Archives/langchain
Harrison/remote paths pdf (#1544)
Co-authored-by: Tim Asp <707699+timothyasp@users.noreply.github.com>fix-searx
parent
cc423f40f1
commit
357d808484
@ -1,30 +1,15 @@
|
|||||||
"""Loader that loads online PDF files."""
|
"""Loader that loads online PDF files."""
|
||||||
|
|
||||||
import tempfile
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
from langchain.docstore.document import Document
|
from langchain.docstore.document import Document
|
||||||
from langchain.document_loaders.base import BaseLoader
|
from langchain.document_loaders.pdf import BasePDFLoader, UnstructuredPDFLoader
|
||||||
from langchain.document_loaders.pdf import UnstructuredPDFLoader
|
|
||||||
|
|
||||||
|
|
||||||
class OnlinePDFLoader(BaseLoader):
|
class OnlinePDFLoader(BasePDFLoader):
|
||||||
"""Loader that loads online PDFs."""
|
"""Loader that loads online PDFs."""
|
||||||
|
|
||||||
def __init__(self, web_path: str):
|
|
||||||
"""Initialize with file path."""
|
|
||||||
self.web_path = web_path
|
|
||||||
|
|
||||||
def load(self) -> List[Document]:
|
def load(self) -> List[Document]:
|
||||||
"""Load documents."""
|
"""Load documents."""
|
||||||
r = requests.get(self.web_path)
|
loader = UnstructuredPDFLoader(str(self.file_path))
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
return loader.load()
|
||||||
file_path = Path(temp_dir) / "online_file.pdf"
|
|
||||||
file = open(file_path, "wb")
|
|
||||||
file.write(r.content)
|
|
||||||
file.close()
|
|
||||||
loader = UnstructuredPDFLoader(str(file_path))
|
|
||||||
return loader.load()
|
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue