Skip to content

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

31.2 KB
Raw Blame
# 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 re
from typing import Any, Optional

from utils.datasets.cells import text_cell_check, unwrap_message_column
from utils.datasets.format_detection import (
    detect_dataset_format,
    detect_video_columns,
    detect_video_structure,
    has_message_prompt_completion,
    video_sample,
    is_plain_text_dataset,
)
from utils.datasets.pretokenized import PRETOKENIZED_FORMAT, is_pretokenized_dataset


def _first_row(dataset) -> Optional[dict]:
    try:
        row = next(iter(dataset))
    except StopIteration:
        return None
    return row if isinstance(row, dict) else None


def _column_names(dataset, sample: Optional[dict] = None) -> list[str]:
    names = getattr(dataset, "column_names", None)
    if names is not None:
        return list(names)
    return list((sample or {}).keys())


def _keyword_in_column(keyword: str, col_name: str) -> bool:
    return re.search(r"\b" + re.escape(keyword) + r"\b", col_name, re.IGNORECASE) is not None


def detect_custom_format_heuristic(dataset):
    sample = _first_row(dataset)
    if sample is None:
        return None
    all_columns = list(sample.keys())
    mapping = {}
    assistant_words = [
        "output",
        "answer",
        "response",
        "assistant",
        "completion",
        "expected",
        "recommendation",
        "reply",
        "result",
        "target",
        "solution",
        "explanation",
        "solve",
    ]
    user_words_high_priority = [
        "input",
        "question",
        "query",
        "prompt",
        "instruction",
        "request",
        "snippet",
        "user",
        "text",
        "problem",
        "exercise",
    ]
    user_words_low_priority = ["task"]
    user_words = user_words_high_priority + user_words_low_priority
    system_words = [
        "system",
        "context",
        "description",
        "persona",
        "role",
        "template",
        "task",
    ]
    # Only pair today: "text" inside "context".
    role_words = assistant_words + user_words + system_words
    metadata_exact_match = {
        "id",
        "idx",
        "index",
        "key",
        "timestamp",
        "date",
        "metadata",
        "source",
        "kind",
        "type",
        "category",
        "score",
        "label",
        "tag",
        "inference_mode",
    }
    metadata_prefix_patterns = [
        "problem_type",
        "problem_source",
        "generation_model",
        "pass_rate",
    ]
    priority_patterns = {
        "generated": 100,
        "gen_": 90,
        "model_": 80,
        "predicted": 70,
        "completion": 60,
    }

    def has_keyword(
        col_name,
        keywords,
        apply_shadowing = True,
    ):
        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:
                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 False

    def is_metadata(col_name):
        col_lower = col_name.lower()
        if col_lower in metadata_exact_match or col_lower in metadata_prefix_patterns:
            return True
        for pattern in metadata_prefix_patterns:
            if col_lower.startswith(pattern.split("_")[0] + "_") and col_lower != pattern:
                if "_" in col_lower:
                    prefix = col_lower.split("_")[0]
                    if prefix in ["generation", "pass", "inference"]:
                        return True
        return len(col_lower) <= 2 and col_lower not in ["qa", "q", "a"]

    def get_priority_score(col_name):
        col_lower = col_name.lower()
        return sum(score for pattern, score in priority_patterns.items() if pattern in col_lower)

    def get_content_length(col_name):
        try:
            return len(str(sample[col_name])) if sample.get(col_name) else 0
        except Exception:
            return 0

    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 0
        score = 10
        if role_type == "user":
            col_lower = col_name.lower()
            if "task" in col_lower and not any(kw in col_lower for kw in user_words_high_priority):
                score -= 15
        score += get_priority_score(col_name)
        if role_type in ["assistant", "user"]:
            avg_length = get_content_length(col_name)
            if num_candidates > 1:
                if avg_length > 1000:
                    score += 50
                elif avg_length > 200:
                    score += 30
                elif avg_length > 50:
                    score += 10
                elif avg_length < 50:
                    score -= 20
            else:
                if avg_length > 1000:
                    score += 50
                elif avg_length > 200:
                    score += 30
                elif avg_length > 50:
                    score += 10
        return score

    context_words = {
        "input",
        "passage",
        "text",
        "document",
        "article",
        "contract",
        "evidence",
        "story",
        "paragraph",
        "definition",
        "background",
        "knowledge",
        "choices",
        "options",
    }

    meta_tokens = {"id", "ids", "idx", "type", "category", "label", "title", "tag"}

    def name_tokens(col_name):
        separated_name = re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", col_name)
        return set(re.findall(r"[a-z]+", separated_name.lower()))

    def is_non_metadata_column(col_name):
        return not meta_tokens & name_tokens(col_name)

    def is_prompt_content_column(col_name):
        return is_non_metadata_column(col_name) and not isinstance(
            sample.get(col_name), (bool, int, float)
        )

    def is_context_column(col_name):
        tokens = name_tokens(col_name)
        return (
            any(token in context_words or token[:-1] in context_words for token in tokens)
            and is_prompt_content_column(col_name)
            and not has_keyword(col_name, assistant_words)
        )

    content_columns = [col for col in all_columns if not is_metadata(col)]
    system_named = [
        col
        for col in content_columns
        if has_keyword(col, ["system"])
        and is_prompt_content_column(col)
        and not has_keyword(col, assistant_words)
    ]
    assistant_potential = [col for col in content_columns if has_keyword(col, assistant_words)]
    user_potential = [
        col
        for col in content_columns
        if col not in system_named and is_non_metadata_column(col) and has_keyword(col, user_words)
    ]
    assistant_candidates = [
        (col, score)
        for col in assistant_potential
        if (score := score_column(col, assistant_words, "assistant", len(assistant_potential))) > 0
    ]
    if assistant_candidates:
        assistant_candidates.sort(key = lambda item: item[1], reverse = True)
        assistant_col = assistant_candidates[0][0]
        mapping[assistant_col] = "assistant"
    else:
        assistant_col = None

    user_candidates = []
    for col in user_potential:
        if col == assistant_col:
            continue
        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):
        # "text" only matches inside "context"; prefer it over an assistant-worded leftover.
        shadowed_potential = [
            col
            for col in content_columns
            if col not in system_named
            and col not in user_potential
            and is_non_metadata_column(col)
            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]
        mapping[user_col] = "user"
    else:
        user_col = None

    remaining_columns = [col for col in content_columns if col not in mapping]
    if user_col is not None and is_context_column(user_col):
        for col in remaining_columns:
            if (
                has_keyword(col, user_words_high_priority)
                and isinstance(sample.get(col), str)
                and not meta_tokens & name_tokens(col)
                and not is_context_column(col)
                and not has_keyword(col, assistant_words)
            ):
                del mapping[user_col]
                mapping[col] = "user"
                remaining_columns = [c for c in remaining_columns if c != col] + [user_col]
                user_col = col
                break

    non_task_system_words = [word for word in system_words if word != "task"]
    system_tiers = [
        lambda col: col in system_named,
        lambda col: has_keyword(col, non_task_system_words) and is_prompt_content_column(col),
        lambda col: user_col is not None and is_context_column(col),
        lambda col: has_keyword(col, system_words) and is_prompt_content_column(col),
    ]
    system_col = next(
        (col for tier in system_tiers for col in remaining_columns if tier(col)), None
    )
    if system_col:
        mapping[system_col] = "system"
        remaining_columns = [col for col in remaining_columns if col != system_col]
    if remaining_columns:
        remaining_col = remaining_columns[0]
        if (
            user_col is None
            and has_keyword(remaining_col, user_words)
            and not has_keyword(remaining_col, assistant_words + system_words)
            and is_non_metadata_column(remaining_col)
        ):
            mapping[remaining_col] = "user"

    has_user = any(role == "user" for role in mapping.values())
    has_assistant = any(role == "assistant" for role in mapping.values())
    if not has_user:
        for col in remaining_columns[1:]:
            if (
                col not in mapping
                and is_prompt_content_column(col)
                and not has_keyword(col, assistant_words + system_words)
            ):
                mapping[col] = "user"
                has_user = True
                break
    if system_col is None:
        for col in remaining_columns:
            if col not in mapping and is_context_column(col):
                mapping[col] = "system"
                break
    if has_message_prompt_completion(dataset):
        mapping = {
            col: role
            for col, role in mapping.items()
            if role == "system" and col not in {"messages", "conversations", "texts"}
        }
        mapping.update({"prompt": "user", "completion": "assistant"})
        has_user = has_assistant = True
    return mapping if has_user and has_assistant else None


