Spaces:
Running
on
Zero
Running
on
Zero
roychao19477
commited on
Commit
Β·
9657939
1
Parent(s):
179e88e
Upload
Browse files
app.py
CHANGED
@@ -58,55 +58,47 @@ model.eval()
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
-
def enhance(
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
out =
|
72 |
-
|
73 |
-
|
74 |
-
if orig_sr != SR: out = librosa.resample(out, SR, orig_sr)
|
75 |
-
# save
|
76 |
sf.write("enhanced.wav", out, orig_sr)
|
77 |
-
|
78 |
D = librosa.stft(out, n_fft=1024, hop_length=512)
|
79 |
S = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
80 |
-
fig,ax=plt.subplots(figsize=(6,3))
|
81 |
librosa.display.specshow(S, sr=orig_sr, hop_length=512, x_axis="time", y_axis="hz", ax=ax)
|
82 |
-
ax.set_title("Enhanced Spectrogram")
|
|
|
|
|
83 |
return "enhanced.wav", fig
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
"""
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
gr.
|
95 |
-
gr.
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
elem_id="in-audio"
|
104 |
-
)
|
105 |
-
run_btn = gr.Button("Enhance Now π", variant="primary", elem_id="run")
|
106 |
-
with gr.Column(scale=1):
|
107 |
-
audio_out = gr.Audio(type="filepath", label="Enhanced Audio")
|
108 |
-
spec_out = gr.Plot(label="Spectrogram")
|
109 |
-
|
110 |
-
run_btn.click(enhance, inputs=audio_in, outputs=[audio_out, spec_out])
|
111 |
|
112 |
demo.launch()
|
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
+
def enhance(filepath):
|
62 |
+
wav_np, orig_sr = librosa.load(filepath, sr=None)
|
63 |
+
if orig_sr != SR:
|
64 |
+
wav_np = librosa.resample(wav_np, orig_sr, SR)
|
65 |
+
wav = torch.from_numpy(wav_np).float().to(device)
|
66 |
+
norm = torch.sqrt(len(wav) / torch.sum(wav**2))
|
67 |
+
wav = (wav * norm).unsqueeze(0)
|
68 |
+
amp, pha, _ = mag_phase_stft(wav, **stft_cfg, compress_factor=model_cfg["compress_factor"])
|
69 |
+
amp_g, pha_g = model(amp, pha)
|
70 |
+
out = mag_phase_istft(amp_g, pha_g, **stft_cfg, compress_factor=model_cfg["compress_factor"])
|
71 |
+
out = (out / norm).squeeze().cpu().numpy()
|
72 |
+
if orig_sr != SR:
|
73 |
+
out = librosa.resample(out, SR, orig_sr)
|
|
|
|
|
74 |
sf.write("enhanced.wav", out, orig_sr)
|
75 |
+
|
76 |
D = librosa.stft(out, n_fft=1024, hop_length=512)
|
77 |
S = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
78 |
+
fig, ax = plt.subplots(figsize=(6, 3))
|
79 |
librosa.display.specshow(S, sr=orig_sr, hop_length=512, x_axis="time", y_axis="hz", ax=ax)
|
80 |
+
ax.set_title("Enhanced Spectrogram")
|
81 |
+
plt.colorbar(format="%+2.0f dB", ax=ax)
|
82 |
+
|
83 |
return "enhanced.wav", fig
|
84 |
|
85 |
+
# --- Header markdown ---
|
86 |
+
ABOUT = """
|
87 |
+
# SEMamba: Speech Enhancement
|
88 |
+
Unofficial demo for [SEMamba](https://github.com/RoyChao19477/SEMamba), a Mamba-based speech enhancement model from SLT 2024.
|
89 |
+
Upload or record a noisy audio file to enhance clarity and view the spectrogram.
|
90 |
"""
|
91 |
|
92 |
+
# --- Build demo ---
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
gr.Markdown(ABOUT)
|
95 |
+
audio_in = gr.Audio(label="Input Audio", type="filepath", interactive=True)
|
96 |
+
run_btn = gr.Button("Enhance", variant="primary")
|
97 |
+
enhanced_audio = gr.Audio(label="Enhanced Audio", type="filepath", interactive=False)
|
98 |
+
spectrogram = gr.Plot(label="Spectrogram")
|
99 |
+
|
100 |
+
run_btn.click(fn=enhance, inputs=audio_in, outputs=[enhanced_audio, spectrogram])
|
101 |
+
|
102 |
+
gr.Markdown("Unofficial demo by [yourname](https://github.com/RoyChao19477)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
demo.launch()
|