harp-dev commited on
Commit
f626c4a
·
verified ·
1 Parent(s): 0b16eb7

Move to ZeroGPU: the Gradio SDK held at 6.17.3, torch 2.8.0, the pinned checkpoint built once in the worker, and requests checked and sized before the GPU.

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -29
  2. README.md +8 -16
  3. app.py +58 -21
  4. requirements.txt +5 -2
Dockerfile DELETED
@@ -1,29 +0,0 @@
1
- FROM python:3.11-slim
2
-
3
- WORKDIR /app
4
-
5
- COPY packages.txt /app/packages.txt
6
- RUN apt-get update && \
7
- apt-get install -y --no-install-recommends git && \
8
- xargs apt-get install -y --no-install-recommends < /app/packages.txt && \
9
- rm -rf /var/lib/apt/lists/*
10
- FROM python:3.11-slim
11
-
12
- WORKDIR /app
13
-
14
- COPY packages.txt /app/packages.txt
15
- RUN apt-get update && \
16
- apt-get install -y --no-install-recommends git && \
17
- xargs apt-get install -y --no-install-recommends < /app/packages.txt && \
18
- rm -rf /var/lib/apt/lists/*
19
-
20
- COPY requirements.txt /app/requirements.txt
21
- RUN pip install --no-cache-dir gradio==5.28.0
22
- RUN pip install --no-cache-dir -r /app/requirements.txt
23
-
24
- COPY app.py /app/app.py
25
-
26
- ENV PORT=7860
27
- EXPOSE 7860
28
-
29
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -3,8 +3,11 @@ title: AudioSR
3
  emoji: 📶
4
  colorFrom: blue
5
  colorTo: green
6
- sdk: docker
7
- sdk_version: 6.24.0
 
 
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
@@ -17,21 +20,10 @@ tags:
17
  - output:audio
18
  - sample-rate:48000
19
  - channels:mono
20
- app_port: 7860
21
  ---
22
 
23
- # AudioSR — HARP Endpoint
24
 
25
- Deploys [AudioSR](https://github.com/haoheliu/versatile_audio_super_resolution) as a [PyHARP](https://github.com/TEAMuP-dev/pyharp) endpoint, accessible from within DAWs (Logic Pro, Reaper, Ableton) via the [HARP plugin](https://github.com/TEAMuP-dev/HARP).
26
 
27
- ## What it does
28
-
29
- Upsamples any audio to 48kHz using audio super-resolution. Files already at 48kHz or above are returned unchanged.
30
-
31
- ## Usage
32
-
33
- Load this Space's URL into HARP as a custom endpoint. Upload an audio file and adjust DDIM Steps (more steps = better quality, slower processing).
34
-
35
- ## Hardware Requirements
36
-
37
- This Space requires at minimum a **T4 Small** GPU instance due to the AudioSR model size (~6GB). It will time out on free CPU hardware.
 
3
  emoji: 📶
4
  colorFrom: blue
5
  colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 6.17.3
8
+ python_version: "3.11"
9
+ preload_from_hub:
10
+ - haoheliu/audiosr_basic pytorch_model.bin 74a47f49061a1e788e968cc43ad45c0b6243f37d
11
  app_file: app.py
12
  pinned: false
13
  license: mit
 
20
  - output:audio
21
  - sample-rate:48000
22
  - channels:mono
 
23
  ---
24
 
25
+ Upsamples any audio to 48 kHz with audio super-resolution.
26
 
27
+ Gradio is held at 6.17.3, the last release that allows the transformers 4.30.2 that audiosr pins.
28
 
29
+ A HARP deployment. See https://github.com/TEAMuP-dev/HARP
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,9 +1,11 @@
1
- import sys
 
2
  import os
3
  import tempfile
4
  import soundfile as sf
5
  import torch
6
  from audiosr import build_model, super_resolution_long_audio
 
7
  from pyharp import ModelCard, build_endpoint, SampleRate, Channels
8
  from pyharp.tags import Subcategory
9
  import gradio as gr
@@ -54,15 +56,18 @@ def _patched_lowpass_filtering_prepare_inference(dl_output):
54
 
55
  _audiosr_pipeline.lowpass_filtering_prepare_inference = _patched_lowpass_filtering_prepare_inference
56
 
 
57
  model = None
58
 
59
- def get_model():
60
- global model
61
- if model is None:
62
- print("Loading AudioSR model...", flush=True)
63
- model = build_model(model_name="basic", device="auto")
64
- print("Model loaded.", flush=True)
65
- return model
 
 
66
 
67
  def match_array_shapes(array_1: np.ndarray, array_2: np.ndarray):
68
  if (len(array_1.shape) == 1) & (len(array_2.shape) == 1):
@@ -101,19 +106,46 @@ model_card = ModelCard(
101
  tags=[Subcategory.BANDWIDTH_EXTENSION, SampleRate(48000), Channels(1)],
102
  )
103
 
104
- @torch.inference_mode()
 
 
 
 
 
 
 
 
 
105
  def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: str, multiband_ensemble: bool, input_cutoff: int) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  try:
107
  seed_val = int(seed) if seed and seed.strip() not in ("0", "") else random.randint(1, 2**32 - 1)
108
  except (TypeError, ValueError):
109
- seed_val = random.randint(1, 2**32 - 1)
110
 
111
  # AudioSR's internal cutoff-frequency detection (lowpass_filtering_prepare_inference)
112
  # hardcodes a 24kHz-Nyquist assumption regardless of actual input sample rate,
113
  # which can produce an invalid filter Wn and crash on input below 48kHz.
114
  # Resample up front so that assumption always holds.
115
- orig_sr = sf.info(input_audio_path).samplerate
116
- original_channels = sf.info(input_audio_path).channels
117
  if orig_sr < 48000:
118
  y, _ = librosa.load(input_audio_path, sr=None, mono=False)
119
  y = librosa.resample(y, orig_sr=orig_sr, target_sr=48000)
@@ -121,15 +153,7 @@ def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, se
121
  sf.write(resampled_path, y.T if y.ndim > 1 else y, samplerate=48000)
122
  input_audio_path = resampled_path
123
 
124
- waveform = super_resolution_long_audio(
125
- get_model(),
126
- input_audio_path,
127
- seed=seed_val,
128
- guidance_scale=float(guidance_scale),
129
- ddim_steps=int(ddim_steps),
130
- )
131
-
132
- output = waveform.cpu().numpy()
133
 
134
  if multiband_ensemble:
135
  crossover_freq = int(input_cutoff) - 1000
@@ -156,6 +180,19 @@ def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, se
156
  sf.write(output_path, output, samplerate=48000)
157
  return output_path
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  if __name__ == "__main__":
160
  with gr.Blocks() as demo:
161
  input_components = [
 
1
+ import spaces # imported before torch, as ZeroGPU requires
2
+
3
  import os
4
  import tempfile
5
  import soundfile as sf
6
  import torch
7
  from audiosr import build_model, super_resolution_long_audio
8
+ from huggingface_hub import hf_hub_download
9
  from pyharp import ModelCard, build_endpoint, SampleRate, Channels
10
  from pyharp.tags import Subcategory
11
  import gradio as gr
 
56
 
57
  _audiosr_pipeline.lowpass_filtering_prepare_inference = _patched_lowpass_filtering_prepare_inference
58
 
59
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
60
  model = None
61
 
62
+ # Built in the worker, which runs process_fn, on its main thread, where ZeroGPU's emulation records
63
+ # the CUDA placement, so that no request spends the caller's allowance on building it. The server
64
+ # never runs process_fn. audiosr downloads its checkpoint unpinned, and ignores a path it is given,
65
+ # so the pinned file, which the README preloads, is handed to it in place of its own download.
66
+ if __name__ != "__main__":
67
+ _checkpoint = hf_hub_download("haoheliu/audiosr_basic", "pytorch_model.bin",
68
+ revision="74a47f49061a1e788e968cc43ad45c0b6243f37d")
69
+ _audiosr_pipeline.download_checkpoint = lambda model_name: _checkpoint
70
+ model = build_model(model_name="basic", device=DEVICE)
71
 
72
  def match_array_shapes(array_1: np.ndarray, array_2: np.ndarray):
73
  if (len(array_1.shape) == 1) & (len(array_2.shape) == 1):
 
106
  tags=[Subcategory.BANDWIDTH_EXTENSION, SampleRate(48000), Channels(1)],
107
  )
108
 
109
+ # The audio is upsampled in 15-second chunks, each at the number of DDIM steps asked for, so the
110
+ # cost grows with both
111
+ MAX_INPUT_SECONDS = 60
112
+ MAX_GPU_SECONDS = 200
113
+
114
+
115
+ def gpu_seconds(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: int) -> float:
116
+ return 20 + 0.5 * sf.info(input_audio_path).duration * int(ddim_steps) / 50
117
+
118
+
119
  def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: str, multiband_ensemble: bool, input_cutoff: int) -> str:
120
+ # Checked before the GPU is reserved, so a request the model cannot take spends none of the
121
+ # caller's ZeroGPU allowance
122
+ if not input_audio_path:
123
+ raise gr.Error("Provide the audio to upsample.")
124
+ try:
125
+ info = sf.info(input_audio_path)
126
+ except Exception as exc:
127
+ raise gr.Error(f"Could not read the audio file: {exc}") from exc
128
+ if info.duration > MAX_INPUT_SECONDS:
129
+ raise gr.Error(
130
+ f"The audio is {info.duration:.1f} s long. Trim it to {MAX_INPUT_SECONDS} seconds or less."
131
+ )
132
+ if gpu_seconds(input_audio_path, ddim_steps, guidance_scale, 0) > MAX_GPU_SECONDS:
133
+ raise gr.Error(
134
+ f"{int(ddim_steps)} DDIM steps on {info.duration:.0f} s of audio need more GPU time than "
135
+ "a request can reserve. Lower the steps or trim the audio."
136
+ )
137
+
138
  try:
139
  seed_val = int(seed) if seed and seed.strip() not in ("0", "") else random.randint(1, 2**32 - 1)
140
  except (TypeError, ValueError):
141
+ seed_val = random.randint(1, 2**32 - 1)
142
 
143
  # AudioSR's internal cutoff-frequency detection (lowpass_filtering_prepare_inference)
144
  # hardcodes a 24kHz-Nyquist assumption regardless of actual input sample rate,
145
  # which can produce an invalid filter Wn and crash on input below 48kHz.
146
  # Resample up front so that assumption always holds.
147
+ orig_sr = info.samplerate
148
+ original_channels = info.channels
149
  if orig_sr < 48000:
150
  y, _ = librosa.load(input_audio_path, sr=None, mono=False)
151
  y = librosa.resample(y, orig_sr=orig_sr, target_sr=48000)
 
153
  sf.write(resampled_path, y.T if y.ndim > 1 else y, samplerate=48000)
154
  input_audio_path = resampled_path
155
 
156
+ output = upsample(input_audio_path, int(ddim_steps), float(guidance_scale), seed_val)
 
 
 
 
 
 
 
 
157
 
158
  if multiband_ensemble:
159
  crossover_freq = int(input_cutoff) - 1000
 
180
  sf.write(output_path, output, samplerate=48000)
181
  return output_path
182
 
183
+
184
+ @spaces.GPU(duration=gpu_seconds)
185
+ @torch.inference_mode()
186
+ def upsample(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: int):
187
+ waveform = super_resolution_long_audio(
188
+ model,
189
+ input_audio_path,
190
+ seed=seed,
191
+ guidance_scale=guidance_scale,
192
+ ddim_steps=ddim_steps,
193
+ )
194
+ return waveform.cpu().numpy()
195
+
196
  if __name__ == "__main__":
197
  with gr.Blocks() as demo:
198
  input_components = [
requirements.txt CHANGED
@@ -1,6 +1,9 @@
1
  git+https://github.com/TEAMuP-dev/pyharp.git@v0.4.2
2
- git+https://github.com/haoheliu/versatile_audio_super_resolution.git
 
 
 
 
3
  librosa==0.9.2
4
  soundfile
5
- torchcodec
6
  spaces>=0.51.1
 
1
  git+https://github.com/TEAMuP-dev/pyharp.git@v0.4.2
2
+ torch==2.8.0
3
+ torchaudio==2.8.0
4
+ torchvision==0.23.0
5
+ # audiosr 0.0.7, at the commit the Space was built from
6
+ git+https://github.com/haoheliu/versatile_audio_super_resolution.git@d312fbab9f0e94087d9f2802d03cf184353cc805
7
  librosa==0.9.2
8
  soundfile
 
9
  spaces>=0.51.1