From c91a19b1a8a17a451353e48772fd791fd274092c Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Tue, 16 May 2023 15:57:24 +0530 Subject: [PATCH 1/3] Support for creating new images for each message --- .../extensions/stable-diffusion/index.js | 76 ++++++++++++++++--- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index 0d3b5a953..bc33a766b 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -37,6 +37,7 @@ const triggerWords = { [generationMode.CHARACTER]: ['you'], [generationMode.USER]: ['me'], [generationMode.SCENARIO]: ['scene'], + [generationMode.FREE]: ['raw_last'], [generationMode.NOW]: ['last'], [generationMode.FACE]: ['face'], @@ -54,7 +55,7 @@ const quietPrompts = { [generationMode.USER]: "[Pause your roleplay and provide a detailed description of {{user}}'s appearance from the perspective of {{char}} in the form of a comma-delimited list of keywords and phrases. Ignore the rest of the story when crafting this description. Do not roleplay as {{char}}}} when writing this description, and do not attempt to continue the story.]", [generationMode.SCENARIO]: "[Pause your roleplay and provide a detailed description for all of the following: a brief recap of recent events in the story, {{char}}'s appearance, and {{char}}'s surroundings. Do not roleplay while writing this description.]", - [generationMode.FREE]: "[Pause your roleplay and provide ONLY an echo this string back to me verbatim: {0}. Do not write anything after the string. Do not roleplay at all in your response.]", + [generationMode.FREE]: "[Pause your roleplay and provide ONLY the last chat message string back to me verbatim. Do not write anything after the string. Do not roleplay at all in your response. Do not continue the roleplay story.]", } const helpString = [ @@ -65,8 +66,9 @@ const helpString = [ `
  • ${m(j(triggerWords[generationMode.USER]))} – user character full body selfie
  • `, `
  • ${m(j(triggerWords[generationMode.SCENARIO]))} – visual recap of the whole chat scenario
  • `, `
  • ${m(j(triggerWords[generationMode.NOW]))} – visual recap of the last chat message
  • `, + `
  • ${m(j(triggerWords[generationMode.FREE]))} – visual recap of the last chat message with no summary
  • `, '', - `Anything else would trigger a "free mode" to make SD generate whatever you prompted.
    + `Anything else would trigger a "free mode" to make SD generate whatever you prompted.
    example: '/sd apple tree' would generate a picture of an apple tree.`, ].join('
    '); @@ -251,7 +253,14 @@ function processReply(str) { return str; } -async function generatePicture(_, trigger) { +function getRawLastMessage(context) { + const lastMessage = context.chat.slice(-1)[0].mes, + characterDescription = context.characters[context.characterId].description, + situation = context.characters[context.characterId].scenario; + return `((${lastMessage})), (${situation}:0.7), (${characterDescription}:0.5)` +} + +async function generatePicture(_, trigger, message, callback) { if (!trigger || trigger.trim().length === 0) { console.log('Trigger word empty, aborting'); return; @@ -264,7 +273,7 @@ async function generatePicture(_, trigger) { const context = getContext(); try { - const prompt = processReply(await new Promise( + const prompt = trigger == 'raw_last' ? message || getRawLastMessage(context) : processReply(await new Promise( async function promptPromise(resolve, reject) { try { await context.generate('quiet', { resolve, reject, quiet_prompt, force_name2: true, }); @@ -278,7 +287,6 @@ async function generatePicture(_, trigger) { hideSwipeButtons(); console.log('Processed Stable Diffusion prompt:', prompt); - const url = new URL(getApiUrl()); url.pathname = '/api/image'; const result = await fetch(url, { @@ -302,7 +310,7 @@ async function generatePicture(_, trigger) { if (result.ok) { const data = await result.json(); const base64Image = `data:image/jpeg;base64,${data.image}`; - sendMessage(prompt, base64Image); + callback? callback(prompt, base64Image):sendMessage(prompt, base64Image); } } catch (err) { console.trace(err); @@ -335,6 +343,10 @@ async function sendMessage(prompt, image) { } function addSDGenButtons() { + const messageButtonHtml = ` +
    + `; + const buttonHtml = `
    `; @@ -351,19 +363,24 @@ function addSDGenButtons() {
  • Me
  • The Whole Story
  • The Last Message
  • +
  • Raw Last Message
  • `; + $('.mes_buttons').prepend(messageButtonHtml); $('#send_but_sheld').prepend(buttonHtml); $('#send_but_sheld').prepend(waitButtonHtml); - $(document.body).append(dropdownHtml) + $(document.body).append(dropdownHtml); + $(document.body).append(dropdownHtml); + const messageButton = $('.sd_message_gen'); const button = $('#sd_gen'); const waitButton = $("#sd_gen_wait"); const dropdown = $('#sd_dropdown'); waitButton.hide(); dropdown.hide(); button.hide(); + messageButton.hide(); let popper = Popper.createPopper(button.get(0), dropdown.get(0), { placement: 'top-start', @@ -396,14 +413,46 @@ async function moduleWorker() { $("#sd_gen_wait").show(200); } */ - context.onlineStatus === 'no_connection' - ? $('#sd_gen').hide(200) - : $('#sd_gen').show(200) + if (context.onlineStatus === 'no_connection'){ + $('#sd_gen').hide(200); + $('.sd_message_gen').hide(200); + } + else{ + $('#sd_gen').show(200); + $('.sd_message_gen').show(200); + } } addSDGenButtons(); + setInterval(moduleWorker, UPDATE_INTERVAL); +function sdMessageButton (e) { + const character = $(e.currentTarget).parents('div.mes_block').children('div.ch_name').children('span.name_text').text(), + message = $(e.currentTarget).parents('div.mes_block').children('div.mes_text').text(); + + console.log("doing /sd raw last"); + generatePicture('sd', 'raw_last', `${character} said: ${message}`, appendImageToMessage); + + function appendImageToMessage(prompt, image){ + const sd_image = document.createElement("img"), + context = getContext(), + message_id = $(e.target).parents('div.mes').attr('mesid'); + sd_image.src = image; + sd_image.title = prompt; + sd_image.classList.add("img_extra"); + $(e.target).parents('div.mes_block').children('div.mes_text').append(sd_image); + + context.chat[message_id].extra.inline_image = true; + context.chat[message_id].extra.image = image; + context.chat[message_id].extra.title = prompt; + + context.saveChat(); + } +}; + +window.sdMessageButton = sdMessageButton; + $("#sd_dropdown [id]").on("click", function () { var id = $(this).attr("id"); if (id == "sd_you") { @@ -430,6 +479,11 @@ $("#sd_dropdown [id]").on("click", function () { console.log("doing /sd last"); generatePicture('sd', 'last'); } + + else if (id == "sd_raw_last") { + console.log("doing /sd raw last"); + generatePicture('sd', 'raw_last'); + } }); jQuery(async () => { @@ -480,4 +534,4 @@ jQuery(async () => { await loadSettings(); -}); \ No newline at end of file +}); From 94a53d575942c1c331825ccbcd52e2fbba108d16 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Tue, 16 May 2023 15:59:35 +0530 Subject: [PATCH 2/3] support for not hiding everything except image --- public/script.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/public/script.js b/public/script.js index dab17418d..bf199378f 100644 --- a/public/script.js +++ b/public/script.js @@ -428,7 +428,7 @@ function getTokenCount(str, padding = 0) { let tokenCount = 0; jQuery.ajax({ async: false, - type: 'POST', // + type: 'POST', // url: `/tokenize_llama`, data: JSON.stringify({ text: str }), dataType: "json", @@ -572,7 +572,7 @@ $.get("/csrf-token").then(async (data) => { }); function checkOnlineStatus() { - ///////// REMOVED LINES THAT DUPLICATE RA_CHeckOnlineStatus FEATURES + ///////// REMOVED LINES THAT DUPLICATE RA_CHeckOnlineStatus FEATURES if (online_status == "no_connection") { $("#online_status_indicator2").css("background-color", "red"); //Kobold @@ -1020,7 +1020,7 @@ function appendImageToMessage(mes, messageElement) { image.src = mes.extra?.image; image.title = mes.extra?.title || mes.title; image.classList.add("img_extra"); - messageElement.find(".mes_text").prepend(image); + mes.extra?.inline_image?messageElement.find(".mes_text").append(image):messageElement.find(".mes_text").prepend(image); } } @@ -1078,7 +1078,7 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true avatarImg = default_avatar; } } - //old processing: + //old processing: //if messge is from sytem, use the name provided in the message JSONL to proceed, //if not system message, use name2 (char's name) to proceed //characterName = mes.is_system || mes.force_avatar ? mes.name : name2; @@ -1816,7 +1816,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, if (novel_tier === 1) { this_max_context = 1024; } else { - this_max_context = 2048 - 60;//fix for fat tokens + this_max_context = 2048 - 60;//fix for fat tokens if (nai_settings.model_novel == 'krake-v2') { this_max_context -= 160; } @@ -1949,7 +1949,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, // where it left off by removing the trailing newline at the end // that was added by chat2 generator. This causes problems with // instruct mode that could not have a trailing newline. So we're - // removing a newline ONLY at the end of the string if it exists. + // removing a newline ONLY at the end of the string if it exists. item = item.replace(/\n?$/, ''); //item = item.substr(0, item.length - 1); } @@ -2127,7 +2127,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, let promptBiasTokens = getTokenCount(promptBias); let mesSendStringTokens = getTokenCount(mesSendString) let ActualChatHistoryTokens = mesSendStringTokens - allAnchorsTokens + power_user.token_padding; - + let totalTokensInPrompt = allAnchorsTokens + // AN and/or legacy anchors //afterScenarioAnchorTokens + //only counts if AN is set to 'after scenario' @@ -2137,21 +2137,21 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, promptBiasTokens + //{{}} ActualChatHistoryTokens + //chat history power_user.token_padding; - + console.log( ` Prompt Itemization ------------------- Extension Add-ins AN: ${allAnchorsTokens} - + World Info: ${worldInfoStringTokens} - + Character Definitions: ${storyStringTokens} -- Description: ${charDescriptionTokens} -- Example Messages: ${examplesStringTokens} -- Character Personality: ${charPersonalityTokens} -- Character Scenario: ${scenarioTextTokens} - + Chat History: ${ActualChatHistoryTokens} {{}} Bias: ${promptBiasTokens} Padding: ${power_user.token_padding} @@ -2160,7 +2160,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, vs finalPrompt: ${finalPromptTokens} Max Context: ${this_max_context} - + ` ); */ @@ -2277,8 +2277,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, } else { jQuery.ajax({ - type: 'POST', // - url: generate_url, // + type: 'POST', // + url: generate_url, // data: JSON.stringify(generate_data), beforeSend: function () { From 78d4f162814aa3f2129b3de91ad74b7d402d1463 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Tue, 16 May 2023 17:05:53 +0530 Subject: [PATCH 3/3] removed the accidental dropdown addition --- public/scripts/extensions/stable-diffusion/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index bc33a766b..fc6d70f8e 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -371,7 +371,6 @@ function addSDGenButtons() { $('#send_but_sheld').prepend(buttonHtml); $('#send_but_sheld').prepend(waitButtonHtml); $(document.body).append(dropdownHtml); - $(document.body).append(dropdownHtml); const messageButton = $('.sd_message_gen'); const button = $('#sd_gen');