slslslrhfem commited on
Commit
d75df89
·
1 Parent(s): 824783f

fix: bypass torchaudio backend dispatch for torchaudio 2.9+

Browse files

torchaudio.load now routes through load_with_torchcodec and ignores the
backend= kwarg, so the previous soundfile-backend patch failed with
'TorchCodec is required'. Replace torchaudio.load / torchaudio.info with
direct soundfile-based implementations to bypass backend dispatch
entirely. Falls back to librosa for formats soundfile cannot decode.

Files changed (1) hide show
  1. app.py +51 -14
app.py CHANGED
@@ -3,29 +3,66 @@ import gradio as gr
3
  import torch
4
  import librosa
5
  import numpy as np
6
-
7
- # Patch torchaudio to use soundfile backend globally.
8
- # PyTorch 2.9+ defaults to torchcodec which requires FFmpeg shared libs
9
- # that are incompatible with the HF Space environment.
 
 
 
 
10
  import torchaudio
11
 
12
- _orig_torchaudio_load = torchaudio.load
13
 
 
 
 
 
 
 
 
14
 
15
- def _patched_load(*args, **kwargs):
16
- kwargs.setdefault("backend", "soundfile")
17
- return _orig_torchaudio_load(*args, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  torchaudio.load = _patched_load
21
- if hasattr(torchaudio, "info"):
22
- _orig_torchaudio_info = torchaudio.info
23
 
24
- def _patched_info(*args, **kwargs):
25
- kwargs.setdefault("backend", "soundfile")
26
- return _orig_torchaudio_info(*args, **kwargs)
27
 
28
- torchaudio.info = _patched_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  from inference import inference
31
  from huggingface_hub import hf_hub_download
 
3
  import torch
4
  import librosa
5
  import numpy as np
6
+ import soundfile as sf
7
+
8
+ # Replace torchaudio.load / torchaudio.info with soundfile-backed versions.
9
+ # Why: torchaudio 2.9+ routes torchaudio.load through load_with_torchcodec and
10
+ # ignores the legacy backend= kwarg. torchcodec is unavailable in this Space,
11
+ # so any indirect torchaudio.load call (inference.py, dataset_f.py, preprocess.py)
12
+ # raises "TorchCodec is required for load_with_torchcodec". Bypassing the
13
+ # backend dispatch entirely is the only stable fix.
14
  import torchaudio
15
 
 
16
 
17
+ def _patched_load(filepath, *args, **kwargs):
18
+ frame_offset = kwargs.pop("frame_offset", 0)
19
+ num_frames = kwargs.pop("num_frames", -1)
20
+ if len(args) >= 1:
21
+ frame_offset = args[0]
22
+ if len(args) >= 2:
23
+ num_frames = args[1]
24
 
25
+ try:
26
+ data, sample_rate = sf.read(
27
+ str(filepath),
28
+ start=int(frame_offset) if frame_offset else 0,
29
+ frames=int(num_frames) if num_frames and num_frames > 0 else -1,
30
+ dtype="float32",
31
+ always_2d=True,
32
+ )
33
+ waveform = torch.from_numpy(data.T).contiguous()
34
+ return waveform, sample_rate
35
+ except Exception:
36
+ data, sample_rate = librosa.load(str(filepath), sr=None, mono=False)
37
+ if data.ndim == 1:
38
+ data = data[np.newaxis, :]
39
+ if frame_offset:
40
+ data = data[:, int(frame_offset):]
41
+ if num_frames and num_frames > 0:
42
+ data = data[:, : int(num_frames)]
43
+ waveform = torch.from_numpy(np.ascontiguousarray(data)).float()
44
+ return waveform, sample_rate
45
 
46
 
47
  torchaudio.load = _patched_load
 
 
48
 
 
 
 
49
 
50
+ class _AudioInfo:
51
+ __slots__ = ("sample_rate", "num_frames", "num_channels", "bits_per_sample", "encoding")
52
+
53
+
54
+ def _patched_info(filepath, *args, **kwargs):
55
+ info = sf.info(str(filepath))
56
+ out = _AudioInfo()
57
+ out.sample_rate = info.samplerate
58
+ out.num_frames = info.frames
59
+ out.num_channels = info.channels
60
+ out.bits_per_sample = 0
61
+ out.encoding = info.format
62
+ return out
63
+
64
+
65
+ torchaudio.info = _patched_info
66
 
67
  from inference import inference
68
  from huggingface_hub import hf_hub_download