File size: 9,849 Bytes
312b755
 
8bc036f
312b755
 
caa9d68
 
be3e940
819affb
6e5883a
8bc036f
 
51e7c22
312b755
651ebfd
312b755
fe17ce1
 
 
 
 
 
 
 
 
2547be7
fe17ce1
 
 
 
be3e940
fe17ce1
2547be7
fe17ce1
2547be7
fe17ce1
651ebfd
 
fe17ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8521c95
 
 
 
fe17ce1
 
0c51cc6
fe17ce1
 
 
be3e940
 
fe17ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8521c95
 
 
 
 
 
 
 
 
 
 
 
 
 
fe17ce1
 
 
 
 
 
 
 
 
 
 
8521c95
 
 
 
 
 
 
 
 
 
 
fe17ce1
 
 
 
f1a805c
 
 
 
2547be7
 
 
 
 
 
 
f1a805c
 
 
2547be7
 
 
 
 
 
 
 
8521c95
2547be7
 
 
 
 
 
f1a805c
 
 
 
2547be7
f1a805c
 
 
8521c95
f1a805c
 
 
 
 
8521c95
f1a805c
 
 
 
8521c95
 
2547be7
 
8521c95
 
 
 
2547be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8521c95
2547be7
8521c95
 
 
f1a805c
 
fe17ce1
f1a805c
fe17ce1
be3e940
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
import subprocess
import sys
import shlex
#update the system
subprocess.check_call(["apt-get", "update"])
subprocess.check_call([sys.executable,"-m","pip","install",
                       "torch==2.2.0",
                       "torchaudio==2.2.0"])
subprocess.check_call([sys.executable,"-m","pip","install",
                       "einops", "encodec"])
def install_mamba():
    subprocess.run(shlex.split("pip install https://github.com/Dao-AILab/causal-conv1d/releases/download/v1.4.0/causal_conv1d-1.4.0+cu122torch2.3cxx11abiFALSE-cp310-cp310-linux_x86_64.whl"))
    subprocess.run(shlex.split("pip install https://github.com/state-spaces/mamba/releases/download/v1.2.0.post1/mamba_ssm-1.2.0.post1+cu122torch2.2cxx11abiTRUE-cp310-cp310-linux_x86_64.whl"))

install_mamba()

import torch
import spaces
import tempfile
import soundfile as sf
import gradio as gr
import librosa as lb
import yaml
import numpy as np
import matplotlib.pyplot as plt
from pydub import AudioSegment
from model.cleanmel import CleanMel
from model.vocos.pretrained import Vocos
from model.stft import InputSTFT, TargetMel

DEVICE = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
def read_audio(file_path):
    assert file_path.endswith(('.wav', '.flac')), "Unsupported audio format. Please upload a .wav, .flac file."
    audio, sample_rate = sf.read(file_path)
    
    if audio.ndim > 1:
        # select the loudest channel if stereo
        audio = audio[:, np.argmax(np.abs(audio).mean(axis=0))]
    if sample_rate != 16000:
        audio = lb.resample(audio, orig_sr=sample_rate, target_sr=16000)
        sample_rate = 16000

    return torch.tensor(audio).float().squeeze().unsqueeze(0)

def stft(audio):
    transform = InputSTFT(
            n_fft=512,
            n_win=512,
            n_hop=128,
            normalize=False,
            center=True,
            onesided=True,
            online=False
        ).eval().to(DEVICE)
    return transform(audio)

def mel_transform(audio, X_norm):
    transform = TargetMel(
        sample_rate=16000,
        n_fft=512,
        n_win=512,
        n_hop=128,
        n_mels=80,
        f_min=0,
        f_max=8000,
        power=2,
        center=True,
        normalize=False,
        onesided=True,
        mel_norm="slaney",
        mel_scale="slaney",
        librosa_mel=True,
        online=False
    ).eval().to(DEVICE)
    return transform(audio, X_norm)

