harp-dev commited on
Commit
c18ea46
·
verified ·
1 Parent(s): c5552cb

Size the GPU reservation by the mixture's length and check requests before the GPU is reserved.

Browse files
Files changed (1) hide show
  1. app.py +28 -1
app.py CHANGED
@@ -40,6 +40,7 @@ import contextlib
40
  import tempfile
41
  from pathlib import Path
42
 
 
43
  import torch
44
  import torchaudio
45
  from huggingface_hub import snapshot_download
@@ -93,8 +94,34 @@ model_card = ModelCard(
93
  )
94
 
95
 
96
- @spaces.GPU
 
 
 
 
 
 
 
 
97
  def process_fn(input_audio, model_name, niter, wiener_win_len):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  audio, rate = torchaudio.load(input_audio)
99
 
100
  # Open-Unmix separates at 44.1 kHz, so the session's rate is kept here and
 
40
  import tempfile
41
  from pathlib import Path
42
 
43
+ import soundfile as sf
44
  import torch
45
  import torchaudio
46
  from huggingface_hub import snapshot_download
 
94
  )
95
 
96
 
97
+ # The whole mixture is separated, so the cost grows with its length
98
+ MAX_INPUT_SECONDS = 600
99
+
100
+
101
+ def gpu_seconds(input_audio, model_name, niter, wiener_win_len):
102
+ # Measured at about 10 s for a minute of audio, building the separator included, padded
103
+ return 15 + 0.25 * sf.info(input_audio).duration
104
+
105
+
106
  def process_fn(input_audio, model_name, niter, wiener_win_len):
107
+ # Checked before the GPU is reserved, so a request the model cannot take spends none of the
108
+ # caller's ZeroGPU allowance
109
+ if not input_audio:
110
+ raise gr.Error("Provide the mixture to separate.")
111
+ try:
112
+ duration = sf.info(input_audio).duration
113
+ except Exception as exc:
114
+ raise gr.Error(f"Could not read the audio file: {exc}") from exc
115
+ if duration > MAX_INPUT_SECONDS:
116
+ raise gr.Error(
117
+ f"The mixture is {duration:.1f} s long. Trim it to {MAX_INPUT_SECONDS // 60} minutes "
118
+ "or less."
119
+ )
120
+ return separate(input_audio, model_name, niter, wiener_win_len)
121
+
122
+
123
+ @spaces.GPU(duration=gpu_seconds)
124
+ def separate(input_audio, model_name, niter, wiener_win_len):
125
  audio, rate = torchaudio.load(input_audio)
126
 
127
  # Open-Unmix separates at 44.1 kHz, so the session's rate is kept here and