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
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
export const CHAT_IMAGE_MIMES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
"image/heic",
"image/heif",
"image/avif",
"image/bmp",
"image/tiff",
];
const CONVERTED_IMAGE_TYPES: Record<string, string> = {
heic: "image/jpeg",
heif: "image/jpeg",
avif: "image/png",
bmp: "image/png",
tif: "image/png",
tiff: "image/png",
};
const CONVERTED_IMAGE_EXTENSIONS = Object.keys(CONVERTED_IMAGE_TYPES).map(
(extension) => `.${extension}`,
);
export const CHAT_IMAGE_EXTENSIONS = [
".jpg,.jpeg,.png,.webp,.gif",
...CONVERTED_IMAGE_EXTENSIONS,
].join(",");
export const CHAT_IMAGE_ACCEPT = [
...CHAT_IMAGE_MIMES,
...CONVERTED_IMAGE_EXTENSIONS,
].join(",");
function imageKind(file: { name: string; type: string }): string | null {
const mime = file.type.toLowerCase();
if (CHAT_IMAGE_MIMES.includes(mime)) {
return mime.slice("image/".length);
}
const extension = file.name.toLowerCase().split(".").pop() ?? "";
return Object.hasOwn(CONVERTED_IMAGE_TYPES, extension) ? extension : null;
}
export function isChatImageFile(file: { name: string; type: string }): boolean {
return imageKind(file) !== null;
}
export function convertedImageType(file: {
name: string;
type: string;
}): string | null {
const kind = imageKind(file);
return kind === null ? null : (CONVERTED_IMAGE_TYPES[kind] ?? null);
}
const MAX_CHAT_IMAGE_BYTES = 20 * 1024 * 1024;
export function getChatImageSizeError(file: {
name: string;
size: number;
}): string | null {
return file.size > MAX_CHAT_IMAGE_BYTES
? `${file.name} is over the 20 MB image limit.`
: null;
}
// iOS Safari, the one browser that decodes HEIC, refuses canvases past 16.7 megapixels.
const MAX_CONVERTED_IMAGE_PIXELS = 16_777_216;
export async function normalizeChatImage(file: File): Promise<File> {
const type = convertedImageType(file);
if (type === null) {
return file;
}
const url = URL.createObjectURL(file);
try {
const image = new Image();
image.src = url;
try {
await image.decode();
} catch {
throw new Error(
`This app can't read ${file.name}. Convert it to JPEG or PNG and attach it again.`,
);
}
const scale = Math.min(
1,
Math.sqrt(
MAX_CONVERTED_IMAGE_PIXELS / (image.naturalWidth * image.naturalHeight),
),
);
const canvas = document.createElement("canvas");
canvas.width = Math.max(1, Math.floor(image.naturalWidth * scale));
canvas.height = Math.max(1, Math.floor(image.naturalHeight * scale));
const context = canvas.getContext("2d");
if (!context) {
throw new Error(`Could not convert ${file.name}.`);
}
if (type === "image/jpeg") {
context.fillStyle = "#fff";
context.fillRect(0, 0, canvas.width, canvas.height);
}
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const blob = await new Promise<Blob | null>((resolve) =>
canvas.toBlob(resolve, type, 0.92),
);
if (!blob) {
throw new Error(`Could not convert ${file.name}.`);
}
if (blob.size > MAX_CHAT_IMAGE_BYTES) {
throw new Error(`${file.name} is over 20 MB once converted.`);
}
const extension = type === "image/jpeg" ? ".jpg" : ".png";
return new File([blob], file.name.replace(/\.[^.]*$/, "") + extension, {
type,
lastModified: file.lastModified,
});
} finally {
URL.revokeObjectURL(url);
}
}