Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torchaudio
|
| 3 |
+
import resampy
|
| 4 |
+
import numpy as np
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
def upsample_audio(audio_path, backend="CPU"):
|
| 9 |
+
# Read audio file
|
| 10 |
+
audio, orig_sr = sf.read(audio_path)
|
| 11 |
+
|
| 12 |
+
# Target sample rate (2x upsampling)
|
| 13 |
+
target_sr = orig_sr * 2
|
| 14 |
+
|
| 15 |
+
if backend == "CPU":
|
| 16 |
+
# Resampy CPU processing
|
| 17 |
+
upsampled = resampy.resample(audio, orig_sr, target_sr, filter="kaiser_best")
|
| 18 |
+
else:
|
| 19 |
+
# Torchaudio GPU processing
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
tensor_audio = torch.from_numpy(audio).float().to(device)
|
| 22 |
+
# If mono, add batch dimension
|
| 23 |
+
if tensor_audio.ndim == 1:
|
| 24 |
+
tensor_audio = tensor_audio.unsqueeze(0)
|
| 25 |
+
resampler = torchaudio.transforms.Resample(
|
| 26 |
+
orig_freq=orig_sr,
|
| 27 |
+
new_freq=target_sr,
|
| 28 |
+
resampling_method="kaiser_window",
|
| 29 |
+
lowpass_filter_width=64
|
| 30 |
+
).to(device)
|
| 31 |
+
upsampled = resampler(tensor_audio).cpu().numpy()
|
| 32 |
+
# Remove batch dimension if mono
|
| 33 |
+
if upsampled.shape[0] == 1:
|
| 34 |
+
upsampled = upsampled[0]
|
| 35 |
+
|
| 36 |
+
# Save temporary output
|
| 37 |
+
output_path = "upsampled_audio.wav"
|
| 38 |
+
sf.write(output_path, upsampled, target_sr)
|
| 39 |
+
|
| 40 |
+
return output_path
|
| 41 |
+
|
| 42 |
+
DESCRIPTION = """
|
| 43 |
+
# Audio Upsampler (2x Sample Rate)
|
| 44 |
+
|
| 45 |
+
**Easily upsample your audio files to double their original sample rate using high-quality algorithms.**
|
| 46 |
+
|
| 47 |
+
Choose between:
|
| 48 |
+
- **CPU (resampy):** High-quality upsampling on any computer.
|
| 49 |
+
- **GPU (torchaudio):** Fast upsampling if you have a compatible graphics card.
|
| 50 |
+
|
| 51 |
+
**How to Use:**
|
| 52 |
+
1. Upload or record an audio file.
|
| 53 |
+
2. Select your processing backend.
|
| 54 |
+
3. Click submit to receive an upsampled audio file.
|
| 55 |
+
|
| 56 |
+
The output will always be a WAV file with double the original sample rate.
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
ARTICLE = """
|
| 60 |
+
## How This App Works
|
| 61 |
+
|
| 62 |
+
This app increases the sample rate of your audio files (upsampling) for improved quality or compatibility with other tools.
|
| 63 |
+
- **CPU backend** uses the `resampy` library for high-quality, anti-aliased upsampling.
|
| 64 |
+
- **GPU backend** uses PyTorch and `torchaudio` to leverage CUDA acceleration (if available).
|
| 65 |
+
|
| 66 |
+
**Supported Input File Types:**
|
| 67 |
+
- WAV
|
| 68 |
+
- FLAC
|
| 69 |
+
- MP3
|
| 70 |
+
- OGG
|
| 71 |
+
- AAC
|
| 72 |
+
- M4A
|
| 73 |
+
|
| 74 |
+
The app automatically detects your audio's sample rate and outputs an upsampled version in standard WAV format.
|
| 75 |
+
|
| 76 |
+
**Maximum File Length:**
|
| 77 |
+
There is no strict limit, but very large files may take longer to process, especially on CPU.
|
| 78 |
+
|
| 79 |
+
**No AudioSR:**
|
| 80 |
+
This app does not use the AudioSR library; it performs real sample rate conversion.
|
| 81 |
+
|
| 82 |
+
**Example Usage:**
|
| 83 |
+
- Upload a 44.1kHz WAV file, select "GPU" if you have a compatible graphics card, and get an 88.2kHz upsampled output.
|
| 84 |
+
- Record a voice memo, select "CPU", and receive a higher-sample-rate version for further editing.
|
| 85 |
+
|
| 86 |
+
If you have questions about supported formats or encounter issues, please let us know!
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
interface = gr.Interface(
|
| 90 |
+
fn=upsample_audio,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Audio(label="Input Audio", type="filepath"),
|
| 93 |
+
gr.Radio(["CPU", "GPU"], label="Processing Backend", value="CPU")
|
| 94 |
+
],
|
| 95 |
+
outputs=gr.Audio(label="Upsampled Audio"),
|
| 96 |
+
title="Audio Upsampler (2x Sample Rate)",
|
| 97 |
+
description=DESCRIPTION,
|
| 98 |
+
article=ARTICLE,
|
| 99 |
+
examples=[
|
| 100 |
+
["sample1.wav", "CPU"],
|
| 101 |
+
["sample2.flac", "GPU"]
|
| 102 |
+
]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
interface.launch()
|