File size: 29,850 Bytes
b82d373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
TODO:
 - Hide voice map its just confusing
 - Delete useless call
*/

import { doExtrasFetch, extension_settings, getApiUrl, modules } from '../../extensions.js';
import { initVoiceMap } from './index.js';
import { POPUP_TYPE, callGenericPopup } from '../../popup.js';

export { CoquiTtsProvider };

const DEBUG_PREFIX = '<Coqui TTS module> ';

let inApiCall = false;
let coquiApiModels = {}; // Initialized only once
let coquiApiModelsFull = {}; // Initialized only once
let coquiLocalModels = []; // Initialized only once
let coquiLocalModelsReceived = false;
/*
coquiApiModels format [language][dataset][name]:coqui-api-model-id, example:
{
    "en": {
        "vctk": {
            "vits": "tts_models/en/vctk/vits"
        }
    },
    "ja": {
        "kokoro": {
            "tacotron2-DDC": "tts_models/ja/kokoro/tacotron2-DDC"
        }
    }
}
*/
const languageLabels = {
    'multilingual': 'Multilingual',
    'en': 'English',
    'fr': 'French',
    'es': 'Spanish',
    'ja': 'Japanese',
};

function throwIfModuleMissing() {
    if (!modules.includes('coqui-tts')) {
        const message = 'Coqui TTS module not loaded. Add coqui-tts to enable-modules and restart the Extras API.';
        // toastr.error(message, { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
        throw new Error(DEBUG_PREFIX, message);
    }
}

function resetModelSettings() {
    $('#coqui_api_model_settings_language').val('none');
    $('#coqui_api_model_settings_speaker').val('none');
}

class CoquiTtsProvider {
    //#############################//
    //  Extension UI and Settings  //
    //#############################//

    settings;

    defaultSettings = {
        voiceMap: {},
        customVoices: {},
        voiceIds: [],
        voiceMapDict: {},
    };

    get settingsHtml() {
        let html = `
        <div class="flex wide100p flexGap10 alignitemscenter">
            <div>
                <div style="flex: 50%;">
                    <small>To use CoquiTTS, select the origin, language, and model, then click Add Voice. The voice will then be available to add to a character. Voices are saved globally. </small><br>
                    <label for="coqui_voicename_select">Select Saved Voice:</label>
                    <select id="coqui_voicename_select">
                        <!-- Populated by JS -->
                    </select>
                    <div class="tts_block">
                        <input id="coqui_remove_voiceId_mapping" class="menu_button" type="button" value="Remove Voice" />
                        <input id="coqui_add_voiceId_mapping" class="menu_button" type="button" value="Add Voice" />
                    </div>
                    <label for="coqui_model_origin">Models:</label>
                    <select id="coqui_model_origin">gpu_mode
                        <option value="none">Select Origin</option>
                        <option value="coqui-api">Coqui API (Tested)</option>
                        <option value="coqui-api-full">Coqui API (Experimental)</option>
                        <option value="local">My Models</option>
                    </select>

                    <div id="coqui_api_model_div">
                        <select id="coqui_api_language">
                            <!-- Populated by JS and request -->
                        </select>

                        <select id="coqui_api_model_name">
                            <!-- Populated by JS and request -->
                        </select>

                        <div id="coqui_api_model_settings">
                            <select id="coqui_api_model_settings_language">
                                <!-- Populated by JS and request -->
                            </select>
                            <select id="coqui_api_model_settings_speaker">
                                <!-- Populated by JS and request -->
                            </select>
                        </div>
                        <span id="coqui_api_model_install_status">Model installed on extras server</span>
                        <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 -->
                        </select>
                    </div>

                </div>
            </div>
        </div>
        `;
        return html;
    }

