Merge pull request #2594 from SillyTavern/sd-stop

Image generation stop handling
This commit is contained in:
Cohee
2024-08-03 12:06:50 +03:00
committed by GitHub
3 changed files with 22 additions and 13 deletions

View File

@ -1,4 +1,8 @@
<div id="sd_gen" class="list-group-item flex-container flexGap5">
<div class="fa-solid fa-paintbrush extensionsMenuExtensionButton" title="Trigger Stable Diffusion" data-i18n="[title]Trigger Stable Diffusion" /></div>
Generate Image
<div class="fa-solid fa-paintbrush extensionsMenuExtensionButton" title="Trigger Stable Diffusion" data-i18n="[title]Trigger Stable Diffusion"></div>
<span>Generate Image</span>
</div>
<div id="sd_stop_gen" class="list-group-item flex-container flexGap5">
<div class="fa-solid fa-circle-stop extensionsMenuExtensionButton" title="Abort current image generation task" data-i18n="[title]Abort current image generation task"></div>
<span>Stop Image Generation</span>
</div>

View File

@ -37,6 +37,7 @@ const MODULE_NAME = 'sd';
const UPDATE_INTERVAL = 1000;
// This is a 1x1 transparent PNG
const PNG_PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
const CUSTOM_STOP_EVENT = 'sd_stop_generation';
const sources = {
extras: 'extras',
@ -2290,6 +2291,7 @@ async function generatePicture(initiator, args, trigger, message, callback) {
const dimensions = setTypeSpecificDimensions(generationType);
const abortController = new AbortController();
const stopButton = document.getElementById('sd_stop_gen');
let negativePromptPrefix = args?.negative || '';
let imagePath = '';
@ -2300,9 +2302,8 @@ async function generatePicture(initiator, args, trigger, message, callback) {
const prompt = await getPrompt(generationType, message, trigger, quietPrompt, combineNegatives);
console.log('Processed image prompt:', prompt);
eventSource.once(event_types.GENERATION_STOPPED, stopListener);
context.deactivateSendButtons();
hideSwipeButtons();
$(stopButton).show();
eventSource.once(CUSTOM_STOP_EVENT, stopListener);
if (typeof args?._abortController?.addEventListener === 'function') {
args._abortController.addEventListener('abort', stopListener);
@ -2311,13 +2312,13 @@ async function generatePicture(initiator, args, trigger, message, callback) {
imagePath = await sendGenerationRequest(generationType, prompt, negativePromptPrefix, characterName, callback, initiator, abortController.signal);
} catch (err) {
console.trace(err);
throw new Error('SD prompt text generation failed.');
toastr.error('SD prompt text generation failed. Reason: ' + err, 'Image Generation');
throw new Error('SD prompt text generation failed. Reason: ' + err);
}
finally {
$(stopButton).hide();
restoreOriginalDimensions(dimensions);
eventSource.removeListener(event_types.GENERATION_STOPPED, stopListener);
context.activateSendButtons();
showSwipeButtons();
eventSource.removeListener(CUSTOM_STOP_EVENT, stopListener);
}
return imagePath;
@ -3395,7 +3396,7 @@ async function addSDGenButtons() {
$(document).on('click touchend', function (e) {
const target = $(e.target);
if (target.is(dropdown) || target.closest(dropdown).length) return;
if (target.is(button) && !dropdown.is(':visible') && $('#send_but').is(':visible')) {
if ((target.is(button) || target.closest(button).length) && !dropdown.is(':visible') && $('#send_but').is(':visible')) {
e.preventDefault();
dropdown.fadeIn(animation_duration);
@ -3425,6 +3426,10 @@ async function addSDGenButtons() {
generatePicture(initiators.wand, {}, param);
}
});
const stopGenButton = $('#sd_stop_gen');
stopGenButton.hide();
stopGenButton.on('click', () => eventSource.emit(CUSTOM_STOP_EVENT));
}
function isValidState() {

View File

@ -1,8 +1,8 @@
<div id="translate_chat" class="list-group-item flex-container flexGap5">
<div class="fa-solid fa-language extensionsMenuExtensionButton" /></div>
<div class="fa-solid fa-language extensionsMenuExtensionButton"></div>
<span data-i18n="ext_translate_btn_chat">Translate Chat</span>
</div>
<div id="translate_input_message" class="list-group-item flex-container flexGap5">
<div class="fa-solid fa-keyboard extensionsMenuExtensionButton" /></div>
<div class="fa-solid fa-keyboard extensionsMenuExtensionButton"></div>
<span data-i18n="ext_translate_btn_input">Translate Input</span>
</div>