File size: 9,020 Bytes
fc903d0
 
3a2e4f6
 
 
 
 
 
 
 
 
 
fc903d0
3a2e4f6
 
 
 
 
 
bfc7784
 
 
fc903d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a2e4f6
 
 
fc903d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99d7bc1
 
 
 
 
 
 
 
 
 
bfc7784
 
99d7bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc903d0
99d7bc1
 
 
fc903d0
99d7bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc903d0
99d7bc1
 
fc903d0
 
99d7bc1
 
 
 
 
fc903d0
99d7bc1
 
 
 
 
 
3a2e4f6
99d7bc1
 
 
 
 
 
3a2e4f6
99d7bc1
 
fc903d0
 
 
99d7bc1
 
4490b3f
99d7bc1
 
 
3a2e4f6
99d7bc1
3a2e4f6
99d7bc1
 
 
bfc7784
3a2e4f6
 
 
bfc7784
fc903d0
3a2e4f6
99d7bc1
 
 
bfc7784
 
99d7bc1
 
 
fc903d0
4490b3f
 
fc903d0
4490b3f
 
 
 
 
 
 
fc903d0
 
 
 
 
 
 
 
 
 
99d7bc1
 
 
fc903d0
 
 
 
 
 
 
 
 
 
 
 
 
99d7bc1
 
bfc7784
 
 
 
 
 
 
 
 
 
 
fc903d0
bfc7784
 
4490b3f
bfc7784
 
 
 
 
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
var configData = {}

$(document).ready(function () {
        $.ajax({
            url: '/admin/get_config',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            headers: {
                'X-CSRFToken': $('meta[name="csrf-token"]').attr('content')
            },
            success: function (response) {
                configData = response;
                show_config(response);
            },
            error: function (response) {

            }
        });
        $(".config-save").click(function () {
            set_config();
        });

        // Add new API key
        $('#add-api-key').click(function () {
            var newKey = {
                key: generateRandomKey(),
                enabled: true
            };
            configData.system.api_keys.push(newKey);
            addApiKeyHtml(newKey, configData.system.api_keys.length - 1);
        });

        // Remove API key
        $('#api-keys').on('click', '.btn-remove-key', function () {
            var index = $(this).closest('.input-group').data('index');
            configData.system.api_keys.splice(index, 1);

            $(this).closest('.input-group').remove();

            $('#api-keys .input-group').each(function (i) {
                $(this).attr('data-index', i);
                $(this).find('.input-group-text').text(`API Key ${i + 1}`);
            });
        });

        // Toggle API Key Enabled status
        $('#api-key-enabled').change(function () {
            configData.system.api_key_enabled = $(this).prop('checked');
        });
    }
);

function renderApiKeys() {
    var container = $('#api-keys');
    container.empty(); // Clear previous keys

    configData.system.api_keys.forEach(function (apiKey, index) {
        addApiKeyHtml(apiKey, index);
    });
}

function addApiKeyHtml(apiKey, index) {
    var container = $('#api-keys');

    var apiKeyHtml = `
        <div class="input-group mb-3" data-index="${index}">
            <span class="input-group-text">API Key ${index + 1}</span>
            <input type="text" class="form-control" value="${apiKey.key}" readonly>
            <button class="btn btn-danger btn-remove-key">Remove</button>
            <div class="form-check form-switch ms-2">
                <input class="form-check-input" type="checkbox" ${apiKey.enabled ? 'checked' : ''}>
                <label class="form-check-label">Enabled</label>
            </div>
        </div>
    `;
    container.append(apiKeyHtml);
}

function generateRandomKey() {
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var keyLength = 24;
    var result = '';
    for (var i = 0; i < keyLength; i++) {
        var randomIndex = Math.floor(Math.random() * characters.length);
        result += characters[randomIndex];
    }
    return result;
}

