-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:api Add support for extracting EPUB files in ExtractProcessor (#…
…3254) Co-authored-by: crazywoola <427733928@qq.com>
- Loading branch information
1 parent
44448ba
commit b00466f
Showing
4 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
api/core/rag/extractor/unstructured/unstructured_epub_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
|
||
from core.rag.extractor.extractor_base import BaseExtractor | ||
from core.rag.models.document import Document | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UnstructuredEpubExtractor(BaseExtractor): | ||
"""Load epub files. | ||
Args: | ||
file_path: Path to the file to load. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
file_path: str, | ||
api_url: str = None, | ||
): | ||
"""Initialize with file path.""" | ||
self._file_path = file_path | ||
self._api_url = api_url | ||
|
||
def extract(self) -> list[Document]: | ||
from unstructured.partition.epub import partition_epub | ||
|
||
elements = partition_epub(filename=self._file_path, xml_keep_tags=True) | ||
from unstructured.chunking.title import chunk_by_title | ||
chunks = chunk_by_title(elements, max_characters=2000, combine_text_under_n_chars=2000) | ||
documents = [] | ||
for chunk in chunks: | ||
text = chunk.text.strip() | ||
documents.append(Document(page_content=text)) | ||
|
||
return documents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters