File size: 9,827 Bytes
bd94e77
 
e8fafc5
a8c2bc7
e8fafc5
 
 
 
 
a8c2bc7
 
 
 
e8fafc5
bd94e77
f1a5461
cdf219b
19b9289
8ce0f99
bd94e77
6f78f1b
115aca3
 
8c3c188
6f78f1b
bd94e77
 
8ce0f99
115aca3
 
 
1e78a70
bd94e77
19b9289
 
 
f1a5461
45bf211
 
 
 
f1a5461
bd94e77
19b9289
 
 
 
bd94e77
 
 
8ce0f99
 
 
 
 
 
 
 
2cc4b35
8ce0f99
 
 
 
 
 
 
bd94e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19b9289
 
 
 
fa467b8
99978ff
45bf211
 
99978ff
45bf211
 
99978ff
 
45bf211
99978ff
45bf211
 
99978ff
 
fa467b8
99978ff
9a0003a
 
 
 
 
69fa971
cdf219b
 
9192cea
 
 
 
 
 
fa467b8
1e78a70
 
69fa971
637d40c
cdf219b
 
 
 
 
115aca3
 
99978ff
 
115aca3
99978ff
115aca3
 
 
 
 
 
 
 
 
8c3c188
 
 
 
 
 
 
 
1e78a70
f1a5461
8c3c188
 
19b9289
1e78a70
 
 
fa467b8
 
1e78a70
 
 
637d40c
 
 
fa467b8
f1a5461
1e78a70
f1a5461
 
115aca3
 
 
f1a5461
 
 
 
 
 
 
 
 
1e78a70
 
 
 
 
115aca3
1e78a70
 
bd94e77
 
 
8ce0f99
 
 
 
 
 
 
 
 
 
 
 
1e78a70
 
 
6f78f1b
cdf219b
 
 
 
 
 
 
 
6f78f1b
 
 
ff8ec88
6f78f1b
 
8c3c188
 
6f78f1b
 
bd94e77
 
e8fafc5
1e78a70
 
 
 
8c3c188
 
 
 
 
 
1e78a70
 
 
115aca3
 
 
 
 
 
 
1e78a70
 
 
8c3c188
115aca3
1e78a70
bec46dd
 
8c3c188
115aca3
bec46dd
fa467b8
 
bec46dd
bd94e77
19b9289
 
 
 
 
 
 
 
 
 
 
e86d760
a8c2bc7
bd94e77
a8c2bc7
bd94e77
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
docker build -t denoise:v20250609_1919 .
docker stop denoise_7865 && docker rm denoise_7865
docker run -itd \
--name denoise_7865 \
--restart=always \
--network host \
-e server_port=7865 \
-e hf_token=hf_coRVvzwAzCwGHKRK***********EX \
denoise:v20250609_1919 /bin/bash

