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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Seed inspect endpoints for data recipe."""
from __future__ import annotations
import asyncio
from core.training.account_jobs import account_path, managed_account
import base64
import binascii
import functools
import json
import os
import re
import shutil
from itertools import islice
from pathlib import Path, PurePosixPath
from typing import Any
from uuid import uuid4
from anyio import CancelScope, CapacityLimiter, Lock as _AnyioLock, to_process, to_thread
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File as FastAPIFile, Form
from auth.authentication import allow_ambient_hf_token
from core.data_recipe.jsonable import to_preview_jsonable
from hub.services.datasets.local_options import (
_sharded_splits,
_IGNORED_DATA_FILENAMES,
_MAX_MODULE_INFERENCE_FILES,
_METADATA_FILENAMES,
_SEP,
_SPLIT_KEYWORDS,
)
from hub.utils.dataset_cache import refuse_unauthorized_dataset_preview
from hub.utils.hf_tokens import HfTokenArg, hf_token_arg
from loggers import get_logger
from utils.paths.lazy import LazyPath
from utils.paths import ensure_dir, seed_uploads_root, unstructured_uploads_root
from utils.utils import log_and_http_error
from utils.upload_limits import (
LOCAL_SEED_UPLOAD_MAX_BYTES,
LOCAL_SEED_UPLOAD_MAX_LABEL,
UNSTRUCTURED_RECIPE_UPLOAD_MAX_BYTES,
UNSTRUCTURED_RECIPE_UPLOAD_MAX_LABEL,
UNSTRUCTURED_RECIPE_UPLOAD_TOTAL_MAX_BYTES,
UNSTRUCTURED_RECIPE_UPLOAD_TOTAL_MAX_LABEL,
)
from models.data_recipe import (
SeedInspectRequest,
SeedInspectResponse,
SeedInspectUploadRequest,
UnstructuredFileUploadResponse,
)
from utils.paths.path_utils import is_appledouble_metadata
logger = get_logger(__name__)
router = APIRouter()
# Lazy: the plugin pulls heavy deps that delay port binding. False = probed, not installed.
_CHUNKING: Any = None
def _chunking() -> Any:
global _CHUNKING
if _CHUNKING is None:
try:
from data_designer_unstructured_seed import chunking
except ImportError:
_CHUNKING = False
else:
_CHUNKING = chunking
return _CHUNKING or None
DATA_EXTS = (".parquet", ".jsonl", ".json", ".csv")
DEFAULT_SPLIT = "train"
DEFAULT_CONFIG = "default"
LOCAL_UPLOAD_EXTS = {".csv", ".json", ".jsonl", ".parquet"}
# Each type needs a core.rag parser or MuPDF; a frontend parity test reads this literal.
UNSTRUCTURED_ALLOWED_EXTS = {
".pdf", ".docx", ".txt", ".md", ".markdown", ".html", ".htm", ".xhtml", ".xht",
".doc", ".docm", ".dotx", ".dotm", ".odt", ".ott", ".rtf",
".epub", ".mobi", ".fb2",
".xlsx", ".xlsm", ".xltx", ".xltm", ".xls", ".xlt", ".ods", ".ots",
".pptx", ".pptm", ".potx", ".potm", ".ppsx", ".ppsm", ".ppt", ".pps", ".odp", ".otp",
".eml", ".msg", ".mht", ".mhtml",
} # fmt: skip
# MuPDF is not thread-safe, so these run in the PDF worker pool.
_MUPDF_EBOOK_EXTS = {".mobi", ".fb2"}
SEED_UPLOAD_DIR = LazyPath(seed_uploads_root)
UNSTRUCTURED_UPLOAD_ROOT = LazyPath(unstructured_uploads_root)
_SAFE_ID_RE = re.compile(r"^[a-zA-Z0-9_-]+$")
# Legacy node ids (n1, ...) never match: their dirs can be shared across recipes.
_UPLOAD_UID_RE = re.compile(r"^[0-9a-f]{32}$")
def _validate_safe_id(value: str, label: str) -> str:
if not value or not _SAFE_ID_RE.match(value):
raise HTTPException(400, f"Invalid {label}: must be alphanumeric/dash/underscore only")
return value
def _serialize_preview_value(value: Any) -> Any:
return to_preview_jsonable(value)
def _serialize_preview_rows(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
return [
{str(key): _serialize_preview_value(value) for key, value in row.items()} for row in rows
]
def _normalize_optional_text(value: str | None) -> str | None:
if value is None:
return None
trimmed = value.strip()
return trimmed if trimmed else None
def _list_hf_repo_files(*, dataset_name: str, token: HfTokenArg) -> list[str]:
"""Lists every repo file, not just loadable ones, so globs are judged against the whole listing."""
try:
from huggingface_hub import HfApi
from huggingface_hub.utils import HfHubHTTPError
except ImportError:
return []
try:
api = HfApi(token = token)
return api.list_repo_files(dataset_name, repo_type = "dataset", token = token)
except (HfHubHTTPError, OSError, ValueError):
return []
def _list_hf_dataset_configs(*, dataset_name: str, token: HfTokenArg) -> list[dict[str, Any]]:
"""Read configs from the card's configs block; config names need not match folder names."""
try:
from huggingface_hub import HfApi
from huggingface_hub.utils import HfHubHTTPError
except ImportError:
return []
try:
api = HfApi(token = token)
card_data = api.dataset_info(dataset_name, token = token).card_data
configs = (card_data or {}).get("configs") if card_data is not None else None
except (HfHubHTTPError, OSError, ValueError, AttributeError):
return []
return [config for config in (configs or []) if isinstance(config, dict)]
def _as_pattern_list(paths: Any) -> list[str]:
if isinstance(paths, str):
return [paths]
if isinstance(paths, list):
return [path for path in paths if isinstance(path, str)]
return []
def _patterns_for_split(
data_files: Any,
split: str,
*,
exact: bool = True,
) -> list[str]:
"""A bare string or list means the train split; an exact name match is tried before folding."""
if isinstance(data_files, str):
return [data_files] if _same_split(DEFAULT_SPLIT, split, exact) else []
if isinstance(data_files, dict):
for name, paths in data_files.items():
if _same_split(str(name), split, exact):
return _as_pattern_list(paths)
return []
if isinstance(data_files, list):
# Scan every entry; a split may be declared more than once, and stopping early drops the rest.
patterns: list[str] = []
for entry in data_files:
if isinstance(entry, str):
if _same_split(DEFAULT_SPLIT, split, exact):
patterns.append(entry)
elif isinstance(entry, dict) and _same_split(
str(entry.get("split") or ""), split, exact
):
patterns.extend(_as_pattern_list(entry.get("path")))
return patterns
return []
def _same_split(declared: str, split: str, exact: bool) -> bool:
return declared == split if exact else declared.lower() == split.lower()
def _declared_split_patterns(
configs: list[dict[str, Any]],
split: str = DEFAULT_SPLIT,
subset: str | None = None,
) -> list[str]:
"""The globs the card declares for this split, under the named config."""
config = _pick_config(configs, subset)
if config is None:
return []
declared = config.get("data_files")
named = _patterns_for_split(declared, split) or _patterns_for_split(
declared, split, exact = False
)
folder = _config_folder(config)
# Mirrors `_resolve_data_files`, which joins data_dir even if the pattern repeats it.
return [_normalized_glob(f"{folder}/{p}" if folder else p) for p in named]
def _normalized_glob(pattern: str) -> str:
"""Drop empty and dot segments the way list_repo_files spells paths, or the glob matches nothing."""
parts: list[str] = []
for part in pattern.strip().split("/"):
if part in ("", "."):
continue
if part == ".." and parts and parts[-1] != "..":
parts.pop()
continue
parts.append(part)
return "/".join(parts)
def _config_folder(config: dict[str, Any]) -> str:
folder = config.get("data_dir")
return _normalized_glob(folder) if isinstance(folder, str) else ""
def _config_scope(configs: list[dict[str, Any]], subset: str | None) -> str:
"""The folder a config is confined to, for a card that names one and lists no files."""
config = _pick_config(configs, subset)
return _config_folder(config) if config else ""
def _pick_config(configs: list[dict[str, Any]], subset: str | None) -> dict[str, Any] | None:
"""With no subset, pick the config flagged default, else the only config, whatever its name."""
if subset:
# Exact name beats a case-folded match (card may carry Foo and foo); no config_name means default.
names = [(c, str(c.get("config_name") or DEFAULT_CONFIG)) for c in configs]
return next(
(c for c, name in names if name == subset),
next((c for c, name in names if name.lower() == subset.lower()), None),
)
flagged = next((c for c in configs if c.get("default") is True), None)
if flagged is not None:
return flagged
if len(configs) == 1:
return configs[0]
# Exactly `default`: Default and DEFAULT are configs to be asked for.
return next(
(c for c in configs if str(c.get("config_name") or DEFAULT_CONFIG) == DEFAULT_CONFIG),
None,
)
def _glob_to_regex(pattern: str) -> re.Pattern[str]:
"""* stops at a folder boundary, ** crosses it; prefix matching would claim train-notes.json."""
parts: list[str] = []
i = 0
while i < len(pattern):
char = pattern[i]
if char == "*" and pattern[i + 1 : i + 2] == "*":
if pattern[i + 2 : i + 3] == "/":
# Whole folders, or none: `data/**/train.*` is not `data/nottrain.x`.
parts.append("(?:[^/]+/)*")
i += 3
else:
parts.append(".*")
i += 2
continue
if char == "*":
parts.append("[^/]*")
elif char == "?":
parts.append("[^/]")
elif char == "[":
end = _class_end(pattern, i)
if end < 0:
parts.append(r"\[")
else:
body = pattern[i + 1 : end].replace("\\", "\\\\")
# Only `!` negates a glob class; a leading `^` is a character.
if body[:1] == "!":
body = f"^{body[1:]}"
elif body[:1] == "^":
body = f"\\^{body[1:]}"
parts.append(f"[{body}]")
i = end
else:
parts.append(re.escape(char))
i += 1
return re.compile("".join(parts) + r"\Z")
def _class_end(pattern: str, start: int) -> int:
"""Index of the `]` closing the class opened at `start`, or -1 if unclosed."""
i = start + 1
if pattern[i : i + 1] in ("!", "^"):
i += 1
if pattern[i : i + 1] == "]":
i += 1
closing = pattern.find("]", i)
return closing if closing > start else -1
def _posix_tail(path: str) -> list[str]:
"""PurePosixPath(path).parts without the root: split on "/", empty and "." parts dropped."""
return [part for part in path.split("/") if part and part != "."]
def _name_parts(path: str) -> tuple[str, str]:
"""Path(path).suffix and .stem; by hand only for plain relative ASCII names, which every pathlib flavour and version reads alike."""
if path.isascii() and not path.startswith("/") and "\\" not in path and ":" not in path:
name = path[path.rfind("/") + 1 :]
dot = name.rfind(".")
if name[:1] not in ("", ".") and dot < len(name) - 1:
return (name[dot:], name[:dot]) if dot > 0 else ("", name)
parsed = Path(path)
return parsed.suffix, parsed.stem
def _marked_parts(path: str, prefix: str, which: str) -> list[str]:
parts = _posix_tail(path)
if which != "parts":
parts = parts[:-1]
return [part for part in parts if part.startswith(prefix) and set(part) != {"."}]
def _files_under_patterns(
patterns: list[str],
data_files: list[str],
*,
as_the_loader_would: bool = False,
) -> list[str]:
"""Card globs obey loader skip rules; module-written patterns are judged on what they plainly match."""
matchers = [(pattern, _glob_to_regex(pattern)) for pattern in patterns]
if not as_the_loader_would:
return [f for f in data_files if any(m.match(f) for _, m in matchers)]
rules = [(m, _loader_path_facts(p)) for p, m in matchers]
kept = []
for f in data_files:
facts = None
for m, pattern_facts in rules:
if not m.match(f):
continue
if facts is None:
facts = _loader_path_facts(f)
if not _skipped_by_the_loader(facts, pattern_facts):
kept.append(f)
break
return kept
def _loader_path_facts(path: str) -> tuple[int, int, str]:
"""Hidden parts anywhere, dunder parts in parents, and the name."""
return (
len(_marked_parts(path, ".", "parts")),
len(_marked_parts(path, "__", "parent_parts")),
(_posix_tail(path) or [""])[-1],
)
def _skipped_by_the_loader(
path_facts: tuple[int, int, str], pattern_facts: tuple[int, int, str]
) -> bool:
"""Loader skips hidden parts anywhere and dunder parts only in parents unless the pattern names them
(data/__train.json is read), and drops names in data_files.FILES_TO_IGNORE from globs unless the pattern is that
file itself."""
if path_facts[:2] != pattern_facts[:2]:
return True
name = path_facts[2]
return name in _IGNORED_DATA_FILENAMES and pattern_facts[2] != name
def _common_parent(paths: list[str]) -> str:
parts = [Path(path).parent.as_posix().split("/") for path in paths]
shared: list[str] = []
for segments in zip(*parts):
if len(set(segments)) != 1 or segments[0] == ".":
break
shared.append(segments[0])
return "/".join(shared)
def _label_in_name(name: str, label: str) -> bool:
"""Match the label in place, not by tokens, so sample-10BT survives; digits count as separators: train1 is train, training is not."""
return _label_matcher(label).search(name) is not None
@functools.lru_cache(maxsize = 256)
def _label_matcher(label: str) -> re.Pattern[str]:
return re.compile(rf"(?:^|{_SEP}){re.escape(label)}(?:$|{_SEP})")
def _split_folder(lowered_path: str, labels: tuple[str, ...]) -> bool:
"""Qualified folders like train_a or en-train count as the split, as the loader reads them."""
return any(
_label_in_name(part, label) for part in lowered_path.split("/")[:-1] for label in labels
)
def _split_labels(split_lower: str) -> tuple[str, ...]:
"""The names `datasets` accepts for this split: dev is validation, training is train."""
for canonical, aliases in _SPLIT_KEYWORDS.items():
if split_lower == canonical or split_lower in aliases:
return aliases
return (split_lower,)
def _carries_another_split(path: str, split_lower: str) -> bool:
"""The path is labelled with a split, and it is not the one being asked for."""
wanted = set(_split_labels(split_lower))
return any(
canonical not in wanted and _split_rank(path, canonical) <= 1
for canonical in _SPLIT_KEYWORDS
)
def _split_rank(path: str, split_lower: str) -> int:
"""Rank 0 if a folder carries the split, 1 if the file name does, else 2; labels match as whole
words."""
lowered = path.lower()
labels = _split_labels(split_lower)
if _split_folder(lowered, labels):
return 0
# Only the final extension comes off: questions.train.parquet keeps its split.
stem = _name_parts(lowered)[1]
if any(_label_in_name(stem, label) for label in labels):
return 1
return 2
def _in_subset(data_files: list[str], subset: str | None, split_lower: str) -> list[str]:
"""A folder carrying the config label is the config; a name-only match outside the split is dropped."""
if not subset:
return data_files
subset_lower = subset.lower()
# Exact folder match first: a repo may hold Foo/ beside foo/, which folding would merge into one.
in_folder = [f for f in data_files if subset in f.split("/")[:-1]] or [
f for f in data_files if subset_lower in f.lower().split("/")[:-1]
]
if in_folder:
return in_folder
named = [f for f in data_files if _label_in_name(_name_parts(f.lower())[1], subset_lower)]
if any(_split_rank(f, split_lower) <= 1 for f in named):
return named
if any(_split_rank(f, split_lower) <= 1 for f in data_files):
return data_files
return named or data_files
def _select_best_file(
data_files: list[str],
split: str = DEFAULT_SPLIT,
subset: str | None = None,
) -> str | None:
if not data_files:
return None
split_lower = split.lower()
return sorted(
_in_subset(data_files, subset, split_lower),
key = lambda p: (_split_rank(p, split_lower), len(p)),
)[0]
def _pattern_fits_the_split(
data_files: list[str], wanted: set[str], root: str, pattern: str
) -> bool:
"""A glob must take every wanted file under the root and no other, judged on the whole listing."""
matcher = _glob_to_regex(pattern)
prefix = f"{root}/" if root and root != "." else ""
for path in data_files:
if not path.startswith(prefix):
continue
if bool(matcher.match(path[len(prefix) :])) != (path in wanted):
return False
return True
def _candidate_patterns(stem: str, suffix: str, split_lower: str) -> list[str]:
"""Globs for the split in one folder, narrowest first, built from the stems this file actually uses."""
stem_lower = stem.lower()
candidates: list[str] = []
for label in _split_labels(split_lower):
if stem_lower.startswith((f"{label}-", f"{label}_", f"{label}.")):
candidates.append(f"{stem[: len(label) + 1]}*{suffix}")
if stem_lower == label or stem_lower.endswith((f"_{label}", f"-{label}")):
# Keep the star: train.jsonl may be sharded beside train_2.jsonl.
candidates.append(f"{stem}*{suffix}")
at = stem_lower.find(label)
if at >= 0:
candidates.append(f"*{stem[at : at + len(label)]}*{suffix}")
# Last and narrowest: the whole name, which keeps train-main off train-socratic.
candidates.append(f"{stem}*{suffix}")
return list(dict.fromkeys(candidates))
def _with_data_extension(pattern: str, suffix: str) -> str:
"""A card glob ends where the file name does; the reader picks by extension."""
current = Path(pattern).suffix
if current.lower() in DATA_EXTS:
return pattern
if current and any(char in current for char in "*?["):
pattern = pattern[: -len(current)]
# fsspec refuses data/**.parquet: `**` has to be a whole component.
if pattern.endswith("**"):
pattern = f"{pattern}/*"
return f"{pattern}{suffix}"
def _ext_rank(suffix: str) -> int:
"""Where `datasets` puts this extension in a draw: parquet, jsonl, json, csv."""
return -DATA_EXTS.index(suffix) if suffix in DATA_EXTS else -99
def _dominant_suffix(paths: list[str]) -> str:
"""The format the loader builds a split from: most files, with parquet winning ties."""
counts: dict[str, int] = {}
# Slice the window as the loader does, then drop metadata within it.
window = sorted(paths)[:_MAX_MODULE_INFERENCE_FILES]
voting = [p for p in window if Path(p).name.lower() not in _METADATA_FILENAMES] or window
for path in voting:
# Each suffix in the name votes once, as load.infer_module_for_data_files_list counts: a.csv.parquet votes csv too; .json does not vote for .jsonl.
for part in Path(path).name.lower().split(".")[1:]:
counts[f".{part}"] = counts.get(f".{part}", 0) + 1
if not counts:
return ""
# Widen only if the builder extension wins alone; skip a winner no file ends in (matches nothing).
for winner in sorted(counts, key = lambda s: (counts[s], _ext_rank(s)), reverse = True):
group = _builder_exts(winner)
named = next((Path(p).suffix for p in paths if Path(p).suffix.lower() in group), "")
if named:
return named
return ""
# Extensions one builder reads: `datasets` loads .json and .jsonl with the same one.
_BUILDER_EXTS = ((".json", ".jsonl"),)
def _builder_exts(suffix: str) -> tuple[str, ...]:
lowered = suffix.lower()
for group in _BUILDER_EXTS:
if lowered in group:
return group
return (lowered,)
def _of_suffix(paths: list[str], suffix: str) -> list[str]:
group = _builder_exts(suffix)
return [p for p in paths if _name_parts(p)[0].lower() in group]
def _extension_glob(paths: list[str], suffix: str, repo_files: list[str]) -> str:
"""Shared .json/.jsonl glob when a split uses both, unless the repo has .json.gz; else one extension."""
group = set(_builder_exts(suffix))
used = {Path(path).suffix.lower() for path in paths} & group
if len(used) < 2:
return suffix
stem = os.path.commonprefix(sorted(used))
matcher = re.compile(rf"{re.escape(stem)}[^/]*\Z", re.IGNORECASE)
# Checks only what the pattern reaches: it is anchored at the declared files' shared folder.
scope = _common_parent(paths)
prefix = f"{scope}/" if scope else ""
if any(
path.startswith(prefix)
and Path(path).suffix.lower() not in group
and matcher.search(Path(path).name)
for path in repo_files
):
return suffix
return f"{stem}*"
def _common_name_prefix(paths: list[str]) -> str:
"""What the file names share before the extension, which the glob supplies."""
return os.path.commonprefix([Path(path).stem for path in paths]) if paths else ""
def _name_initials(paths: list[str]) -> str:
"""Only letters and digits go into the glob class, so nothing there can read as glob syntax."""
initials = sorted({Path(path).name[:1] for path in paths if Path(path).name[:1].isalnum()})
if not initials or len(initials) != len({Path(path).name[:1] for path in paths}):
return ""
return "".join(initials)
def _staged_split_files(
data_files: list[str],
labels: tuple[str, ...],
scope: str = "",
) -> set[str]:
"""Sharded names come first: once one is there, a train-named file elsewhere is not in the split; strip the scope first, since sharded names start at data/."""
prefix = f"{scope}/" if scope else ""
inside = [path[len(prefix) :] for path in data_files if path.startswith(prefix)]
grouped = _sharded_splits([PurePosixPath(path) for path in inside])
if not grouped:
return set()
staged = {
f"{prefix}{path.as_posix()}"
for split, paths in grouped.items()
if split.lower() in labels
for path in paths
}
return staged if staged < set(data_files) else set()
def _anchor(dataset_name: str, root: str) -> str:
"""The recipe path up to the folder a pattern is written relative to."""
base = f"datasets/{dataset_name}"
return f"{base}/{root}" if root and root != "." else base
def _name_finals(paths: list[str]) -> str:
"""The last characters of these names before the extension, as a class body."""
finals = {Path(path).stem[-1:] for path in paths}
return "".join(sorted(finals)) if finals and all(c.isalnum() for c in finals) else ""
def _widened_declared_pattern(
declared_files: list[str],
data_files: list[str],
split_lower: str,
suffix: str,
*,
fallback_glob: bool = False,
) -> str:
"""One widened glob over declared files, used only when it takes exactly those files and no others."""
parent = _common_parent(declared_files)
base = f"{parent}/**" if parent else "**"
prefix = f"{parent}/" if parent else ""
wanted = set(declared_files)
# Split label may be in file names or folders; a validation set may be dev-*.parquet.
candidates = [f"{base}/*{label}*{suffix}" for label in _split_labels(split_lower)]
candidates += [f"{prefix}**/*{label}*/**/*{suffix}" for label in _split_labels(split_lower)]
# Unlabelled declared files may share a name that neighbouring splits lack, e.g. data/part-a.
shared_name = _common_name_prefix(declared_files)
if shared_name:
candidates.append(f"{prefix}{shared_name}*{suffix}")
candidates.append(f"{prefix}**/{shared_name}*{suffix}")
# A glob class is the only union the reader understands; it rejects `{a,b}`.
initials = _name_initials(declared_files)
finals = _name_finals(declared_files)
if initials:
candidates.append(f"{prefix}**/[{initials}]*{suffix}")
# Names may share a first letter yet end differently; a class at each end separates them.
if finals:
candidates.append(f"{prefix}**/*[{finals}]{suffix}")
if initials and finals:
candidates.append(f"{prefix}**/[{initials}]*[{finals}]{suffix}")
# Files no glob can gather fall back to their own names, which read only what the card declared.
candidates += sorted(wanted)
for candidate in candidates:
if set(_files_under_patterns([candidate], data_files)) == wanted:
return candidate
# No glob selects exactly these files; a card refuses that, while a guess falls back to the folder.
return f"{base}/*{suffix}" if fallback_glob else ""
def _resolve_seed_hf_path(
dataset_name: str,
data_files: list[str],
split: str = DEFAULT_SPLIT,
subset: str | None = None,
configs: list[dict[str, Any]] | None = None,
repo_files: list[str] | None = None,
) -> str | None:
declared = _declared_split_patterns(configs or [], split, subset)
declared_files = _files_under_patterns(declared, data_files, as_the_loader_would = True)
# The card's own mapping beats any folder-name guess, as long as it resolves to files that exist.
if declared_files:
suffix = _dominant_suffix(declared_files)
declared_files = _of_suffix(declared_files, suffix)
exact = suffix
suffix = _extension_glob(declared_files, suffix, repo_files or data_files)
if suffix == exact and len({Path(f).suffix.lower() for f in declared_files}) > 1:
# A card putting both builder extensions in one split is refused, since no glob takes just the two.
return None
pattern = ""
if len(declared) == 1:
pattern = _with_data_extension(declared[0], suffix)
# The reader skips no hidden folders, so the pattern must match exactly the declared files.
if not pattern or set(declared_files) != set(_files_under_patterns([pattern], data_files)):
pattern = _widened_declared_pattern(declared_files, data_files, split.lower(), suffix)
# Unrepresentable mappings are refused (422), never approximated.
return f"datasets/{dataset_name}/{pattern}" if pattern else None
# No card means the subset is just a label; narrow first so the pattern cannot span other configs.
folder = _config_scope(configs or [], subset)
scoped = _in_subset(
[f for f in data_files if f.startswith(f"{folder}/")] or data_files
if folder
else data_files,
subset,
split.lower(),
)
split_lower = split.lower()
labels = _split_labels(split_lower)
# Sharded names come first and stop the search, whatever else is labelled.
staged = _staged_split_files(scoped, labels, folder)
selected = _select_best_file(sorted(staged) or scoped, split)
if not selected:
return None
suffix = Path(selected).suffix
ext = suffix.lower()
if ext not in DATA_EXTS:
return f"datasets/{dataset_name}/{selected}"
parent = Path(selected).parent.as_posix()
base = f"datasets/{dataset_name}"
if parent and parent != ".":
base = f"{base}/{parent}"
if _split_folder(selected.lower(), labels):
# train_a and train_b are one split, so rooting at either drops half.
folders = staged or {f for f in scoped if _split_rank(f, split_lower) == 0}
root = _common_parent(sorted(folders | {selected}))
if root != parent:
for label in labels:
# The four shapes `local_options._DIR_NAME_KEYWORD_PATTERNS` accepts.
for shape in (
f"**/{label}/**/*{ext}",
f"**/{label}{_SEP}*/**/*{ext}",
f"**/*{_SEP}{label}/**/*{ext}",
f"**/*{_SEP}{label}{_SEP}*/**/*{ext}",
# train/ beside sets/train_a/ needs a looser pattern, taken only where the listing confirms it.
f"**/{label}*/**/*{ext}",
f"**/*{label}*/**/*{ext}",
):
if _pattern_fits_the_split(data_files, folders, root, shape):
return f"{_anchor(dataset_name, root)}/{shape}"
else:
# The files this request is for: in the chosen subset, and of this split.
wanted = staged or {f for f in scoped if _split_rank(f, split_lower) <= 1}
# Sibling-folder shards anchor at their shared parent, since the loader reads them as one split.
spread = any(Path(f).parent.as_posix() != parent for f in wanted)
root = _common_parent(sorted(wanted | {selected})) if spread else parent
stem = Path(selected).name[: -len(suffix)]
for candidate in _candidate_patterns(stem, suffix, split_lower):
pattern = f"**/{candidate}" if spread else candidate
if _pattern_fits_the_split(data_files, wanted, root, pattern):
return f"{_anchor(dataset_name, root)}/{pattern}"
# Widen only if every neighbour is labelled, since an unlabelled file may belong to this split.
prefix = f"{root}/" if root and root != "." else ""
neighbours = [f for f in data_files if f.startswith(prefix) and f not in wanted]
if len(wanted) > 1 and all(_carries_another_split(f, split_lower) for f in neighbours):
widened = _widened_declared_pattern(
sorted(wanted), data_files, split_lower, suffix, fallback_glob = True
)
return f"datasets/{dataset_name}/{widened}"
return f"{base}/**/*{ext}"
def _build_stream_load_kwargs(
*,
dataset_name: str,
split: str,
subset: str | None,
token: HfTokenArg,
data_file: str | None = None,
) -> dict[str, Any]:
kwargs: dict[str, Any] = {
"path": dataset_name,
"split": split,
"streaming": True,
"trust_remote_code": False,
"token": token,
}
if data_file:
kwargs["data_files"] = [data_file]
if subset:
kwargs["name"] = subset
return kwargs
def _load_preview_rows(
*, load_dataset_fn, load_kwargs: dict[str, Any], preview_size: int
) -> list[dict[str, Any]]:
streamed_ds = load_dataset_fn(**load_kwargs)
features = getattr(streamed_ds, "features", None) if managed_account() else None
undecoded = _images_undecoded(features) if features else None
if undecoded is None or undecoded == features:
return [row for row in islice(streamed_ds, preview_size)]
# Image cells, nested ones too, stay dicts so a local path is checked against the account before it is opened; Hub images decode here.
tokens = getattr(streamed_ds, "_token_per_repo_id", None)
return [
_decode_hub_images(undecoded, row, tokens)
for row in islice(streamed_ds.cast(undecoded), preview_size)
]
def _images_undecoded(feature):
import dataclasses
from datasets import Image
if isinstance(feature, Image):
return dataclasses.replace(feature, decode = False)
if isinstance(feature, dict):
return type(feature)({key: _images_undecoded(value) for key, value in feature.items()})
if isinstance(feature, list):
return [_images_undecoded(value) for value in feature]
if dataclasses.is_dataclass(feature) and getattr(feature, "feature", None) is not None:
return dataclasses.replace(feature, feature = _images_undecoded(feature.feature))
return feature
def _is_hub_path(path) -> bool:
from datasets import config
if not isinstance(path, str):
return False
parts = path.split("::")
source = parts[-1]
return (source.startswith("hf://") or source.startswith(f"{config.HF_ENDPOINT}/")) and not any(
part.startswith("file:") for part in parts
)
def _decode_hub_images(feature, value, tokens):
from datasets import Image
if isinstance(feature, Image):
if (
isinstance(value, dict)
and value.get("bytes") is None
and _is_hub_path(value.get("path"))
):
try:
return Image(mode = feature.mode).decode_example(value, token_per_repo_id = tokens)
except Exception:
return value
return value
if isinstance(feature, dict) and isinstance(value, dict):
return {
key: _decode_hub_images(feature[key], item, tokens) if key in feature else item
for key, item in value.items()
}
inner = (
feature[0] if isinstance(feature, list) and feature else getattr(feature, "feature", None)
)
if inner is not None and isinstance(value, list):
return [_decode_hub_images(inner, item, tokens) for item in value]
return value
def _extract_columns(rows: list[dict[str, Any]]) -> list[str]:
columns_seen: dict[str, None] = {}
for row in rows:
for key in row.keys():
columns_seen[str(key)] = None
return list(columns_seen.keys())
def _sanitize_filename(filename: str) -> str:
name = Path(filename).name.strip().replace("\x00", "")
if not name:
return "seed_upload"
return name
def _decode_base64_payload(content_base64: str) -> bytes:
raw = content_base64.strip()
if "," in raw and raw.lower().startswith("data:"):
raw = raw.split(",", 1)[1]
try:
return base64.b64decode(raw, validate = True)
except binascii.Error as exc:
raise HTTPException(status_code = 400, detail = "invalid base64 payload") from exc
# Match the recipe's DuckDB read: inference made "007" -> 7 and a created_at number a date.
_JSON_READ_OPTIONS = {"dtype": False, "convert_dates": False}
def _read_preview_rows_from_local_file(path: Path, preview_size: int) -> list[dict[str, Any]]:
account_path(path)
try:
import pandas as pd
except ImportError as exc:
raise log_and_http_error(
exc,
500,
"seed inspect dependencies unavailable",
event = "data_recipe.seed.dependencies_unavailable",
log = logger,
) from exc
ext = path.suffix.lower()
try:
if ext == ".csv":
# Keep text as written: inference made 007 -> 7, 3 -> 3.0, NA/None -> missing.
csv_options = {
"encoding": "utf-8-sig",
"dtype": str,
"keep_default_na": False,
"na_values": [""],
}
df = pd.read_csv(path, nrows = preview_size, **csv_options)
df.columns = df.columns.str.strip()
unnamed = [c for c in df.columns if c == "" or c.startswith("Unnamed:")]
if unnamed:
df = df.drop(columns = unnamed)
full_df = pd.read_csv(path, **csv_options)
full_df.columns = full_df.columns.str.strip()
full_df = full_df.drop(columns = unnamed)
tmp_csv = path.with_suffix(".tmp.csv")
full_df.to_csv(tmp_csv, index = False, encoding = "utf-8")
tmp_csv.replace(path)
elif ext == ".jsonl":
df = pd.read_json(path, lines = True, **_JSON_READ_OPTIONS).head(preview_size)
elif ext == ".json":
try:
df = pd.read_json(path, **_JSON_READ_OPTIONS).head(preview_size)
except ValueError:
df = pd.read_json(path, lines = True, **_JSON_READ_OPTIONS).head(preview_size)
elif ext == ".parquet":
import pyarrow.parquet as pq
# Decode only the preview rows: a 100MB compressed upload can inflate to GBs.
parquet_file = pq.ParquetFile(path)
batch = next(parquet_file.iter_batches(batch_size = preview_size), None)
df = (
batch if batch is not None else parquet_file.schema_arrow.empty_table()
).to_pandas()
else:
raise HTTPException(status_code = 422, detail = f"unsupported file type: {ext}")
except HTTPException:
raise
except (ValueError, OSError) as exc:
raise log_and_http_error(
exc,
422,
"seed inspect failed",
event = "data_recipe.seed.local_preview_failed",
log = logger,
) from exc
rows = df.to_dict(orient = "records")
return _serialize_preview_rows(rows)
def _read_preview_rows_from_unstructured_file(
*, path: Path, preview_size: int, chunk_size: int | None, chunk_overlap: int | None
) -> list[dict[str, Any]]:
account_path(path)
chunking = _chunking()
if chunking is None:
raise HTTPException(
500,
"Unstructured seed support not available (missing data_designer_unstructured_seed)",
)
size, overlap = chunking.resolve_chunking(chunk_size, chunk_overlap)
try:
rows = chunking.build_unstructured_preview_rows(
source_path = path,
preview_size = preview_size,
chunk_size = size,
chunk_overlap = overlap,
)
except (FileNotFoundError, RuntimeError, ValueError, OSError) as exc:
raise log_and_http_error(
exc,
422,
"seed inspect failed",
event = "data_recipe.seed.unstructured_preview_failed",
log = logger,
) from exc
return _serialize_preview_rows(rows)
def _read_preview_rows_from_multi_files(
*,
block_id: str,
file_ids: list[str],
file_names: list[str],
preview_size: int,
chunk_size: int | None,
chunk_overlap: int | None,
) -> tuple[list[dict[str, str]], int]:
chunking = _chunking()
if chunking is None:
raise HTTPException(
500,
"Unstructured seed support not available (missing data_designer_unstructured_seed)",
)
return chunking.build_multi_file_preview(
file_entries = _multi_file_entries(block_id, file_ids, file_names),
preview_size = preview_size,
chunk_size = chunk_size,
chunk_overlap = chunk_overlap,
)
def _multi_file_entries(
block_id: str, file_ids: list[str], file_names: list[str]
) -> list[tuple[Path, str]]:
_validate_safe_id(block_id, "block_id")
block_dir = UNSTRUCTURED_UPLOAD_ROOT / block_id
file_entries: list[tuple[Path, str]] = []
for fid, fname in zip(file_ids, file_names):
extracted = block_dir / f"{fid}.extracted.txt"
if not extracted.exists():
raise HTTPException(404, f"Extracted text not found for file: {fname} (id: {fid})")
file_entries.append((extracted, fname))
return file_entries
def _require_own_seed_source(dataset_name: str) -> None:
"""load_dataset also reads a local folder, its dataset card's data_files and any links in it, and a
check before the read cannot pin what the read opens. A managed account previews Hub ids only, as
the UI does; its own files go through /seed/inspect-upload."""
if not managed_account():
return
parts = dataset_name.split("/")
hub_id = len(parts) == 2 and all(
part not in {"", ".", ".."} and all(c.isalnum() or c in "-_." for c in part)
for part in parts
)
if hub_id:
# Only a well-formed id reaches the filesystem: a UNC name would start SMB auth on Windows.
try:
hub_id = not Path(dataset_name).expanduser().exists()
except (ValueError, RuntimeError, OSError):
hub_id = False
if not hub_id:
raise HTTPException(
status_code = 403,
detail = "dataset_name must be a Hugging Face repo id like org/repo",
)
@router.post("/seed/inspect", response_model = SeedInspectResponse)
def inspect_seed_dataset(
payload: SeedInspectRequest, allow_ambient_token: bool = Depends(allow_ambient_hf_token)
) -> SeedInspectResponse:
dataset_name = payload.dataset_name.strip()
if not dataset_name or dataset_name.count("/") < 1:
raise HTTPException(
status_code = 400,
detail = "dataset_name must be a Hugging Face repo id like org/repo",
)
_require_own_seed_source(dataset_name)
split = _normalize_optional_text(payload.split) or DEFAULT_SPLIT
subset = _normalize_optional_text(payload.subset)
# From the caller: a hardcoded False takes the ambient fallback from UI sessions too.
token = hf_token_arg(
_normalize_optional_text(payload.hf_token),
allow_ambient_token = allow_ambient_token and not managed_account(),
)
preview_size = int(payload.preview_size)
refuse_unauthorized_dataset_preview(token, dataset_name)
try:
from datasets import load_dataset
except ImportError as exc:
raise log_and_http_error(
exc,
500,
"seed inspect dependencies unavailable",
event = "data_recipe.seed.dependencies_unavailable",
log = logger,
) from exc
preview_rows: list[dict[str, Any]] = []
repo_files = _list_hf_repo_files(dataset_name = dataset_name, token = token)
data_files = [file for file in repo_files if file.lower().endswith(DATA_EXTS)]
configs = _list_hf_dataset_configs(dataset_name = dataset_name, token = token)
# Preview the same files the recipe will read, not from a config the resolved path excludes.
declared_files = _files_under_patterns(
_declared_split_patterns(configs, split, subset),
data_files,
as_the_loader_would = True,
)
# Same format the resolved path reads, or the preview rows come from a file the recipe never opens.
declared_files = _of_suffix(declared_files, _dominant_suffix(declared_files))
# A folder-only config declares no files; its name may differ, so the fallback previews that folder.
folder = _config_scope(configs, subset)
in_folder = [f for f in data_files if f.startswith(f"{folder}/")] if folder else []
selected_file = _select_best_file(declared_files or in_folder or data_files, split, subset)
if selected_file:
try:
single_file_kwargs = _build_stream_load_kwargs(
dataset_name = dataset_name,
split = split,
subset = subset,
token = token,
data_file = selected_file,
)
preview_rows = _load_preview_rows(
load_dataset_fn = load_dataset,
load_kwargs = single_file_kwargs,
preview_size = preview_size,
)
except (ValueError, OSError, RuntimeError):
preview_rows = []
if not preview_rows:
try:
split_kwargs = _build_stream_load_kwargs(
dataset_name = dataset_name,
split = split,
subset = subset,
token = token,
)
preview_rows = _load_preview_rows(
load_dataset_fn = load_dataset,
load_kwargs = split_kwargs,
preview_size = preview_size,
)
except (ValueError, OSError, RuntimeError) as exc:
raise log_and_http_error(
exc,
422,
"seed inspect failed",
event = "data_recipe.seed.hf_preview_failed",
log = logger,
) from exc
if not preview_rows:
raise HTTPException(status_code = 422, detail = "dataset appears empty or unreadable")
preview_rows = _serialize_preview_rows(preview_rows)
columns = _extract_columns(preview_rows)
if not data_files:
resolved_path = f"datasets/{dataset_name}/**/*.parquet"
else:
resolved_path = _resolve_seed_hf_path(
dataset_name, data_files, split, subset, configs, repo_files
)
if not resolved_path:
raise HTTPException(status_code = 422, detail = "unable to resolve seed dataset path")
return SeedInspectResponse(
dataset_name = dataset_name,
resolved_path = resolved_path,
columns = columns,
preview_rows = preview_rows,
split = split,
subset = subset,
)
def _extract_text_from_file(file_path: Path, ext: str) -> str:
"""Extract text from an uploaded file by extension, to markdown where possible."""
if ext in {".txt", ".md"}:
from core.rag import parsers
raw = "\n\n".join(page.text for page in parsers.parse(str(file_path)))
elif ext == ".pdf":
from core.rag import config, pdf_ocr
raw = pdf_ocr.extract_text(str(file_path), config.OCR_SCANNED, config.OCR_MAX_PAGES)
elif ext in _MUPDF_EBOOK_EXTS:
from core.rag import parsers
raw = parsers.mupdf_document_text(str(file_path))
elif ext in UNSTRUCTURED_ALLOWED_EXTS:
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}")
chunking = _chunking()
if chunking is None:
return raw
return chunking.normalize_unstructured_text(raw)
_pdf_extraction_limiter = CapacityLimiter(2)
_document_extraction_limiter = CapacityLimiter(2)
async def _extract_text_from_file_async(file_path: Path, ext: str) -> str:
if ext in _MUPDF_EBOOK_EXTS:
from core.rag import parsers
raw = await to_process.run_sync(
parsers.mupdf_document_text,
str(file_path),
cancellable = True,
limiter = _pdf_extraction_limiter,
)
chunking = _chunking()
return chunking.normalize_unstructured_text(raw) if chunking is not None else raw
if ext != ".pdf":
# A large spreadsheet must not stall the event loop, nor several fill the shared thread pool.
return await to_thread.run_sync(
_extract_text_from_file, file_path, ext, limiter = _document_extraction_limiter
)
from core.rag import config, pdf_ocr
# MuPDF is not thread-safe; each worker owns its document and OCR state.
raw = await to_process.run_sync(
pdf_ocr.extract_text,
str(file_path),
config.OCR_SCANNED,
config.OCR_MAX_PAGES,
cancellable = True,
limiter = _pdf_extraction_limiter,
)
chunking = _chunking()
return chunking.normalize_unstructured_text(raw) if chunking is not None else raw
# One upload per block between its budget check and its raw write: the scan, read and write await threads.
_BLOCK_UPLOAD_LOCKS: dict[str, _AnyioLock] = {}
async def _write_to_completion(write_fn, *args) -> None:
"""A threaded write that, if cancelled, still waits for the thread so cleanup cannot race it."""
write = asyncio.ensure_future(to_thread.run_sync(write_fn, *args))
try:
await asyncio.shield(write)
except BaseException:
# Neither a repeated task cancel nor an AnyIO cancel scope may run cleanup before the thread ends.
with CancelScope(shield = True):
while not write.done():
try:
await asyncio.wait([write])
except asyncio.CancelledError:
continue
raise
def _get_block_total_size(block_dir: Path) -> int:
"""Sum raw upload sizes for the whole block from server-owned files."""
if not block_dir.exists():
return 0
total = 0
for f in block_dir.iterdir():
if not f.is_file():
continue
if f.name.endswith(".extracted.txt") or f.name.endswith(".meta.json"):
continue
# remove_unstructured_file keys on the name up to the first dot.
if is_appledouble_metadata(f):
continue
# Runs in a worker thread now, so a delete on the loop can remove a file mid-scan.
try:
total += f.stat().st_size
except FileNotFoundError:
continue
return total
def _require_within_budget(size_bytes: int, budget: int) -> None:
"""413 on the tighter of the per-file cap and what the block has left."""
if size_bytes > UNSTRUCTURED_RECIPE_UPLOAD_MAX_BYTES:
raise HTTPException(
413,
f"File too large ({size_bytes} bytes). Maximum is {UNSTRUCTURED_RECIPE_UPLOAD_MAX_LABEL}.",
)
if size_bytes > budget:
raise HTTPException(
413,
f"Total upload limit ({UNSTRUCTURED_RECIPE_UPLOAD_TOTAL_MAX_LABEL}) exceeded",
)
def _require_unstructured_ext(filename: str) -> str:
"""Reject an unsupported type before any bytes are read."""
ext = Path(filename).suffix.lower()
if ext not in UNSTRUCTURED_ALLOWED_EXTS:
raise HTTPException(
400,
f"Unsupported file type: {ext}. Allowed: {', '.join(sorted(UNSTRUCTURED_ALLOWED_EXTS))}",
)
return ext
def _read_native_drop(lease: str, budget: int) -> tuple[str, bytes]:
"""Read a desktop drop only via its signed lease, re-verified and size-checked; read stops one byte past the budget in case the file grew."""
from utils.native_path_leases import NativePathLeaseError, verify_native_path_lease
try:
grant = verify_native_path_lease(
lease,
operation = "attach",
expected_kind = "attachment",
expected_path_type = "file",
allowed_suffixes = sorted(UNSTRUCTURED_ALLOWED_EXTS),
)
except NativePathLeaseError as exc:
raise HTTPException(400, str(exc)) from exc
account_path(grant.canonical_path)
_require_unstructured_ext(grant.canonical_path.name)
try:
size_bytes = grant.canonical_path.stat().st_size
except OSError as exc:
raise HTTPException(400, "Dropped file could not be read.") from exc
_require_within_budget(size_bytes, budget)
try:
with grant.canonical_path.open("rb") as source:
content = source.read(budget + 1)
except OSError as exc:
raise HTTPException(400, "Dropped file could not be read.") from exc
_require_within_budget(len(content), budget)
return grant.canonical_path.name, content
async def _store_unstructured_raw(file, block_dir: Path, native_path_lease):
# Reads 0 for a block with no dir yet, without creating it, so an upload about to be refused leaves no directory behind.
budget = UNSTRUCTURED_RECIPE_UPLOAD_TOTAL_MAX_BYTES - await to_thread.run_sync(
_get_block_total_size, block_dir
)
# Tauri drops arrive as a signed path; isinstance since an unfilled Form param is truthy.
lease = native_path_lease if isinstance(native_path_lease, str) else None
if lease:
original_filename, content = await to_thread.run_sync(_read_native_drop, lease, budget)
elif file is not None and hasattr(file, "read"):
original_filename = file.filename or "upload"
# Before the read: a rejected 500 MB upload must not be pulled into memory first.
_require_unstructured_ext(original_filename)
limit = max(0, min(UNSTRUCTURED_RECIPE_UPLOAD_MAX_BYTES, budget))
content = await file.read(limit + 1)
if len(content) > limit:
# Only limit + 1 bytes were read: report the real size the full read used to.
declared = getattr(file, "size", None)
_require_within_budget(
declared if isinstance(declared, int) and declared > limit else len(content),
budget,
)
else:
raise HTTPException(400, "No file was provided.")
ext = Path(original_filename).suffix.lower()
size_bytes = len(content)
if size_bytes == 0:
raise HTTPException(400, "Empty file not allowed")
_require_within_budget(size_bytes, budget)
ensure_dir(block_dir)
file_id = uuid4().hex
raw_path = block_dir / f"{file_id}{ext}"
try:
await _write_to_completion(raw_path.write_bytes, content)
except BaseException:
raw_path.unlink(missing_ok = True)
raise
return original_filename, content, raw_path, file_id
@router.post("/seed/upload-unstructured-file")
async def upload_unstructured_file(
file: UploadFile | None = FastAPIFile(None),
block_id: str = Form(...),
native_path_lease: str | None = Form(None, alias = "nativePathLease"),
) -> UnstructuredFileUploadResponse:
_validate_safe_id(block_id, "block_id")
block_dir = UNSTRUCTURED_UPLOAD_ROOT / block_id
lock = _BLOCK_UPLOAD_LOCKS.setdefault(block_id, _AnyioLock())
try:
async with lock:
original_filename, content, raw_path, file_id = await _store_unstructured_raw(
file, block_dir, native_path_lease
)
finally:
if not lock.locked() and not lock.statistics().tasks_waiting:
_BLOCK_UPLOAD_LOCKS.pop(block_id, None)
ext = Path(original_filename).suffix.lower()
size_bytes = len(content)
extracted_path = block_dir / f"{file_id}.extracted.txt"
try:
extracted_text = await _extract_text_from_file_async(raw_path, ext)
# isspace() == (not strip()) on a non-empty str, without copying a 500 MB text on the event loop.
if not extracted_text or extracted_text.isspace():
raw_path.unlink(missing_ok = True)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "error",
error = "No extractable text found in file",
)
await _write_to_completion(
functools.partial(extracted_path.write_text, extracted_text, encoding = "utf-8")
)
except ImportError as e:
raw_path.unlink(missing_ok = True)
extracted_path.unlink(missing_ok = True)
missing = getattr(e, "name", None)
expected_missing = {
".pdf": "pymupdf4llm",
**dict.fromkeys((".docx", ".docm", ".dotx", ".dotm"), "docx"),
**dict.fromkeys(_MUPDF_EBOOK_EXTS, "pymupdf"),
}.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),
missing = missing,
exc_info = True,
)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "error",
error = f"Cannot read {ext} files: the '{package}' package is not installed.",
)
logger.error(
"data_recipe.seed.text_extraction_failed",
error = str(e),
exc_info = True,
)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "error",
error = "Text extraction failed.",
)
except Exception as e:
from core.rag.pdf_ocr import PDFOCRError
raw_path.unlink(missing_ok = True)
extracted_path.unlink(missing_ok = True)
logger.error(
"data_recipe.seed.text_extraction_failed",
error = str(e),
exc_info = True,
)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "error",
error = str(e) if isinstance(e, PDFOCRError) else "Text extraction failed.",
)
except BaseException:
# Cancellation kills the OCR worker before its input can be removed.
raw_path.unlink(missing_ok = True)
extracted_path.unlink(missing_ok = True)
raise
try:
meta_path = block_dir / f"{file_id}.meta.json"
meta_path.write_text(
json.dumps({"original_filename": original_filename, "size_bytes": size_bytes}),
encoding = "utf-8",
)
except OSError:
raw_path.unlink(missing_ok = True)
extracted_path.unlink(missing_ok = True)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "error",
error = "Failed to save file metadata",
)
return UnstructuredFileUploadResponse(
file_id = file_id,
filename = original_filename,
size_bytes = size_bytes,
status = "ok",
)
@router.delete("/seed/unstructured-file/{block_id}/{file_id}")
async def remove_unstructured_file(block_id: str, file_id: str):
_validate_safe_id(block_id, "block_id")
_validate_safe_id(file_id, "file_id")
block_dir = UNSTRUCTURED_UPLOAD_ROOT / block_id
if not block_dir.exists():
raise HTTPException(404, "Block not found")
deleted = False
for f in block_dir.iterdir():
stem = f.name.split(".")[0]
if stem == file_id:
f.unlink(missing_ok = True)
deleted = True
if not deleted:
raise HTTPException(404, "File not found")
try:
if not any(block_dir.iterdir()):
block_dir.rmdir()
except OSError:
pass
return {"status": "ok"}
@router.delete("/seed/unstructured-block/{block_id}")
async def remove_unstructured_block(block_id: str):
"""Delete a block's upload directory; files on disk still count toward its quota.
Only uid-namespaced directories may be bulk-deleted: they have exactly one
owning block. Legacy node-id directories (n1, ...) can be shared by other
recipes, so they are managed file-by-file instead.
"""
_validate_safe_id(block_id, "block_id")
if not _UPLOAD_UID_RE.match(block_id):
raise HTTPException(400, "Invalid block_id: only uid-namespaced blocks can be deleted")
block_dir = (UNSTRUCTURED_UPLOAD_ROOT / block_id).resolve()
if not block_dir.is_relative_to(UNSTRUCTURED_UPLOAD_ROOT.resolve()):
raise HTTPException(400, "Invalid block_id: outside upload root")
if not block_dir.exists():
return {"status": "ok", "deleted": False}
try:
shutil.rmtree(block_dir)
except OSError as exc:
raise log_and_http_error(
exc,
500,
"failed to delete uploaded files",
event = "data_recipe.seed.unstructured_block_delete_failed",
log = logger,
) from exc
if block_dir.exists():
raise HTTPException(500, "failed to delete uploaded files")
return {"status": "ok", "deleted": True}
@router.post("/seed/inspect-upload", response_model = SeedInspectResponse)
def inspect_seed_upload(payload: SeedInspectUploadRequest) -> SeedInspectResponse:
if payload.file_ids is not None:
if len(payload.file_ids) == 0:
raise HTTPException(400, "file_ids must not be empty")
_validate_safe_id(payload.block_id, "block_id")
for fid in payload.file_ids:
_validate_safe_id(fid, "file_id")
preview_rows, total_rows = _read_preview_rows_from_multi_files(
block_id = payload.block_id,
file_ids = payload.file_ids,
file_names = payload.file_names,
preview_size = payload.preview_size,
chunk_size = payload.unstructured_chunk_size,
chunk_overlap = payload.unstructured_chunk_overlap,
)
columns = ["chunk_text", "source_file"] if preview_rows else []
resolved_paths = [
str(UNSTRUCTURED_UPLOAD_ROOT / payload.block_id / f"{fid}.extracted.txt")
for fid in payload.file_ids
]
return SeedInspectResponse(
dataset_name = "unstructured_seed",
resolved_path = resolved_paths[0] if resolved_paths else "",
resolved_paths = resolved_paths,
columns = columns,
preview_rows = _serialize_preview_rows(preview_rows),
total_rows = total_rows,
)
seed_source_type = _normalize_optional_text(payload.seed_source_type) or "local"
filename = _sanitize_filename(payload.filename)
ext = Path(filename).suffix.lower()
# Legacy single-file path is .txt/.md only; PDF/DOCX use multi-file upload
_LEGACY_UNSTRUCTURED_EXTS = {".txt", ".md"}
if seed_source_type == "unstructured":
if ext not in _LEGACY_UNSTRUCTURED_EXTS:
allowed = ", ".join(sorted(_LEGACY_UNSTRUCTURED_EXTS))
raise HTTPException(
status_code = 400,
detail = f"unsupported file type: {ext}. allowed: {allowed}",
)
else:
if ext not in LOCAL_UPLOAD_EXTS:
allowed = ", ".join(sorted(LOCAL_UPLOAD_EXTS))
raise HTTPException(
status_code = 400,
detail = f"unsupported file type: {ext}. allowed: {allowed}",
)
file_bytes = _decode_base64_payload(payload.content_base64)
if not file_bytes:
raise HTTPException(status_code = 400, detail = "empty upload payload")
if len(file_bytes) > LOCAL_SEED_UPLOAD_MAX_BYTES:
raise HTTPException(
status_code = 413,
detail = f"file too large (max {LOCAL_SEED_UPLOAD_MAX_LABEL})",
)
ensure_dir(SEED_UPLOAD_DIR)
stored_name = f"{uuid4().hex}_{filename}"
stored_path = SEED_UPLOAD_DIR / stored_name
stored_path.write_bytes(file_bytes)
if seed_source_type == "unstructured":
preview_rows = _read_preview_rows_from_unstructured_file(
path = stored_path,
preview_size = int(payload.preview_size),
chunk_size = payload.unstructured_chunk_size,
chunk_overlap = payload.unstructured_chunk_overlap,
)
else:
preview_rows = _read_preview_rows_from_local_file(
stored_path,
int(payload.preview_size),
)
if not preview_rows:
raise HTTPException(status_code = 422, detail = "dataset appears empty or unreadable")
columns = _extract_columns(preview_rows)
return SeedInspectResponse(
dataset_name = filename,
resolved_path = str(stored_path),
columns = columns,
preview_rows = preview_rows,
split = None,
subset = None,
)
@router.get("/seed/github/env-token")
def get_github_env_token_status() -> dict:
"""Report whether the server has a GH_TOKEN / GITHUB_TOKEN env var.
The value is never returned; the UI uses this to tell the user they
can leave the token field blank.
"""
if managed_account():
return {"has_token": False}
has_token = bool(os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN"))
return {"has_token": has_token}