File size: 11,681 Bytes
2b7fd6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
import logging
import tempfile

from inference.infer_tool import Svc
from typing import *
import api.base
import os
import io
import wave
import numpy as np
from service.tool import audio_normalize, read_wav_file_to_numpy_array
from utils import get_hparams_from_file

logger = logging.getLogger(__name__)

_svc: Optional[Svc] = None
_model_paths: Optional[List] = None


def init():
    global _svc, _model_paths
    _svc = Svc()
    _model_paths = []

    # get the path to the parent directory
    parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.curdir))

    # construct the path to the "checkpoints" directory
    checkpoints_dir = os.path.join(parent_dir, "checkpoints")

    logger.debug(f"CkPoints Dir: {checkpoints_dir}")

    for root, dirs, files in os.walk(checkpoints_dir):
        for dir in dirs:
            _model_paths.append(dir)


# noinspection PyAbstractClass
class ModelListHandler(api.base.ApiHandler):
    async def get(self):
        self.write({
            "code": 200,
            "msg": "ok",
            "data": _model_paths
        })


# noinspection PyAbstractClass
class SwitchHandler(api.base.ApiHandler):
    async def post(self):
        model_name = self.get_argument("model", "")  # model name
        mode = self.get_argument("mode", "single")  # running mode: single or batch
        device = self.get_argument("device", "cuda")  # "cpu" or "cuda"

        if model_name == "":
            self.set_status(400)
            self.write({
                "code": 400,
                "msg": "未选择模型!",
                "data": None
            })
            return

        if mode not in ("single", "batch"):
            self.set_status(400)
            self.write({
                "code": 400,
                "msg": "运行模式选择错误!",
                "data": None
            })
            return

        if device not in ("cpu", "cuda"):
            self.set_status(400)
            self.write({
                "code": 400,
                "msg": "设备选择错误!",
                "data": None
            })
            return

        logger.debug(f"modelname: {model_name}\n"
                     f"mode: {mode}\n"
                     f"device: {device}\n")
        try:
            _svc.set_device(device=device)
            logger.debug(f"Device set.")
            _svc.load_checkpoint(path=model_name)
            logger.debug(f"Model set.")
        except Exception as e:
            logger.exception(e)
            self.set_status(500)
            self.write({
                "code": 500,
                "msg": "system_error",
                "data": None
            })
            return

        self.write({
            "code": 200,
            "msg": "ok",
            "data": {
                "mode": mode
            }
        })


# noinspection PyAbstractClass
class SingleInferenceHandler(api.base.ApiHandler):
    async def post(self):
        try:
            from scipy.io import wavfile

            dsid = self.get_argument("dsid", "")
            tran = self.get_argument("tran", "0")
            th = self.get_argument("th", "-40.0")
            ns = self.get_argument("ns", "0.4")
            audiofile_dict = self.request.files.get("srcaudio", [])

            if not audiofile_dict:
                self.set_status(400)
                self.write({
                    "code": 400,
                    "msg": "未上传文件!",
                    "data": None
                })
                return

            if dsid == "":
                self.set_status(400)
                self.write({
                    "code": 400,
                    "msg": "未选择模型!",
                    "data": None
                })
                return

            audiofile = audiofile_dict[0]
            audio_filename = audiofile['filename']
            audio_filebody = audiofile['body']
            audio_fileext = os.path.splitext(audio_filename)[-1].lower()

            with tempfile.NamedTemporaryFile(suffix=audio_fileext, delete=False) as temp_file:
                temp_file.write(audio_filebody)
                temp_file.close()

                converted_file = await audio_normalize(full_filename=audio_filename, file_data=audio_filebody)
                # if audio_fileext != ".wav":
                #     logger.debug(f"file format is {audio_fileext}, not wav\n"
                #                  f"converting to standard wav data...")
                #     converted_file = await audio_normalize(full_filename=audio_filename, file_data=audio_filebody)
                #     logger.debug(f"wav conversion completed.")
                # else:
                #     converted_file = temp_file.name

                sampling_rate, audio_array = read_wav_file_to_numpy_array(converted_file)
                os.remove(converted_file)

            scraudio = (sampling_rate, audio_array)

            logger.debug(f"read file {audio_filename}\n"
                         f"sampling rate: {sampling_rate}")

            tran = float(tran)
            th = float(th)
            ns = float(ns)

            hparams = get_hparams_from_file(f"checkpoints/{dsid}/config.json")
            spk = hparams.spk
            real_dsid = ""
            for k, v in spk.items():
                if v == 0:
                    real_dsid = k
            logger.debug(f"read dsid is: {real_dsid}")

            output_audio_sr, output_audio_array = _svc.inference(srcaudio=scraudio,
                                                                 chara=real_dsid,
                                                                 tran=tran,
                                                                 slice_db=th,
                                                                 ns=ns)

            logger.debug(f"svc for {audio_filename} succeed. \n"
                         f"audio data type: {type(output_audio_array)}\n"
                         f"audio data sr: {output_audio_sr}")

            logger.debug(f"start output data.")

            # Convert the NumPy array to WAV format
            with io.BytesIO() as wav_file:
                wavfile.write(wav_file, sampling_rate, output_audio_array)
                wav_data = wav_file.getvalue()

            # set the response headers and body
            self.set_header('Content-Type', 'audio/wav')
            self.set_header('Content-Disposition', f'attachment; filename="svc_output.wav"')
            self.write(wav_data)
            await self.flush()
            logger.debug(f"response completed.")
        except Exception as e:
            logger.exception(e)
            self.set_status(500)
            self.write({
                "code": 500,
                "msg": "system_error",
                "data": None
            })
            return


