Spaces:
Running
on
Zero
Running
on
Zero
roychao19477
commited on
Commit
Β·
c58812e
1
Parent(s):
545af0d
Upload
Browse files
app.py
CHANGED
@@ -58,13 +58,13 @@ model.eval()
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
-
def enhance(
|
62 |
-
if
|
63 |
-
|
64 |
if orig_sr != sr:
|
65 |
wav_np = librosa.resample(wav_np, orig_sr, sr)
|
66 |
wav = torch.from_numpy(wav_np).float().to(device)
|
67 |
-
norm = torch.sqrt(len(wav) / torch.sum(wav
|
68 |
wav = (wav * norm).unsqueeze(0)
|
69 |
|
70 |
amp, pha, _ = mag_phase_stft(wav, **stft_cfg, compress_factor=model_cfg["compress_factor"])
|
@@ -74,42 +74,35 @@ def enhance(audio):
|
|
74 |
if orig_sr != sr:
|
75 |
out = librosa.resample(out, sr, orig_sr)
|
76 |
|
77 |
-
#
|
|
|
|
|
|
|
|
|
78 |
D = librosa.stft(out, n_fft=1024, hop_length=512)
|
79 |
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
80 |
fig, ax = plt.subplots(figsize=(6, 3))
|
81 |
librosa.display.specshow(S_db, sr=orig_sr, hop_length=512, x_axis='time', y_axis='hz', ax=ax)
|
82 |
ax.set_title("Enhanced Spectrogram")
|
83 |
plt.colorbar(format="%+2.0f dB", ax=ax)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
examples=[
|
104 |
-
["examples/noisy_sample_16k.wav"],
|
105 |
-
],
|
106 |
-
inputs=audio_input,
|
107 |
-
outputs=[enhanced_audio, spec_plot],
|
108 |
-
fn=enhance,
|
109 |
-
cache_examples=True,
|
110 |
-
label="π Try These Examples"
|
111 |
-
)
|
112 |
-
|
113 |
-
gr.Markdown("<p style='text-align: center'><a href='https://arxiv.org/abs/2405.15144' target='_blank'>π SEMamba: Mamba for Long-Context Speech Enhancement (SLT 2024)</a></p>")
|
114 |
|
115 |
demo.launch()
|
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
+
def enhance(filepath):
|
62 |
+
if filepath is None: return None, None
|
63 |
+
wav_np, orig_sr = librosa.load(filepath, sr=None)
|
64 |
if orig_sr != sr:
|
65 |
wav_np = librosa.resample(wav_np, orig_sr, sr)
|
66 |
wav = torch.from_numpy(wav_np).float().to(device)
|
67 |
+
norm = torch.sqrt(len(wav) / torch.sum(wav**2))
|
68 |
wav = (wav * norm).unsqueeze(0)
|
69 |
|
70 |
amp, pha, _ = mag_phase_stft(wav, **stft_cfg, compress_factor=model_cfg["compress_factor"])
|
|
|
74 |
if orig_sr != sr:
|
75 |
out = librosa.resample(out, sr, orig_sr)
|
76 |
|
77 |
+
# write output to temp file
|
78 |
+
out_path = "enhanced_output.wav"
|
79 |
+
sf.write(out_path, out, orig_sr)
|
80 |
+
|
81 |
+
# plot spectrum
|
82 |
D = librosa.stft(out, n_fft=1024, hop_length=512)
|
83 |
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
84 |
fig, ax = plt.subplots(figsize=(6, 3))
|
85 |
librosa.display.specshow(S_db, sr=orig_sr, hop_length=512, x_axis='time', y_axis='hz', ax=ax)
|
86 |
ax.set_title("Enhanced Spectrogram")
|
87 |
plt.colorbar(format="%+2.0f dB", ax=ax)
|
88 |
+
|
89 |
+
return out_path, fig
|
90 |
+
|
91 |
+
# --- Gradio Interface ---
|
92 |
+
demo = gr.Interface(
|
93 |
+
fn=enhance,
|
94 |
+
inputs=gr.Audio(sources=["upload", "microphone"], type="filepath", label="Input Audio"),
|
95 |
+
outputs=[
|
96 |
+
gr.Audio(label="Enhanced Audio", type="filepath"),
|
97 |
+
gr.Plot(label="Spectrogram")
|
98 |
+
],
|
99 |
+
title="<a href='https://github.com/RoyChao19477/SEMamba' target='_blank'>SEMamba</a>: Speech Enhancement",
|
100 |
+
description="Upload or record a noisy audio file and SEMamba will enhance the speech and show its spectrogram.",
|
101 |
+
article="<p style='text-align: center'><a href='https://arxiv.org/abs/2405.15144' target='_blank'>π SEMamba: Mamba for Long-Context Speech Enhancement (SLT 2024)</a></p>",
|
102 |
+
examples=[
|
103 |
+
["examples/noisy_sample_16k.wav"]
|
104 |
+
],
|
105 |
+
cache_examples=True
|
106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
demo.launch()
|