mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Compress too large images for Google captions
This commit is contained in:
@ -22,15 +22,19 @@ export async function getMultimodalCaption(base64Img, prompt) {
|
|||||||
throw new Error('MakerSuite API key is not set.');
|
throw new Error('MakerSuite API key is not set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenRouter has a payload limit of ~2MB
|
// OpenRouter has a payload limit of ~2MB. Google is 4MB, but we love democracy.
|
||||||
|
const isGoogle = extension_settings.caption.multimodal_api === 'google';
|
||||||
const base64Bytes = base64Img.length * 0.75;
|
const base64Bytes = base64Img.length * 0.75;
|
||||||
const compressionLimit = 2 * 1024 * 1024;
|
const compressionLimit = 2 * 1024 * 1024;
|
||||||
if (extension_settings.caption.multimodal_api === 'openrouter' && base64Bytes > compressionLimit) {
|
if (['google', 'openrouter'].includes(extension_settings.caption.multimodal_api) && base64Bytes > compressionLimit) {
|
||||||
const maxSide = 1024;
|
const maxSide = 1024;
|
||||||
base64Img = await createThumbnail(base64Img, maxSide, maxSide, 'image/jpeg');
|
base64Img = await createThumbnail(base64Img, maxSide, maxSide, 'image/jpeg');
|
||||||
|
|
||||||
|
if (isGoogle) {
|
||||||
|
base64Img = base64Img.split(',')[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isGoogle = extension_settings.caption.multimodal_api === 'google';
|
|
||||||
const apiResult = await fetch(`/api/${isGoogle ? 'google' : 'openai'}/caption-image`, {
|
const apiResult = await fetch(`/api/${isGoogle ? 'google' : 'openai'}/caption-image`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: getRequestHeaders(),
|
headers: getRequestHeaders(),
|
||||||
|
@ -1030,6 +1030,11 @@ export function loadFileToDocument(url, type) {
|
|||||||
* @returns {Promise<string>} A promise that resolves to the thumbnail data URL.
|
* @returns {Promise<string>} A promise that resolves to the thumbnail data URL.
|
||||||
*/
|
*/
|
||||||
export function createThumbnail(dataUrl, maxWidth, maxHeight, type = 'image/jpeg') {
|
export function createThumbnail(dataUrl, maxWidth, maxHeight, type = 'image/jpeg') {
|
||||||
|
// Someone might pass in a base64 encoded string without the data URL prefix
|
||||||
|
if (!dataUrl.includes('data:')) {
|
||||||
|
dataUrl = `data:image/jpeg;base64,${dataUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.src = dataUrl;
|
img.src = dataUrl;
|
||||||
|
Reference in New Issue
Block a user