harp-dev commited on
Commit
1eb50b8
·
verified ·
1 Parent(s): 25d08d8

Check inputs before reserving the GPU, so a request the model cannot take spends none of the caller's allowance.

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -102,9 +102,9 @@ if __name__ != "__main__":
102
  inference_pipeline.device = DEVICE
103
 
104
 
105
- @spaces.GPU(duration=60)
106
- @torch.inference_mode()
107
  def process_fn(vocal_filepath, n_timesteps): # add , cfg_scale to re-enable
 
 
108
  if model_loading:
109
  raise gr.Error("Model is still loading, please wait a moment and try again.")
110
  if inference_pipeline is None:
@@ -115,11 +115,17 @@ def process_fn(vocal_filepath, n_timesteps): # add , cfg_scale to re-enable
115
 
116
  try:
117
  duration = librosa.get_duration(path=vocal_filepath)
118
- if not (3 <= duration <= 30):
119
- raise gr.Error("Audio duration must be between 3 and 30 seconds.")
120
  except Exception as e:
121
- raise gr.Error(f"Cannot read audio file or get duration: {e}")
 
 
 
 
122
 
 
 
 
 
123
  try:
124
  # The session's format, read before the downmix and resample the model
125
  # needs, and restored on both outputs
 
102
  inference_pipeline.device = DEVICE
103
 
104
 
 
 
105
  def process_fn(vocal_filepath, n_timesteps): # add , cfg_scale to re-enable
106
+ # Checked before the GPU is reserved, so a request the model cannot take spends none of the
107
+ # caller's ZeroGPU allowance
108
  if model_loading:
109
  raise gr.Error("Model is still loading, please wait a moment and try again.")
110
  if inference_pipeline is None:
 
115
 
116
  try:
117
  duration = librosa.get_duration(path=vocal_filepath)
 
 
118
  except Exception as e:
119
+ raise gr.Error(f"Cannot read the audio file: {e}")
120
+ if not (3 <= duration <= 30):
121
+ raise gr.Error(f"Audio must be 3 to 30 seconds long, not {duration:.1f}.")
122
+
123
+ return accompany(vocal_filepath, n_timesteps)
124
 
125
+
126
+ @spaces.GPU(duration=60)
127
+ @torch.inference_mode()
128
+ def accompany(vocal_filepath, n_timesteps):
129
  try:
130
  # The session's format, read before the downmix and resample the model
131
  # needs, and restored on both outputs