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
Seed preview: check path-backed image cells against the account's workspace (#12404)
Daniel Hanc0a60448e3ec
diff --git a/studio/backend/core/data_recipe/jsonable.py b/studio/backend/core/data_recipe/jsonable.pyindex d8107341687..989d0ce1897 100644--- a/studio/backend/core/data_recipe/jsonable.py+++ b/studio/backend/core/data_recipe/jsonable.py@@ -50,6 +50,14 @@ def _to_pil_from_hf_image_dict(value: Any) -> Any | None:path_value = value.get("path")if isinstance(path_value, str) and path_value.strip():+ # Row cells are user data: a managed account may only preview files it could read itself.+ from fastapi import HTTPException+ from core.training.account_jobs import account_path++ try:+ account_path(path_value)+ except (HTTPException, OSError, RuntimeError, ValueError, TypeError):+ return Nonetry:from PIL import Image # type: ignorewith Image.open(Path(path_value)) as image:diff --git a/studio/backend/core/data_recipe/service.py b/studio/backend/core/data_recipe/service.pyindex 214a8309a73..afd9c0a80b0 100644--- a/studio/backend/core/data_recipe/service.py+++ b/studio/backend/core/data_recipe/service.py@@ -46,6 +46,11 @@ def _load_image_file_to_base64(path_value: str, *, base_path: str | None = None)for candidate in candidates:if not candidate.exists() or not candidate.is_file():continue+ # The cwd fallback is a different file from the one checked above.+ try:+ account_path(candidate)+ except HTTPException:+ continuewith candidate.open("rb") as f:return _encode_bytes_to_base64(f.read())except (OSError, TypeError, ValueError):diff --git a/studio/backend/routes/data_recipe/seed.py b/studio/backend/routes/data_recipe/seed.pyindex 7f18aa669cf..b589f06d516 100644--- a/studio/backend/routes/data_recipe/seed.py+++ b/studio/backend/routes/data_recipe/seed.py@@ -881,7 +881,72 @@ 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)- return [row for row in islice(streamed_ds, preview_size)]+ 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 included, stay {bytes, path} dicts so a local path is checked+ # against the account before it is opened; Hub-hosted images are decoded 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 valuedef _extract_columns(rows: list[dict[str, Any]]) -> list[str]:diff --git a/studio/backend/tests/test_data_recipe_preview_image_paths.py b/studio/backend/tests/test_data_recipe_preview_image_paths.pynew file mode 100644index 00000000000..06a6b2694b0--- /dev/null+++ b/studio/backend/tests/test_data_recipe_preview_image_paths.py@@ -0,0 +1,185 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Image cells are row data: a managed account must not preview pixels outside its workspace."""++from __future__ import annotations++from pathlib import Path++import pytest++PIL = pytest.importorskip("PIL.Image")++from core.data_recipe.jsonable import to_preview_jsonable_row+from utils.account_context import AccountContext, run_as+from utils.paths.storage_roots import workspace_root++ALICE = AccountContext("a" * 32, "alice")+++def _png(path: Path) -> Path:+ path.parent.mkdir(parents = True, exist_ok = True)+ PIL.new("RGB", (4, 4), (255, 0, 0)).save(path)+ return path+++def _is_image_payload(value) -> bool:+ return isinstance(value, dict) and value.get("type") == "image" and bool(value.get("data"))+++def studio_home(monkeypatch, tmp_path):+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio"))+ return tmp_path+++def test_managed_account_cannot_preview_a_path_outside_its_workspace(studio_home):+ secret = _png(studio_home / "elsewhere" / "private.png")+ row = {"label": "x", "image": {"bytes": None, "path": str(secret)}}++ preview = run_as(ALICE, to_preview_jsonable_row, row)++ assert preview["label"] == "x"+ assert not _is_image_payload(preview["image"])+++def test_managed_account_previews_its_own_workspace_image(studio_home):+ own = run_as(ALICE, lambda: _png(workspace_root() / "data" / "mine.png"))+ row = {"image": {"bytes": None, "path": str(own)}}++ preview = run_as(ALICE, to_preview_jsonable_row, row)++ assert _is_image_payload(preview["image"])++[email protected]("path", ["~unsloth-no-such-user-xyz/image.png", "data/bad\x00name.png"])+def test_managed_account_unresolvable_path_is_not_an_image(studio_home, path):+ preview = run_as(ALICE, to_preview_jsonable_row, {"image": {"bytes": None, "path": path}})++ assert not _is_image_payload(preview["image"])+++def test_owner_previews_any_local_path(studio_home):+ image = _png(studio_home / "elsewhere" / "local.png")++ preview = to_preview_jsonable_row({"image": {"bytes": None, "path": str(image)}})++ assert _is_image_payload(preview["image"])+++def _path_only_parquet(+ tmp_path: Path,+ image: Path,+ nested: bool = False,+) -> Path:+ import datasets+ import pyarrow.parquet as pq++ cell = {"bytes": None, "path": str(image)}+ image_feature = [datasets.Image()] if nested else datasets.Image()+ features = datasets.Features({"image": image_feature, "label": datasets.Value("string")})+ out = tmp_path / "rows.parquet"+ dataset = datasets.Dataset.from_dict(+ {"image": [[cell] if nested else cell], "label": ["x"]}, features = features+ )+ # pyarrow directly: to_parquet stats every path, and the Hub one does not resolve offline.+ pq.write_table(dataset.data.table, str(out))+ return out+++def _hub_preview(parquet: Path) -> list[dict]:+ from datasets import load_dataset+ from routes.data_recipe.seed import _load_preview_rows, _serialize_preview_rows++ rows = _load_preview_rows(+ load_dataset_fn = load_dataset,+ load_kwargs = {+ "path": "parquet",+ "data_files": [str(parquet)],+ "split": "train",+ "streaming": True,+ },+ preview_size = 1,+ )+ return _serialize_preview_rows(rows)++[email protected]("nested", [False, True])+def test_managed_account_streamed_image_feature_is_checked_before_it_is_opened(studio_home, nested):+ pytest.importorskip("datasets")+ private = _png(studio_home / "elsewhere" / "private.png")+ parquet = _path_only_parquet(studio_home, private, nested = nested)++ (row,) = run_as(ALICE, _hub_preview, parquet)++ assert row["label"] == "x"+ cells = row["image"] if nested else [row["image"]]+ assert cells and not any(_is_image_payload(cell) for cell in cells)++[email protected]("nested", [False, True])+def test_managed_account_streamed_image_inside_its_workspace_previews(studio_home, nested):+ pytest.importorskip("datasets")+ own = run_as(ALICE, lambda: _png(workspace_root() / "data" / "mine.png"))+ parquet = _path_only_parquet(studio_home, own, nested = nested)++ (row,) = run_as(ALICE, _hub_preview, parquet)++ cells = row["image"] if nested else [row["image"]]+ assert cells and all(_is_image_payload(cell) for cell in cells)++[email protected]("nested", [False, True])+def test_owner_streamed_image_feature_still_previews(studio_home, nested):+ pytest.importorskip("datasets")+ image = _png(studio_home / "elsewhere" / "local.png")+ parquet = _path_only_parquet(studio_home, image, nested = nested)++ (row,) = _hub_preview(parquet)++ cells = row["image"] if nested else [row["image"]]+ assert cells and all(_is_image_payload(cell) for cell in cells)+++def test_managed_account_image_file_lookup_skips_the_cwd_fallback(studio_home, monkeypatch):+ from core.data_recipe.service import _load_image_file_to_base64++ _png(studio_home / "cwd" / "private.png")+ monkeypatch.chdir(studio_home / "cwd")+ base = run_as(ALICE, lambda: workspace_root() / "data")++ assert run_as(ALICE, _load_image_file_to_base64, "private.png", base_path = str(base)) is None+ assert _load_image_file_to_base64("private.png", base_path = str(base)) is not None+++def test_managed_account_hub_hosted_images_still_preview(studio_home, monkeypatch):+ datasets = pytest.importorskip("datasets")+ hub = "hf://datasets/org/repo@0123abc/cat.png"+ parquet = _path_only_parquet(studio_home, hub)+ opened = []++ def fake_decode(+ self,+ value,+ token_per_repo_id = None,+ ):+ opened.append(value["path"])+ return PIL.new("RGB", (4, 4))++ monkeypatch.setattr(datasets.Image, "decode_example", fake_decode)++ (row,) = run_as(ALICE, _hub_preview, parquet)++ assert opened == [hub]+ assert _is_image_payload(row["image"])+++def test_managed_account_file_url_is_not_treated_as_hub_hosted(studio_home):+ pytest.importorskip("datasets")+ private = _png(studio_home / "elsewhere" / "private.png")+ parquet = _path_only_parquet(studio_home, f"file://{private.as_posix()}")++ (row,) = run_as(ALICE, _hub_preview, parquet)++ assert not _is_image_payload(row["image"])