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
Studio: train an uploaded CSV's NA, None and 00501 cells as written (#12379)
Nilay14ebbe876d08
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.pyindex ad2d3bbecbe..5e6eb888f92 100644--- a/studio/backend/core/training/trainer.py+++ b/studio/backend/core/training/trainer.py@@ -76,6 +76,7 @@ from utils.models.model_identity import restore_hf_cache_repo_identityfrom utils.models.unsloth_mirror import unsloth_public_mirrorfrom utils.models.model_config import _env_offlinefrom utils.datasets import format_and_template_dataset+from utils.datasets.cells import csv_as_text_kwargsfrom utils.datasets.chat_templates import get_training_chat_templatefrom utils.datasets.completion_masking import apply_completion_maskingfrom utils.datasets.iterable import is_streaming_dataset as detect_streaming_dataset@@ -2807,7 +2808,12 @@ class UnslothTrainer:if all_files:loader = self._loader_for_files(all_files)- dataset = load_dataset(loader, data_files = all_files, split = "train")+ dataset = load_dataset(+ loader,+ data_files = all_files,+ split = "train",+ **csv_as_text_kwargs(all_files),+ )if self.should_stop:logger.info("Stopped during dataset loading\n")@@ -2824,7 +2830,10 @@ class UnslothTrainer:if eval_all_files:eval_loader = self._loader_for_files(eval_all_files)eval_dataset = load_dataset(- eval_loader, data_files = eval_all_files, split = "train"+ eval_loader,+ data_files = eval_all_files,+ split = "train",+ **csv_as_text_kwargs(eval_all_files),)has_separate_eval_source = Truelogger.info(diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.pyindex 7b6bd33970d..abfc0bec771 100644--- a/studio/backend/core/training/worker.py+++ b/studio/backend/core/training/worker.py@@ -2952,8 +2952,15 @@ def _run_mlx_training(event_queue, stop_queue, config):all_files = _resolve_mlx_local_dataset_files(file_paths)if not all_files:raise ValueError("No local dataset files found")+ from utils.datasets.cells import csv_as_text_kwargs+loader = _mlx_local_dataset_loader_for_files(all_files)- return load_dataset(loader, data_files = all_files, split = "train")+ return load_dataset(+ loader,+ data_files = all_files,+ split = "train",+ **csv_as_text_kwargs(all_files),+ )eval_dataset = Noneif hf_dataset:diff --git a/studio/backend/hub/services/datasets/local.py b/studio/backend/hub/services/datasets/local.pyindex 12042f5cd8c..7dc3f9001e9 100644--- a/studio/backend/hub/services/datasets/local.py+++ b/studio/backend/hub/services/datasets/local.py@@ -203,7 +203,7 @@ def _build_local_dataset_items() -> list[LocalDatasetItem]:return items-def _stream_file_preview_slice(path: Path, preview_size: int):+def _stream_file_preview_slice(path: Path, preview_size: int, **load_kwargs):"""Stream the first ``preview_size`` rows so a large file is never fully parsed into Arrow; returns ``(Dataset, None)`` or ``None`` if empty/unsupported."""from itertools import islice@@ -228,16 +228,18 @@ def _stream_file_preview_slice(path: Path, preview_size: int):data_files = str(path),split = "train",streaming = True,+ **load_kwargs,)rows = list(islice(streamed, preview_size))if not rows:return None- return Dataset.from_list(rows), None+ return Dataset.from_list(rows, features = load_kwargs.get("features")), Nonedef _load_local_preview_slice(*, dataset_path: Path, train_split: str, preview_size: int):# Non-streaming loads take the cached builder lock; use the EACCES-safe wrapper.from utils.datasets.cache_safe import load_dataset_cache_safe as load_dataset+ from utils.datasets.cells import csv_as_text_kwargsif dataset_path.is_dir():parquet_dir = (@@ -275,7 +277,9 @@ def _load_local_preview_slice(*, dataset_path: Path, train_split: str, preview_sreturn preview_slice, total_rowsif suffix in (".json", ".jsonl", ".csv"):- preview = _stream_file_preview_slice(dataset_path, preview_size)+ preview = _stream_file_preview_slice(+ dataset_path, preview_size, **csv_as_text_kwargs([dataset_path])+ )if preview is None:raise HTTPException(status_code = 400,diff --git a/studio/backend/hub/utils/dataset_format.py b/studio/backend/hub/utils/dataset_format.pyindex 792617b72f3..c93a342d871 100644--- a/studio/backend/hub/utils/dataset_format.py+++ b/studio/backend/hub/utils/dataset_format.py@@ -6,6 +6,7 @@ from __future__ import annotationsimport refrom typing import Any, Optional+from utils.datasets.cells import text_cell_checkfrom utils.datasets.format_detection import detect_dataset_format@@ -426,6 +427,7 @@ def detect_vlm_dataset_structure(dataset):"messages_column": None,}column_names = set(sample.keys())+ is_text = text_cell_check(dataset)if "messages" in column_names:messages = sample["messages"]if messages and len(messages) > 0:@@ -546,7 +548,7 @@ def detect_vlm_dataset_structure(dataset):score = 100elif isinstance(value, dict) and ("bytes" in value or "path" in value):score = 75- elif isinstance(value, str):+ elif isinstance(value, str) and is_text(col, value):score = (55if is_metadata_column(col)@@ -567,7 +569,7 @@ def detect_vlm_dataset_structure(dataset):):continuevalue = sample[col]- if isinstance(value, str) and value:+ if isinstance(value, str) and value and is_text(col, value):text_candidates.append((col, min(len(value), 1000)))elif isinstance(value, list) and value and isinstance(value[0], str):text_candidates.append((col, min(len(value[0]), 1000) // 2))diff --git a/studio/backend/tests/test_csv_cells_as_written.py b/studio/backend/tests/test_csv_cells_as_written.pynew file mode 100644index 00000000000..f800602cba0--- /dev/null+++ b/studio/backend/tests/test_csv_cells_as_written.py@@ -0,0 +1,210 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from __future__ import annotations++import sys+from pathlib import Path++_BACKEND = Path(__file__).resolve().parent.parent+if str(_BACKEND) not in sys.path:+ sys.path.insert(0, str(_BACKEND))++import importlib # noqa: E402+import types # noqa: E402+from unittest.mock import MagicMock # noqa: E402++import pytest # noqa: E402++_STUBBED: list[str] = []+++def _stub_if_missing(name, attrs):+ if name in sys.modules:+ return+ try:+ importlib.import_module(name)+ return+ except Exception: # noqa: BLE001 - stub unusable imports+ pass+ _STUBBED.append(name)+ mod = types.ModuleType(name)+ mod.__spec__ = None+ for attr in attrs:+ setattr(mod, attr, MagicMock())+ sys.modules[name] = mod+ parent, _, child = name.rpartition(".")+ if parent and parent in sys.modules:+ setattr(sys.modules[parent], child, mod)+++_stub_if_missing("unsloth", ("FastLanguageModel", "FastVisionModel", "is_bfloat16_supported"))+_stub_if_missing("unsloth.chat_templates", ("get_chat_template",))+_stub_if_missing("trl", ("SFTTrainer", "SFTConfig"))++from core.training import trainer as tmod # noqa: E402+from hub.services.datasets.local import _load_local_preview_slice # noqa: E402+from hub.utils.dataset_format import check_dataset_format # noqa: E402+from utils.datasets.format_detection import detect_vlm_dataset_structure # noqa: E402++for _name in reversed(_STUBBED):+ sys.modules.pop(_name, None)+++ALPACA_CSV = (+ "instruction,output\n"+ "List the side effects.,None\n"+ "What is the airline code?,N/A\n"+ "What does the API return on a miss?,null\n"+ "Price for item 7?,NA\n"+ "What is 8 x 9?,72\n"+ "What is 23 / 4?,5.75\n"+ "Zip code of Holtsville NY?,00501\n"+ "Say nothing.,\n"+)+WRITTEN = ["None", "N/A", "null", "NA", "72", "5.75", "00501", ""]+++class _Tokenizer:+ chat_template = "{{ messages }}"+ eos_token = "</s>"+++def trainer(monkeypatch):+ monkeypatch.setattr(tmod, "should_use_mlx_training_backend", lambda *a, **k: False)+ monkeypatch.setattr(tmod, "ensure_audio_decoding", lambda: True)+ t = tmod.UnslothTrainer()+ t.model_name = "Qwen2ForCausalLM"+ t.tokenizer = _Tokenizer()+ return t+++def _responses(dataset):+ return [text.split("### Response:\n", 1)[1].removesuffix("</s>") for text in dataset["text"]]+++def test_uploaded_csv_trains_na_and_none_answers_as_written(trainer, tmp_path):+ train = tmp_path / "train.csv"+ train.write_text(ALPACA_CSV)+ evals = tmp_path / "eval.csv"+ evals.write_text(ALPACA_CSV)++ result = trainer.load_and_format_dataset(+ None,+ local_datasets = [str(train)],+ local_eval_datasets = [str(evals)],+ eval_steps = 0.1,+ )++ assert result is not None+ dataset_info, eval_dataset = result+ assert dataset_info["final_format"] == "alpaca"+ assert _responses(dataset_info["dataset"]) == WRITTEN+ assert _responses(eval_dataset) == WRITTEN+++def test_upload_preview_shows_csv_cells_as_written(tmp_path):+ path = tmp_path / "upload.csv"+ path.write_text(ALPACA_CSV)++ preview, _ = _load_local_preview_slice(dataset_path = path, train_split = "train", preview_size = 10)++ assert [row["output"] for row in preview] == WRITTEN[:-1] + [None]++ numbers = tmp_path / "numbers.csv"+ numbers.write_text("question,answer\nq1,72\nq2,\nq3,00501\n")+ preview, _ = _load_local_preview_slice(+ dataset_path = numbers, train_split = "train", preview_size = 10+ )+ assert [row["answer"] for row in preview] == ["72", None, "00501"]+++def test_cpt_csv_still_trains_the_text_column_not_the_id(trainer, tmp_path):+ path = tmp_path / "docs.csv"+ path.write_text(+ "id,score,is_clean,notes,passage\n1,1e5,True,,NA\n2,.5,False,N/A,The second document.\n"+ )++ dataset_info, _ = trainer.load_and_format_dataset(None, local_datasets = [str(path)], is_cpt = True)++ assert list(dataset_info["dataset"]["text"]) == ["NA</s>", "The second document.</s>"]++ mixed = tmp_path / "mixed.csv"+ mixed.write_text("content,source\n101,corpus\nTrue,corpus\n")+ dataset_info, _ = trainer.load_and_format_dataset(+ None, local_datasets = [str(mixed)], is_cpt = True+ )+ assert list(dataset_info["dataset"]["text"]) == ["101</s>", "True</s>"]++ numbers = tmp_path / "numbers.jsonl"+ numbers.write_text(+ '{"content": "101", "source": "corpus"}\n{"content": "102", "source": "corpus"}\n'+ )+ dataset_info, _ = trainer.load_and_format_dataset(+ None, local_datasets = [str(numbers)], is_cpt = True+ )+ assert list(dataset_info["dataset"]["text"]) == ["101</s>", "102</s>"]+++def test_csv_files_missing_a_column_still_load_together(trainer, tmp_path):+ wide = tmp_path / "a.csv"+ wide.write_text("instruction,input,output\nZip code?,Holtsville,00501\n")+ narrow = tmp_path / "b.csv"+ narrow.write_text("instruction,output\nPrice?,NA\n")++ dataset_info, _ = trainer.load_and_format_dataset(None, local_datasets = [str(wide), str(narrow)])++ assert len(dataset_info["dataset"]) == 2+++def test_vision_detection_skips_typed_cells_only_in_csv_read_as_text(tmp_path):+ image = tmp_path / "a.png"+ image.write_bytes(b"")+ path = tmp_path / "vision.csv"+ path.write_text(f"file_name,image,caption,label\n101,{image},cat,False\n102,{image},dog,True\n")++ preview, _ = _load_local_preview_slice(dataset_path = path, train_split = "train", preview_size = 10)++ detected = check_dataset_format(preview, is_vlm = True)+ assert (detected["detected_image_column"], detected["detected_text_column"]) == (+ "image",+ "caption",+ )+ structure = detect_vlm_dataset_structure(preview)+ assert (structure["image_column"], structure["text_column"]) == ("image", "caption")++ unresolved = tmp_path / "unresolved.csv"+ unresolved.write_text("photo,file_name,caption\n101,missing/a.png,cat\n")+ preview, _ = _load_local_preview_slice(+ dataset_path = unresolved, train_split = "train", preview_size = 10+ )+ assert detect_vlm_dataset_structure(preview)["image_column"] == "file_name"++ vqa = tmp_path / "vqa.jsonl"+ vqa.write_text(+ '{"image": "https://example.com/a.png", "question": "How many?", "answer": "2"}\n'+ )+ preview, _ = _load_local_preview_slice(dataset_path = vqa, train_split = "train", preview_size = 10)+ assert check_dataset_format(preview, is_vlm = True)["detected_text_column"] == "answer"+ assert detect_vlm_dataset_structure(preview)["text_column"] == "answer"++ ocr = tmp_path / "ocr.csv"+ ocr.write_text(+ "image,caption\n"+ + "https://example.com/a.png,12345678\n" * 10+ + "https://example.com/b.png,hello\n"+ )+ preview, _ = _load_local_preview_slice(dataset_path = ocr, train_split = "train", preview_size = 10)+ assert check_dataset_format(preview, is_vlm = True)["detected_text_column"] == "caption"+ assert detect_vlm_dataset_structure(preview)["text_column"] == "caption"++ missing = tmp_path / "missing.csv"+ missing.write_text(+ "image,caption,text\nhttps://example.com/a.png,None,hi\nhttps://example.com/b.png,a dog,yo\n"+ )+ preview, _ = _load_local_preview_slice(+ dataset_path = missing, train_split = "train", preview_size = 10+ )+ assert check_dataset_format(preview, is_vlm = True)["detected_text_column"] == "text"+ assert detect_vlm_dataset_structure(preview)["text_column"] == "text"diff --git a/studio/backend/utils/datasets/cells.py b/studio/backend/utils/datasets/cells.pyindex 0adaf3b47fc..c04267c8961 100644--- a/studio/backend/utils/datasets/cells.py+++ b/studio/backend/utils/datasets/cells.py@@ -1,7 +1,20 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""Turning one raw dataset cell into the text we are willing to train on."""+"""Turning raw dataset cells into the text we are willing to train on."""++from pathlib import Path++# Feature ids tagging each column of a CSV read by `csv_as_text_kwargs`.+_CSV_TEXT = "csv_text"+_CSV_TYPED = "csv_typed"+# The csv loader types each column from the first chunk of the first file.+_CSV_CHUNK_ROWS = 10_000+# pandas' default missing-value markers.+_NA_CELLS = frozenset(+ ("", "#N/A", "#N/A N/A", "#NA", "-1.#IND", "-1.#QNAN", "-NaN", "-nan", "1.#IND", "1.#QNAN")+ + ("<NA>", "N/A", "NA", "NULL", "NaN", "None", "n/a", "nan", "null")+)def cell_text(value):@@ -27,3 +40,50 @@ def cell_text(value):if isinstance(value, float) and value != value:return ""return str(value)+++def _column_ids(dataset) -> dict:+ features = getattr(dataset, "features", None) or {}+ return {column: getattr(feature, "id", None) for column, feature in features.items()}+++def typed_csv_columns(dataset) -> frozenset:+ """CSV columns the csv loader would have typed: numbers, booleans or only missing cells."""+ return frozenset(column for column, tag in _column_ids(dataset).items() if tag == _CSV_TYPED)+++def text_cell_check(dataset):+ """`(column, cell) -> bool`: False for CSV cells the csv loader would not read as strings."""+ ids = _column_ids(dataset)++ def is_text(column, cell):+ tag = ids.get(column)+ return tag not in (_CSV_TEXT, _CSV_TYPED) or (tag == _CSV_TEXT and cell not in _NA_CELLS)++ return is_text+++def csv_as_text_kwargs(files):+ if Path(files[0]).suffix.lower() != ".csv":+ return {}+ import pandas as pd+ import pyarrow as pa+ from datasets import Features, Value++ with pd.read_csv(files[0], chunksize = _CSV_CHUNK_ROWS) as chunks:+ first = next(chunks)+ headers = [first.columns] + [pd.read_csv(path, nrows = 0).columns for path in files[1:]]+ # A string schema needs the same columns in every file; else keep the loader's defaults.+ if any(set(columns) != set(headers[0]) for columns in headers):+ return {}+ strings = {+ field.name+ for field in pa.Table.from_pandas(first).schema+ if pa.types.is_string(field.type) or pa.types.is_large_string(field.type)+ }+ tags = {name: _CSV_TEXT if name in strings else _CSV_TYPED for name in first.columns}+ return {+ "features": Features({name: Value("string", id = tag) for name, tag in tags.items()}),+ "keep_default_na": False,+ "na_values": [""],+ }diff --git a/studio/backend/utils/datasets/format_detection.py b/studio/backend/utils/datasets/format_detection.pyindex 4feb6768ff1..2ff0a3adbed 100644--- a/studio/backend/utils/datasets/format_detection.py+++ b/studio/backend/utils/datasets/format_detection.py@@ -640,6 +640,9 @@ def _has_image_header(data: bytes) -> bool:def detect_vlm_dataset_structure(dataset):"""Detect which VLM dataset shape this is: standard VLM messages (image objects in content), Llava format (image indices plus a separate images column), or a simple image + text pair needing conversion."""+ # Imported here: this module is also loaded on its own by file path.+ from .cells import text_cell_check+try:sample = next(iter(dataset))except StopIteration:@@ -652,6 +655,7 @@ def detect_vlm_dataset_structure(dataset):}column_names = set(sample.keys())+ is_text = text_cell_check(dataset)if "messages" in column_names:messages = sample["messages"]@@ -791,7 +795,7 @@ def detect_vlm_dataset_structure(dataset):if isinstance(sample_value, dict) and ("bytes" in sample_value or "path" in sample_value):return 75- if isinstance(sample_value, str):+ if isinstance(sample_value, str) and is_text(col, sample_value):if sample_value.startswith(("http://", "https://")):return 70 if not is_metadata_column(col) else 55if is_metadata_column(col):@@ -868,7 +872,11 @@ def detect_vlm_dataset_structure(dataset):if any(_keyword_in_column(keyword, col) for keyword in text_keywords):sample_value = sample[col]- if isinstance(sample_value, str) and len(sample_value) > 0:+ if (+ isinstance(sample_value, str)+ and len(sample_value) > 0+ and is_text(col, sample_value)+ ):# Longer text = higher priority (content, not a label).priority = min(len(sample_value), 1000)candidates.append((col, priority))diff --git a/studio/backend/utils/datasets/raw_text.py b/studio/backend/utils/datasets/raw_text.pyindex ce06eee7d8d..df534e03da3 100644--- a/studio/backend/utils/datasets/raw_text.py+++ b/studio/backend/utils/datasets/raw_text.py@@ -10,6 +10,8 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Literal, TYPE_CHECKING+from .cells import typed_csv_columns+if TYPE_CHECKING:from datasets import Dataset@@ -64,6 +66,12 @@ def _string_columns(dataset: Dataset) -> list[str]:return string_cols+def _pick_text_column(dataset: Dataset, string_cols: list[str]) -> str:+ # Every column of an uploaded CSV is text; skip one pandas would have typed, like an id.+ typed = typed_csv_columns(dataset)+ return next((col for col in string_cols if col not in typed), string_cols[0])++def _split_scope(split_name: str | None) -> str:return f"the {split_name} split" if split_name else "this dataset"@@ -139,7 +147,7 @@ def prepare_raw_text_dataset(f"was found in {split_scope} (columns: {col_names}).")- renamed_col = string_cols[0]+ renamed_col = _pick_text_column(dataset, string_cols)if len(string_cols) > 1:notices.append(RawTextNotice(