Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop adding backslashes to Word files in Data Recipes (#12488)

Nilay48075b4f018d
diff --git a/studio/backend/routes/data_recipe/seed.py b/studio/backend/routes/data_recipe/seed.py
index b589f06d516..3352dee9bc1 100644
--- a/studio/backend/routes/data_recipe/seed.py
+++ b/studio/backend/routes/data_recipe/seed.py
@@ -1217,10 +1217,8 @@ def _extract_text_from_file(file_path: Path, ext: str) -> str:
from core.rag import config, pdf_ocr
raw = pdf_ocr.extract_text(str(file_path), config.OCR_SCANNED, config.OCR_MAX_PAGES)
elif ext == ".docx":
- import mammoth
- with open(str(file_path), "rb") as f:
- result = mammoth.convert_to_markdown(f)
- raw = result.value
+ from core.rag import parsers
+ raw = "\n\n".join(page.text for page in parsers.parse(str(file_path)))
else:
raise ValueError(f"Unsupported file type: {ext}")
@@ -1386,8 +1384,10 @@ async def upload_unstructured_file(
raw_path.unlink(missing_ok = True)
extracted_path.unlink(missing_ok = True)
missing = getattr(e, "name", None)
- expected_missing = {".pdf": "pymupdf4llm", ".docx": "mammoth"}.get(ext)
+ expected_missing = {".pdf": "pymupdf4llm", ".docx": "docx"}.get(ext)
if isinstance(e, ModuleNotFoundError) and missing == expected_missing:
+ # Name what to install: python-docx imports as docx, and PyPI's "docx" is another package.
+ package = {"docx": "python-docx"}.get(missing, missing)
logger.error(
"data_recipe.seed.text_extraction_dependency_missing",
error = str(e),
@@ -1399,7 +1399,7 @@ async def upload_unstructured_file(
filename = original_filename,
size_bytes = size_bytes,
status = "error",
- error = f"Cannot read {ext} files: the '{missing}' package is not installed.",
+ error = f"Cannot read {ext} files: the '{package}' package is not installed.",
)
logger.error(
"data_recipe.seed.text_extraction_failed",
diff --git a/studio/backend/tests/test_data_recipe_seed.py b/studio/backend/tests/test_data_recipe_seed.py
index a2fbdbeba29..024659a6bc7 100644
--- a/studio/backend/tests/test_data_recipe_seed.py
+++ b/studio/backend/tests/test_data_recipe_seed.py
@@ -2,7 +2,9 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import asyncio
+import base64
import importlib.util
+import io
import sys
from pathlib import Path
from types import SimpleNamespace
@@ -103,20 +105,20 @@ def test_local_csv_seed_keeps_its_values_as_written(monkeypatch, tmp_path):
@pytest.mark.parametrize(
- ("filename", "package"),
+ ("filename", "module", "package"),
[
- ("paper.pdf", "pymupdf4llm"),
- ("notes.docx", "mammoth"),
+ ("paper.pdf", "pymupdf4llm", "pymupdf4llm"),
+ ("notes.docx", "docx", "python-docx"),
],
)
def test_unstructured_upload_names_missing_extractor_dependency(
- monkeypatch, tmp_path, filename, package
+ monkeypatch, tmp_path, filename, module, package
):
seed_route = _load_seed_route(monkeypatch, tmp_path)
monkeypatch.setattr(
seed_route,
"_extract_text_from_file",
- _raise(ModuleNotFoundError(f"No module named {package!r}", name = package)),
+ _raise(ModuleNotFoundError(f"No module named {module!r}", name = module)),
)
result = _run_upload(seed_route, filename, b"%PDF-1.7")
@@ -140,6 +142,39 @@ def test_unstructured_upload_keeps_txt_path_working(monkeypatch, tmp_path):
assert any(name.endswith(".extracted.txt") for name in _block_files(seed_route))
+_PNG_1X1 = base64.b64decode(
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
+)
+
+
+def test_unstructured_docx_upload_extracts_plain_text(monkeypatch, tmp_path):
+ docx = pytest.importorskip("docx")
+ seed_route = _load_seed_route(monkeypatch, tmp_path, inline_extraction = False)
+ document = docx.Document()
+ document.add_heading("Refunds", 1)
+ document.add_paragraph("Within 30 days (see section 4.2) - email [email protected]!")
+ address = document.add_paragraph("Ship to:")
+ address.add_run().add_break()
+ address.add_run("221B Baker Street")
+ document.add_paragraph("Prices: $5.99 + tax. Use file_name.py")
+ saved = document.add_paragraph(r"Saved to C:\data\file_1.txt")
+ saved.add_run().add_picture(io.BytesIO(_PNG_1X1))
+ source = tmp_path / "policy.docx"
+ document.save(source)
+
+ result = _run_upload(seed_route, "policy.docx", source.read_bytes())
+
+ assert result.status == "ok"
+ extracted = seed_route.UNSTRUCTURED_UPLOAD_ROOT / "block" / f"{result.file_id}.extracted.txt"
+ assert extracted.read_text(encoding = "utf-8").strip() == (
+ "Refunds\n"
+ "Within 30 days (see section 4.2) - email [email protected]!\n"
+ "Ship to:\n221B Baker Street\n"
+ "Prices: $5.99 + tax. Use file_name.py\n"
+ r"Saved to C:\data\file_1.txt"
+ )
+
+
@pytest.mark.parametrize(
"exc",
[