def load_cleanmel(model_name):
    if "S" in model_name:
        model_config = f"./configs/cleanmel_offline_S.yaml"
    else:
        model_config = f"./configs/cleanmel_offline_L.yaml"
    model_config = yaml.safe_load(open(model_config, "r"))["model"]["arch"]["init_args"]
    cleanmel = CleanMel(**model_config)
    cleanmel.load_state_dict(torch.load(f"./ckpts/CleanMel/{model_name}.ckpt", map_location=DEVICE))
    return cleanmel.eval()

def load_vocos():
    vocos = Vocos.from_hparams(config_path="./configs/vocos_offline.yaml")
    vocos = Vocos.from_pretrained(None, model_path=f"./ckpts/Vocos/vocos_offline.pt", model=vocos)
    return vocos.eval()

def get_mrm_pred(Y_hat, x, X_norm):
    X_noisy = mel_transform(x, X_norm)
    Y_hat = Y_hat.squeeze()
    Y_hat = torch.square(Y_hat * (torch.sqrt(X_noisy) + 1e-10))
    return Y_hat

def safe_log(x):           
    return torch.log(torch.clip(x, min=1e-5)) 

def output(y_hat, logMel_hat):
    with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
        sf.write(tmp_file.name, y_hat.squeeze().cpu().numpy(), 16000)
    with tempfile.NamedTemporaryFile(suffix='.npy', delete=False) as tmp_logmel_np_file:
        np.save(tmp_logmel_np_file.name, logMel_hat.squeeze().cpu().numpy())
    logMel_img = logMel_hat.squeeze().cpu().numpy()[::-1, :]
    with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_logmel_img:
        # give a plt figure size according to the logMel shape
        plt.figure(figsize=(logMel_img.shape[1] / 100, logMel_img.shape[0] / 50))
        plt.clf()
        plt.imshow(logMel_img, vmin=-11, cmap="jet")
        plt.tight_layout()
        plt.ylabel("Mel bands")
        plt.xlabel("Time (second)")
        plt.yticks([0, 80], [80, 0])
        dur = y_hat.shape[-1] / 16000
        xticks = [int(x) for x in np.linspace(0, logMel_img.shape[-1], 11)]
        xticks_str = ["{:.1f}".format(x) for x in np.linspace(0, dur, 11)]
        plt.xticks(xticks, xticks_str)
        plt.savefig(tmp_logmel_img.name)
    
    return tmp_file.name, tmp_logmel_img.name, tmp_logmel_np_file.name

@spaces.GPU
@torch.inference_mode()
def enhance_cleanmel_L_mask(audio_path):
    model = load_cleanmel("offline_CleanMel_L_mask").to(DEVICE)
    vocos = load_vocos().to(DEVICE)
    x = read_audio(audio_path).to(DEVICE)
    X, X_norm = stft(x)
    Y_hat = model(X, inference=True)
    MRM_hat = torch.sigmoid(Y_hat)
    Y_hat = get_mrm_pred(MRM_hat, x, X_norm)
    logMel_hat = safe_log(Y_hat)
    y_hat = vocos(logMel_hat, X_norm).clamp(min=-1, max=1)
    return output(y_hat, logMel_hat)

@spaces.GPU
@torch.inference_mode()
def enhance_cleanmel_S_mask(audio_path):
    model = load_cleanmel("offline_CleanMel_S_mask").to(DEVICE)
    vocos = load_vocos().to(DEVICE)
    x = read_audio(audio_path).to(DEVICE)
    X, X_norm = stft(x)
    Y_hat = model(X, inference=True)
    MRM_hat = torch.sigmoid(Y_hat)
    Y_hat = get_mrm_pred(MRM_hat, x, X_norm)
    logMel_hat = safe_log(Y_hat)
    y_hat = vocos(logMel_hat, X_norm).clamp(min=-1, max=1)
    return output(y_hat, logMel_hat)

@spaces.GPU
@torch.inference_mode()
def enhance_cleanmel_L_map(audio_path):
    model = load_cleanmel("offline_CleanMel_L_map").to(DEVICE)
    vocos = load_vocos().to(DEVICE)
    x = read_audio(audio_path).to(DEVICE)
    X, X_norm = stft(x)
    logMel_hat = model(X, inference=True)
    y_hat = vocos(logMel_hat, X_norm).clamp(min=-1, max=1)
    return output(y_hat, logMel_hat)

