Zander/online pdf loader (#984)

This commit is contained in:
zanderchase 2023-02-10 15:42:30 -08:00 committed by GitHub
parent 055a53c27f
commit c2d1d903fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@ -49,6 +49,8 @@ There are a lot of different document loaders that LangChain supports. Below are
`Gutenberg <./examples/gutenberg.html>`_: A walkthrough of how to load data from a Gutenberg ebook text. `Gutenberg <./examples/gutenberg.html>`_: A walkthrough of how to load data from a Gutenberg ebook text.
`Online PDF <./examples/online_pdf.html>`_: A walkthrough of how to load data from an online PDF.
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
:glob: :glob:

View File

@ -14,6 +14,7 @@ from langchain.document_loaders.html import UnstructuredHTMLLoader
from langchain.document_loaders.imsdb import IMSDbLoader from langchain.document_loaders.imsdb import IMSDbLoader
from langchain.document_loaders.notion import NotionDirectoryLoader from langchain.document_loaders.notion import NotionDirectoryLoader
from langchain.document_loaders.obsidian import ObsidianLoader from langchain.document_loaders.obsidian import ObsidianLoader
from langchain.document_loaders.online_pdf import OnlinePDFLoader
from langchain.document_loaders.paged_pdf import PagedPDFSplitter from langchain.document_loaders.paged_pdf import PagedPDFSplitter
from langchain.document_loaders.pdf import UnstructuredPDFLoader from langchain.document_loaders.pdf import UnstructuredPDFLoader
from langchain.document_loaders.powerpoint import UnstructuredPowerPointLoader from langchain.document_loaders.powerpoint import UnstructuredPowerPointLoader
@ -52,4 +53,5 @@ __all__ = [
"GutenbergLoader", "GutenbergLoader",
"PagedPDFSplitter", "PagedPDFSplitter",
"EveryNoteLoader", "EveryNoteLoader",
"OnlinePDFLoader",
] ]

View File

@ -0,0 +1,29 @@
"""Loader that loads online PDF files."""
import tempfile
from typing import List
import requests
from langchain.docstore.document import Document
from langchain.document_loaders.base import BaseLoader
from langchain.document_loaders.pdf import UnstructuredPDFLoader
class OnlinePDFLoader(BaseLoader):
"""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]:
"""Load documents."""
r = requests.get(self.web_path)
with tempfile.TemporaryDirectory() as temp_dir:
file_path = f"{temp_dir}/online_file.pdf"
file = open(file_path, "wb")
file.write(r.content)
file.close()
loader = UnstructuredPDFLoader(file_path)
return loader.load()