"""
import argparse
import json
from functools import lru_cache
import logging
from pathlib import Path
import platform
import shutil
import tempfile
import time
from typing import Tuple
import zipfile

import gradio as gr
from huggingface_hub import snapshot_download
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np

import log
from project_settings import environment, project_path, log_directory
from toolbox.os.command import Command
from toolbox.torchaudio.models.dfnet.inference_dfnet import InferenceDfNet
from toolbox.torchaudio.models.dfnet2.inference_dfnet2 import InferenceDfNet2
from toolbox.torchaudio.models.dtln.inference_dtln import InferenceDTLN
from toolbox.torchaudio.models.frcrn.inference_frcrn import InferenceFRCRN
from toolbox.torchaudio.models.mpnet.inference_mpnet import InferenceMPNet


log.setup_size_rotating(log_directory=log_directory)

logger = logging.getLogger("main")


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--examples_dir",
        # default=(project_path / "data").as_posix(),
        default=(project_path / "data/examples").as_posix(),
        type=str
    )
    parser.add_argument(
        "--models_repo_id",
        default="qgyd2021/nx_denoise",
        type=str
    )
    parser.add_argument(
        "--trained_model_dir",
        default=(project_path / "trained_models").as_posix(),
        type=str
    )
    parser.add_argument(
        "--hf_token",
        default=environment.get("hf_token"),
        type=str,
    )
    parser.add_argument(
        "--server_port",
        default=environment.get("server_port", 7860),
        type=int
    )

    args = parser.parse_args()
    return args


def shell(cmd: str):
    return Command.popen(cmd)


denoise_engines = {
    "dtln-256-nx-dns3": {
        "infer_cls": InferenceDTLN,
        "kwargs": {
            "pretrained_model_path_or_zip_file": (project_path / "trained_models/dtln-256-nx-dns3.zip").as_posix()
        }
    },
    "dtln-512-nx-dns3": {
        "infer_cls": InferenceDTLN,
        "kwargs": {
            "pretrained_model_path_or_zip_file": (project_path / "trained_models/dtln-512-nx-dns3.zip").as_posix()
        }
    },
    "dfnet2-nx-dns3": {
        "infer_cls": InferenceDfNet2,
        "kwargs": {
            "pretrained_model_path_or_zip_file": (project_path / "trained_models/dfnet2-nx-dns3.zip").as_posix()
        }
    },
    "frcrn-dns3": {
        "infer_cls": InferenceFRCRN,
        "kwargs": {
            "pretrained_model_path_or_zip_file": (project_path / "trained_models/frcrn-dns3.zip").as_posix()
        }
    },
    "mpnet-nx-speech": {
        "infer_cls": InferenceMPNet,
        "kwargs": {
            "pretrained_model_path_or_zip_file": (project_path / "trained_models/mpnet-nx-speech.zip").as_posix()
        }
    },
}


@lru_cache(maxsize=1)
def load_denoise_model(infer_cls, **kwargs):
    infer_engine = infer_cls(**kwargs)

    return infer_engine


def generate_spectrogram(signal: np.ndarray, sample_rate: int = 8000, title: str = "Spectrogram"):
    mag = np.abs(librosa.stft(signal))
    # mag_db = librosa.amplitude_to_db(mag, ref=np.max)
    mag_db = librosa.amplitude_to_db(mag, ref=20)

    plt.figure(figsize=(10, 4))
    librosa.display.specshow(mag_db, sr=sample_rate)
    plt.title(title)

    temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    plt.savefig(temp_file.name, bbox_inches="tight")
    plt.close()
    return temp_file.name


def when_click_denoise_button(noisy_audio_file_t = None, noisy_audio_microphone_t = None, engine: str = None):
    if noisy_audio_file_t is None and noisy_audio_microphone_t is None:
        raise gr.Error(f"audio file and microphone is null.")
    if noisy_audio_file_t is not None and noisy_audio_microphone_t is not None:
        gr.Warning(f"both audio file and microphone file is provided, audio file taking priority.")

    noisy_audio_t: Tuple = noisy_audio_file_t or noisy_audio_microphone_t

    sample_rate, signal = noisy_audio_t
    audio_duration = signal.shape[-1] // 8000

    # Test: 使用 microphone 时,显示采样率是 44100,但 signal 实际是按 8000 的采样率的。
    logger.info(f"run denoise; engine: {engine}, sample_rate: {sample_rate}, signal dtype: {signal.dtype}, signal shape: {signal.shape}")

    noisy_audio = np.array(signal / (1 << 15), dtype=np.float32)

    infer_engine_param = denoise_engines.get(engine)
    if infer_engine_param is None:
        raise gr.Error(f"invalid denoise engine: {engine}.")

    try:
        infer_cls = infer_engine_param["infer_cls"]
        kwargs = infer_engine_param["kwargs"]
        infer_engine = load_denoise_model(infer_cls=infer_cls, **kwargs)

        begin = time.time()
        enhanced_audio = infer_engine.enhancement_by_ndarray(noisy_audio)
        time_cost = time.time() - begin

        noisy_mag_db = generate_spectrogram(noisy_audio, title="noisy")
        denoise_mag_db = generate_spectrogram(enhanced_audio, title="denoise")

        fpr = time_cost / audio_duration

        info = {
            "time_cost": round(time_cost, 4),
            "audio_duration": round(audio_duration, 4),
            "fpr": round(fpr, 4)
        }
        message = json.dumps(info, ensure_ascii=False, indent=4)

        enhanced_audio = np.array(enhanced_audio * (1 << 15), dtype=np.int16)
    except Exception as e:
        raise gr.Error(f"enhancement failed, error type: {type(e)}, error text: {str(e)}.")

    enhanced_audio_t = (sample_rate, enhanced_audio)
    return enhanced_audio_t, message, noisy_mag_db, denoise_mag_db


def main():
    args = get_args()

    examples_dir = Path(args.examples_dir)
    trained_model_dir = Path(args.trained_model_dir)

    # download models
    if not trained_model_dir.exists():
        trained_model_dir.mkdir(parents=True, exist_ok=True)
        _ = snapshot_download(
            repo_id=args.models_repo_id,
            local_dir=trained_model_dir.as_posix(),
            token=args.hf_token,
        )

    # choices
    denoise_engine_choices = list(denoise_engines.keys())

    # examples
    if not examples_dir.exists():
        example_zip_file = trained_model_dir / "examples.zip"
        with zipfile.ZipFile(example_zip_file.as_posix(), "r") as f_zip:
            out_root = examples_dir
            if out_root.exists():
                shutil.rmtree(out_root.as_posix())
            out_root.mkdir(parents=True, exist_ok=True)
            f_zip.extractall(path=out_root)

    # examples
    examples = list()
    for filename in examples_dir.glob("**/*.wav"):
        examples.append([
            filename.as_posix(),
            None,
            denoise_engine_choices[0],
        ])

    # ui
    with gr.Blocks() as blocks:
        gr.Markdown(value="denoise.")
        with gr.Tabs():
            with gr.TabItem("denoise"):
                with gr.Row():
                    with gr.Column(variant="panel", scale=5):
                        with gr.Tabs():
                            with gr.TabItem("file"):
                                dn_noisy_audio_file = gr.Audio(label="noisy_audio")
                            with gr.TabItem("microphone"):
                                dn_noisy_audio_microphone = gr.Audio(sources="microphone", label="noisy_audio")

                        dn_engine = gr.Dropdown(choices=denoise_engine_choices, value=denoise_engine_choices[0], label="engine")
                        dn_button = gr.Button(variant="primary")
                    with gr.Column(variant="panel", scale=5):
                        with gr.Tabs():
                            with gr.TabItem("audio"):
                                dn_enhanced_audio = gr.Audio(label="enhanced_audio")
                                dn_message = gr.Textbox(lines=1, max_lines=20, label="message")
                            with gr.TabItem("mag_db"):
                                dn_noisy_mag_db = gr.Image(label="noisy_mag_db")
                                dn_denoise_mag_db = gr.Image(label="denoise_mag_db")

                dn_button.click(
                    when_click_denoise_button,
                    inputs=[dn_noisy_audio_file, dn_noisy_audio_microphone, dn_engine],
                    outputs=[dn_enhanced_audio, dn_message, dn_noisy_mag_db, dn_denoise_mag_db]
                )
                gr.Examples(
                    examples=examples,
                    inputs=[dn_noisy_audio_file, dn_noisy_audio_microphone, dn_engine],
                    outputs=[dn_enhanced_audio, dn_message, dn_noisy_mag_db, dn_denoise_mag_db],
                    fn=when_click_denoise_button,
                    # cache_examples=True,
                    # cache_mode="lazy",
                )

            with gr.TabItem("shell"):
                shell_text = gr.Textbox(label="cmd")
                shell_button = gr.Button("run")
                shell_output = gr.Textbox(label="output")

                shell_button.click(
                    shell,
                    inputs=[shell_text,],
                    outputs=[shell_output],
                )

    # http://127.0.0.1:7865/
    # http://10.75.27.247:7865/
    blocks.queue().launch(
        # share=True,
        share=False if platform.system() == "Windows" else False,
        server_name="127.0.0.1" if platform.system() == "Windows" else "0.0.0.0",
        server_port=args.server_port
    )
    return


if __name__ == "__main__":
    main()