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.

4.1 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

import { useState } from "react";

export type FamilyOverrideArtifactKind = "diffusers_pipeline" | "diffusers_modular_pipeline";

const LABELS: Readonly<Record<string, string>> = {
  "flux.1": "FLUX.1",
  "flux.2-klein": "FLUX.2 Klein",
  "flux.2-dev": "FLUX.2 Dev",
  "flux.3-klein": "FLUX.3 Klein",
  "flux.3-klein-base": "FLUX.3 Klein Base",
  "flux.1-kontext": "FLUX.1 Kontext",
  "qwen-image": "Qwen Image",
  "qwen-image-2.1": "Qwen Image 2.1",
  "qwen-image-edit": "Qwen Image Edit",
  "qwen-image-layered": "Qwen Image Layered",
  "z-image": "Z-Image",
  "krea-2": "Krea 2",
  "lumina-2": "Lumina 2",
  "hunyuanimage-2.1": "HunyuanImage 2.1",
  "hidream-i1": "HiDream-I1",
  "ideogram-4": "Ideogram 4",
  "moss-image": "MOSS-Image",
  "minimax-h3-image": "MiniMax-H3-Image",
  chroma: "Chroma",
  anima: "Anima",
  sdxl: "SDXL",
  "minimax-h3": "MiniMax-H3",
  "ltx-2": "LTX-2",
  "ltx-2.5": "LTX-2.5",
  "wan2.2-ti2v-5b": "Wan2.2 TI2V 5B",
  "wan2.2-t2v-a14b": "Wan2.2 T2V A14B",
  "wan2.1-t2v-1.3b": "Wan2.1 T2V 1.3B",
  "wan2.1-t2v-14b": "Wan2.1 T2V 14B",
  "hunyuanvideo-1.5": "HunyuanVideo 1.5 (480p)",
  "hunyuanvideo-1.5-720p": "HunyuanVideo 1.5 (720p)",
  "cosmos3-nano": "Cosmos3-Nano",
  "cosmos3-edge": "Cosmos3-Edge",
};

export const FAMILY_OVERRIDE_HINT =
  "Architecture family. Auto detects it from the repository or pipeline metadata. Choose one only for a custom Diffusers pipeline whose metadata does not identify a supported family.";

export function familyOverrideOptions(supported: readonly string[] | null | undefined): [string, string][] {
  const names = [...new Set(supported ?? [])].filter(Boolean);
  return [["auto", "Auto (detect)"], ...names.map((n): [string, string] => [n, LABELS[n] ?? n])];
}

export function explicitFamily(value: unknown): string | undefined {
  const family = typeof value === "string" ? value.trim() : "";
  return family && family.toLowerCase() !== "auto" ? family : undefined;
}

/** A family choice admits only a structurally valid row whose task is unknown; dual roots satisfy either loader. */
export function taskOpaqueArtifactSupportsFamilyOverride(
  task: string | null | undefined,
  artifactKind: string | null | undefined,
  requiredKind: FamilyOverrideArtifactKind | null | undefined,
): boolean {
  return (
    !task?.trim() &&
    Boolean(requiredKind) &&
    (artifactKind === requiredKind || artifactKind === "diffusers_dual_pipeline")
  );
}

/** Restore the selector from the canonical family that engaged, not a request alias. */
export function resolvedFamilyOverrideSelection(
  control: { source?: "auto" | "explicit"; value?: unknown; requested?: unknown } | null | undefined,
): string | undefined {
  if (control?.source === "auto") return "auto";
  for (const v of [control?.value, control?.requested]) {
    if (typeof v === "string" && v.trim()) return v;
  }
  return undefined;
}

export function familyOverrideArtifactKind(
  familyOverride: string | null | undefined,
  modularFamilies?: readonly string[] | null,
): FamilyOverrideArtifactKind | undefined {
  const family = explicitFamily(familyOverride)?.toLowerCase();
  if (!family) return undefined;
  return modularFamilies?.some((f) => f.trim().toLowerCase() === family)
    ? "diffusers_modular_pipeline"
    : "diffusers_pipeline";
}

/** A pinned snapshot is labelled by its logical id in the selector. */
export function useFamilyOverride(
  status: { loaded: boolean; repo_id: string | null; display_repo_id?: string | null; modular_families?: string[] } | null,
  supportedFamilies?: readonly string[],
) {
  const [familyOverride, setFamilyOverride] = useState("auto");
  return {
    familyOverride,
    setFamilyOverride,
    familySelect: {
      label: "Family",
      hint: FAMILY_OVERRIDE_HINT,
      value: familyOverride,
      onValueChange: setFamilyOverride,
      options: familyOverrideOptions(supportedFamilies),
    },
    opaqueKind: familyOverrideArtifactKind(familyOverride, status?.modular_families),
    selectorModelId: status?.loaded && status.repo_id ? (status.display_repo_id ?? status.repo_id) : undefined,
  };
}