File size: 7,175 Bytes
960cd20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
* 获取已加载模型信息
* */
$(document).ready(function () {
        get_models_info();
        get_model_to_load();
        $("#save-current-model").click(function () {
            save_current_model();
        });
    }
);

function get_models_info() {
    $.ajax({
        url: '/admin/get_models_info',
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        success: function (response) {
            var model_list = response;
            show_model(model_list);
        }
    });
}


var modelTypes = {
    'VITS': $('#VITS'),
    'HUBERT-VITS': $('#HUBERT-VITS'),
    'W2V2-VITS': $('#W2V2-VITS'),
    'BERT-VITS2': $('#BERT-VITS2'),
    'GPT-SOVITS': $('#GPT-SOVITS')
};


function show_model(model_list) {
    for (var modelType in modelTypes) {
        modelTypes[modelType].empty();
        modelTypes[modelType].prev('label').remove();
    }

    for (var model_type in model_list) {
        if (model_list[model_type].length > 0) {
            var model_datas = model_list[model_type];
            var label = $('<label></label>').text(model_type);
            modelTypes[model_type].before(label);

            $.each(model_datas, function (key, model_data) {
                renderModelCard(model_data, model_type);
            });
        }
    }
}

function renderModelCard(model_data, model_type) {
    var id = String(model_data["model_id"]);
    var model_path = String(model_data["model_path"]);
    var n_speakers = String(model_data["n_speakers"]);
    var sovits_path = String(model_data["sovits_path"]);
    var gpt_path = String(model_data["gpt_path"]);

    var card = $('<div></div>').addClass("card model-card " + model_type).attr({
        "data-model-type": model_type,
        "data-model-id": id
    });

    var wrap = $('<div></div>').addClass("wrap");

    // $('<div></div>').text("id: " + id).appendTo(wrap);
    if (model_type == "GPT-SOVITS") {
        $('<div></div>').text(sovits_path).addClass("model-card-info1").appendTo(wrap);
        $('<div></div>').text(gpt_path).addClass("model-card-info3").appendTo(wrap);
        $('<div></div>').text("x").addClass("unload-model").appendTo(wrap);
    } else {
        $('<div></div>').text(model_path).addClass("model-card-info1").appendTo(wrap);
        $('<div></div>').text("n_speakers: " + n_speakers).addClass("model-card-info2").appendTo(wrap);
        $('<div></div>').text("x").addClass("unload-model").appendTo(wrap);
    }


    card.append(wrap);

    modelTypes[model_type].append(card);
}


/*
* 获取项目模型目录下的模型与配置文件路径
* */
var modelLoadContent = $('.model-load-content');
var isRequestInProgress = false;

function get_model_to_load() {
    $.get('/admin/get_path', function (response) {
        var model_datas = response;
        renderModelLoadCards(model_datas);
    });
}

function renderModelLoadCards(data) {
    modelLoadContent.empty();

    $.each(data, function (index, model) {
        var card = $('<div></div>').addClass('model-load-item flex');
        var model_id = model.model_id;
        var model_type = model.model_type;
        var model_path = model.model_path;
        var config_path = model.config_path;
        var sovits_path = model.sovits_path;
        var gpt_path = model.gpt_path;

        var folder = null;
        var model_name = null;
        var config_name = null;
        var sovits_name = null;
        var gpt_name = null;

        $('<div></div>').text(model_id.toString()).addClass("unload-model-id").appendTo(card);


        if (model_path != null && config_path != null) {
            folder = model_path.split("/")[0];
            model_name = model_path.split("/")[1];
            config_name = config_path.split("/")[1];
            $('<div></div>').text(folder).addClass("unload-model-folder").appendTo(card);
            $('<div></div>').text(model_name).addClass("unload-model-path model1").appendTo(card);
            $('<div></div>').text(config_name).addClass("unload-model-config model2").appendTo(card);
        } else {
            folder = sovits_path.split("/")[0];
            sovits_name = sovits_path.split("/")[1];
            gpt_name = gpt_path.split("/")[1];
            $('<div></div>').text(folder).addClass("unload-model-folder").appendTo(card);
            $('<div></div>').text(sovits_name).addClass("unload-sovits-path model1").appendTo(card);
            $('<div></div>').text(gpt_name).addClass("unload-gpt-config model2").appendTo(card);
        }


        // $('<div></div>').text(model_type).addClass("model-type").appendTo(card);


        // var formattedString = folder + " | " + filename + " | " + config_path;
        // $('<div></div>').text(formattedString).addClass("unload-model-path").appendTo(card);
        // card.text(formattedString);
        card.on('click', function () {
            if (!isRequestInProgress) {
                isRequestInProgress = true;
                loadModel(card, model_path, config_path, sovits_path, gpt_path);
            }

        });


        modelLoadContent.append(card);
    });

}


/*
* 加载模型
* */
function loadModel(card, model_path, config_path, sovits_path, gpt_path) {
    var csrftoken = $('meta[name="csrf-token"]').attr('content');

    $.ajax({
        url: '/admin/load_model',
        type: 'POST',
        contentType: 'application/json',
        headers: {
            'X-CSRFToken': csrftoken
        },
        data: JSON.stringify({
            model_path: model_path,
            config_path: config_path,
            sovits_path: sovits_path,
            gpt_path: gpt_path
        }),
        success: function (response) {
            card.fadeOut();
            get_models_info();
            isRequestInProgress = false;
        },
        error: function (response) {
            alert("Model loading failed!");
            isRequestInProgress = false;
        }
    });

}


/*
* 卸载模型
* */
$('#model-data').on('click', '.unload-model', function (event) {
    var cardElement = $(event.target).closest('.card');
    var modelType = cardElement.attr('data-model-type');
    var modelId = cardElement.attr('data-model-id');

    var params = {
        "model_type": modelType,
        "model_id": modelId
    };

    $.ajax({
        url: '/admin/unload_model',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(params),
        headers: {
            'X-CSRFToken': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (response) {
            get_models_info();
        },
        error: function (response) {
            alert("Unload model failed!");
        }
    });
});

function save_current_model() {
    $.ajax({
        url: '/admin/save_current_model',
        type: 'POST',
        headers: {
            'X-CSRFToken': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (response) {
            alert("当前已加载的模型已保存,下次启动将自动加载。");
        },
        error: function (response) {
            alert("保存失败!");
        }
    });
}