_AUDIO_EXTENSIONS = (
    ".wav",
    ".mp3",
    ".flac",
    ".ogg",
    ".opus",
    ".m4a",
    ".aac",
    ".wma",
    ".webm",
)


def _is_audio_value(value) -> bool:
    if value is None:
        return False
    if isinstance(value, dict):
        if "array" in value and "sampling_rate" in value:
            return True
        if "bytes" in value or "path" in value:
            path = value.get("path") or ""
            return isinstance(path, str) and any(
                path.lower().endswith(ext) for ext in _AUDIO_EXTENSIONS
            )
    return False


def _has_image_header(data: bytes) -> bool:
    if len(data) < 4:
        return False
    return (
        data[:2] == b"\xff\xd8"
        or data[:4] == b"\x89PNG"
        or data[:3] == b"GIF"
        or (data[:4] == b"RIFF" and len(data) >= 12 and data[8:12] == b"WEBP")
        or data[:2] == b"BM"
    )


def _is_decoded_image_value(value) -> bool:
    try:
        from PIL.Image import Image as PILImage
        if isinstance(value, PILImage):
            return True
    except ImportError:
        pass
    return False


def _is_image_value(value) -> bool:
    if value is None:
        return False
    if _is_decoded_image_value(value):
        return True
    if isinstance(value, dict):
        if "array" in value and "sampling_rate" in value:
            return False
        if "bytes" in value and "path" in value:
            path = value.get("path") or ""
            if isinstance(path, str) and any(
                path.lower().endswith(ext) for ext in _AUDIO_EXTENSIONS
            ):
                return False
            return True
    if isinstance(value, (bytes, bytearray)):
        return _has_image_header(value)
    image_exts = (".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp", ".tiff", ".svg")
    if isinstance(value, str) and len(value) < 1000:
        lower = value.strip().lower()
        if lower.startswith(("http://", "https://")):
            return any(lower.split("?")[0].endswith(ext) for ext in image_exts)
        return any(lower.endswith(ext) for ext in image_exts)
    return False


