Skip to content

server : (webui) rename has_multimodal --> modalities #13393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified tools/server/public/index.html.gz
Binary file not shown.
68 changes: 64 additions & 4 deletions tools/server/webui/src/components/useChatExtraContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,26 @@ export function useChatExtraContext(): ChatExtraContextApi {
break;
}

if (mimeType.startsWith('image/') && mimeType !== 'image/svg+xml') {
if (!serverProps?.has_multimodal) {
if (mimeType.startsWith('image/')) {
if (!serverProps?.modalities?.vision) {
toast.error('Multimodal is not supported by this server or model.');
break;
}
const reader = new FileReader();
reader.onload = (event) => {
reader.onload = async (event) => {
if (event.target?.result) {
let base64Url = event.target.result as string;

if (mimeType === 'image/svg+xml') {
// Convert SVG to PNG
base64Url = await svgBase64UrlToPngDataURL(base64Url);
}

addItems([
{
type: 'imageFile',
name: file.name,
base64Url: event.target.result as string,
base64Url,
},
]);
}
Expand Down Expand Up @@ -172,3 +179,56 @@ export function isLikelyNotBinary(str: string): boolean {
const ratio = suspiciousCharCount / sampleLength;
return ratio <= options.suspiciousCharThresholdRatio;
}

// WARN: vibe code below
// Converts a Base64URL encoded SVG string to a PNG Data URL using browser Canvas API.
function svgBase64UrlToPngDataURL(base64UrlSvg: string): Promise<string> {
const backgroundColor = 'white'; // Default background color for PNG

return new Promise((resolve, reject) => {
try {
const img = new Image();

img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

if (!ctx) {
reject(new Error('Failed to get 2D canvas context.'));
return;
}

// Use provided dimensions or SVG's natural dimensions, with fallbacks
// Fallbacks (e.g., 300x300) are for SVGs without explicit width/height
// or when naturalWidth/Height might be 0 before full processing.
const targetWidth = img.naturalWidth || 300;
const targetHeight = img.naturalHeight || 300;

canvas.width = targetWidth;
canvas.height = targetHeight;

if (backgroundColor) {
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}

ctx.drawImage(img, 0, 0, targetWidth, targetHeight);
resolve(canvas.toDataURL('image/png'));
};

img.onerror = () => {
reject(
new Error('Failed to load SVG image. Ensure the SVG data is valid.')
);
};

// Load SVG string into an Image element
img.src = base64UrlSvg;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const errorMessage = `Error converting SVG to PNG: ${message}`;
toast.error(errorMessage);
reject(new Error(errorMessage));
}
});
}
4 changes: 3 additions & 1 deletion tools/server/webui/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export interface LlamaCppServerProps {
build_info: string;
model_path: string;
n_ctx: number;
has_multimodal: boolean;
modalities?: {
vision: boolean;
};
// TODO: support params
}