Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
unstructured_xml_extractor.py42 linesDownload Raw Back to unstructured
1import logging2 3from core.rag.extractor.extractor_base import BaseExtractor4from core.rag.models.document import Document5 6logger = logging.getLogger(__name__)7 8 9class UnstructuredXmlExtractor(BaseExtractor):10    """Load xml files.11 12 13    Args:14        file_path: Path to the file to load.15    """16 17    def __init__(self, file_path: str, api_url: str, api_key: str):18        """Initialize with file path."""19        self._file_path = file_path20        self._api_url = api_url21        self._api_key = api_key22 23    def extract(self) -> list[Document]:24        if self._api_url:25            from unstructured.partition.api import partition_via_api26 27            elements = partition_via_api(filename=self._file_path, api_url=self._api_url, api_key=self._api_key)28        else:29            from unstructured.partition.xml import partition_xml30 31            elements = partition_xml(filename=self._file_path, xml_keep_tags=True)32 33        from unstructured.chunking.title import chunk_by_title34 35        chunks = chunk_by_title(elements, max_characters=2000, combine_text_under_n_chars=2000)36        documents = []37        for chunk in chunks:38            text = chunk.text.strip()39            documents.append(Document(page_content=text))40 41        return documents42