Spaces:
Build error
Build error
Iliass Lasri commited on
Commit ·
7bb5640
1
Parent(s): e1aaeac
add fallback sample and snr
Browse files- app.py +15 -18
- samples/fallback_sample.wav +3 -0
app.py
CHANGED
|
@@ -177,7 +177,7 @@ def demo_fn(speech_upl: str, noise_type: str, snr: int, mic_input: Optional[str]
|
|
| 177 |
snr = int(snr)
|
| 178 |
noise_fn = NOISES[noise_type]
|
| 179 |
|
| 180 |
-
#
|
| 181 |
max_s = 10
|
| 182 |
if speech_upl is not None:
|
| 183 |
sample, _ = load_audio_torch(speech_upl, sr)
|
|
@@ -186,24 +186,23 @@ def demo_fn(speech_upl: str, noise_type: str, snr: int, mic_input: Optional[str]
|
|
| 186 |
start = torch.randint(0, sample.shape[-1] - max_len, ()).item()
|
| 187 |
sample = sample[..., start : start + max_len]
|
| 188 |
else:
|
| 189 |
-
|
| 190 |
-
sample, _ = load_audio_torch("samples/p232_013_clean.wav", sr)
|
| 191 |
sample = sample[..., : max_s * sr]
|
| 192 |
|
| 193 |
# Ensure channels first
|
| 194 |
if sample.dim() > 1 and sample.shape[0] > 1:
|
| 195 |
sample = sample.mean(dim=0, keepdim=True)
|
| 196 |
|
| 197 |
-
#
|
| 198 |
if noise_fn is not None:
|
| 199 |
noise, _ = load_audio_torch(noise_fn, sr)
|
| 200 |
_, _, sample = mix_at_snr(sample, noise, snr)
|
| 201 |
|
| 202 |
-
#
|
| 203 |
noisy_wav_path = tempfile.NamedTemporaryFile(suffix="noisy.wav", delete=False).name
|
| 204 |
save_audio_torch(noisy_wav_path, sample, sr)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
enhanced_wav_path = tempfile.NamedTemporaryFile(suffix="enhanced.wav", delete=False).name
|
| 208 |
|
| 209 |
logger.info("Starting enhancement...")
|
|
@@ -212,10 +211,8 @@ def demo_fn(speech_upl: str, noise_type: str, snr: int, mic_input: Optional[str]
|
|
| 212 |
enhance_audio_hf(None, noisy_wav_path, enhanced_wav_path)
|
| 213 |
logger.info("Enhancement finished")
|
| 214 |
|
| 215 |
-
# 5. Load Enhanced Audio for Visualization
|
| 216 |
enhanced, _ = load_audio_torch(enhanced_wav_path, sr)
|
| 217 |
|
| 218 |
-
# 6. Generate Visuals
|
| 219 |
ax_noisy.clear()
|
| 220 |
ax_enh.clear()
|
| 221 |
noisy_im = spec_im(sample, sr=sr, figure=fig_noisy, ax=ax_noisy)
|
|
@@ -248,16 +245,16 @@ with gr.Blocks() as demo:
|
|
| 248 |
audio_file = gr.Audio(type="filepath", label="File Input", visible=True)
|
| 249 |
inputs = [
|
| 250 |
audio_file,
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
mic_input,
|
| 262 |
]
|
| 263 |
btn = gr.Button("Denoise", variant="primary")
|
|
|
|
| 177 |
snr = int(snr)
|
| 178 |
noise_fn = NOISES[noise_type]
|
| 179 |
|
| 180 |
+
# Load Clean Speech
|
| 181 |
max_s = 10
|
| 182 |
if speech_upl is not None:
|
| 183 |
sample, _ = load_audio_torch(speech_upl, sr)
|
|
|
|
| 186 |
start = torch.randint(0, sample.shape[-1] - max_len, ()).item()
|
| 187 |
sample = sample[..., start : start + max_len]
|
| 188 |
else:
|
| 189 |
+
sample, _ = load_audio_torch("samples/fallback_sample.wav", sr)
|
|
|
|
| 190 |
sample = sample[..., : max_s * sr]
|
| 191 |
|
| 192 |
# Ensure channels first
|
| 193 |
if sample.dim() > 1 and sample.shape[0] > 1:
|
| 194 |
sample = sample.mean(dim=0, keepdim=True)
|
| 195 |
|
| 196 |
+
# Add Noise (if selected)
|
| 197 |
if noise_fn is not None:
|
| 198 |
noise, _ = load_audio_torch(noise_fn, sr)
|
| 199 |
_, _, sample = mix_at_snr(sample, noise, snr)
|
| 200 |
|
| 201 |
+
# Save Noisy File (Input for enhance_audio)
|
| 202 |
noisy_wav_path = tempfile.NamedTemporaryFile(suffix="noisy.wav", delete=False).name
|
| 203 |
save_audio_torch(noisy_wav_path, sample, sr)
|
| 204 |
|
| 205 |
+
# Run Inference using your Custom Function
|
| 206 |
enhanced_wav_path = tempfile.NamedTemporaryFile(suffix="enhanced.wav", delete=False).name
|
| 207 |
|
| 208 |
logger.info("Starting enhancement...")
|
|
|
|
| 211 |
enhance_audio_hf(None, noisy_wav_path, enhanced_wav_path)
|
| 212 |
logger.info("Enhancement finished")
|
| 213 |
|
|
|
|
| 214 |
enhanced, _ = load_audio_torch(enhanced_wav_path, sr)
|
| 215 |
|
|
|
|
| 216 |
ax_noisy.clear()
|
| 217 |
ax_enh.clear()
|
| 218 |
noisy_im = spec_im(sample, sr=sr, figure=fig_noisy, ax=ax_noisy)
|
|
|
|
| 245 |
audio_file = gr.Audio(type="filepath", label="File Input", visible=True)
|
| 246 |
inputs = [
|
| 247 |
audio_file,
|
| 248 |
+
gr.Dropdown(
|
| 249 |
+
label="Add background noise",
|
| 250 |
+
choices=list(NOISES.keys()),
|
| 251 |
+
value="None",
|
| 252 |
+
),
|
| 253 |
+
gr.Dropdown(
|
| 254 |
+
label="Noise Level (SNR)",
|
| 255 |
+
choices=["-5", "0", "10", "20"],
|
| 256 |
+
value="10",
|
| 257 |
+
),
|
| 258 |
mic_input,
|
| 259 |
]
|
| 260 |
btn = gr.Button("Denoise", variant="primary")
|
samples/fallback_sample.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:572cb9eef7bf796a102c5d82d09e592b15ac97d22daf15c54f7b5a91139720a8
|
| 3 |
+
size 320058
|