function show_config() {
    $.each(configData.vits_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        //为了避免id冲突,组合key作为id
        var itemId = 'vits-config-' + formattedKey;
        $('#vits-config').append(`
        <div class="input-group mb-3 item">
            <span class="input-group-text">${key}</span>
            <input type="text" class="form-control" id="${itemId}" value="${value}">
        </div>
        `);
    })

    $.each(configData.w2v2_vits_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        var itemId = 'w2v2-vits-config-' + formattedKey;
        $('#w2v2-vits-config').append(`
        <div class="input-group mb-3 item">
            <span class="input-group-text">${key}</span>
            <input type="text" class="form-control" id="${itemId}" value="${value}">
        </div>
        `);
    })

    $.each(configData.hubert_vits_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        var itemId = 'hubert-vits-config-' + formattedKey;
        $('#hubert-vits-config').append(`
        <div class="input-group mb-3 item">
            <span class="input-group-text">${key}</span>
            <input type="text" class="form-control" id="${itemId}" value="${value}">
        </div>
        `);
    })

    $.each(configData.bert_vits2_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        var itemId = 'bert-vits2-config-' + formattedKey;
        var inputValue = (value === null) ? '' : value;
        $('#bert-vits2-config').append(`
        <div class="input-group mb-3 item">
            <span class="input-group-text">${key}</span>
            <input type="text" class="form-control" id="${itemId}" value="${inputValue}">
        </div>
        `);
    })

    $.each(configData.log_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');

        if (key != 'logging_level') {
            $('#log-config').append(`
            <div class="input-group mb-3 item">
                <span class="input-group-text">${key}</span>
                <input type="text" class="form-control" id="${formattedKey}" value="${value}">
            </div>
            `);
        }

    });

    $.each(configData.tts_model_config, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');

        if (formattedKey !== "tts-models") {
            $('#tts-model-config').append(`
        <div class="input-group mb-3 item">
            <span class="input-group-text">${key}</span>
            <input type="text" class="form-control" id="${formattedKey}" value="${value}">
        </div>
        `);
        }
    });

    $.each(configData.language_identification, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        $('#language-identification ' + '#' + formattedKey).val(value)
    });

    $.each(configData.http_service, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        if (formattedKey == 'api-key-enable' || formattedKey == 'debug') {
            $('#' + formattedKey).prop('checked', value);
        } else {
            $('#' + formattedKey).val(value);
        }
    });

    // api keys
    renderApiKeys(configData);

    $.each(configData.system, function (key, value) {
        var formattedKey = key.replace(/_/g, '-');
        if (formattedKey == 'api-key-enabled' || formattedKey == 'cache-audio') {
            $('#' + formattedKey).prop('checked', value);
        } else {
            $('#' + formattedKey).val(value);
        }
    });

    $.each(configData.admin, function (key, value) {
        $('#' + key).val(value);
    });


}

function set_config() {
    configData = {}

    $('.configuration .form-label').each(function () {
        var labelId = $(this).next().attr('id');
        var nestedDict = {};


        $('#' + labelId).find('.item').each(function () {
            var itemId = $(this).find('input, select').attr('id').replace(/-/g, '_');

            // 还原组合 key
            itemId = itemId.replace(labelId.replace(/-/g, '_') + "_", "");

            var itemValue;
            if ($(this).find('input').is(':checkbox')) {
                // 如果是复选框,获取复选框的状态
                itemValue = $(this).find('input').prop('checked');
            } else {
                // 如果不是复选框,获取输入框或选择框的值
                itemValue = $(this).find('input, select').val();

                // 将空字符串转换为 null
                if (itemValue === "") {
                    itemValue = null;
                }

                // 特殊处理 language_automatic_detect 字段
                if (itemId === "language_automatic_detect") {
                    itemValue = itemValue ? itemValue.split(" ") : [];
                }
            }
            nestedDict[itemId] = itemValue;
        });

        // 处理 system 配置中的 api_keys 信息
        if (labelId === "system") {
            nestedDict['api_keys'] = [];
            $('#api-keys .input-group').each(function () {
                var apiKey = $(this).find('input[type="text"]').val();
                var apiKeyEnabled = $(this).find('.form-check-input').is(':checked');
                nestedDict['api_keys'].push({
                    key: apiKey,
                    enabled: apiKeyEnabled
                });
            });
        }

        configData[labelId.replace(/-/g, '_')] = nestedDict;
    });

    $.ajax({
        type: "POST",
        url: "/admin/set_config",
        data: JSON.stringify(configData), // 将配置数据转换为JSON字符串
        contentType: "application/json",
        headers: {
            'X-CSRFToken': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (response) {
            alert("配置已保存");
            // location.reload();
        },
        error: function (error) {
            alert("保存配置时出错,请查看日志!");
        }
    });

    return configData
}