From 99244a0c114387eeaae813bb7fa542d18f868d93 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 2 Jan 2024 07:04:32 +0200 Subject: [PATCH 01/18] Add support for XTTS streaming --- public/scripts/extensions/tts/index.js | 28 ++++++++++++++++---------- public/scripts/extensions/tts/xtts.js | 18 ++++++++++++++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/public/scripts/extensions/tts/index.js b/public/scripts/extensions/tts/index.js index 44b388cbc..7bca9c258 100644 --- a/public/scripts/extensions/tts/index.js +++ b/public/scripts/extensions/tts/index.js @@ -1,6 +1,6 @@ import { callPopup, cancelTtsPlay, eventSource, event_types, name2, saveSettingsDebounced } from '../../../script.js'; import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js'; -import { delay, escapeRegex, getStringHash, onlyUnique } from '../../utils.js'; +import { delay, escapeRegex, getBase64Async, getStringHash, onlyUnique } from '../../utils.js'; import { EdgeTtsProvider } from './edge.js'; import { ElevenLabsTtsProvider } from './elevenlabs.js'; import { SileroTtsProvider } from './silerotts.js'; @@ -316,12 +316,14 @@ async function playAudioData(audioBlob) { if (currentAudioJob == null) { console.log('Cancelled TTS playback because currentAudioJob was null'); } - const reader = new FileReader(); - reader.onload = function (e) { - const srcUrl = e.target.result; + if (audioBlob instanceof Blob) { + const srcUrl = await getBase64Async(audioBlob); audioElement.src = srcUrl; - }; - reader.readAsDataURL(audioBlob); + } else if (typeof audioBlob === 'string') { + audioElement.src = audioBlob; + } else { + throw `TTS received invalid audio data type ${typeof audioBlob}`; + } audioElement.addEventListener('ended', completeCurrentAudioJob); audioElement.addEventListener('canplay', () => { console.debug('Starting TTS playback'); @@ -417,11 +419,15 @@ function completeCurrentAudioJob() { * @param {Response} response */ async function addAudioJob(response) { - const audioData = await response.blob(); - if (!audioData.type.startsWith('audio/')) { - throw `TTS received HTTP response with invalid data format. Expecting audio/*, got ${audioData.type}`; + if (typeof response === 'string') { + audioJobQueue.push(response); + } else { + const audioData = await response.blob(); + if (!audioData.type.startsWith('audio/')) { + throw `TTS received HTTP response with invalid data format. Expecting audio/*, got ${audioData.type}`; + } + audioJobQueue.push(audioData); } - audioJobQueue.push(audioData); console.debug('Pushed audio job to queue.'); } @@ -432,7 +438,7 @@ async function processAudioJobQueue() { } try { audioQueueProcessorReady = false; - currentAudioJob = audioJobQueue.pop(); + currentAudioJob = audioJobQueue.shift(); playAudioData(currentAudioJob); talkingAnimation(true); } catch (error) { diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index 7b271232a..1f54317c1 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -52,6 +52,7 @@ class XTTSTtsProvider { provider_endpoint: 'http://localhost:8020', language: 'en', voiceMap: {}, + streaming: false, }; get settingsHtml() { @@ -75,7 +76,10 @@ class XTTSTtsProvider { Provider Endpoint: - + + + Streaming (RVC not supported) + `; html += ` @@ -90,6 +94,7 @@ class XTTSTtsProvider { // Used when provider settings are updated from UI this.settings.provider_endpoint = $('#xtts_tts_endpoint').val(); this.settings.language = $('#xtts_api_language').val(); + this.settings.streaming = $('#xtts_tts_streaming').is(':checked'); saveTtsProviderSettings(); } @@ -125,6 +130,8 @@ class XTTSTtsProvider { $('#xtts_tts_endpoint').on('input', () => { this.onSettingsChange(); }); $('#xtts_api_language').val(this.settings.language); $('#xtts_api_language').on('change', () => { this.onSettingsChange(); }); + $('#xtts_tts_streaming').prop('checked', this.settings.streaming); + $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); await this.checkReady(); @@ -176,6 +183,15 @@ class XTTSTtsProvider { async fetchTtsGeneration(inputText, voiceId) { console.info(`Generating new TTS for voice_id ${voiceId}`); + + if (this.settings.streaming) { + const params = new URLSearchParams(); + params.append('text', inputText); + params.append('speaker_wav', voiceId); + params.append('language', this.settings.language); + return `${this.settings.provider_endpoint}/tts_stream/?${params.toString()}`; + } + const response = await doExtrasFetch( `${this.settings.provider_endpoint}/tts_to_audio/`, { From c69724e1da38abb01447c78a5984d2fd3cbf5078 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:28:34 +0200 Subject: [PATCH 02/18] Fix GUI Kobold --- public/script.js | 3 ++- public/scripts/kai-settings.js | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index d80bb98d8..a437e6423 100644 --- a/public/script.js +++ b/public/script.js @@ -2891,7 +2891,7 @@ export async function generateRaw(prompt, api, instructOverride) { case 'kobold': case 'koboldhorde': if (preset_settings === 'gui') { - generateData = { prompt: prompt, gui_settings: true, max_length: amount_gen, max_context_length: max_context }; + generateData = { prompt: prompt, gui_settings: true, max_length: amount_gen, max_context_length: max_context, api_server }; } else { const isHorde = api === 'koboldhorde'; const koboldSettings = koboldai_settings[koboldai_setting_names[preset_settings]]; @@ -3724,6 +3724,7 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu gui_settings: true, max_length: maxLength, max_context_length: max_context, + api_server, }; if (preset_settings != 'gui') { diff --git a/public/scripts/kai-settings.js b/public/scripts/kai-settings.js index d88a6ee87..f2f295dc3 100644 --- a/public/scripts/kai-settings.js +++ b/public/scripts/kai-settings.js @@ -4,7 +4,6 @@ import { getStoppingStrings, substituteParams, api_server, - main_api, } from '../script.js'; import { @@ -142,7 +141,6 @@ export function getKoboldGenerationData(finalPrompt, settings, maxLength, maxCon sampler_seed: kai_settings.seed >= 0 ? kai_settings.seed : undefined, api_server, - main_api, }; return generate_data; } From 4190e9f16d2c25fbb1e480a1a6a4425031b5bcf4 Mon Sep 17 00:00:00 2001 From: Danil Boldyrev Date: Tue, 2 Jan 2024 19:01:12 +0300 Subject: [PATCH 03/18] change xtts generation settings --- public/scripts/extensions/tts/xtts.js | 119 +++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index 624e926cc..185d68f1e 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -32,11 +32,19 @@ class XTTSTtsProvider { "Hungarian": "hu", "Hindi": "hi", } - + defaultSettings = { provider_endpoint: "http://localhost:8020", language: "en", - voiceMap: {} + temperature : 0.75, + length_penalty : 1.0, + repetition_penalty: 5.0, + top_k : 50, + top_p : 0.85, + speed : 1, + enable_text_splitting: true, + voiceMap: {}, + streaming: false, } get settingsHtml() { @@ -58,9 +66,37 @@ class XTTSTtsProvider { html += ` + + XTTS Settings: + Speed: ${this.defaultSettings.speed} + + + Temperature: ${this.defaultSettings.temperature} + + + Length Penalty: ${this.defaultSettings.length_penalty} + + + Repetition Penalty: ${this.defaultSettings.repetition_penalty} + + + Top K: ${this.defaultSettings.top_k} + + + Top P: ${this.defaultSettings.top_p} + + + Enable Text Splitting: + + Provider Endpoint: + + + Streaming (RVC not supported) + + `; html += ` @@ -75,9 +111,30 @@ class XTTSTtsProvider { // Used when provider settings are updated from UI this.settings.provider_endpoint = $('#xtts_tts_endpoint').val() this.settings.language = $('#xtts_api_language').val() + + // Update the default TTS settings based on input fields + this.settings.speed = $('#xtts_speed').val(); + this.settings.temperature = $('#xtts_temperature').val(); + this.settings.length_penalty = $('#xtts_length_penalty').val(); + this.settings.repetition_penalty = $('#xtts_repetition_penalty').val(); + this.settings.top_k = $('#xtts_top_k').val(); + this.settings.top_p = $('#xtts_top_p').val(); + this.settings.enable_text_splitting = $('#xtts_enable_text_splitting').is(':checked'); + this.settings.streaming = $('#xtts_tts_streaming').is(':checked'); + + // Update the UI to reflect changes + $('#xtts_tts_speed_output').text(this.settings.speed); + $('#xtts_tts_temperature_output').text(this.settings.temperature); + $('#xtts_length_penalty_output').text(this.settings.length_penalty); + $('#xtts_repetition_penalty_output').text(this.settings.repetition_penalty); + $('#xtts_top_k_output').text(this.settings.top_k); + $('#xtts_top_p_output').text(this.settings.top_p); + saveTtsProviderSettings() + this.changeTTSSetting() } + async loadSettings(settings) { // Pupulate Provider UI given input settings if (Object.keys(settings).length == 0) { @@ -111,6 +168,27 @@ class XTTSTtsProvider { $('#xtts_api_language').val(this.settings.language) $('#xtts_api_language').on("change", () => { this.onSettingsChange() }) + // Set initial values from the settings + $('#xtts_speed').val(this.settings.speed); + $('#xtts_temperature').val(this.settings.temperature); + $('#xtts_length_penalty').val(this.settings.length_penalty); + $('#xtts_repetition_penalty').val(this.settings.repetition_penalty); + $('#xtts_top_k').val(this.settings.top_k); + $('#xtts_top_p').val(this.settings.top_p); + $('#xtts_enable_text_splitting').prop('checked', this.settings.enable_text_splitting); + + // Register input/change event listeners to update settings on user interaction + $('#xtts_speed').on("input", () => { this.onSettingsChange() }); + $('#xtts_temperature').on("input", () => { this.onSettingsChange() }); + $('#xtts_length_penalty').on("input", () => { this.onSettingsChange() }); + $('#xtts_repetition_penalty').on("input", () => { this.onSettingsChange() }); + $('#xtts_top_k').on("input", () => { this.onSettingsChange() }); + $('#xtts_top_p').on("input", () => { this.onSettingsChange() }); + $('#xtts_enable_text_splitting').on("change", () => { this.onSettingsChange() }); + + $('#xtts_tts_streaming').prop('checked', this.settings.streaming); + $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); + await this.checkReady() console.debug("XTTS: Settings loaded") @@ -160,8 +238,45 @@ class XTTSTtsProvider { return responseJson } + // Each time a parameter is changed, we change the configuration + async changeTTSSetting() { + const response = await doExtrasFetch( + `${this.settings.provider_endpoint}/set_tts_settings`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'no-cache' + }, + body: JSON.stringify({ + "temperature": this.settings.temperature, + "speed": this.settings.speed, + "length_penalty": this.settings.length_penalty, + "repetition_penalty": this.settings.repetition_penalty, + "top_p": this.settings.top_p, + "top_k": this.settings.top_k, + "enable_text_splitting": this.settings.enable_text_splitting, + }) + } + ) + if (!response.ok) { + toastr.error(response.statusText, 'An error occurred when changing TTS settings'); + throw new Error(`HTTP ${response.status}: ${await response.text()}`); + } + return response + } + async fetchTtsGeneration(inputText, voiceId) { console.info(`Generating new TTS for voice_id ${voiceId}`) + + if (this.settings.streaming) { + const params = new URLSearchParams(); + params.append('text', inputText); + params.append('speaker_wav', voiceId); + params.append('language', this.settings.language); + return `${this.settings.provider_endpoint}/tts_stream/?${params.toString()}`; + } + const response = await doExtrasFetch( `${this.settings.provider_endpoint}/tts_to_audio/`, { From 757eba1ef8ae1cfec76bbac2efbe0b4e088b9e25 Mon Sep 17 00:00:00 2001 From: Danil Boldyrev Date: Tue, 2 Jan 2024 19:07:30 +0300 Subject: [PATCH 04/18] add missing space --- public/scripts/extensions/tts/xtts.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index 185d68f1e..bf4e6b285 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -66,8 +66,7 @@ class XTTSTtsProvider { html += ` - - XTTS Settings: + XTTS Settings: Speed: ${this.defaultSettings.speed} From 36db08d6b8147647082fe21f540718ad6c0c67e9 Mon Sep 17 00:00:00 2001 From: Danil Boldyrev Date: Tue, 2 Jan 2024 19:23:42 +0300 Subject: [PATCH 05/18] Remove error about updating xtts parameters --- public/scripts/extensions/tts/xtts.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index bf4e6b285..7a6a912e7 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -258,10 +258,6 @@ class XTTSTtsProvider { }) } ) - if (!response.ok) { - toastr.error(response.statusText, 'An error occurred when changing TTS settings'); - throw new Error(`HTTP ${response.status}: ${await response.text()}`); - } return response } From 8ef91ae1815466b8fdd39310b6b609843c3ee34f Mon Sep 17 00:00:00 2001 From: Danil Boldyrev Date: Tue, 2 Jan 2024 19:50:13 +0300 Subject: [PATCH 06/18] Add ability to customize xtts stream chunk size --- public/scripts/extensions/tts/xtts.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index 7a6a912e7..d76f74060 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -43,6 +43,7 @@ class XTTSTtsProvider { top_p : 0.85, speed : 1, enable_text_splitting: true, + stream_chunk_size: 100, voiceMap: {}, streaming: false, } @@ -85,6 +86,9 @@ class XTTSTtsProvider { Top P: ${this.defaultSettings.top_p} + Stream Chunk Size: ${this.defaultSettings.stream_chunk_size} + + Enable Text Splitting: @@ -95,7 +99,6 @@ class XTTSTtsProvider { Streaming (RVC not supported) - `; html += ` @@ -118,6 +121,7 @@ class XTTSTtsProvider { this.settings.repetition_penalty = $('#xtts_repetition_penalty').val(); this.settings.top_k = $('#xtts_top_k').val(); this.settings.top_p = $('#xtts_top_p').val(); + this.settings.stream_chunk_size = $('#xtts_stream_chunk_size').val(); this.settings.enable_text_splitting = $('#xtts_enable_text_splitting').is(':checked'); this.settings.streaming = $('#xtts_tts_streaming').is(':checked'); @@ -128,6 +132,7 @@ class XTTSTtsProvider { $('#xtts_repetition_penalty_output').text(this.settings.repetition_penalty); $('#xtts_top_k_output').text(this.settings.top_k); $('#xtts_top_p_output').text(this.settings.top_p); + $('#xtts_stream_chunk_size_output').text(this.settings.stream_chunk_size); saveTtsProviderSettings() this.changeTTSSetting() @@ -185,6 +190,7 @@ class XTTSTtsProvider { $('#xtts_top_p').on("input", () => { this.onSettingsChange() }); $('#xtts_enable_text_splitting').on("change", () => { this.onSettingsChange() }); + $('#xtts_stream_chunk_size').on("input", () => { this.onSettingsChange() }); $('#xtts_tts_streaming').prop('checked', this.settings.streaming); $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); @@ -255,6 +261,7 @@ class XTTSTtsProvider { "top_p": this.settings.top_p, "top_k": this.settings.top_k, "enable_text_splitting": this.settings.enable_text_splitting, + "stream_chunk_size": this.settings.stream_chunk_size }) } ) From cc2df979594b63c99baf98f2758df59e39e7e05f Mon Sep 17 00:00:00 2001 From: Danil Boldyrev Date: Tue, 2 Jan 2024 20:13:45 +0300 Subject: [PATCH 07/18] Increased the maximum limit of tokens for xtts streaming --- public/scripts/extensions/tts/xtts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index d76f74060..fa11169e9 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -87,7 +87,7 @@ class XTTSTtsProvider { Stream Chunk Size: ${this.defaultSettings.stream_chunk_size} - + Enable Text Splitting: From 04d3dcf39fdefe5b3b0a19fe3618c96f46f185fb Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:28:56 +0200 Subject: [PATCH 08/18] Respect match index in regex scripts replacement --- public/scripts/extensions/regex/engine.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/public/scripts/extensions/regex/engine.js b/public/scripts/extensions/regex/engine.js index 8194a807f..069e71aad 100644 --- a/public/scripts/extensions/regex/engine.js +++ b/public/scripts/extensions/regex/engine.js @@ -62,7 +62,13 @@ function getRegexedString(rawString, placement, { characterOverride, isMarkdown, return finalString; } -// Runs the provided regex script on the given string +/** + * Runs the provided regex script on the given string + * @param {object} regexScript The regex script to run + * @param {string} rawString The string to run the regex script on + * @param {object} params The parameters to use for the regex script + * @returns {string} The new string + */ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { let newString = rawString; if (!regexScript || !!(regexScript.disabled) || !regexScript?.findRegex || !rawString) { @@ -102,9 +108,9 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { }, ); if (!newString) { - newString = rawString.replace(fencedMatch, subReplaceString); + newString = rawString.slice(0, match.index) + rawString.slice(match.index).replace(fencedMatch, subReplaceString); } else { - newString = newString.replace(fencedMatch, subReplaceString); + newString = newString.slice(0, match.index) + newString.slice(match.index).replace(fencedMatch, subReplaceString); } // If the regex isn't global, break out of the loop From 18b11825b3b40189682c1e675bbd57bfbb8421d7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 19:53:24 +0200 Subject: [PATCH 09/18] #1629 Use string replace instead of while loop --- public/scripts/extensions/regex/engine.js | 93 ++++++++++++++--------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/public/scripts/extensions/regex/engine.js b/public/scripts/extensions/regex/engine.js index 069e71aad..7cd22c511 100644 --- a/public/scripts/extensions/regex/engine.js +++ b/public/scripts/extensions/regex/engine.js @@ -6,20 +6,33 @@ export { runRegexScript, }; +/** + * @enum {number} Where the regex script should be applied + */ const regex_placement = { - // MD Display is deprecated. Do not use. + /** + * @deprecated MD Display is deprecated. Do not use. + */ MD_DISPLAY: 0, USER_INPUT: 1, AI_OUTPUT: 2, SLASH_COMMAND: 3, }; +/** + * @enum {number} How the regex script should replace the matched string + */ const regex_replace_strategy = { REPLACE: 0, OVERLAY: 1, }; -// Originally from: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js +/** + * Instantiates a regular expression from a string. + * @param {string} input The input string. + * @returns {RegExp} The regular expression instance. + * @copyright Originally from: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js + */ function regexFromString(input) { try { // Parse input @@ -37,8 +50,21 @@ function regexFromString(input) { } } -// Parent function to fetch a regexed version of a raw string +/** + * Parent function to fetch a regexed version of a raw string + * @param {string} rawString The raw string to be regexed + * @param {regex_placement} placement The placement of the string + * @param {RegexParams} params The parameters to use for the regex script + * @returns {string} The regexed string + * @typedef {{characterOverride?: string, isMarkdown?: boolean, isPrompt?: boolean }} RegexParams The parameters to use for the regex script + */ function getRegexedString(rawString, placement, { characterOverride, isMarkdown, isPrompt } = {}) { + // WTF have you passed me? + if (typeof rawString !== 'string') { + console.warn('getRegexedString: rawString is not a string. Returning empty string.'); + return ''; + } + let finalString = rawString; if (extension_settings.disabledExtensions.includes('regex') || !rawString || placement === undefined) { return finalString; @@ -66,8 +92,9 @@ function getRegexedString(rawString, placement, { characterOverride, isMarkdown, * Runs the provided regex script on the given string * @param {object} regexScript The regex script to run * @param {string} rawString The string to run the regex script on - * @param {object} params The parameters to use for the regex script + * @param {RegexScriptParams} params The parameters to use for the regex script * @returns {string} The new string + * @typedef {{characterOverride?: string}} RegexScriptParams The parameters to use for the regex script */ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { let newString = rawString; @@ -75,7 +102,6 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { return newString; } - let match; const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex); // The user skill issued. Return with nothing. @@ -83,46 +109,31 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { return newString; } - while ((match = findRegex.exec(rawString)) !== null) { - const fencedMatch = match[0]; - const capturedMatch = match[1]; + newString = rawString.replace(findRegex, (fencedMatch) => { + let trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings, { characterOverride }); - let trimCapturedMatch; - let trimFencedMatch; - if (capturedMatch) { - const tempTrimCapture = filterString(capturedMatch, regexScript.trimStrings, { characterOverride }); - trimFencedMatch = fencedMatch.replaceAll(capturedMatch, tempTrimCapture); - trimCapturedMatch = tempTrimCapture; - } else { - trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings, { characterOverride }); - } - - // TODO: Use substrings for replacement. But not necessary at this time. - // A substring is from match.index to match.index + match[0].length or fencedMatch.length const subReplaceString = substituteRegexParams( regexScript.replaceString, - trimCapturedMatch ?? trimFencedMatch, + trimFencedMatch, { characterOverride, replaceStrategy: regexScript.replaceStrategy ?? regex_replace_strategy.REPLACE, }, ); - if (!newString) { - newString = rawString.slice(0, match.index) + rawString.slice(match.index).replace(fencedMatch, subReplaceString); - } else { - newString = newString.slice(0, match.index) + newString.slice(match.index).replace(fencedMatch, subReplaceString); - } - // If the regex isn't global, break out of the loop - if (!findRegex.flags.includes('g')) { - break; - } - } + return subReplaceString; + }); return newString; } -// Filters anything to trim from the regex match +/** + * Filters anything to trim from the regex match + * @param {string} rawString The raw string to filter + * @param {string[]} trimStrings The strings to trim + * @param {RegexScriptParams} params The parameters to use for the regex filter + * @returns {string} The filtered string + */ function filterString(rawString, trimStrings, { characterOverride } = {}) { let finalString = rawString; trimStrings.forEach((trimString) => { @@ -133,7 +144,14 @@ function filterString(rawString, trimStrings, { characterOverride } = {}) { return finalString; } -// Substitutes regex-specific and normal parameters +/** + * Substitutes regex-specific and normal parameters + * @param {string} rawString + * @param {string} regexMatch + * @param {RegexSubstituteParams} params The parameters to use for the regex substitution + * @returns {string} The substituted string + * @typedef {{characterOverride?: string, replaceStrategy?: number}} RegexSubstituteParams The parameters to use for the regex substitution + */ function substituteRegexParams(rawString, regexMatch, { characterOverride, replaceStrategy } = {}) { let finalString = rawString; finalString = substituteParams(finalString, undefined, characterOverride); @@ -188,8 +206,13 @@ function substituteRegexParams(rawString, regexMatch, { characterOverride, repla return finalString; } -// Splices common sentence symbols and whitespace from the beginning and end of a string -// Using a for loop due to sequential ordering +/** + * Splices common sentence symbols and whitespace from the beginning and end of a string. + * Using a for loop due to sequential ordering. + * @param {string} rawString The raw string to splice + * @param {boolean} isSuffix String is a suffix + * @returns {string} The spliced string + */ function spliceSymbols(rawString, isSuffix) { let offset = 0; From 7168dbe508591329574e6e92f4f2fbff01a38557 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:17:36 +0200 Subject: [PATCH 10/18] Lint fix --- public/scripts/extensions/tts/xtts.js | 96 +++++++++++++-------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index fbd97da7f..8e8c03923 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -29,28 +29,28 @@ class XTTSTtsProvider { } languageLabels = { - "Arabic": "ar", - "Brazilian Portuguese": "pt", - "Chinese": "zh-cn", - "Czech": "cs", - "Dutch": "nl", - "English": "en", - "French": "fr", - "German": "de", - "Italian": "it", - "Polish": "pl", - "Russian": "ru", - "Spanish": "es", - "Turkish": "tr", - "Japanese": "ja", - "Korean": "ko", - "Hungarian": "hu", - "Hindi": "hi", - } + 'Arabic': 'ar', + 'Brazilian Portuguese': 'pt', + 'Chinese': 'zh-cn', + 'Czech': 'cs', + 'Dutch': 'nl', + 'English': 'en', + 'French': 'fr', + 'German': 'de', + 'Italian': 'it', + 'Polish': 'pl', + 'Russian': 'ru', + 'Spanish': 'es', + 'Turkish': 'tr', + 'Japanese': 'ja', + 'Korean': 'ko', + 'Hungarian': 'hu', + 'Hindi': 'hi', + }; defaultSettings = { - provider_endpoint: "http://localhost:8020", - language: "en", + provider_endpoint: 'http://localhost:8020', + language: 'en', temperature : 0.75, length_penalty : 1.0, repetition_penalty: 5.0, @@ -61,7 +61,7 @@ class XTTSTtsProvider { stream_chunk_size: 100, voiceMap: {}, streaming: false, - } + }; get settingsHtml() { let html = ` @@ -125,10 +125,10 @@ class XTTSTtsProvider { } onSettingsChange() { // Used when provider settings are updated from UI - this.settings.provider_endpoint = $('#xtts_tts_endpoint').val() - this.settings.language = $('#xtts_api_language').val() + this.settings.provider_endpoint = $('#xtts_tts_endpoint').val(); + this.settings.language = $('#xtts_api_language').val(); - // Update the default TTS settings based on input fields + // Update the default TTS settings based on input fields this.settings.speed = $('#xtts_speed').val(); this.settings.temperature = $('#xtts_temperature').val(); this.settings.length_penalty = $('#xtts_length_penalty').val(); @@ -148,8 +148,8 @@ class XTTSTtsProvider { $('#xtts_top_p_output').text(this.settings.top_p); $('#xtts_stream_chunk_size_output').text(this.settings.stream_chunk_size); - saveTtsProviderSettings() - this.changeTTSSetting() + saveTtsProviderSettings(); + this.changeTTSSetting(); } @@ -198,15 +198,15 @@ class XTTSTtsProvider { $('#xtts_enable_text_splitting').prop('checked', this.settings.enable_text_splitting); // Register input/change event listeners to update settings on user interaction - $('#xtts_speed').on("input", () => { this.onSettingsChange() }); - $('#xtts_temperature').on("input", () => { this.onSettingsChange() }); - $('#xtts_length_penalty').on("input", () => { this.onSettingsChange() }); - $('#xtts_repetition_penalty').on("input", () => { this.onSettingsChange() }); - $('#xtts_top_k').on("input", () => { this.onSettingsChange() }); - $('#xtts_top_p').on("input", () => { this.onSettingsChange() }); - $('#xtts_enable_text_splitting').on("change", () => { this.onSettingsChange() }); + $('#xtts_speed').on('input', () => { this.onSettingsChange(); }); + $('#xtts_temperature').on('input', () => { this.onSettingsChange(); }); + $('#xtts_length_penalty').on('input', () => { this.onSettingsChange(); }); + $('#xtts_repetition_penalty').on('input', () => { this.onSettingsChange(); }); + $('#xtts_top_k').on('input', () => { this.onSettingsChange(); }); + $('#xtts_top_p').on('input', () => { this.onSettingsChange(); }); + $('#xtts_enable_text_splitting').on('change', () => { this.onSettingsChange(); }); - $('#xtts_stream_chunk_size').on("input", () => { this.onSettingsChange() }); + $('#xtts_stream_chunk_size').on('input', () => { this.onSettingsChange(); }); $('#xtts_tts_streaming').prop('checked', this.settings.streaming); $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); @@ -266,25 +266,25 @@ class XTTSTtsProvider { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache' + 'Cache-Control': 'no-cache', }, body: JSON.stringify({ - "temperature": this.settings.temperature, - "speed": this.settings.speed, - "length_penalty": this.settings.length_penalty, - "repetition_penalty": this.settings.repetition_penalty, - "top_p": this.settings.top_p, - "top_k": this.settings.top_k, - "enable_text_splitting": this.settings.enable_text_splitting, - "stream_chunk_size": this.settings.stream_chunk_size - }) - } - ) - return response + 'temperature': this.settings.temperature, + 'speed': this.settings.speed, + 'length_penalty': this.settings.length_penalty, + 'repetition_penalty': this.settings.repetition_penalty, + 'top_p': this.settings.top_p, + 'top_k': this.settings.top_k, + 'enable_text_splitting': this.settings.enable_text_splitting, + 'stream_chunk_size': this.settings.stream_chunk_size, + }), + }, + ); + return response; } async fetchTtsGeneration(inputText, voiceId) { - console.info(`Generating new TTS for voice_id ${voiceId}`) + console.info(`Generating new TTS for voice_id ${voiceId}`); if (this.settings.streaming) { const params = new URLSearchParams(); From dc8e742502587a5a617af3e937c016ac97faa896 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:19:46 +0200 Subject: [PATCH 11/18] Only change remote setting if endpoint is configured --- public/scripts/extensions/tts/xtts.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index 8e8c03923..d6e6d05b2 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -149,10 +149,9 @@ class XTTSTtsProvider { $('#xtts_stream_chunk_size_output').text(this.settings.stream_chunk_size); saveTtsProviderSettings(); - this.changeTTSSetting(); + this.changeTTSSettings(); } - async loadSettings(settings) { // Pupulate Provider UI given input settings if (Object.keys(settings).length == 0) { @@ -259,7 +258,11 @@ class XTTSTtsProvider { } // Each time a parameter is changed, we change the configuration - async changeTTSSetting() { + async changeTTSSettings() { + if (!this.settings.provider_endpoint) { + return; + } + const response = await doExtrasFetch( `${this.settings.provider_endpoint}/set_tts_settings`, { From f0a330455f87d6eadce217a3c2ddf5e117bcc6da Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:23:01 +0200 Subject: [PATCH 12/18] More smooth speed step --- public/scripts/extensions/tts/xtts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index d6e6d05b2..b09d0f497 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -84,7 +84,7 @@ class XTTSTtsProvider { XTTS Settings: Speed: ${this.defaultSettings.speed} - + Temperature: ${this.defaultSettings.temperature} From 446445ab9084ef357bb5973b656aff57be0e33cf Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:25:08 +0200 Subject: [PATCH 13/18] Put streaming checkbox on top --- public/scripts/extensions/tts/xtts.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index b09d0f497..eaddcaf4f 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -83,6 +83,10 @@ class XTTSTtsProvider { html += ` XTTS Settings: + + + Streaming (RVC not supported) + Speed: ${this.defaultSettings.speed} @@ -104,15 +108,13 @@ class XTTSTtsProvider { Stream Chunk Size: ${this.defaultSettings.stream_chunk_size} - Enable Text Splitting: - + + + Enable Text Splitting + Provider Endpoint: - - - Streaming (RVC not supported) - `; html += ` From 03f9d8d278e3a1ebcc276e0317b7137ce061d7af Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:35:04 +0200 Subject: [PATCH 14/18] Fix settings UI loading --- public/scripts/extensions/tts/xtts.js | 52 +++++++++++++-------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/public/scripts/extensions/tts/xtts.js b/public/scripts/extensions/tts/xtts.js index eaddcaf4f..1408cae2d 100644 --- a/public/scripts/extensions/tts/xtts.js +++ b/public/scripts/extensions/tts/xtts.js @@ -51,12 +51,12 @@ class XTTSTtsProvider { defaultSettings = { provider_endpoint: 'http://localhost:8020', language: 'en', - temperature : 0.75, - length_penalty : 1.0, + temperature: 0.75, + length_penalty: 1.0, repetition_penalty: 5.0, - top_k : 50, - top_p : 0.85, - speed : 1, + top_k: 50, + top_p: 0.85, + speed: 1, enable_text_splitting: true, stream_chunk_size: 100, voiceMap: {}, @@ -68,9 +68,7 @@ class XTTSTtsProvider { Language `; - for (let language in this.languageLabels) { - if (this.languageLabels[language] == this.settings?.language) { html += `${language}`; continue; @@ -79,10 +77,12 @@ class XTTSTtsProvider { html += `${language}`; } - html += ` XTTS Settings: + Provider Endpoint: + + Use XTTSv2 TTS Server. Streaming (RVC not supported) @@ -112,19 +112,11 @@ class XTTSTtsProvider { Enable Text Splitting - - Provider Endpoint: - - `; - - html += ` - - - Use XTTSv2 TTS Server. `; return html; } + onSettingsChange() { // Used when provider settings are updated from UI this.settings.provider_endpoint = $('#xtts_tts_endpoint').val(); @@ -182,14 +174,9 @@ class XTTSTtsProvider { } }, 2000); - $('#xtts_tts_endpoint').val(this.settings.provider_endpoint); - $('#xtts_tts_endpoint').on('input', () => { this.onSettingsChange(); }); - $('#xtts_api_language').val(this.settings.language); - $('#xtts_api_language').on('change', () => { this.onSettingsChange(); }); - $('#xtts_tts_streaming').prop('checked', this.settings.streaming); - $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); - // Set initial values from the settings + $('#xtts_tts_endpoint').val(this.settings.provider_endpoint); + $('#xtts_api_language').val(this.settings.language); $('#xtts_speed').val(this.settings.speed); $('#xtts_temperature').val(this.settings.temperature); $('#xtts_length_penalty').val(this.settings.length_penalty); @@ -197,8 +184,21 @@ class XTTSTtsProvider { $('#xtts_top_k').val(this.settings.top_k); $('#xtts_top_p').val(this.settings.top_p); $('#xtts_enable_text_splitting').prop('checked', this.settings.enable_text_splitting); + $('#xtts_stream_chunk_size').val(this.settings.stream_chunk_size); + $('#xtts_tts_streaming').prop('checked', this.settings.streaming); + + // Update the UI to reflect changes + $('#xtts_tts_speed_output').text(this.settings.speed); + $('#xtts_tts_temperature_output').text(this.settings.temperature); + $('#xtts_length_penalty_output').text(this.settings.length_penalty); + $('#xtts_repetition_penalty_output').text(this.settings.repetition_penalty); + $('#xtts_top_k_output').text(this.settings.top_k); + $('#xtts_top_p_output').text(this.settings.top_p); + $('#xtts_stream_chunk_size_output').text(this.settings.stream_chunk_size); // Register input/change event listeners to update settings on user interaction + $('#xtts_tts_endpoint').on('input', () => { this.onSettingsChange(); }); + $('#xtts_api_language').on('change', () => { this.onSettingsChange(); }); $('#xtts_speed').on('input', () => { this.onSettingsChange(); }); $('#xtts_temperature').on('input', () => { this.onSettingsChange(); }); $('#xtts_length_penalty').on('input', () => { this.onSettingsChange(); }); @@ -206,9 +206,7 @@ class XTTSTtsProvider { $('#xtts_top_k').on('input', () => { this.onSettingsChange(); }); $('#xtts_top_p').on('input', () => { this.onSettingsChange(); }); $('#xtts_enable_text_splitting').on('change', () => { this.onSettingsChange(); }); - $('#xtts_stream_chunk_size').on('input', () => { this.onSettingsChange(); }); - $('#xtts_tts_streaming').prop('checked', this.settings.streaming); $('#xtts_tts_streaming').on('change', () => { this.onSettingsChange(); }); await this.checkReady(); @@ -218,7 +216,7 @@ class XTTSTtsProvider { // Perform a simple readiness check by trying to fetch voiceIds async checkReady() { - await this.fetchTtsVoiceObjects(); + await Promise.allSettled([this.fetchTtsVoiceObjects(), this.changeTTSSettings()]); } async onRefreshClick() { From 5ca909ab13c5dfc0251227a5bc2869511335a8a8 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Jan 2024 23:47:32 +0200 Subject: [PATCH 15/18] Don't trim whitespace start of story string --- public/scripts/power-user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index d4378a71a..986c9eac3 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -1849,7 +1849,7 @@ export function renderStoryString(params) { output = substituteParams(output, params.user, params.char); // remove leading whitespace - output = output.trimStart(); + // output = output.trimStart(); // add a newline to the end of the story string if it doesn't have one if (output.length > 0 && !output.endsWith('\n')) { From 8a07edf656ab8f03b64feb11a4ef1dd5c651342b Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 4 Jan 2024 00:00:47 +0200 Subject: [PATCH 16/18] Only remove leading newlines --- public/scripts/power-user.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index 986c9eac3..cb6e2ca11 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -1848,8 +1848,8 @@ export function renderStoryString(params) { // substitute {{macro}} params that are not defined in the story string output = substituteParams(output, params.user, params.char); - // remove leading whitespace - // output = output.trimStart(); + // remove leading newlines + output = output.replace(/^\n+/, ''); // add a newline to the end of the story string if it doesn't have one if (output.length > 0 && !output.endsWith('\n')) { From 926f94b01da789b1392ac1f58cd0d713891178b4 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 4 Jan 2024 04:34:38 +0200 Subject: [PATCH 17/18] Add regex test mode --- public/scripts/extensions/regex/editor.html | 23 ++++++++++++++++++- public/scripts/extensions/regex/index.js | 25 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/regex/editor.html b/public/scripts/extensions/regex/editor.html index 72c95d3d9..1789dbe98 100644 --- a/public/scripts/extensions/regex/editor.html +++ b/public/scripts/extensions/regex/editor.html @@ -1,9 +1,14 @@ - Regex Editor + + Regex Editor ? + + + Test Mode + @@ -11,6 +16,22 @@ + + + + Input + + + + + + Output + + + + + + diff --git a/public/scripts/extensions/regex/index.js b/public/scripts/extensions/regex/index.js index d691c0119..c5fabdac8 100644 --- a/public/scripts/extensions/regex/index.js +++ b/public/scripts/extensions/regex/index.js @@ -165,6 +165,31 @@ async function onRegexEditorOpenClick(existingId) { .prop('checked', true); } + editorHtml.find('#regex_test_mode_toggle').on('click', function () { + editorHtml.find('#regex_test_mode').toggleClass('displayNone'); + updateTestResult(); + }); + + function updateTestResult() { + if (!editorHtml.find('#regex_test_mode').is(':visible')) { + return; + } + + const testScript = { + scriptName: editorHtml.find('.regex_script_name').val(), + findRegex: editorHtml.find('.find_regex').val(), + replaceString: editorHtml.find('.regex_replace_string').val(), + trimStrings: String(editorHtml.find('.regex_trim_strings').val()).split('\n').filter((e) => e.length !== 0) || [], + substituteRegex: editorHtml.find('input[name="substitute_regex"]').prop('checked'), + replaceStrategy: Number(editorHtml.find('select[name="replace_strategy_select"]').find(':selected').val()) ?? 0, + }; + const rawTestString = String(editorHtml.find('#regex_test_input').val()); + const result = runRegexScript(testScript, rawTestString); + editorHtml.find('#regex_test_output').text(result); + } + + editorHtml.find('input, textarea, select').on('input', updateTestResult); + const popupResult = await callPopup(editorHtml, 'confirm', undefined, { okButton: 'Save' }); if (popupResult) { const newRegexScript = { From 99dc3fb6fef0a37c111f93268156a1e074543fd8 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 4 Jan 2024 04:52:42 +0200 Subject: [PATCH 18/18] Unhide aphro multiswipe in zen sliders --- public/scripts/power-user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index cb6e2ca11..84fad8c65 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -520,7 +520,7 @@ async function switchZenSliders() { $('#clickSlidersTips').hide(); $('#pro-settings-block input[type=\'number\']').hide(); //hide number inputs that are not 'seed' inputs - $(`#textgenerationwebui_api-settings :input[type='number']:not([id^='seed']), + $(`#textgenerationwebui_api-settings :input[type='number']:not([id^='seed']):not([id^='n_']), #kobold_api-settings :input[type='number']:not([id^='seed'])`).hide(); //hide original sliders $(`#textgenerationwebui_api-settings input[type='range'],