Nick088 commited on
Commit
6e70b11
·
verified ·
1 Parent(s): de71e88

add dynamic zerogpu duration & catch no audio file errors

Browse files
Files changed (1) hide show
  1. app.py +53 -3
app.py CHANGED
@@ -3,10 +3,60 @@ import gradio as gr
3
  from audiosr import super_resolution, build_model
4
  import torch
5
  import gc # free up memory
 
 
6
 
7
 
8
- @spaces.GPU(duration=120)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def inference(audio_file, model_name, guidance_scale, ddim_steps, seed):
 
 
 
 
 
 
10
  audiosr = build_model(model_name=model_name)
11
 
12
  if torch.cuda.is_available():
@@ -45,7 +95,7 @@ iface = gr.Interface(
45
  ],
46
  outputs=gr.Audio(type="numpy", label="Output Audio"),
47
  title="AudioSR",
48
- description="Audio Super Resolution with AudioSR"
49
  )
50
 
51
- iface.launch(share=False)
 
3
  from audiosr import super_resolution, build_model
4
  import torch
5
  import gc # free up memory
6
+ import soundfile as sf # read audio
7
+ import math # For dynamic gpu duration calculation
8
 
9
 
10
+ # Estimate a dynamic gpu duration done by a private Benchmarking HuggingFace ZeroGPU (H200) Space on the 16th November 2025 for saving quota
11
+ def get_duration(audio_file, model_name, guidance_scale, ddim_steps, seed):
12
+ if not audio_file:
13
+ return 0
14
+
15
+ try:
16
+ info = sf.info(audio_file)
17
+ audio_duration = info.duration
18
+
19
+
20
+ # 1. Base overhead for model loading (using the higher 'speech' model value).
21
+ base_overhead = 24 # seconds
22
+
23
+ # 2. Multipliers for the core ML task.
24
+ # From benchmark: ~11s for 8s audio @ 50 steps.
25
+ # Formula: (8s * C1) + (50 steps * C2) = 11s.
26
+ # We'll estimate C1=1.0 and C2=0.06.
27
+ time_per_audio_second = 1.0
28
+ time_per_ddim_step = 0.06
29
+
30
+ # 3. Calculate the estimated processing time.
31
+ estimated_time = base_overhead + (audio_duration * time_per_audio_second) + (ddim_steps * time_per_ddim_step)
32
+
33
+ # 4. Add a safety buffer to prevent unexpected timeouts.
34
+ safety_buffer = 10
35
+
36
+ calculated_duration = estimated_time + safety_buffer
37
+
38
+ # 5. Apply min/max constraints.
39
+ min_duration = 50 # Must be enough for model load + buffer
40
+ max_duration = 180 # Current ZeroGPU maximum duration
41
+
42
+ final_duration = max(min_duration, min(max_duration, calculated_duration))
43
+ print("FINAL DURATION", final_duration)
44
+ return math.ceil(final_duration)
45
+
46
+ except Exception as e:
47
+ # Fallback to a safe default duration if reading the audio fails.
48
+ print(f"Error in get_duration, using fallback (60): {e}")
49
+ return 60
50
+
51
+
52
+ @spaces.GPU(duration=get_duration)
53
  def inference(audio_file, model_name, guidance_scale, ddim_steps, seed):
54
+
55
+ if not audio_file:
56
+ print("No audio file provided, skipping inference.")
57
+ raise gr.Error(
58
+ "Please upload an audio file."
59
+ )
60
  audiosr = build_model(model_name=model_name)
61
 
62
  if torch.cuda.is_available():
 
95
  ],
96
  outputs=gr.Audio(type="numpy", label="Output Audio"),
97
  title="AudioSR",
98
+ description="Audio Super Resolution with AudioSR. <br> It estimates a dynamic gpu duration done by a private Benchmarking HuggingFace ZeroGPU (H200) Space on the 16th November 2025 for saving quota."
99
  )
100
 
101
+ iface.launch(share=False)