Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,121 +4,178 @@ import soundfile as sf
|
|
| 4 |
import torchaudio
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import tempfile
|
| 8 |
-
import
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return (target_sr, y_resampled)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
y_resampled = librosa.resample(y, orig_sr=sr, target_sr=target_sr)
|
| 23 |
|
| 24 |
-
return (target_sr, y_resampled)
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
y, sr = audio
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
resampler = torchaudio.transforms.Resample(
|
| 31 |
-
orig_freq=sr, new_freq=target_sr
|
| 32 |
-
)
|
| 33 |
-
waveform_resampled = resampler(waveform)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
gr.Markdown(
|
| 41 |
"""
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
)
|
| 47 |
|
| 48 |
-
audio_input = gr.Audio(type="numpy", label="Upload Audio")
|
| 49 |
-
|
| 50 |
with gr.Tabs():
|
| 51 |
|
| 52 |
-
# =====
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
out_ta_16k = gr.Audio(label="Torchaudio Output")
|
| 60 |
-
|
| 61 |
-
btn_16k = gr.Button("Process @ 16kHz")
|
| 62 |
-
|
| 63 |
-
btn_16k.click(
|
| 64 |
-
fn=lambda x: (
|
| 65 |
-
process_with_librosa(x, 16000),
|
| 66 |
-
process_with_soundfile(x, 16000),
|
| 67 |
-
process_with_torchaudio(x, 16000),
|
| 68 |
-
),
|
| 69 |
-
inputs=audio_input,
|
| 70 |
-
outputs=[out_librosa_16k, out_sf_16k, out_ta_16k],
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# ===== TAB 2: 22.05kHz =====
|
| 74 |
-
with gr.Tab("22.05 kHz"):
|
| 75 |
-
gr.Markdown("### π Target Sample Rate: **22,050 Hz**")
|
| 76 |
-
|
| 77 |
with gr.Row():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# =====
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
gr.Markdown(
|
| 116 |
"""
|
| 117 |
---
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
"""
|
| 123 |
)
|
| 124 |
|
|
|
|
| 4 |
import torchaudio
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
+
import zipfile
|
| 8 |
import tempfile
|
| 9 |
+
from pathlib import Path
|
| 10 |
|
| 11 |
+
# =========================================================
|
| 12 |
+
# Core Resampling Logic
|
| 13 |
+
# =========================================================
|
| 14 |
|
| 15 |
+
def resample_audio(y, sr, target_sr, backend):
|
| 16 |
+
if backend == "librosa":
|
| 17 |
+
y_out = librosa.resample(y, orig_sr=sr, target_sr=target_sr)
|
|
|
|
| 18 |
|
| 19 |
+
elif backend == "soundfile":
|
| 20 |
+
# soundfile is I/O only, librosa used for resampling
|
| 21 |
+
y_out = librosa.resample(y, orig_sr=sr, target_sr=target_sr)
|
| 22 |
+
|
| 23 |
+
elif backend == "torchaudio":
|
| 24 |
+
waveform = torch.tensor(y).unsqueeze(0)
|
| 25 |
+
resampler = torchaudio.transforms.Resample(
|
| 26 |
+
orig_freq=sr,
|
| 27 |
+
new_freq=target_sr
|
| 28 |
+
)
|
| 29 |
+
y_out = resampler(waveform).squeeze(0).numpy()
|
| 30 |
|
| 31 |
+
return y_out, target_sr
|
|
|
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
+
# =========================================================
|
| 35 |
+
# Single File Processing
|
| 36 |
+
# =========================================================
|
| 37 |
+
|
| 38 |
+
def single_file_process(audio, target_sr, backend):
|
| 39 |
y, sr = audio
|
| 40 |
+
y_out, sr_out = resample_audio(y, sr, target_sr, backend)
|
| 41 |
+
return (sr_out, y_out)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# =========================================================
|
| 45 |
+
# Batch ZIP Processing
|
| 46 |
+
# =========================================================
|
| 47 |
+
|
| 48 |
+
def batch_process_zip(zip_file, target_sr, backend):
|
| 49 |
+
output_zip_path = tempfile.NamedTemporaryFile(
|
| 50 |
+
delete=False, suffix=".zip"
|
| 51 |
+
).name
|
| 52 |
+
|
| 53 |
+
with zipfile.ZipFile(zip_file, "r") as zin, \
|
| 54 |
+
zipfile.ZipFile(output_zip_path, "w") as zout:
|
| 55 |
+
|
| 56 |
+
for file in zin.namelist():
|
| 57 |
+
if not file.lower().endswith((".wav", ".mp3", ".flac", ".ogg")):
|
| 58 |
+
continue
|
| 59 |
+
|
| 60 |
+
# Read file from ZIP
|
| 61 |
+
with zin.open(file) as f:
|
| 62 |
+
with tempfile.NamedTemporaryFile(suffix=".wav") as tmp:
|
| 63 |
+
tmp.write(f.read())
|
| 64 |
+
tmp.flush()
|
| 65 |
+
|
| 66 |
+
# Load audio
|
| 67 |
+
y, sr = librosa.load(tmp.name, sr=None, mono=True)
|
| 68 |
+
|
| 69 |
+
# Resample
|
| 70 |
+
y_out, sr_out = resample_audio(y, sr, target_sr, backend)
|
| 71 |
|
| 72 |
+
# Save output
|
| 73 |
+
out_name = f"{Path(file).stem}_{backend}_{target_sr}.wav"
|
| 74 |
+
with tempfile.NamedTemporaryFile(suffix=".wav") as out_tmp:
|
| 75 |
+
sf.write(out_tmp.name, y_out, sr_out)
|
| 76 |
+
zout.write(out_tmp.name, out_name)
|
| 77 |
|
| 78 |
+
return output_zip_path
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# =========================================================
|
| 82 |
+
# Gradio UI
|
| 83 |
+
# =========================================================
|
| 84 |
+
|
| 85 |
+
with gr.Blocks(title="Audio Resampling Studio") as demo:
|
| 86 |
gr.Markdown(
|
| 87 |
"""
|
| 88 |
+
# π Audio Resampling Studio
|
| 89 |
+
Resample audio using **librosa**, **soundfile**, or **torchaudio**.
|
| 90 |
+
|
| 91 |
+
**Features**
|
| 92 |
+
- π§ Single-file processing
|
| 93 |
+
- π¦ Batch ZIP processing
|
| 94 |
+
- π― Sample rates: 16k, 22.05k, 44.1k, 48k
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
|
|
|
|
|
|
|
| 98 |
with gr.Tabs():
|
| 99 |
|
| 100 |
+
# =================================================
|
| 101 |
+
# Single File Tab
|
| 102 |
+
# =================================================
|
| 103 |
+
with gr.Tab("π§ Single File"):
|
| 104 |
+
gr.Markdown("### Process a single audio file")
|
| 105 |
|
| 106 |
+
audio_input = gr.Audio(
|
| 107 |
+
type="numpy",
|
| 108 |
+
label="Upload Audio"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
+
backend = gr.Radio(
|
| 113 |
+
["librosa", "soundfile", "torchaudio"],
|
| 114 |
+
value="librosa",
|
| 115 |
+
label="Backend"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
target_sr = gr.Dropdown(
|
| 119 |
+
[16000, 22050, 44100, 48000],
|
| 120 |
+
value=16000,
|
| 121 |
+
label="Target Sample Rate (Hz)"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
process_btn = gr.Button("Resample Audio")
|
| 125 |
+
audio_output = gr.Audio(label="Resampled Output")
|
| 126 |
+
|
| 127 |
+
process_btn.click(
|
| 128 |
+
fn=single_file_process,
|
| 129 |
+
inputs=[audio_input, target_sr, backend],
|
| 130 |
+
outputs=audio_output
|
| 131 |
)
|
| 132 |
|
| 133 |
+
# =================================================
|
| 134 |
+
# Batch ZIP Tab
|
| 135 |
+
# =================================================
|
| 136 |
+
with gr.Tab("π¦ Batch ZIP"):
|
| 137 |
+
gr.Markdown(
|
| 138 |
+
"""
|
| 139 |
+
### Batch ZIP Processing
|
| 140 |
+
Upload a ZIP file containing audio files.
|
| 141 |
+
You will receive a ZIP of **resampled WAV files**.
|
| 142 |
+
"""
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
zip_input = gr.File(
|
| 146 |
+
label="Upload ZIP",
|
| 147 |
+
file_types=[".zip"]
|
| 148 |
+
)
|
| 149 |
|
| 150 |
with gr.Row():
|
| 151 |
+
backend_zip = gr.Radio(
|
| 152 |
+
["librosa", "soundfile", "torchaudio"],
|
| 153 |
+
value="librosa",
|
| 154 |
+
label="Backend"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
target_sr_zip = gr.Dropdown(
|
| 158 |
+
[16000, 22050, 44100, 48000],
|
| 159 |
+
value=16000,
|
| 160 |
+
label="Target Sample Rate (Hz)"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
zip_btn = gr.Button("Process ZIP")
|
| 164 |
+
zip_output = gr.File(label="Download Resampled ZIP")
|
| 165 |
+
|
| 166 |
+
zip_btn.click(
|
| 167 |
+
fn=batch_process_zip,
|
| 168 |
+
inputs=[zip_input, target_sr_zip, backend_zip],
|
| 169 |
+
outputs=zip_output
|
| 170 |
)
|
| 171 |
|
| 172 |
gr.Markdown(
|
| 173 |
"""
|
| 174 |
---
|
| 175 |
+
**Output format:** WAV
|
| 176 |
+
**CPU-safe:** Yes (HF Spaces compatible)
|
| 177 |
+
**Filename format:**
|
| 178 |
+
`originalname_backend_samplerate.wav`
|
| 179 |
"""
|
| 180 |
)
|
| 181 |
|