harp-dev commited on
Commit
cf22e8a
·
verified ·
1 Parent(s): f21713e

Check inputs before reserving the GPU, and size the reservation by the recording's length and the steps.

Browse files
Files changed (1) hide show
  1. app.py +22 -1
app.py CHANGED
@@ -11,6 +11,7 @@ import tempfile
11
  from pathlib import Path
12
 
13
  import gradio as gr
 
14
  import spaces
15
  from pyharp import (Channels, ModelCard, SampleRate, Subcategory,
16
  build_endpoint)
@@ -43,7 +44,6 @@ model_card = ModelCard(
43
  )
44
 
45
 
46
- @spaces.GPU(duration=240)
47
  def process_fn(input_audio_path, prompt, num_inference_steps, guidance_scale,
48
  solver, seed):
49
  """
@@ -61,9 +61,30 @@ def process_fn(input_audio_path, prompt, num_inference_steps, guidance_scale,
61
  output_path (str): The edited audio.
62
  """
63
 
 
 
64
  if not (prompt or "").strip():
65
  raise gr.Error("Describe what to change about the audio.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
 
67
  output_path = Path(tempfile.mkdtemp()) / "sonicmaster.wav"
68
 
69
  return sonicmaster_runtime.enhance(
 
11
  from pathlib import Path
12
 
13
  import gradio as gr
14
+ import soundfile as sf
15
  import spaces
16
  from pyharp import (Channels, ModelCard, SampleRate, Subcategory,
17
  build_endpoint)
 
44
  )
45
 
46
 
 
47
  def process_fn(input_audio_path, prompt, num_inference_steps, guidance_scale,
48
  solver, seed):
49
  """
 
61
  output_path (str): The edited audio.
62
  """
63
 
64
+ # Checked before the GPU is reserved, so a request the model cannot take spends none of the
65
+ # caller's ZeroGPU allowance
66
  if not (prompt or "").strip():
67
  raise gr.Error("Describe what to change about the audio.")
68
+ if gpu_seconds(input_audio_path, prompt, num_inference_steps) > MAX_GPU_SECONDS:
69
+ raise gr.Error(
70
+ "This recording is too long to treat in one request at this many solver steps. Trim "
71
+ "it, or use fewer steps."
72
+ )
73
+
74
+ return treat(input_audio_path, prompt, num_inference_steps, guidance_scale, solver, seed)
75
+
76
+
77
+ # The most a request may reserve: 1.5 times this is a free account's whole daily allowance
78
+ MAX_GPU_SECONDS = 200
79
+
80
+
81
+ def gpu_seconds(input_audio_path, prompt, num_inference_steps, *_):
82
+ # The work is per chunk and per step. Measured at about 6 s for 30 s at 10 steps, padded
83
+ return 15 + 0.02 * sf.info(input_audio_path).duration * int(num_inference_steps)
84
+
85
 
86
+ @spaces.GPU(duration=gpu_seconds)
87
+ def treat(input_audio_path, prompt, num_inference_steps, guidance_scale, solver, seed):
88
  output_path = Path(tempfile.mkdtemp()) / "sonicmaster.wav"
89
 
90
  return sonicmaster_runtime.enhance(