def _is_image_list_item(value) -> bool:
    if isinstance(value, (dict, bytes, bytearray)):
        return False
    if isinstance(value, str):
        normalized = value.strip().lower()
        if normalized.startswith(("http://", "https://")) or normalized.endswith(".svg"):
            return False
    return _is_image_value(value)


def _holds_images(value) -> bool:
    if not isinstance(value, list) or not value:
        return False
    if all(_is_image_list_item(item) for item in value):
        return True
    for item in value:
        content = item.get("content") if isinstance(item, dict) else None
        if isinstance(content, list) and any(
            isinstance(part, dict) and part.get("type") == "image" and part.get("image") is not None
            for part in content
        ):
            return True
    return False


def detect_multimodal_dataset(dataset):
    sample = _first_row(dataset)
    if sample is None:
        return {
            "is_image": False,
            "is_video": False,
            "multimodal_columns": [],
            "modality_types": [],
            "is_audio": False,
            "audio_columns": [],
            "detected_audio_column": None,
            "detected_text_column": None,
            "detected_speaker_column": None,
        }
    column_names = list(sample.keys())
    image_keywords = [
        "image",
        "img",
        "pixel",
        "jpg",
        "jpeg",
        "png",
        "webp",
        "bmp",
        "gif",
        "tiff",
        "svg",
        "photo",
        "pic",
        "picture",
        "visual",
        "file_name",
        "filename",
    ]
    audio_keywords = ["audio", "speech", "wav", "waveform", "sound"]
    multimodal_columns = []
    audio_columns = []
    modality_types = set()
    for col_name in column_names:
        if any(_keyword_in_column(keyword, col_name) for keyword in image_keywords):
            multimodal_columns.append(col_name)
            modality_types.add("image")
    for col_name in column_names:
        value = sample[col_name]
        if col_name not in multimodal_columns and (_is_image_value(value) or _holds_images(value)):
            multimodal_columns.append(col_name)
            modality_types.add("image")
    for col_name in column_names:
        if any(_keyword_in_column(keyword, col_name) for keyword in audio_keywords):
            audio_columns.append(col_name)
            modality_types.add("audio")
    for col_name in column_names:
        if col_name not in audio_columns and _is_audio_value(sample[col_name]):
            audio_columns.append(col_name)
            modality_types.add("audio")
    if audio_columns:
        multimodal_columns = [col for col in multimodal_columns if col not in set(audio_columns)]
    video_columns = detect_video_columns(video_sample(dataset, sample))
    audio_columns = [col for col in audio_columns if col not in video_columns]
    if video_columns:
        multimodal_columns = [col for col in multimodal_columns if col not in video_columns]
        multimodal_columns += video_columns
        modality_types.add("video")

    detected_text_col = None
    if audio_columns:
        # Two passes so a real "transcript" beats an instruction-like "prompt".
        transcript_names = ("text", "sentence", "transcript", "transcription", "label")
        # TTS sets name the line to speak: SparkTTS "prompt", LJSpeech "normalized_text".
        fallback_names = ("prompt", "normalized_text")
        for candidates in (transcript_names, fallback_names):
            for col_name in column_names:
                if col_name.lower() in candidates:
                    detected_text_col = col_name
                    break
            if detected_text_col:
                break
    detected_speaker_col = None
    if audio_columns:
        for col_name in column_names:
            if col_name.lower() in ["source", "speaker", "speaker_id"]:
                detected_speaker_col = col_name
                break
    return {
        "is_image": len(multimodal_columns) > 0,
        "is_video": bool(video_columns),
        "multimodal_columns": multimodal_columns,
        "modality_types": list(modality_types),
        "is_audio": len(audio_columns) > 0,
        "audio_columns": audio_columns,
        "detected_audio_column": audio_columns[0] if audio_columns else None,
        "detected_text_column": detected_text_col,
        "detected_speaker_column": detected_speaker_col,
    }


