mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Harrison/unstructured validation (#2111)
Co-authored-by: kravetsmic <79907559+kravetsmic@users.noreply.github.com>
This commit is contained in:
parent
b25dbcb5b3
commit
6e85cbcce3
@ -11,17 +11,36 @@ logger = logging.getLogger(__file__)
|
|||||||
class UnstructuredURLLoader(BaseLoader):
|
class UnstructuredURLLoader(BaseLoader):
|
||||||
"""Loader that uses unstructured to load HTML files."""
|
"""Loader that uses unstructured to load HTML files."""
|
||||||
|
|
||||||
def __init__(self, urls: List[str], continue_on_failure: bool = True):
|
def __init__(
|
||||||
|
self, urls: List[str], continue_on_failure: bool = True, headers: dict = {}
|
||||||
|
):
|
||||||
"""Initialize with file path."""
|
"""Initialize with file path."""
|
||||||
try:
|
try:
|
||||||
import unstructured # noqa:F401
|
import unstructured # noqa:F401
|
||||||
|
from unstructured.__version__ import __version__ as __unstructured_version__
|
||||||
|
|
||||||
|
self.__version = __unstructured_version__
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"unstructured package not found, please install it with "
|
"unstructured package not found, please install it with "
|
||||||
"`pip install unstructured`"
|
"`pip install unstructured`"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not self.__is_headers_available() and len(headers.keys()) != 0:
|
||||||
|
logger.warning(
|
||||||
|
"You are using old version of unstructured. "
|
||||||
|
"The headers parameter is ignored"
|
||||||
|
)
|
||||||
|
|
||||||
self.urls = urls
|
self.urls = urls
|
||||||
self.continue_on_failure = continue_on_failure
|
self.continue_on_failure = continue_on_failure
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
|
def __is_headers_available(self) -> bool:
|
||||||
|
_unstructured_version = self.__version.split("-")[0]
|
||||||
|
unstructured_version = tuple([int(x) for x in _unstructured_version.split(".")])
|
||||||
|
|
||||||
|
return unstructured_version >= (0, 5, 7)
|
||||||
|
|
||||||
def load(self) -> List[Document]:
|
def load(self) -> List[Document]:
|
||||||
"""Load file."""
|
"""Load file."""
|
||||||
@ -30,6 +49,9 @@ class UnstructuredURLLoader(BaseLoader):
|
|||||||
docs: List[Document] = list()
|
docs: List[Document] = list()
|
||||||
for url in self.urls:
|
for url in self.urls:
|
||||||
try:
|
try:
|
||||||
|
if self.__is_headers_available():
|
||||||
|
elements = partition_html(url=url, headers=self.headers)
|
||||||
|
else:
|
||||||
elements = partition_html(url=url)
|
elements = partition_html(url=url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if self.continue_on_failure:
|
if self.continue_on_failure:
|
||||||
|
Loading…
Reference in New Issue
Block a user