Repository navigation
feat: add EPUB and optional MOBI dataset loading - #14453
Conversation
|
@codex review |
|
@codex security review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e18c3c0ff6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if item.get("media-type") not in {"application/xhtml+xml", "text/html"}: | ||
| continue |
There was a problem hiding this comment.
Follow XHTML fallbacks for foreign spine items
For an EPUB whose spine references a foreign content item (for example SVG) with a manifest fallback to XHTML, this branch skips the item without traversing the fallback. Those EPUBs consequently lose the chapter—and an otherwise text-bearing book is reported as unreadable—even though a supported XHTML representation is present, so resolve the fallback chain before applying this filter.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: SVG spine items with an XHTML fallback are rare in real books. A book made only of them already fails with a clear "contains no readable text" error naming the file.
…U export in the test [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cbe1cb7123
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| text = _html_text(read(name)) | ||
| if text: | ||
| if chapter_markers: | ||
| text = "<|chapter_start|>\n" + text + "\n<|chapter_end|>" |
There was a problem hiding this comment.
Keep chapter markers intact across chunks
With chapter_markers=True, overlap=0, and a tokenizer that represents these literals with multiple tokens (the normal case unless they were registered as added tokens), _chunk_text can place a window boundary inside a marker; a byte tokenizer at chunk_size=8, for example, emits <|chapte, r_start|, and > in separate dataset rows. Trainers treat rows as separate examples or insert packing separators, so no complete delimiter survives; make marker spans indivisible and reject budgets that cannot hold one.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: chapters are joined with a blank line, which the chunker prefers as a break, so at real chunk sizes (thousands of tokens) a window boundary inside a marker is very unlikely; chunk_size=8 is not a realistic setting.
| while char_start < len(text) and text[char_start].isspace(): | ||
| char_start += 1 |
There was a problem hiding this comment.
Preserve preformatted indentation at chunk starts
With overlap=0, whenever a <pre> block starts a chapter or crosses a token window, this loop advances char_start over the block's newline and indentation before slicing; <pre>first\n second</pre> with one-token chunks therefore emits a second row of second, not second. The new placeholder restoration only protects _html_text, while the end-to-end load_ebook_dataset path still changes code or poetry semantics here and at the later .strip(), so retain source indentation at continuation boundaries or carry preformatted spans into the chunker.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: it only drops the indentation of the first line of a chunk that starts mid code block, which is a minor effect on training text, so I am leaving it.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cbe1cb7123
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def handle_starttag(self, tag, attrs): | ||
| if tag == "body": | ||
| self.handle_endtag("head") | ||
| attrs = dict(attrs) |
There was a problem hiding this comment.
Normalize namespace-prefixed XHTML tags
When a valid EPUB serializes the XHTML namespace with a prefix (for example, <x:p>), HTMLParser passes x:p here, so none of the body, ignored-tag, or block-tag comparisons match. A prefixed chapter containing a head and two paragraphs is consequently emitted like ClutterCSSFirstSecond, silently adding head contents and joining words; normalize qualified tag names to their local names before dispatching them.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: EPUB chapters from real publishers and Calibre use the default XHTML namespace without a prefix, so prefixed tags are too rare to handle here.
| columns.update({key: [] for key in ("source", "chunk_id", "title", "author", "genre")}) | ||
| for book in files: | ||
| text, metadata = _read_book(book, chapter_markers) | ||
| if not text: |
There was a problem hiding this comment.
Reject whitespace-only books after pre restoration
When an otherwise empty chapter contains a whitespace-only <pre>, the verbatim restoration makes _html_text return a truthy whitespace string, so this check accepts it. With byte-level tokenizers that expose whitespace offsets, _chunk_text then yields no rows and the API returns an empty Dataset instead of the documented no-readable-text error (other tokenizers fail with an unrelated no-tokens error); check for non-whitespace content here.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: it needs a book whose only content is a whitespace-only
, which does not occur in real ebooks.
| return re.sub( | ||
| "\ue000(\\d+)\ue001", | ||
| lambda m: unicodedata.normalize("NFC", parser.preformatted[int(m[1])]), | ||
| text, |
There was a problem hiding this comment.
Prevent private-use text from matching pre placeholders
When XHTML prose legitimately contains U+E000, digits, and U+E001 in sequence, this global substitution mistakes the author's text for an internal <pre> placeholder. With no corresponding pre block the valid book is rejected, while with one the sequence is silently replaced by unrelated preformatted text; use placeholders that are escaped or guaranteed not to occur in the input.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: prose would need the exact private-use sequence U+E000, digits, U+E001, which real books do not contain.
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
744d3e1 to
cea307e
Compare
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…processor (#15501) On a host without an accelerator, `import unsloth` hands off to the CPU backend (`unsloth/_cpu_init.py`, #14717). The GPU backend exports the dataprep helpers from `unsloth` (`_gpu_init.py`), but the CPU backend did not. So the documented `from unsloth import load_ebook_dataset` (#14453) raises `ImportError: cannot import name 'load_ebook_dataset'` on CPU-only hosts, and so do `RawTextDataLoader` and `TextPreprocessor`. The CPU backend now exports all three. It loads `dataprep/raw_text.py` and `dataprep/ebooks.py` by file, the way the MLX backend already does, because `dataprep/__init__` also imports `synthetic.py`, which needs a GPU device type. Neither file has relative imports. `test_cpu_backend_init.py::test_no_accelerator_loads_the_cpu_backend` now also checks the three names in its no-accelerator subprocess probe. The GPU-only module list it asserts against is unchanged. | | main | this PR | |---|---|---| | `test_cpu_backend_init.py` (CUDA hidden) | 1 failed, 6 passed, 1 skipped | 7 passed, 1 skipped |
Summary
Add
load_ebook_dataset: EPUB/MOBI → cleaned text → overlapping token chunks → Hugging Face Dataset.Preserves reading order and paragraphs, supports optional metadata/chapter markers, and shares one reader mapping for extensions. Requires a fast tokenizer; MOBI uses optional Calibre.
Closes #14377.
Evidence
main.Merge Danger
Reversible, scoped to ebook preprocessing and its public exports. Model training was not run.