def detect_vlm_dataset_structure(dataset):
    sample = _first_row(dataset)
    if sample is None:
        return {
            "format": "unknown",
            "needs_conversion": None,
            "image_column": None,
            "text_column": None,
            "messages_column": None,
        }
    video_structure = detect_video_structure(video_sample(dataset, sample))
    if video_structure is not None:
        return video_structure
    column_names = set(sample.keys())
    is_text = text_cell_check(dataset)
    if "messages" in column_names:
        messages = sample["messages"]
        message_rows = messages if isinstance(messages, list) else []
        image_parts = [
            part
            for message in message_rows
            if isinstance(message, dict) and isinstance(message.get("content"), list)
            for part in message["content"]
            if isinstance(part, dict) and part.get("type") == "image"
        ]
        if image_parts:
            embedded_images = [
                part.get("image") for part in image_parts if part.get("image") is not None
            ]
            if embedded_images:
                if len(embedded_images) == len(image_parts) and all(
                    _is_decoded_image_value(image) for image in embedded_images
                ):
                    return {
                        "format": "vlm_messages",
                        "needs_conversion": False,
                        "messages_column": "messages",
                        "image_column": None,
                        "text_column": None,
                    }
            else:
                images = sample.get("images")
                if (
                    isinstance(images, list)
                    and images
                    and all(_is_image_list_item(image) for image in images)
                ):
                    return {
                        "format": "vlm_messages_llava",
                        "needs_conversion": True,
                        "messages_column": "messages",
                        "image_column": "images",
                        "text_column": None,
                    }

    for chat_col in ("conversations", "messages"):
        if chat_col not in column_names:
            continue
        chat_data = sample[chat_col]
        if not isinstance(chat_data, list) or not chat_data:
            continue
        has_image_placeholder = any(
            "<image>" in str(message.get("value", "") or message.get("content", ""))
            for message in chat_data
            if isinstance(message, dict)
        )
        if not has_image_placeholder:
            continue
        image_col = next(
            (
                col
                for col in column_names
                if col != chat_col
                and (_keyword_in_column("image", col) or _keyword_in_column("img", col))
            ),
            None,
        )
        if image_col:
            return {
                "format": "sharegpt_with_images",
                "needs_conversion": True,
                "image_column": image_col,
                "text_column": None,
                "messages_column": chat_col,
            }

    metadata_suffixes = (
        "_id",
        "_url",
        "_name",
        "_filename",
        "_uri",
        "_link",
        "_key",
        "_index",
    )
    metadata_prefixes = (
        "id_",
        "url_",
        "name_",
        "filename_",
        "uri_",
        "link_",
        "key_",
        "index_",
    )
    image_keywords = [
        "image",
        "img",
        "photo",
        "picture",
        "pic",
        "visual",
        "scan",
        "file_name",
        "filename",
    ]
    text_keywords = [
        "text",
        "caption",
        "captions",
        "description",
        "answer",
        "output",
        "response",
        "label",
    ]

    def is_metadata_column(col_name):
        lower = col_name.lower()
        return any(lower.endswith(suffix) for suffix in metadata_suffixes) or any(
            lower.startswith(prefix) for prefix in metadata_prefixes
        )

    image_candidates = []
    for col in column_names:
        value = sample[col]
        if any(_keyword_in_column(keyword, col) for keyword in image_keywords) or _is_image_value(
            value
        ):
            if hasattr(value, "size") and hasattr(value, "mode"):
                score = 100
            elif isinstance(value, dict) and ("bytes" in value or "path" in value):
                score = 75
            elif isinstance(value, str) and is_text(col, value):
                score = (
                    55
                    if is_metadata_column(col)
                    else 70
                    if value.startswith(("http://", "https://"))
                    else 50
                )
            else:
                score = 0
            if score > 0:
                image_candidates.append((col, score))
    image_candidates.sort(key = lambda item: item[1], reverse = True)

    text_candidates = []
    for col in column_names:
        if is_metadata_column(col) or not any(
            _keyword_in_column(keyword, col) for keyword in text_keywords
        ):
            continue
        value = sample[col]
        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))
    text_candidates.sort(key = lambda item: item[1], reverse = True)

    found_image = image_candidates[0][0] if image_candidates else None
    found_text = text_candidates[0][0] if text_candidates else None
    if found_image and found_text:
        return {
            "format": "simple_image_text",
            "needs_conversion": True,
            "image_column": found_image,
            "text_column": found_text,
            "messages_column": None,
        }
    return {
        "format": "unknown",
        "needs_conversion": None,
        "image_column": found_image,
        "text_column": found_text,
        "messages_column": None,
    }


