Spaces:
Running
on
Zero
Running
on
Zero
roychao19477
commited on
Commit
Β·
7279258
1
Parent(s):
ff0b6ec
Upload
Browse files
app.py
CHANGED
@@ -58,15 +58,15 @@ model.eval()
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
-
def enhance(
|
62 |
-
wav_np, orig_sr = librosa.load(
|
63 |
-
if orig_sr!=SR: wav_np=librosa.resample(wav_np,orig_sr,SR)
|
64 |
wav = torch.from_numpy(wav_np).float().to(device)
|
65 |
norm= torch.sqrt(len(wav)/torch.sum(wav**2)); wav=(wav*norm).unsqueeze(0)
|
66 |
amp,pha,_ = mag_phase_stft(wav,**stft_cfg,compress_factor=model_cfg["compress_factor"])
|
67 |
amp_g,pha_g = model(amp,pha)
|
68 |
out = mag_phase_istft(amp_g,pha_g,**stft_cfg,compress_factor=model_cfg["compress_factor"])
|
69 |
-
out
|
70 |
if orig_sr!=SR: out=librosa.resample(out,SR,orig_sr)
|
71 |
sf.write("enhanced.wav", out, orig_sr)
|
72 |
|
@@ -74,35 +74,44 @@ def enhance(filepath):
|
|
74 |
S = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
75 |
fig,ax=plt.subplots(figsize=(6,3))
|
76 |
librosa.display.specshow(S,sr=orig_sr,hop_length=512,x_axis="time",y_axis="hz",ax=ax)
|
77 |
-
ax.set_title("Enhanced Spectrogram"); plt.colorbar(format="%+2.0f
|
78 |
return "enhanced.wav", fig
|
79 |
|
80 |
-
# β
|
81 |
CSS = """
|
82 |
-
|
83 |
-
#
|
84 |
-
|
85 |
-
|
86 |
-
#
|
87 |
-
#
|
88 |
"""
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
gr.HTML("<
|
93 |
-
gr.HTML("<p id='subtitle'>Upload or record your noisy clip, then click Enhance to boost clarity and view its spectrogram.</p>")
|
94 |
-
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
95 |
|
96 |
with gr.Row():
|
97 |
-
with gr.Column(
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
demo.launch()
|
|
|
58 |
|
59 |
|
60 |
@spaces.GPU
|
61 |
+
def enhance(path):
|
62 |
+
wav_np, orig_sr = librosa.load(path, sr=None)
|
63 |
+
if orig_sr!=SR: wav_np=librosa.resample(wav_np, orig_sr, SR)
|
64 |
wav = torch.from_numpy(wav_np).float().to(device)
|
65 |
norm= torch.sqrt(len(wav)/torch.sum(wav**2)); wav=(wav*norm).unsqueeze(0)
|
66 |
amp,pha,_ = mag_phase_stft(wav,**stft_cfg,compress_factor=model_cfg["compress_factor"])
|
67 |
amp_g,pha_g = model(amp,pha)
|
68 |
out = mag_phase_istft(amp_g,pha_g,**stft_cfg,compress_factor=model_cfg["compress_factor"])
|
69 |
+
out=(out/norm).squeeze().cpu().numpy()
|
70 |
if orig_sr!=SR: out=librosa.resample(out,SR,orig_sr)
|
71 |
sf.write("enhanced.wav", out, orig_sr)
|
72 |
|
|
|
74 |
S = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
75 |
fig,ax=plt.subplots(figsize=(6,3))
|
76 |
librosa.display.specshow(S,sr=orig_sr,hop_length=512,x_axis="time",y_axis="hz",ax=ax)
|
77 |
+
ax.set_title("Enhanced Spectrogram"); plt.colorbar(format="%+2.0fβdB",ax=ax)
|
78 |
return "enhanced.wav", fig
|
79 |
|
80 |
+
# β custom CSS to remove all borders and style panels β
|
81 |
CSS = """
|
82 |
+
.gr-block {padding:2rem;}
|
83 |
+
#input-audio .gradio-component, #output-audio .gradio-component, #spec-plot .gradio-component {
|
84 |
+
border:1px solid #ddd; border-radius:8px; background:#fafafa; padding:1rem;
|
85 |
+
}
|
86 |
+
#input-audio label, #output-audio label, #spec-plot label {font-weight:600; color:#333;}
|
87 |
+
#run-btn {width:100%; margin-top:1rem;}
|
88 |
"""
|
89 |
|
90 |
+
with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
|
91 |
+
gr.HTML("<h2 style='text-align:center;'>π§ SEMamba: Speech Enhancement</h2>")
|
92 |
+
gr.HTML("<p style='text-align:center;color:#555;'>Upload or record noisy speech, then click Enhance to clean it.</p>")
|
|
|
|
|
93 |
|
94 |
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
input_audio = gr.Audio(
|
97 |
+
sources=["upload","microphone"],
|
98 |
+
type="filepath",
|
99 |
+
label="Input Audio",
|
100 |
+
elem_id="input-audio"
|
101 |
+
)
|
102 |
+
run_btn = gr.Button("Enhance", variant="primary", elem_id="run-btn")
|
103 |
+
|
104 |
+
with gr.Column():
|
105 |
+
output_audio = gr.Audio(
|
106 |
+
type="filepath",
|
107 |
+
label="Enhanced Audio",
|
108 |
+
elem_id="output-audio"
|
109 |
+
)
|
110 |
+
spec_plot = gr.Plot(
|
111 |
+
label="Enhanced Spectrogram",
|
112 |
+
elem_id="spec-plot"
|
113 |
+
)
|
114 |
+
|
115 |
+
run_btn.click(enhance, inputs=input_audio, outputs=[output_audio, spec_plot])
|
116 |
|
117 |
demo.launch()
|