@spaces.GPU
@torch.inference_mode()
def enhance_cleanmel_S_map(audio_path):
    model = load_cleanmel("offline_CleanMel_S_map").to(DEVICE)
    vocos = load_vocos().to(DEVICE)
    x = read_audio(audio_path).to(DEVICE)
    X, X_norm = stft(x)
    logMel_hat = model(X, inference=True)
    y_hat = vocos(logMel_hat, X_norm).clamp(min=-1, max=1)
    return output(y_hat, logMel_hat)

def reset_everything():
    """Reset all components to initial state"""
    return None, None, None


demo = gr.Blocks()
with gr.Blocks(title="CleanMel Demo") as demo:
    gr.Markdown("## CleanMel Demo")
    gr.Markdown("This demo showcases the CleanMel model for speech enhancement. <br>  \
        Only **.wav** and **.flac** files are supported. <br>  \
        --- <br> \
        The model is running on CPU. Please be patient and wait for the result. <br> \
        Inference time reference: <br> \
            - CleanMel_L: **10 mins** for **10-second** audio <br> \
            - CleanMel_S: **4 mins** for **10-second** audio <br> ")
    
    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(label="Input Audio", type="filepath", sources="upload")
            audio_input_record = gr.Audio(label="Input Audio (Record)", type="filepath", sources="microphone")
    with gr.Row():
        with gr.Column():
            enhance_button_map_S = gr.Button("Enhance File (offline CleanMel_S_map)")
            enhance_button_mask_S = gr.Button("Enhance File (offline CleanMel_S_mask)")
            enhance_button_map_L = gr.Button("Enhance File (offline CleanMel_L_map)")
            enhance_button_mask_L = gr.Button("Enhance File (offline CleanMel_L_mask)")
            
        with gr.Column():
            enhance_button_map_Sr = gr.Button("Enhance Recorded Audio (offline CleanMel_S_map)")
            enhance_button_mask_Sr = gr.Button("Enhance Recorded Audio (offline CleanMel_S_mask)")
            enhance_button_map_Lr = gr.Button("Enhance Recorded Audio (offline CleanMel_L_map)")
            enhance_button_mask_Lr = gr.Button("Enhance Recorded Audio (offline CleanMel_L_mask)")
    with gr.Row():
            clear_btn = gr.Button(
                "🗑️ Clear All",
                variant="secondary",
                size="lg"
            ) 
    output_audio = gr.Audio(label="Enhanced Audio", type="filepath")
    output_mel = gr.Image(label="Output LogMel Spectrogram", type="filepath", visible=True)
    output_np = gr.File(label="Enhanced LogMel Spec. (.npy)", type="filepath")
    enhance_button_map_L.click(
        enhance_cleanmel_L_map, 
        inputs=audio_input, 
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_mask_L.click(
        enhance_cleanmel_L_mask, 
        inputs=audio_input, 
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_map_S.click(
        enhance_cleanmel_S_map, 
        inputs=audio_input, 
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_mask_S.click(
        enhance_cleanmel_S_mask, 
        inputs=audio_input, 
        outputs=[output_audio, output_mel, output_np]
    )
    enhance_button_map_Lr.click(
        enhance_cleanmel_L_map, 
        inputs=audio_input_record, 
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_mask_Lr.click(
        enhance_cleanmel_L_mask, 
        inputs=audio_input_record, 
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_map_Sr.click(
        enhance_cleanmel_S_map,
        inputs=audio_input_record,
        outputs=[output_audio, output_mel, output_np]
    )
    
    enhance_button_mask_Sr.click(
        enhance_cleanmel_S_mask,
        inputs=audio_input_record,
        outputs=[output_audio, output_mel, output_np]
    )
    
    clear_btn.click(
            fn=reset_everything,
            outputs=[output_audio, output_mel, output_np]
    )

demo.launch(debug=False)