SillyTavern/public/scripts/extensions/tts/xtts.js

207 lines
6.2 KiB
JavaScript
Raw Normal View History

2023-12-02 20:11:06 +01:00
import { doExtrasFetch, getApiUrl, modules } from '../../extensions.js';
import { saveTtsProviderSettings } from './index.js';
2023-11-21 11:16:56 +01:00
2023-12-02 20:11:06 +01:00
export { XTTSTtsProvider };
2023-11-21 11:16:56 +01:00
class XTTSTtsProvider {
//########//
// Config //
//########//
2023-12-02 20:11:06 +01:00
settings;
ready = false;
voices = [];
separator = '. ';
2023-11-21 11:16:56 +01:00
/**
* Perform any text processing before passing to TTS engine.
* @param {string} text Input text
* @returns {string} Processed text
*/
processText(text) {
// Replace fancy ellipsis with "..."
2023-11-28 15:56:50 +01:00
text = text.replace(/…/g, '...');
// Remove quotes
text = text.replace(/["“”‘’]/g, '');
// Replace multiple "." with single "."
2023-11-28 15:56:50 +01:00
text = text.replace(/\.+/g, '.');
return text;
}
2023-11-21 11:16:56 +01:00
languageLabels = {
2023-12-02 19:04:51 +01:00
'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',
2023-12-02 20:11:06 +01:00
};
2023-11-21 11:16:56 +01:00
defaultSettings = {
2023-12-02 19:04:51 +01:00
provider_endpoint: 'http://localhost:8020',
language: 'en',
2023-12-02 21:06:57 +01:00
voiceMap: {},
2023-12-02 20:11:06 +01:00
};
2023-11-21 11:16:56 +01:00
2023-11-22 16:47:58 +01:00
get settingsHtml() {
2023-11-21 11:16:56 +01:00
let html = `
<label for="xtts_api_language">Language</label>
<select id="xtts_api_language">`;
2023-11-22 16:47:58 +01:00
2023-11-21 11:16:56 +01:00
for (let language in this.languageLabels) {
2023-11-22 16:47:58 +01:00
if (this.languageLabels[language] == this.settings?.language) {
2023-11-21 11:16:56 +01:00
html += `<option value="${this.languageLabels[language]}" selected="selected">${language}</option>`;
2023-12-02 20:11:06 +01:00
continue;
2023-11-21 11:16:56 +01:00
}
html += `<option value="${this.languageLabels[language]}">${language}</option>`;
}
2023-11-22 16:47:58 +01:00
2023-11-21 11:16:56 +01:00
html += `
</select>
<label for="xtts_tts_endpoint">Provider Endpoint:</label>
<input id="xtts_tts_endpoint" type="text" class="text_pole" maxlength="250" value="${this.defaultSettings.provider_endpoint}"/>
2023-11-22 16:47:58 +01:00
2023-11-21 11:16:56 +01:00
`;
html += `
2023-11-22 16:47:58 +01:00
2023-11-21 11:16:56 +01:00
<span>
<span>Use <a target="_blank" href="https://github.com/daswer123/xtts-api-server">XTTSv2 TTS Server</a>.</span>
`;
2023-11-22 16:47:58 +01:00
2023-11-21 11:16:56 +01:00
return html;
}
onSettingsChange() {
// Used when provider settings are updated from UI
2023-12-02 20:11:06 +01:00
this.settings.provider_endpoint = $('#xtts_tts_endpoint').val();
this.settings.language = $('#xtts_api_language').val();
saveTtsProviderSettings();
2023-11-21 11:16:56 +01:00
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
2023-12-02 20:11:06 +01:00
console.info('Using default TTS Provider settings');
2023-11-21 11:16:56 +01:00
}
// Only accept keys defined in defaultSettings
2023-12-02 20:11:06 +01:00
this.settings = this.defaultSettings;
2023-11-21 11:16:56 +01:00
2023-11-22 16:47:58 +01:00
for (const key in settings) {
if (key in this.settings) {
2023-12-02 20:11:06 +01:00
this.settings[key] = settings[key];
2023-11-21 11:16:56 +01:00
} else {
2023-12-02 20:11:06 +01:00
throw `Invalid setting passed to TTS Provider: ${key}`;
2023-11-21 11:16:56 +01:00
}
}
const apiCheckInterval = setInterval(() => {
// Use Extras API if TTS support is enabled
if (modules.includes('tts') || modules.includes('xtts-tts')) {
const baseUrl = new URL(getApiUrl());
baseUrl.pathname = '/api/tts';
this.settings.provider_endpoint = baseUrl.toString();
$('#xtts_tts_endpoint').val(this.settings.provider_endpoint);
clearInterval(apiCheckInterval);
}
}, 2000);
2023-12-02 20:11:06 +01:00
$('#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(); });
2023-11-21 11:16:56 +01:00
2023-12-02 20:11:06 +01:00
await this.checkReady();
2023-11-21 11:16:56 +01:00
2023-12-02 20:11:06 +01:00
console.debug('XTTS: Settings loaded');
2023-11-21 11:16:56 +01:00
}
// Perform a simple readiness check by trying to fetch voiceIds
2023-11-22 16:47:58 +01:00
async checkReady() {
2023-12-02 20:11:06 +01:00
await this.fetchTtsVoiceObjects();
2023-11-21 11:16:56 +01:00
}
async onRefreshClick() {
2023-12-02 20:11:06 +01:00
return;
2023-11-21 11:16:56 +01:00
}
//#################//
// TTS Interfaces //
//#################//
async getVoice(voiceName) {
if (this.voices.length == 0) {
2023-12-02 20:11:06 +01:00
this.voices = await this.fetchTtsVoiceObjects();
2023-11-21 11:16:56 +01:00
}
const match = this.voices.filter(
2023-12-02 21:06:57 +01:00
XTTSVoice => XTTSVoice.name == voiceName,
2023-12-02 20:11:06 +01:00
)[0];
2023-11-21 11:16:56 +01:00
if (!match) {
2023-12-02 20:11:06 +01:00
throw `TTS Voice name ${voiceName} not found`;
2023-11-21 11:16:56 +01:00
}
2023-12-02 20:11:06 +01:00
return match;
2023-11-21 11:16:56 +01:00
}
2023-11-22 16:47:58 +01:00
async generateTts(text, voiceId) {
2023-12-02 20:11:06 +01:00
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
2023-11-21 11:16:56 +01:00
}
//###########//
// API CALLS //
//###########//
async fetchTtsVoiceObjects() {
2023-12-02 20:11:06 +01:00
const response = await doExtrasFetch(`${this.settings.provider_endpoint}/speakers`);
2023-11-21 11:16:56 +01:00
if (!response.ok) {
2023-12-02 20:11:06 +01:00
throw new Error(`HTTP ${response.status}: ${await response.json()}`);
2023-11-21 11:16:56 +01:00
}
2023-12-02 20:11:06 +01:00
const responseJson = await response.json();
return responseJson;
2023-11-21 11:16:56 +01:00
}
async fetchTtsGeneration(inputText, voiceId) {
2023-12-02 20:11:06 +01:00
console.info(`Generating new TTS for voice_id ${voiceId}`);
2023-11-21 11:16:56 +01:00
const response = await doExtrasFetch(
`${this.settings.provider_endpoint}/tts_to_audio/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
2023-12-02 21:06:57 +01:00
'Cache-Control': 'no-cache', // Added this line to disable caching of file so new files are always played - Rolyat 7/7/23
2023-11-21 11:16:56 +01:00
},
body: JSON.stringify({
2023-12-02 19:04:51 +01:00
'text': inputText,
'speaker_wav': voiceId,
2023-12-02 21:06:57 +01:00
'language': this.settings.language,
}),
},
2023-12-02 20:11:06 +01:00
);
2023-11-21 11:16:56 +01:00
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
2023-12-02 20:11:06 +01:00
return response;
2023-11-21 11:16:56 +01:00
}
// Interface not used by XTTS TTS
async fetchTtsFromHistory(history_item_id) {
return Promise.resolve(history_item_id);
}
}