Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from pathlib import Path
|
|
| 8 |
from typing import List, Optional, Tuple
|
| 9 |
|
| 10 |
import gradio as gr
|
|
|
|
| 11 |
import numpy as np
|
| 12 |
import soundfile as sf
|
| 13 |
import torch
|
|
@@ -198,7 +199,30 @@ def resample_waveform(
|
|
| 198 |
|
| 199 |
def load_audio_file(file_path: str, log: bool = True) -> Tuple[torch.Tensor, int]:
|
| 200 |
log_progress(f"Loading audio: {Path(file_path).name}", enabled=log)
|
| 201 |
-
waveform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
waveform = ensure_mono(waveform)
|
| 203 |
if sample_rate != DEFAULT_SAMPLE_RATE:
|
| 204 |
log_progress(
|
|
|
|
| 8 |
from typing import List, Optional, Tuple
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
+
import librosa
|
| 12 |
import numpy as np
|
| 13 |
import soundfile as sf
|
| 14 |
import torch
|
|
|
|
| 199 |
|
| 200 |
def load_audio_file(file_path: str, log: bool = True) -> Tuple[torch.Tensor, int]:
|
| 201 |
log_progress(f"Loading audio: {Path(file_path).name}", enabled=log)
|
| 202 |
+
waveform = None
|
| 203 |
+
sample_rate = None
|
| 204 |
+
|
| 205 |
+
try:
|
| 206 |
+
waveform, sample_rate = torchaudio.load(file_path)
|
| 207 |
+
waveform = ensure_mono(waveform)
|
| 208 |
+
except Exception as exc:
|
| 209 |
+
log_progress(f"torchaudio load failed: {exc}", 2, enabled=log)
|
| 210 |
+
|
| 211 |
+
if waveform is None or sample_rate is None:
|
| 212 |
+
try:
|
| 213 |
+
data, sample_rate = sf.read(
|
| 214 |
+
file_path, always_2d=True, dtype="float32"
|
| 215 |
+
)
|
| 216 |
+
waveform = torch.from_numpy(data.T)
|
| 217 |
+
except Exception as exc:
|
| 218 |
+
log_progress(f"soundfile load failed: {exc}", 2, enabled=log)
|
| 219 |
+
data, sample_rate = librosa.load(
|
| 220 |
+
file_path, sr=None, mono=False, dtype=np.float32
|
| 221 |
+
)
|
| 222 |
+
if data.ndim == 1:
|
| 223 |
+
data = data[None, :]
|
| 224 |
+
waveform = torch.from_numpy(data)
|
| 225 |
+
|
| 226 |
waveform = ensure_mono(waveform)
|
| 227 |
if sample_rate != DEFAULT_SAMPLE_RATE:
|
| 228 |
log_progress(
|