Convert dataurl to blob
This commit is contained in:
parent
cc8f99084b
commit
710e866de9
|
|
@ -284,8 +284,19 @@ namespace Prompt {
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const objectUrls = new Map<string, string>();
|
||||||
function stripDataUrlImages(text: string): string {
|
function stripDataUrlImages(text: string): string {
|
||||||
return text.replace(/!\[([^\]]*)\]\(data:[^)]+\)/g, (_, alt) => `[image: ${alt}]`);
|
return text.replace(/!\[([^\]]*)\]\((data:([^;]*);base64,[^)]+)\)/g, (_, alt, dataUrl, type) => {
|
||||||
|
let blobUrl = objectUrls.get(dataUrl);
|
||||||
|
if (!blobUrl) {
|
||||||
|
const b64 = dataUrl.replace(/^data:[^;]*;base64,/, '');
|
||||||
|
const imageData = atob(b64);
|
||||||
|
const blob = new Blob([imageData], { type });
|
||||||
|
blobUrl = URL.createObjectURL(blob);
|
||||||
|
objectUrls.set(dataUrl, blobUrl);
|
||||||
|
}
|
||||||
|
return ``;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function substituteVars(state: AppState, text: string): string {
|
export function substituteVars(state: AppState, text: string): string {
|
||||||
|
|
|
||||||
|
|
@ -478,14 +478,14 @@ export namespace Tools {
|
||||||
if (!appState.imageModel) {
|
if (!appState.imageModel) {
|
||||||
return 'Error: No image model configured';
|
return 'Error: No image model configured';
|
||||||
}
|
}
|
||||||
const { width: defaultWidth, height: defaultHeight, negative_prompt, sampler_name } = appState.imageGenerationSettings;
|
const { width, height, negative_prompt, sampler_name } = appState.imageGenerationSettings;
|
||||||
const response = await LLM.generateImage(appState.connection, {
|
const response = await LLM.generateImage(appState.connection, {
|
||||||
model: appState.imageModel.id,
|
model: appState.imageModel.id,
|
||||||
prompt: args.prompt,
|
prompt: args.prompt,
|
||||||
output_format: 'jpeg',
|
output_format: 'jpeg',
|
||||||
image_settings: {
|
image_settings: {
|
||||||
width: args.width ?? defaultWidth,
|
width,
|
||||||
height: args.height ?? defaultHeight,
|
height,
|
||||||
negative_prompt: negative_prompt || undefined,
|
negative_prompt: negative_prompt || undefined,
|
||||||
sampler_name: sampler_name || undefined,
|
sampler_name: sampler_name || undefined,
|
||||||
},
|
},
|
||||||
|
|
@ -502,8 +502,6 @@ export namespace Tools {
|
||||||
description: 'Generate an image from a text prompt. Format prompt as tags: masterpiece, best quality, ...',
|
description: 'Generate an image from a text prompt. Format prompt as tags: masterpiece, best quality, ...',
|
||||||
parameters: Type.Object({
|
parameters: Type.Object({
|
||||||
prompt: Type.String({ description: 'The image generation prompt' }),
|
prompt: Type.String({ description: 'The image generation prompt' }),
|
||||||
width: Type.Optional(Type.Integer({ description: 'Image width in pixels' })),
|
|
||||||
height: Type.Optional(Type.Integer({ description: 'Image height in pixels' })),
|
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue