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
fix studio dataset column role detection
Nilay48fb85c9aadb
diff --git a/studio/backend/hub/utils/dataset_format.py b/studio/backend/hub/utils/dataset_format.pyindex 2c8756aa95f..af6d80cfb4e 100644--- a/studio/backend/hub/utils/dataset_format.py+++ b/studio/backend/hub/utils/dataset_format.py@@ -131,6 +131,8 @@ def detect_custom_format_heuristic(dataset):"template","task",]+ # Only pair today: "text" inside "context".+ role_words = assistant_words + user_words + system_wordsmetadata_exact_match = {"id","idx",@@ -162,10 +164,24 @@ def detect_custom_format_heuristic(dataset):"completion": 60,}- def has_keyword(col_name, keywords):+ def has_keyword(+ col_name,+ keywords,+ apply_shadowing = True,+ ):col_lower = col_name.lower()col_normalized = col_lower.replace("_", "").replace("-", "").replace(" ", "")- return any(keyword in col_lower or keyword in col_normalized for keyword in keywords)+ for keyword in keywords:+ if keyword in col_lower or keyword in col_normalized:+ if not apply_shadowing:+ return True+ shadowed = any(+ keyword != other and keyword in other and other in col_normalized+ for other in role_words+ )+ if not shadowed:+ return True+ return Falsedef is_metadata(col_name):col_lower = col_name.lower()@@ -189,8 +205,14 @@ def detect_custom_format_heuristic(dataset):except Exception:return 0- def score_column(col_name, keywords, role_type, num_candidates):- if not has_keyword(col_name, keywords):+ def score_column(+ col_name,+ keywords,+ role_type,+ num_candidates,+ apply_shadowing = True,+ ):+ if not has_keyword(col_name, keywords, apply_shadowing = apply_shadowing):return 0score = 10if role_type == "user":@@ -240,6 +262,23 @@ def detect_custom_format_heuristic(dataset):score = score_column(col, user_words, "user", len(user_potential))if score > 0:user_candidates.append((col, score))+ if not user_candidates and not any(col != assistant_col for col in user_potential):+ # has_keyword drops "context" from user_potential because "text" only matches+ # inside it. When nothing else can hold the user turn, that column is a better+ # user turn than an assistant-worded leftover.+ shadowed_potential = [+ col+ for col in content_columns+ if col not in user_potential and has_keyword(col, user_words, apply_shadowing = False)+ ]+ for col in shadowed_potential:+ if col == assistant_col:+ continue+ score = score_column(+ col, user_words, "user", len(shadowed_potential), apply_shadowing = False+ )+ if score > 0:+ user_candidates.append((col, score))if user_candidates:user_candidates.sort(key = lambda item: item[1], reverse = True)user_col = user_candidates[0][0]diff --git a/studio/backend/tests/test_dataset_heuristic_role_keywords.py b/studio/backend/tests/test_dataset_heuristic_role_keywords.pynew file mode 100644index 00000000000..0cabed2c0e2--- /dev/null+++ b/studio/backend/tests/test_dataset_heuristic_role_keywords.py@@ -0,0 +1,134 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Role detection on columns whose names contain a shorter role word: "text" inside+"context" used to win the user slot, sending the passage to the user turn and the+question to the system prompt."""++import sys+from pathlib import Path++import pytest++_BACKEND_ROOT = Path(__file__).resolve().parents[1]+if str(_BACKEND_ROOT) not in sys.path:+ sys.path.insert(0, str(_BACKEND_ROOT))++from hub.utils import dataset_format # noqa: E402+from utils.datasets import format_detection # noqa: E402++_HEURISTICS = [+ pytest.param(format_detection.detect_custom_format_heuristic, id = "utils"),+ pytest.param(dataset_format.detect_custom_format_heuristic, id = "hub"),+]++_LONG = "x" * 600+_MID = "y" * 120++_CASES = [+ (+ {"context": _LONG, "question": _MID, "answer": _MID},+ {"question": "user", "context": "system", "answer": "assistant"},+ ),+ (+ {"id": "1", "title": "t", "context": _LONG, "question": _MID, "answers": _MID},+ {"question": "user", "context": "system", "title": "system", "answers": "assistant"},+ ),+ (+ {"instruction": _MID, "context": _LONG, "response": _MID, "category": "qa"},+ {"instruction": "user", "context": "system", "response": "assistant"},+ ),+ (+ {"retrieved_contexts": _LONG, "question": _MID, "ground_truth_answer": _MID},+ {"question": "user", "retrieved_contexts": "system", "ground_truth_answer": "assistant"},+ ),+ (+ {"context": _LONG, "response": _MID},+ {"context": "user", "response": "assistant"},+ ),+ (+ {"instruction": _MID, "input": _MID, "output": _MID},+ {"instruction": "user", "input": "system", "output": "assistant"},+ ),+ (+ {"user_input": _MID, "assistant_output": _MID},+ {"user_input": "user", "assistant_output": "assistant"},+ ),+ (+ {"input_text": _MID, "target_text": _MID},+ {"input_text": "user", "target_text": "assistant"},+ ),+ (+ {"userInput": _MID, "assistantResponse": _MID},+ {"userInput": "user", "assistantResponse": "assistant"},+ ),+ (+ {"inputs": _MID, "targets": _MID},+ {"inputs": "user", "targets": "assistant"},+ ),+ (+ {"fulltext": _LONG, "answer": _MID},+ {"fulltext": "user", "answer": "assistant"},+ ),+ (+ {"subtask": _MID, "answer": _MID},+ {"subtask": "user", "answer": "assistant"},+ ),+ (+ {"task": _MID, "input": _MID, "output": _MID},+ {"input": "user", "task": "system", "output": "assistant"},+ ),+ (+ {"context": "Background.", "prompt": "Summarize.", "question": "Why?", "answer": _MID},+ {"context": "system", "prompt": "user", "answer": "assistant"},+ ),+ (+ {"context": _LONG, "response_text": _MID},+ {"context": "user", "response_text": "assistant"},+ ),+]+++# An assistant column must never be drafted into the user turn just because the shadow+# rule emptied the user candidates: the context column is the better user turn.+_ASSISTANT_LEFTOVER_CASES = [+ (+ {"context": _LONG, "answer": _MID, "explanation": _MID},+ {"answer": "assistant", "context": "user", "explanation": "system"},+ ),+ (+ {"context": _LONG, "answer": _MID, "output": _MID},+ {"answer": "assistant", "context": "user", "output": "system"},+ ),+ (+ {"context": _LONG, "response": _MID, "target": _MID},+ {"response": "assistant", "context": "user", "target": "system"},+ ),+]++# A column that is only ever a system word stays unmapped, so the caller keeps asking+# for a manual mapping instead of silently training the system prompt as the user turn.+_NO_USER_COLUMN_ROWS = [+ {"system": _MID, "output": _MID},+ {"persona": _MID, "reply": _MID},+ {"role": _MID, "response": _MID},+ {"template": _MID, "output": _MID},+]++[email protected]("heuristic", _HEURISTICS)[email protected]("row, expected", _CASES)+def test_context_column_is_not_matched_as_text(heuristic, row, expected):+ assert heuristic([row]) == expected++[email protected]("heuristic", _HEURISTICS)[email protected]("row, expected", _ASSISTANT_LEFTOVER_CASES)+def test_assistant_column_is_not_promoted_to_the_user_turn(heuristic, row, expected):+ assert heuristic([row]) == expected++[email protected]("heuristic", _HEURISTICS)[email protected]("row", _NO_USER_COLUMN_ROWS)+def test_system_only_column_is_not_promoted_to_the_user_turn(heuristic, row):+ assert heuristic([row]) is Nonediff --git a/studio/backend/utils/datasets/format_detection.py b/studio/backend/utils/datasets/format_detection.pyindex 5b52fe546a9..60a66d01c52 100644--- a/studio/backend/utils/datasets/format_detection.py+++ b/studio/backend/utils/datasets/format_detection.py@@ -209,6 +209,9 @@ def detect_custom_format_heuristic(dataset):"task",]+ # Only pair today: "text" inside "context".+ role_words = assistant_words + user_words + system_words+metadata_exact_match = {"id","idx",@@ -242,14 +245,26 @@ def detect_custom_format_heuristic(dataset):"completion": 60,}- def has_keyword(col_name, keywords):- """True if any keyword appears in the column name."""+ def has_keyword(+ col_name,+ keywords,+ apply_shadowing = True,+ ):+ """True if any keyword appears in the column name, ignoring a keyword that only+ matches inside a longer role word the name also carries ("text" in "context")."""col_lower = col_name.lower()col_normalized = col_lower.replace("_", "").replace("-", "").replace(" ", "")for keyword in keywords:if keyword in col_lower or keyword in col_normalized:- return True+ if not apply_shadowing:+ return True+ shadowed = any(+ keyword != other and keyword in other and other in col_normalized+ for other in role_words+ )+ if not shadowed:+ return Truereturn Falsedef is_metadata(col_name):@@ -295,9 +310,15 @@ def detect_custom_format_heuristic(dataset):except:return 0- def score_column(col_name, keywords, role_type, num_candidates):+ def score_column(+ col_name,+ keywords,+ role_type,+ num_candidates,+ apply_shadowing = True,+ ):"""Score how likely a column is to be a given role."""- if not has_keyword(col_name, keywords):+ if not has_keyword(col_name, keywords, apply_shadowing = apply_shadowing):return 0score = 0@@ -360,6 +381,24 @@ def detect_custom_format_heuristic(dataset):if score > 0:user_candidates.append((col, score))+ if not user_candidates and not any(col != assistant_col for col in user_potential):+ # has_keyword drops "context" from user_potential because "text" only matches+ # inside it. When nothing else can hold the user turn, that column is a better+ # user turn than an assistant-worded leftover.+ shadowed_potential = [+ col+ for col in content_columns+ if col not in user_potential and has_keyword(col, user_words, apply_shadowing = False)+ ]+ for col in shadowed_potential:+ if col == assistant_col:+ continue+ score = score_column(+ col, user_words, "user", len(shadowed_potential), apply_shadowing = False+ )+ if score > 0:+ user_candidates.append((col, score))+if user_candidates:user_candidates.sort(key = lambda x: x[1], reverse = True)user_col = user_candidates[0][0]