roychao19477 commited on
Commit
c58812e
Β·
1 Parent(s): 545af0d
Files changed (1) hide show
  1. app.py +28 -35
app.py CHANGED
@@ -58,13 +58,13 @@ model.eval()
58
 
59
 
60
  @spaces.GPU
61
- def enhance(audio):
62
- if audio is None: return None, None
63
- orig_sr, wav_np = audio
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,42 +74,35 @@ def enhance(audio):
74
  if orig_sr != sr:
75
  out = librosa.resample(out, sr, orig_sr)
76
 
77
- # spectrogram
 
 
 
 
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
- return (orig_sr, out), fig
85
-
86
- # --- Layout with Blocks ---
87
- with gr.Blocks(css=".gr-box {border: none !important}") as demo:
88
- gr.Markdown("<h1 style='text-align: center;'>🎧 <a href='https://github.com/RoyChao19477/SEMamba' target='_blank'>SEMamba</a>: Speech Enhancement</h1>")
89
- gr.Markdown("Enhance real-world noisy speech using Mamba. Upload or record an audio clip and view the spectrogram.")
90
-
91
- with gr.Row():
92
- with gr.Column():
93
- audio_input = gr.Audio(sources=["upload", "microphone"], type="numpy", label="Upload or Record", elem_id="input-audio")
94
- run_btn = gr.Button("Enhance Now πŸš€", variant="primary")
95
-
96
- with gr.Column():
97
- enhanced_audio = gr.Audio(label="Enhanced Output", type="numpy")
98
- spec_plot = gr.Plot(label="Spectrogram")
99
-
100
- run_btn.click(enhance, inputs=audio_input, outputs=[enhanced_audio, spec_plot])
101
-
102
- gr.Examples(
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()