    async loadSettings(settings) {
        // Only accept keys defined in defaultSettings
        this.settings = this.defaultSettings;

        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}`;
            }
        }

        await initLocalModels();
        this.updateCustomVoices(); // Overide any manual modification

        $('#coqui_api_model_div').hide();
        $('#coqui_local_model_div').hide();

        $('#coqui_api_language').show();
        $('#coqui_api_model_name').hide();
        $('#coqui_api_model_settings').hide();
        $('#coqui_api_model_install_status').hide();
        $('#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_remove_voiceId_mapping').on('click', function () { that.onRemoveClick(); });
        $('#coqui_add_voiceId_mapping').on('click', function () { that.onAddClick(); });

        // Load coqui-api settings from json file
        await 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);
            /*
            $('#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);
            }*/
            });

        // Load coqui-api FULL settings from json file
        await fetch('/scripts/extensions/tts/coqui_api_models_settings_full.json')
            .then(response => response.json())
            .then(json => {
                coquiApiModelsFull = json;
                console.debug(DEBUG_PREFIX,'initialized coqui-api full model list to', coquiApiModelsFull);
            /*
            $('#coqui_api_full_language')
                .find('option')
                .remove()
                .end()
                .append('<option value="none">Select model language</option>')
                .val('none');

            for(let language in coquiApiModelsFull) {
                $("#coqui_api_full_language").append(new Option(languageLabels[language],language));
                console.log(DEBUG_PREFIX,"added language",language);
            }*/
            });
    }

    // Perform a simple readiness check by trying to fetch voiceIds
    async checkReady(){
        throwIfModuleMissing();
        await this.fetchTtsVoiceObjects();
    }

    updateCustomVoices() {
        // Takes voiceMapDict and converts it to a string to save to voiceMap
        this.settings.customVoices = {};
        for (let voiceName in this.settings.voiceMapDict) {
            const voiceId = this.settings.voiceMapDict[voiceName];
            this.settings.customVoices[voiceName] = voiceId['model_id'];

            if (voiceId['model_language'] != null)
                this.settings.customVoices[voiceName] += '[' + voiceId['model_language'] + ']';

            if (voiceId['model_speaker'] != null)
                this.settings.customVoices[voiceName] += '[' + voiceId['model_speaker'] + ']';
        }

        // Update UI select list with voices
        $('#coqui_voicename_select').empty();
        $('#coqui_voicename_select')
            .find('option')
            .remove()
            .end()
            .append('<option value="none">Select Voice</option>')
            .val('none');
        for (const voiceName in this.settings.voiceMapDict) {
            $('#coqui_voicename_select').append(new Option(voiceName, voiceName));
        }

        this.onSettingsChange();
    }

    onSettingsChange() {
        console.debug(DEBUG_PREFIX, 'Settings changes', this.settings);
        extension_settings.tts.Coqui = this.settings;
    }

    async onRefreshClick() {
        this.checkReady();
    }

    async onAddClick() {
        if (inApiCall) {
            return; //TODO: block dropdown
        }

        // Ask user for voiceId name to save voice
        const voiceName = await callGenericPopup('Name of Coqui voice to add to voice select dropdown:', POPUP_TYPE.INPUT);

        const model_origin = $('#coqui_model_origin').val();
        const model_language = $('#coqui_api_language').val();
        const model_name = $('#coqui_api_model_name').val();
        let model_setting_language = $('#coqui_api_model_settings_language').val();
        let model_setting_speaker = $('#coqui_api_model_settings_speaker').val();


        if (!voiceName) {
            toastr.error('Voice name empty, please enter one.', DEBUG_PREFIX + ' voice mapping voice name', { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
            this.updateCustomVoices(); // 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 });
            this.updateCustomVoices(); // Overide any manual modification
            return;
        }

        if (model_origin == 'local') {
            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 });
                this.updateCustomVoices(); // Overide any manual modification
                return;
            }

            this.settings.voiceMapDict[voiceName] = { model_type: 'local', model_id: 'local/' + model_id };
            console.debug(DEBUG_PREFIX, 'Registered new voice map: ', voiceName, ':', this.settings.voiceMapDict[voiceName]);
            this.updateCustomVoices(); // 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 });
            this.updateCustomVoices(); // 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 });
            this.updateCustomVoices(); // 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;

        let modelDict = coquiApiModels;
        if (model_origin == 'coqui-api-full')
            modelDict = coquiApiModelsFull;

        if (model_setting_language == null & 'languages' in modelDict[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 });
            return;
        }

        if (model_setting_speaker == null & 'speakers' in modelDict[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 });
            return;
        }

        console.debug(DEBUG_PREFIX, 'Current custom voices: ', this.settings.customVoices);

        this.settings.voiceMapDict[voiceName] = { 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: ', voiceName, ':', this.settings.voiceMapDict[voiceName]);

        this.updateCustomVoices();
        initVoiceMap(); // Update TTS extension voiceMap

        let successMsg = voiceName + ':' + 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 });

        return;
    }

    async getVoice(voiceName) {
        let match = await this.fetchTtsVoiceObjects();
        match = match.filter(
            voice => voice.name == voiceName,
        )[0];
        if (!match) {
            throw `TTS Voice name ${voiceName} not found in CoquiTTS Provider voice list`;
        }
        return match;
    }

    async onRemoveClick() {
        const voiceName = $('#coqui_voicename_select').val();

        if (voiceName === 'none') {
            toastr.error('Voice not selected, please select one.', DEBUG_PREFIX + ' voice mapping voiceId', { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
            return;
        }

        // Todo erase from voicemap
        delete (this.settings.voiceMapDict[voiceName]);
        this.updateCustomVoices();
        initVoiceMap(); // Update TTS extension voiceMap
    }

    async onModelOriginChange() {
        throwIfModuleMissing();
        resetModelSettings();
        const model_origin = $('#coqui_model_origin').val();

        if (model_origin == 'none') {
            $('#coqui_local_model_div').hide();
            $('#coqui_api_model_div').hide();
        }

        // show coqui model selected list (SAFE)
        if (model_origin == 'coqui-api') {
            $('#coqui_local_model_div').hide();

            $('#coqui_api_language')
                .find('option')
                .remove()
                .end()
                .append('<option value="none">Select model language</option>')
                .val('none');

            for(let language in coquiApiModels) {
                let languageLabel = language;
                if (language in languageLabels)
                    languageLabel = languageLabels[language];
                $('#coqui_api_language').append(new Option(languageLabel,language));
                console.log(DEBUG_PREFIX,'added language',languageLabel,'(',language,')');
            }

            $('#coqui_api_model_div').show();
        }

        // show coqui model full list (UNSAFE)
        if (model_origin == 'coqui-api-full') {
            $('#coqui_local_model_div').hide();

            $('#coqui_api_language')
                .find('option')
                .remove()
                .end()
                .append('<option value="none">Select model language</option>')
                .val('none');

            for(let language in coquiApiModelsFull) {
                let languageLabel = language;
                if (language in languageLabels)
                    languageLabel = languageLabels[language];
                $('#coqui_api_language').append(new Option(languageLabel,language));
                console.log(DEBUG_PREFIX,'added language',languageLabel,'(',language,')');
            }

            $('#coqui_api_model_div').show();
        }


        // show local model list
        if (model_origin == 'local') {
            $('#coqui_api_model_div').hide();
            $('#coqui_local_model_div').show();
        }
    }

    async onModelLanguageChange() {
        throwIfModuleMissing();
        resetModelSettings();
        $('#coqui_api_model_settings').hide();
        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')
            .remove()
            .end()
            .append('<option value="none">Select model</option>')
            .val('none');

        let modelDict = coquiApiModels;
        if (model_origin == 'coqui-api-full')
            modelDict = coquiApiModelsFull;

        for(let model_dataset in modelDict[model_language])
            for(let model_name in modelDict[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));
            }
    }

    async onModelNameChange() {
        throwIfModuleMissing();
        resetModelSettings();
        $('#coqui_api_model_settings').hide();
        const model_origin = $('#coqui_model_origin').val();

        // No model selected
        if ($('#coqui_api_model_name').val() == 'none') {
            $('#coqui_api_model_install_button').off('click');
            $('#coqui_api_model_install_button').hide();
            return;
        }

        // Get languages and speakers options
        const model_language = $('#coqui_api_language').val();
        const tokens = $('#coqui_api_model_name').val().split('/');
        const model_dataset = tokens[0];
        const model_name = tokens[1];

        let modelDict = coquiApiModels;
        if (model_origin == 'coqui-api-full')
            modelDict = coquiApiModelsFull;

        const model_settings = modelDict[model_language][model_dataset][model_name];

        if ('languages' in model_settings) {
            $('#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');

            for (let 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 {
            $('#coqui_api_model_settings_language').hide();
        }

        if ('speakers' in model_settings) {
            $('#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');

            for (let 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 {
            $('#coqui_api_model_settings_speaker').hide();
        }

        $('#coqui_api_model_install_status').text('Requesting model to extras server...');
        $('#coqui_api_model_install_status').show();

        // Check if already installed and propose to do it otherwise
        const model_id = modelDict[model_language][model_dataset][model_name]['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);

        if (model_state == 'installed') {
            $('#coqui_api_model_install_status').text('Model already installed on extras server');
            $('#coqui_api_model_install_button').hide();
        }
        else {
            let action = 'download';
            if (model_state == 'corrupted') {
                action = 'repare';
                //toastr.error("Click install button to reinstall the model "+$("#coqui_api_model_name").find(":selected").text(), DEBUG_PREFIX+" corrupted model install", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
                $('#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 });
                $('#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 () {
                try {
                    $('#coqui_api_model_install_status').text('Downloading model...');
                    $('#coqui_api_model_install_button').hide();
                    //toastr.info("For model "+model_id, DEBUG_PREFIX+" Started "+action, { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
                    let apiResult = await CoquiTtsProvider.installModel(model_id, action);
                    apiResult = await apiResult.json();

                    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 });
                        $('#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 });
                    onModelNameChange_pointer();
                }
                // will refresh model status
            });

            $('#coqui_api_model_install_button').show();
            return;
        }

    }


    //#############################//
    //  API Calls                  //
    //#############################//

    /*
        Check model installation state, return one of ["installed", "corrupted", "absent"]
    */
    static async checkmodel_state(model_id) {
        throwIfModuleMissing();
        const url = new URL(getApiUrl());
        url.pathname = '/api/text-to-speech/coqui/coqui-api/check-model-state';

        const apiResult = await doExtrasFetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache',
            },
            body: JSON.stringify({
                'model_id': model_id,
            }),
        });

        if (!apiResult.ok) {
            toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Check model state request failed');
            throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
        }

        return apiResult;
    }

    static async installModel(model_id, action) {
        throwIfModuleMissing();
        const url = new URL(getApiUrl());
        url.pathname = '/api/text-to-speech/coqui/coqui-api/install-model';

        const apiResult = await doExtrasFetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache',
            },
            body: JSON.stringify({
                'model_id': model_id,
                'action': action,
            }),
        });

        if (!apiResult.ok) {
            toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Install model ' + model_id + ' request failed');
            throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
        }

        return apiResult;
    }

    /*
        Retrieve user custom models
    */
    static async getLocalModelList() {
        throwIfModuleMissing();
        const url = new URL(getApiUrl());
        url.pathname = '/api/text-to-speech/coqui/local/get-models';

        const apiResult = await doExtrasFetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache',
            },
            body: JSON.stringify({
                'model_id': 'model_id',
                'action': 'action',
            }),
        });

        if (!apiResult.ok) {
            toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Get local model list request failed');
            throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
        }

        return apiResult;
    }


    // Expect voiceId format to be like:
    // tts_models/multilingual/multi-dataset/your_tts[2][1]
    // tts_models/en/ljspeech/glow-tts
    // ts_models/ja/kokoro/tacotron2-DDC
    async generateTts(text, voiceId) {
        throwIfModuleMissing();
        voiceId = this.settings.customVoices[voiceId];

        const url = new URL(getApiUrl());
        url.pathname = '/api/text-to-speech/coqui/generate-tts';

        let language = 'none';
        let speaker = 'none';
        const tokens = voiceId.replaceAll(']', '').replaceAll('"', '').split('[');
        const model_id = tokens[0];

        console.debug(DEBUG_PREFIX, 'Preparing TTS request for', tokens);

        // First option
        if (tokens.length > 1) {
            const option1 = tokens[1];

            if (model_id.includes('multilingual'))
                language = option1;
            else
                speaker = option1;
        }

        // Second option
        if (tokens.length > 2)
            speaker = tokens[2];

        const apiResult = await doExtrasFetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache',
            },
            body: JSON.stringify({
                'text': text,
                'model_id': model_id,
                'language_id': parseInt(language),
                'speaker_id': parseInt(speaker),
            }),
        });

        if (!apiResult.ok) {
            toastr.error(apiResult.statusText, 'TTS Generation Failed');
            throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
        }

        return apiResult;
    }

    // Dirty hack to say not implemented
    async fetchTtsVoiceObjects() {
        const voiceIds = Object
            .keys(this.settings.voiceMapDict)
            .map(voice => ({ name: voice, voice_id: voice, preview_url: false }));
        return voiceIds;
    }

    // Do nothing
    previewTtsVoice(id) {
        return;
    }

    async fetchTtsFromHistory(history_item_id) {
        return Promise.resolve(history_item_id);
    }
}

async function initLocalModels() {
    if (!modules.includes('coqui-tts'))
        return;

    // Initialized local model once
    if (!coquiLocalModelsReceived) {
        let result = await CoquiTtsProvider.getLocalModelList();
        result = await result.json();

        coquiLocalModels = result['models_list'];

        $('#coqui_local_model_name').show();
        $('#coqui_local_model_name')
            .find('option')
            .remove()
            .end()
            .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));

        coquiLocalModelsReceived = true;
    }
}