harp-dev commited on
Commit
17c5062
·
verified ·
1 Parent(s): d667d48

Split the GPU work from process_fn, and import spaces first

Browse files

process_fn carried the GPU decorator itself, so nothing was checked before a GPU would be reserved on ZeroGPU. It now checks the inputs and hands the model work to a decorated function, so the Space runs unchanged on any hardware. spaces is now imported before torch and PyHARP, as ZeroGPU requires.

Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import tempfile
3
  from pathlib import Path
@@ -11,7 +13,6 @@ import torch
11
  from pyharp import ModelCard, build_endpoint, SampleRate, Channels
12
  from pyharp.tags import Subcategory
13
  from df import enhance, init_df
14
- import spaces
15
 
16
  # -----------------------------
17
  # Model metadata for HARP
@@ -84,12 +85,22 @@ def apply_attenuation_db(noisy: np.ndarray, enhanced: np.ndarray, noise_atten_db
84
  return out.astype(np.float32)
85
 
86
 
87
- @torch.inference_mode()
88
- @spaces.GPU(duration=120)
89
  def process_fn(input_audio_path: str, noise_atten_db: float) -> str:
 
 
90
  if not input_audio_path:
91
- raise ValueError("No input audio provided.")
 
 
 
 
92
 
 
 
 
 
 
 
93
  # The session's format, read before the downmix and resample that
94
  # DeepFilterNet needs, and restored before saving
95
  info = sf.info(input_audio_path)
 
1
+ import spaces # imported first, as ZeroGPU requires
2
+
3
  import os
4
  import tempfile
5
  from pathlib import Path
 
13
  from pyharp import ModelCard, build_endpoint, SampleRate, Channels
14
  from pyharp.tags import Subcategory
15
  from df import enhance, init_df
 
16
 
17
  # -----------------------------
18
  # Model metadata for HARP
 
85
  return out.astype(np.float32)
86
 
87
 
 
 
88
  def process_fn(input_audio_path: str, noise_atten_db: float) -> str:
89
+ # Checked before the GPU is reserved, so a request the model cannot take spends none of the
90
+ # caller's ZeroGPU allowance
91
  if not input_audio_path:
92
+ raise gr.Error("Provide the speech to denoise.")
93
+ try:
94
+ sf.info(input_audio_path)
95
+ except Exception as exc:
96
+ raise gr.Error(f"Could not read the audio file: {exc}") from exc
97
 
98
+ return denoise(input_audio_path, noise_atten_db)
99
+
100
+
101
+ @spaces.GPU(duration=120)
102
+ @torch.inference_mode()
103
+ def denoise(input_audio_path: str, noise_atten_db: float) -> str:
104
  # The session's format, read before the downmix and resample that
105
  # DeepFilterNet needs, and restored before saving
106
  info = sf.info(input_audio_path)