Reformat rvc/tts plugins

This commit is contained in:
Cohee
2023-08-17 12:05:17 +03:00
parent c4700b38fe
commit 50005b5617
2 changed files with 162 additions and 163 deletions

View File

@ -4,7 +4,6 @@ TODO:
- Delete useless call
*/
import { saveSettingsDebounced } from "../../../script.js"
import { doExtrasFetch, extension_settings, getApiUrl, getContext, modules, ModuleWorkerWrapper } from "../../extensions.js"
export { CoquiTtsProvider }
@ -34,16 +33,16 @@ coquiApiModels format [language][dataset][name]:coqui-api-model-id, example:
*/
const languageLabels = {
"multilingual": "Multilingual",
"en" : "English",
"fr" : "French",
"es" : "Spanish",
"ja" : "Japanese"
"en": "English",
"fr": "French",
"es": "Spanish",
"ja": "Japanese"
}
function throwIfModuleMissing() {
if (!modules.includes('coqui-tts')) {
toastr.error(`Add coqui-tts to enable-modules and restart the Extras API.`, "Coqui TTS module not loaded.", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
throw new Error(DEBUG_PREFIX,`Coqui TTS module not loaded.`);
throw new Error(DEBUG_PREFIX, `Coqui TTS module not loaded.`);
}
}
@ -59,7 +58,7 @@ function updateCharactersList() {
}
currentcharacters = Array.from(currentcharacters)
if (JSON.stringify(charactersList) !== JSON.stringify(currentcharacters)) {
charactersList = currentcharacters
@ -69,12 +68,12 @@ function updateCharactersList() {
.end()
.append('<option value="none">Select Character</option>')
.val('none')
for(const charName of charactersList) {
$("#coqui_character_select").append(new Option(charName,charName));
for (const charName of charactersList) {
$("#coqui_character_select").append(new Option(charName, charName));
}
console.debug(DEBUG_PREFIX,"Updated character list to:", charactersList);
console.debug(DEBUG_PREFIX, "Updated character list to:", charactersList);
}
}
@ -87,7 +86,7 @@ class CoquiTtsProvider {
defaultSettings = {
voiceMap: "",
voiceMapDict : {}
voiceMapDict: {}
}
get settingsHtml() {
@ -99,7 +98,7 @@ class CoquiTtsProvider {
<select id="coqui_character_select">
<!-- Populated by JS -->
</select>
<input id="coqui_remove_char_mapping" class="menu_button" type="button" value="Remove from Voice Map" />
<label for="coqui_model_origin">Models:</label>
@ -130,7 +129,7 @@ class CoquiTtsProvider {
<input id="coqui_api_model_install_button" class="menu_button" type="button" value="Install" />
</div>
<div id="coqui_local_model_div">
<select id="coqui_local_model_name">
<!-- Populated by JS and request -->
@ -148,11 +147,11 @@ class CoquiTtsProvider {
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
for (const key in settings){
if (key in this.settings){
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
} else {
throw DEBUG_PREFIX+`Invalid setting passed to extension: ${key}`
throw DEBUG_PREFIX + `Invalid setting passed to extension: ${key}`
}
}
@ -160,7 +159,7 @@ class CoquiTtsProvider {
$("#coqui_api_model_div").hide();
$("#coqui_local_model_div").hide();
$("#coqui_api_language").show();
$("#coqui_api_model_name").hide();
$("#coqui_api_model_settings").hide();
@ -168,11 +167,11 @@ class CoquiTtsProvider {
$("#coqui_api_model_install_button").hide();
let that = this
$("#coqui_model_origin").on("change",function(){that.onModelOriginChange()});
$("#coqui_api_language").on("change",function(){that.onModelLanguageChange()});
$("#coqui_api_model_name").on("change",function(){that.onModelNameChange()});
$("#coqui_model_origin").on("change", function () { that.onModelOriginChange() });
$("#coqui_api_language").on("change", function () { that.onModelLanguageChange() });
$("#coqui_api_model_name").on("change", function () { that.onModelNameChange() });
$("#coqui_remove_char_mapping").on("click", function(){that.onRemoveClick()});
$("#coqui_remove_char_mapping").on("click", function () { that.onRemoveClick() });
// Load characters list
$('#coqui_character_select')
@ -181,35 +180,35 @@ class CoquiTtsProvider {
.end()
.append('<option value="none">Select Character</option>')
.val('none')
for(const charName of charactersList) {
$("#coqui_character_select").append(new Option(charName,charName));
for (const charName of charactersList) {
$("#coqui_character_select").append(new Option(charName, charName));
}
// Load coqui-api settings from json file
fetch("/scripts/extensions/tts/coqui_api_models_settings.json")
.then(response => response.json())
.then(json => {
coquiApiModels = json;
console.debug(DEBUG_PREFIX,"initialized coqui-api model list to", coquiApiModels);
.then(response => response.json())
.then(json => {
coquiApiModels = json;
console.debug(DEBUG_PREFIX, "initialized coqui-api model list to", coquiApiModels);
$('#coqui_api_language')
.find('option')
.remove()
.end()
.append('<option value="none">Select model language</option>')
.val('none');
$('#coqui_api_language')
.find('option')
.remove()
.end()
.append('<option value="none">Select model language</option>')
.val('none');
for(let language in coquiApiModels) {
$("#coqui_api_language").append(new Option(languageLabels[language],language));
console.log(DEBUG_PREFIX,"added language",language);
}
});
for (let language in coquiApiModels) {
$("#coqui_api_language").append(new Option(languageLabels[language], language));
console.log(DEBUG_PREFIX, "added language", language);
}
});
}
updateVoiceMap(){
updateVoiceMap() {
this.settings.voiceMap = "";
for(let i in this.settings.voiceMapDict) {
for (let i in this.settings.voiceMapDict) {
const voice_settings = this.settings.voiceMapDict[i];
this.settings.voiceMap += i + ":" + voice_settings["model_id"];
@ -224,9 +223,9 @@ class CoquiTtsProvider {
$("#tts_voice_map").val(this.settings.voiceMap);
extension_settings.tts.Coqui = this.settings;
}
onSettingsChange() {
console.debug(DEBUG_PREFIX,"Settings changes",this.settings);
console.debug(DEBUG_PREFIX, "Settings changes", this.settings);
extension_settings.tts.Coqui = this.settings;
}
@ -242,15 +241,15 @@ class CoquiTtsProvider {
let model_setting_language = $("#coqui_api_model_settings_language").val();
let model_setting_speaker = $("#coqui_api_model_settings_speaker").val();
if (character === "none") {
toastr.error(`Character not selected, please select one.`, DEBUG_PREFIX+" voice mapping character", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Character not selected, please select one.`, DEBUG_PREFIX + " voice mapping character", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
this.updateVoiceMap(); // Overide any manual modification
return;
}
if (model_origin == "none") {
toastr.error(`Origin not selected, please select one.`, DEBUG_PREFIX+" voice mapping origin", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Origin not selected, please select one.`, DEBUG_PREFIX + " voice mapping origin", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
this.updateVoiceMap(); // Overide any manual modification
return;
}
@ -259,72 +258,72 @@ class CoquiTtsProvider {
const model_id = $("#coqui_local_model_name").val();
if (model_name == "none") {
toastr.error(`Model not selected, please select one.`, DEBUG_PREFIX+" voice mapping model", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Model not selected, please select one.`, DEBUG_PREFIX + " voice mapping model", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
this.updateVoiceMap(); // Overide any manual modification
return;
}
this.settings.voiceMapDict[character] = {model_type: "local", model_id: "local/"+model_id};
console.debug(DEBUG_PREFIX,"Registered new voice map: ",character,":",this.settings.voiceMapDict[character]);
this.settings.voiceMapDict[character] = { model_type: "local", model_id: "local/" + model_id };
console.debug(DEBUG_PREFIX, "Registered new voice map: ", character, ":", this.settings.voiceMapDict[character]);
this.updateVoiceMap(); // Overide any manual modification
return;
}
if (model_language == "none") {
toastr.error(`Language not selected, please select one.`, DEBUG_PREFIX+" voice mapping language", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Language not selected, please select one.`, DEBUG_PREFIX + " voice mapping language", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
this.updateVoiceMap(); // Overide any manual modification
return;
}
if (model_name == "none") {
toastr.error(`Model not selected, please select one.`, DEBUG_PREFIX+" voice mapping model", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Model not selected, please select one.`, DEBUG_PREFIX + " voice mapping model", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
this.updateVoiceMap(); // Overide any manual modification
return;
}
if (model_setting_language == "none")
model_setting_language = null;
if (model_setting_speaker == "none")
model_setting_speaker = null;
const tokens = $('#coqui_api_model_name').val().split("/");
const model_dataset = tokens[0];
const model_label = tokens[1];
const model_id = "tts_models/" + model_language + "/" + model_dataset + "/" + model_label
if (model_setting_language == null & "languages" in coquiApiModels[model_language][model_dataset][model_label]) {
toastr.error(`Model language not selected, please select one.`, DEBUG_PREFIX+" voice mapping model language", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Model language not selected, please select one.`, DEBUG_PREFIX + " voice mapping model language", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
return;
}
if (model_setting_speaker == null & "speakers" in coquiApiModels[model_language][model_dataset][model_label]) {
toastr.error(`Model speaker not selected, please select one.`, DEBUG_PREFIX+" voice mapping model speaker", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Model speaker not selected, please select one.`, DEBUG_PREFIX + " voice mapping model speaker", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
return;
}
console.debug(DEBUG_PREFIX,"Current voice map: ",this.settings.voiceMap);
console.debug(DEBUG_PREFIX, "Current voice map: ", this.settings.voiceMap);
this.settings.voiceMapDict[character] = {model_type: "coqui-api", model_id: model_id, model_language:model_setting_language, model_speaker:model_setting_speaker};
this.settings.voiceMapDict[character] = { model_type: "coqui-api", model_id: model_id, model_language: model_setting_language, model_speaker: model_setting_speaker };
console.debug(DEBUG_PREFIX,"Registered new voice map: ",character,":",this.settings.voiceMapDict[character]);
console.debug(DEBUG_PREFIX, "Registered new voice map: ", character, ":", this.settings.voiceMapDict[character]);
this.updateVoiceMap();
let successMsg = character+":"+model_id;
let successMsg = character + ":" + model_id;
if (model_setting_language != null)
successMsg += "[" + model_setting_language + "]";
if (model_setting_speaker != null)
successMsg += "[" + model_setting_speaker + "]";
toastr.info(successMsg, DEBUG_PREFIX+" voice map updated", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.info(successMsg, DEBUG_PREFIX + " voice map updated", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
return
}
// DBG: assume voiceName is correct
// TODO: check voice is correct
async getVoice(voiceName) {
console.log(DEBUG_PREFIX,"getVoice",voiceName);
const output = {voice_id: voiceName};
console.log(DEBUG_PREFIX, "getVoice", voiceName);
const output = { voice_id: voiceName };
return output;
}
@ -332,12 +331,12 @@ class CoquiTtsProvider {
const character = $("#coqui_character_select").val();
if (character === "none") {
toastr.error(`Character not selected, please select one.`, DEBUG_PREFIX+" voice mapping character", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(`Character not selected, please select one.`, DEBUG_PREFIX + " voice mapping character", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
return;
}
// Todo erase from voicemap
delete(this.settings.voiceMapDict[character]);
delete (this.settings.voiceMapDict[character]);
this.updateVoiceMap(); // TODO
}
@ -350,13 +349,13 @@ class CoquiTtsProvider {
$("#coqui_local_model_div").hide();
$("#coqui_api_model_div").hide();
}
// show coqui model list
if (model_origin == "coqui-api") {
$("#coqui_local_model_div").hide();
$("#coqui_api_model_div").show();
}
// show local model list
if (model_origin == "local") {
@ -372,12 +371,12 @@ class CoquiTtsProvider {
const model_origin = $('#coqui_model_origin').val();
const model_language = $('#coqui_api_language').val();
console.debug(model_language);
if (model_language == "none") {
$("#coqui_api_model_name").hide();
return;
}
$("#coqui_api_model_name").show();
$('#coqui_api_model_name')
.find('option')
@ -386,12 +385,12 @@ class CoquiTtsProvider {
.append('<option value="none">Select model</option>')
.val('none');
for(let model_dataset in coquiApiModels[model_language])
for(let model_name in coquiApiModels[model_language][model_dataset]) {
for (let model_dataset in coquiApiModels[model_language])
for (let model_name in coquiApiModels[model_language][model_dataset]) {
const model_id = model_dataset + "/" + model_name
const model_label = model_name + " ("+model_dataset+" dataset)"
$("#coqui_api_model_name").append(new Option(model_label,model_id));
}
const model_label = model_name + " (" + model_dataset + " dataset)"
$("#coqui_api_model_name").append(new Option(model_label, model_id));
}
}
async onModelNameChange() {
@ -418,15 +417,15 @@ class CoquiTtsProvider {
$("#coqui_api_model_settings").show();
$("#coqui_api_model_settings_language").show();
$('#coqui_api_model_settings_language')
.find('option')
.remove()
.end()
.append('<option value="none">Select language</option>')
.val('none');
.find('option')
.remove()
.end()
.append('<option value="none">Select language</option>')
.val('none');
for(var i = 0; i < model_settings["languages"].length; i++) {
const language_label = JSON.stringify(model_settings["languages"][i]).replaceAll("\"","");
$("#coqui_api_model_settings_language").append(new Option(language_label,i));
for (var i = 0; i < model_settings["languages"].length; i++) {
const language_label = JSON.stringify(model_settings["languages"][i]).replaceAll("\"", "");
$("#coqui_api_model_settings_language").append(new Option(language_label, i));
}
}
else {
@ -437,15 +436,15 @@ class CoquiTtsProvider {
$("#coqui_api_model_settings").show();
$("#coqui_api_model_settings_speaker").show();
$('#coqui_api_model_settings_speaker')
.find('option')
.remove()
.end()
.append('<option value="none">Select speaker</option>')
.val('none');
.find('option')
.remove()
.end()
.append('<option value="none">Select speaker</option>')
.val('none');
for(var i = 0; i < model_settings["speakers"].length;i++) {
const speaker_label = JSON.stringify(model_settings["speakers"][i]).replaceAll("\"","");
$("#coqui_api_model_settings_speaker").append(new Option(speaker_label,i));
for (var i = 0; i < model_settings["speakers"].length; i++) {
const speaker_label = JSON.stringify(model_settings["speakers"][i]).replaceAll("\"", "");
$("#coqui_api_model_settings_speaker").append(new Option(speaker_label, i));
}
}
else {
@ -457,12 +456,12 @@ class CoquiTtsProvider {
// Check if already installed and propose to do it otherwise
const model_id = coquiApiModels[model_language][model_dataset][model_name]["id"]
console.debug(DEBUG_PREFIX,"Check if model is already installed",model_id);
console.debug(DEBUG_PREFIX, "Check if model is already installed", model_id);
let result = await CoquiTtsProvider.checkmodel_state(model_id);
result = await result.json();
const model_state = result["model_state"];
console.debug(DEBUG_PREFIX," Model state:", model_state)
console.debug(DEBUG_PREFIX, " Model state:", model_state)
if (model_state == "installed") {
$("#coqui_api_model_install_status").text("Model already installed on extras server");
@ -476,13 +475,13 @@ class CoquiTtsProvider {
$("#coqui_api_model_install_status").text("Model found but incomplete try install again (maybe still downloading)"); // (remove and download again)
}
else {
toastr.info("Click download button to install the model "+$("#coqui_api_model_name").find(":selected").text(), DEBUG_PREFIX+" model not installed", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.info("Click download button to install the model " + $("#coqui_api_model_name").find(":selected").text(), DEBUG_PREFIX + " model not installed", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
$("#coqui_api_model_install_status").text("Model not found on extras server");
}
const onModelNameChange_pointer = this.onModelNameChange;
$("#coqui_api_model_install_button").off("click").on("click", async function (){
$("#coqui_api_model_install_button").off("click").on("click", async function () {
try {
$("#coqui_api_model_install_status").text("Downloading model...");
$("#coqui_api_model_install_button").hide();
@ -490,33 +489,33 @@ class CoquiTtsProvider {
let apiResult = await CoquiTtsProvider.installModel(model_id, action);
apiResult = await apiResult.json();
console.debug(DEBUG_PREFIX,"Response:",apiResult);
console.debug(DEBUG_PREFIX, "Response:", apiResult);
if (apiResult["status"] == "done") {
$("#coqui_api_model_install_status").text("Model installed and ready to use!");
$("#coqui_api_model_install_button").hide();
onModelNameChange_pointer();
}
if (apiResult["status"] == "downloading") {
toastr.error("Check extras console for progress", DEBUG_PREFIX+" already downloading", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error("Check extras console for progress", DEBUG_PREFIX + " already downloading", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
$("#coqui_api_model_install_status").text("Already downloading a model, check extras console!");
$("#coqui_api_model_install_button").show();
}
} catch (error) {
console.error(error)
toastr.error(error, DEBUG_PREFIX+" error with model download", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
toastr.error(error, DEBUG_PREFIX + " error with model download", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
onModelNameChange_pointer();
}
// will refresh model status
// will refresh model status
});
$("#coqui_api_model_install_button").show();
return;
}
}
//#############################//
// API Calls //
@ -542,7 +541,7 @@ class CoquiTtsProvider {
});
if (!apiResult.ok) {
toastr.error(apiResult.statusText, DEBUG_PREFIX+' Check model state request failed');
toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Check model state request failed');
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
@ -567,7 +566,7 @@ class CoquiTtsProvider {
});
if (!apiResult.ok) {
toastr.error(apiResult.statusText, DEBUG_PREFIX+' Install model '+model_id+' request failed');
toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Install model ' + model_id + ' request failed');
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
@ -595,7 +594,7 @@ class CoquiTtsProvider {
})
if (!apiResult.ok) {
toastr.error(apiResult.statusText, DEBUG_PREFIX+' Get local model list request failed');
toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Get local model list request failed');
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
@ -614,10 +613,10 @@ class CoquiTtsProvider {
let language = "none"
let speaker = "none"
const tokens = voiceId.replaceAll("]","").replaceAll("\"","").split("[");
const tokens = voiceId.replaceAll("]", "").replaceAll("\"", "").split("[");
const model_id = tokens[0]
console.debug(DEBUG_PREFIX,"Preparing TTS request for",tokens)
console.debug(DEBUG_PREFIX, "Preparing TTS request for", tokens)
// First option
if (tokens.length > 1) {
@ -657,12 +656,12 @@ class CoquiTtsProvider {
// Dirty hack to say not implemented
async fetchTtsVoiceIds() {
return [{name:"Voice samples not implemented for coqui TTS yet, search for the model samples online", voice_id:"",lang:"",}]
return [{ name: "Voice samples not implemented for coqui TTS yet, search for the model samples online", voice_id: "", lang: "", }]
}
// Do nothing
previewTtsVoice(id) {
return
return
}
async fetchTtsFromHistory(history_item_id) {
@ -681,7 +680,7 @@ async function moduleWorker() {
return
// Initialized local model once
if (!coquiLocalModelsReceived){
if (!coquiLocalModelsReceived) {
let result = await CoquiTtsProvider.getLocalModelList();
result = await result.json();
@ -695,8 +694,8 @@ async function moduleWorker() {
.append('<option value="none">Select model</option>')
.val('none');
for(const model_dataset of coquiLocalModels)
$("#coqui_local_model_name").append(new Option(model_dataset,model_dataset));
for (const model_dataset of coquiLocalModels)
$("#coqui_local_model_name").append(new Option(model_dataset, model_dataset));
coquiLocalModelsReceived = true;
}
@ -706,4 +705,4 @@ $(document).ready(function () {
const wrapper = new ModuleWorkerWrapper(moduleWorker);
setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL);
moduleWorker();
})
})