def check_dataset_format(dataset, is_vlm: bool = False) -> dict:
    sample = _first_row(dataset)
    columns = _column_names(dataset, sample)
    multimodal_info = detect_multimodal_dataset(dataset)
    is_audio = multimodal_info.get("is_audio", False)
    audio_fields = {
        "is_audio": is_audio,
        "detected_audio_column": multimodal_info.get("detected_audio_column"),
        "detected_speaker_column": multimodal_info.get("detected_speaker_column"),
    }

    if is_plain_text_dataset(dataset):
        # Before the VLM branch: text-only data trains as raw text on a vision model too.
        # Media flags off: a name match (a "speech" column) would route an audio model to its audio path.
        return {
            "requires_manual_mapping": False,
            "detected_format": "raw",
            "columns": columns,
            "suggested_mapping": None,
            "detected_image_column": None,
            "detected_text_column": None,
            "is_image": False,
            "multimodal_columns": None,
            "is_audio": False,
            "detected_audio_column": None,
            "detected_speaker_column": None,
        }

    if is_vlm:
        vlm_structure = detect_vlm_dataset_structure(dataset)
        requires_mapping = vlm_structure["format"] == "unknown"
        warning = None
        if requires_mapping:
            missing = []
            if not (vlm_structure.get("image_column") or vlm_structure.get("video_column")):
                missing.append("image")
            if not vlm_structure.get("text_column"):
                missing.append("text")
            if missing:
                warning = (
                    f"Could not auto-detect {' or '.join(missing)} column. "
                    "Please assign image and text columns manually."
                )
        return {
            "requires_manual_mapping": requires_mapping,
            "detected_format": vlm_structure["format"],
            "columns": columns,
            "suggested_mapping": None,
            "detected_image_column": vlm_structure.get("image_column"),
            "detected_text_column": vlm_structure.get("text_column"),
            "is_image": multimodal_info["is_image"],
            "multimodal_columns": multimodal_info.get("multimodal_columns"),
            "warning": warning,
            **audio_fields,
        }

    if is_audio:
        detected_audio = multimodal_info.get("detected_audio_column")
        detected_text = multimodal_info.get("detected_text_column")
        return {
            "requires_manual_mapping": not detected_audio or not detected_text,
            "detected_format": "audio",
            "columns": columns,
            "suggested_mapping": None,
            "detected_image_column": None,
            "detected_text_column": detected_text,
            "is_image": False,
            "multimodal_columns": multimodal_info.get("audio_columns"),
            **audio_fields,
        }

    detected = detect_dataset_format(dataset)
    if detected["format"] == "unknown":
        heuristic_mapping = detect_custom_format_heuristic(dataset)
        if heuristic_mapping:
            return {
                "requires_manual_mapping": False,
                "detected_format": "custom_heuristic",
                "columns": columns,
                "suggested_mapping": heuristic_mapping,
                "detected_image_column": None,
                "detected_text_column": None,
                "is_image": multimodal_info["is_image"],
                "multimodal_columns": multimodal_info.get("multimodal_columns"),
                **audio_fields,
            }
        if is_pretokenized_dataset(dataset):
            return {
                "requires_manual_mapping": False,
                "detected_format": PRETOKENIZED_FORMAT,
                "columns": columns,
                "suggested_mapping": None,
                "detected_image_column": None,
                "detected_text_column": None,
                "is_image": multimodal_info["is_image"],
                "multimodal_columns": multimodal_info.get("multimodal_columns"),
                **audio_fields,
            }
        return {
            "requires_manual_mapping": True,
            "detected_format": "unknown",
            "columns": columns,
            "suggested_mapping": None,
            "detected_image_column": None,
            "detected_text_column": None,
            "is_image": multimodal_info["is_image"],
            "multimodal_columns": multimodal_info.get("multimodal_columns"),
            "warning": (
                f"Could not auto-detect column roles for columns: {columns}. "
                "Please assign roles manually, or use AI Assist."
            ),
            **audio_fields,
        }

    return {
        "requires_manual_mapping": False,
        "detected_format": detected["format"],
        "columns": columns,
        "suggested_mapping": None,
        "detected_image_column": None,
        "detected_text_column": None,
        "chat_column": detected.get("chat_column"),
        "is_image": multimodal_info["is_image"],
        "multimodal_columns": multimodal_info.get("multimodal_columns"),
        **audio_fields,
    }