# noinspection PyAbstractClass
class BatchInferenceHandler(api.base.ApiHandler):
    async def post(self):
        try:
            from zipfile import ZipFile
            from scipy.io import wavfile
            import uuid

            dsid = self.get_argument("dsid", "")
            tran = self.get_argument("tran", "0")
            th = self.get_argument("th", "-40.0")
            ns = self.get_argument("ns", "0.4")
            audiofile_dict = self.request.files.get("srcaudio", [])

            logger.debug(len(self.request.files))

            if not audiofile_dict:
                self.set_status(400)
                self.write({
                    "code": 400,
                    "msg": "未上传文件!",
                    "data": None
                })
                return

            if dsid == "":
                self.set_status(400)
                self.write({
                    "code": 400,
                    "msg": "未选择模型!",
                    "data": None
                })
                return

            temp_dir_name = "temp"

            # get the path to the parent directory
            parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.curdir))

            # construct the path to the "temp" directory
            temp_dir = os.path.join(parent_dir, temp_dir_name)

            logger.debug(f"TempDir: {temp_dir}")

            if not os.path.exists(temp_dir):
                os.mkdir(temp_dir)

            tmp_workdir_name = f"{temp_dir}/batch_{uuid.uuid4()}"
            if not os.path.exists(tmp_workdir_name):
                os.mkdir(tmp_workdir_name)

            output_files = []

            tran = float(tran)
            th = float(th)
            ns = float(ns)

            hparams = get_hparams_from_file(f"checkpoints/{dsid}/config.json")
            spk = hparams.spk
            real_dsid = ""
            for k, v in spk.items():
                if v == 0:
                    real_dsid = k
            logger.debug(f"read dsid is: {real_dsid}")

            for idx, file in enumerate(audiofile_dict):
                audio_filename = file["filename"]
                audio_filebody = file["body"]
                filename = os.path.basename(audio_filename)
                audio_fileext = os.path.splitext(audio_filename)[-1].lower()

                with tempfile.NamedTemporaryFile(suffix=audio_fileext, delete=False) as temp_file:
                    temp_file.write(audio_filebody)
                    temp_file.close()

                    converted_file = await audio_normalize(full_filename=audio_filename, file_data=audio_filebody)

                    # if audio_fileext != ".wav":
                    #     logger.debug(f"file format is {audio_fileext}, not wav\n"
                    #                  f"converting to standard wav data...")
                    #     converted_file = await audio_normalize(full_filename=audio_filename, file_data=audio_filebody)
                    #     logger.debug(f"wav conversion completed.")
                    # else:
                    #     converted_file = temp_file.name

                    sampling_rate, audio_array = read_wav_file_to_numpy_array(converted_file)
                    os.remove(converted_file)

                scraudio = (sampling_rate, audio_array)

                print(f"{idx}, {len(audio_filebody)}, {filename}")

                output_sampling_rate, output_audio = _svc.inference(scraudio, chara=real_dsid, tran=tran,
                                                                    slice_db=th, ns=ns)
                new_filepath = f"{tmp_workdir_name}/{filename}"
                wavfile.write(filename=new_filepath, rate=output_sampling_rate, data=output_audio)
                output_files.append(new_filepath)

            zipfilename = f"{tmp_workdir_name}/output.zip"
            with ZipFile(zipfilename, "w") as zip_obj:
                for idx, filepath in enumerate(output_files):
                    zip_obj.write(filepath, os.path.basename(filepath))

            # todo: remove data

            logger.debug(f"start output data.")
            # set response header and body
            self.set_header("Content-Type", "application/zip")
            self.set_header("Content-Disposition", "attachment; filename=output.zip")
            with open(zipfilename, "rb") as file:
                self.write(file.read())
            await self.flush()
            logger.debug(f"response completed.")
        except Exception as e:
            logger.exception(e)
            self.set_status(500)
            self.write({
                "code": 500,
                "msg": "system_error",
                "data": None
            })
            return


if __name__ == "__main__":
    init()
    print(_model_paths)