# Aliases standardize_data_formats accepts; keys are normalised, so use _normalize_role_alias.
_ROLE_MAP = {
    "human": "user",
    "user": "user",
    "input": "user",
    "gpt": "assistant",
    "assistant": "assistant",
    "output": "assistant",
    "system": "system",
}


def _normalize_role_alias(role: Any) -> str:
    """Match aliases the way the trainer does: `role.strip().lower()`, as
    `standardize_data_formats` compares them (unslothai/unsloth-zoo#1225)."""
    if role is None:
        return ""
    return str(role).strip().lower()


def _standardize_sharegpt_row(row: dict[str, Any], chat_column: str) -> dict[str, Any]:
    chat_data = row.get(chat_column)
    if not isinstance(chat_data, list):
        return row
    messages = []
    for message in chat_data:
        if not isinstance(message, dict):
            continue
        role = message.get("role") or message.get("from")
        content = message.get("content")
        if content is None:
            content = message.get("value")
        normalized = _normalize_role_alias(role)
        new_message = {key: value for key, value in message.items() if key not in ("from", "value")}
        new_message.update(
            {
                # Unknown alias shown as written; blank falls back to "user", as before.
                "role": _ROLE_MAP.get(normalized, str(role)) if normalized else "user",
                "content": "" if content is None and not message.get("tool_calls") else content,
            }
        )
        messages.append(new_message)
    return {chat_column: messages}


def format_dataset_preview(dataset):
    detected = detect_dataset_format(dataset)
    chat_column = detected.get("chat_column")
    if (
        detected.get("format") not in ("chatml", "sharegpt")
        or not isinstance(chat_column, str)
        or not hasattr(dataset, "map")
    ):
        return dataset
    dataset = unwrap_message_column(dataset, chat_column)
    if detected["format"] == "sharegpt":
        return dataset.map(lambda row: _standardize_sharegpt_row(row, chat_column))
    return dataset