ambujm22 commited on
Commit
6a3576c
·
verified ·
1 Parent(s): 1432ed4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  os.environ.setdefault("GRADIO_USE_CDN", "true")
3
 
@@ -11,10 +12,16 @@ import numpy as np
11
  import soundfile as sf
12
  from huggingface_hub import hf_hub_download
13
 
14
- # Detect ZeroGPU
 
 
 
 
 
 
 
 
15
  USE_ZEROGPU = os.getenv("SPACE_RUNTIME", "").lower() == "zerogpu"
16
- if USE_ZEROGPU:
17
- import spaces
18
 
19
  SPACE_ROOT = Path(__file__).parent.resolve()
20
  REPO_DIR = SPACE_ROOT / "SonicMasterRepo"
@@ -23,7 +30,6 @@ WEIGHTS_FILE = "model.safetensors"
23
  CACHE_DIR = SPACE_ROOT / "weights"
24
  CACHE_DIR.mkdir(parents=True, exist_ok=True)
25
 
26
-
27
  # ---------- 1) Pull weights from HF Hub ----------
28
  def get_weights_path() -> Path:
29
  return Path(
@@ -37,7 +43,6 @@ def get_weights_path() -> Path:
37
  )
38
  )
39
 
40
-
41
  # ---------- 2) Clone GitHub repo ----------
42
  def ensure_repo() -> Path:
43
  if not REPO_DIR.exists():
@@ -51,7 +56,6 @@ def ensure_repo() -> Path:
51
  sys.path.append(REPO_DIR.as_posix())
52
  return REPO_DIR
53
 
54
-
55
  # ---------- 3) Examples ----------
56
  def build_examples():
57
  repo = ensure_repo()
@@ -72,7 +76,6 @@ def build_examples():
72
  return [[p.as_posix(), prompts[i] if i < len(prompts) else prompts[-1]]
73
  for i, p in enumerate(wav_paths[:10])]
74
 
75
-
76
  # ---------- 4) I/O helpers ----------
77
  def save_temp_wav(wav: np.ndarray, sr: int, path: Path):
78
  if wav.ndim == 2 and wav.shape[0] < wav.shape[1]:
@@ -83,7 +86,6 @@ def read_audio(path: str) -> Tuple[np.ndarray, int]:
83
  wav, sr = sf.read(path, always_2d=False)
84
  return wav.astype(np.float32) if wav.dtype == np.float64 else wav, sr
85
 
86
-
87
  # ---------- 5) Core inference ----------
88
  def run_sonicmaster_cli(input_wav_path: Path,
89
  prompt: str,
@@ -127,15 +129,16 @@ def run_sonicmaster_cli(input_wav_path: Path,
127
  continue
128
  return False
129
 
130
-
131
- # ---------- 6) ZeroGPU wrapper ----------
132
- if USE_ZEROGPU:
133
- @spaces.GPU(duration=180)
134
- def enhance_on_gpu(input_path: str, prompt: str, output_path: str) -> bool:
135
- import torch # ensures CUDA initializes inside GPU context
136
- return run_sonicmaster_cli(Path(input_path), prompt, Path(output_path),
137
- _logs=[], progress=None)
138
-
 
139
 
140
  # ---------- 7) Gradio callback ----------
141
  def enhance_audio_ui(audio_path: str,
@@ -151,6 +154,8 @@ def enhance_audio_ui(audio_path: str,
151
  except: pass
152
  save_temp_wav(wav, sr, tmp_in)
153
 
 
 
154
  if USE_ZEROGPU:
155
  ok = enhance_on_gpu(tmp_in.as_posix(), prompt, tmp_out.as_posix())
156
  else:
@@ -162,7 +167,6 @@ def enhance_audio_ui(audio_path: str,
162
  else:
163
  return (sr, wav)
164
 
165
-
166
  # ---------- 8) Gradio UI ----------
167
  with gr.Blocks(title="SonicMaster – Text-Guided Restoration & Mastering", fill_height=True) as demo:
168
  gr.Markdown("## 🎧 SonicMaster\nUpload or choose an example, write a text prompt, then click **Enhance**.")
@@ -179,7 +183,6 @@ with gr.Blocks(title="SonicMaster – Text-Guided Restoration & Mastering", fill
179
  outputs=[out_audio],
180
  concurrency_limit=1)
181
 
182
-
183
  # ---------- 9) FastAPI mount ----------
184
  from fastapi import FastAPI, Request
185
  from starlette.responses import PlainTextResponse
 
1
+ # ---------- Gradio CDN fix ----------
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
 
12
  import soundfile as sf
13
  from huggingface_hub import hf_hub_download
14
 
15
+ # >>> NEW: import spaces unconditionally and define a probe so ZeroGPU sees a @spaces.GPU fn at import time
16
+ import spaces
17
+
18
+ @spaces.GPU(duration=30)
19
+ def _gpu_probe() -> str:
20
+ # Dummy function to satisfy ZeroGPU startup check. We never call it.
21
+ return "ok"
22
+
23
+ # Detect ZeroGPU (used only to decide whether to call GPU code)
24
  USE_ZEROGPU = os.getenv("SPACE_RUNTIME", "").lower() == "zerogpu"
 
 
25
 
26
  SPACE_ROOT = Path(__file__).parent.resolve()
27
  REPO_DIR = SPACE_ROOT / "SonicMasterRepo"
 
30
  CACHE_DIR = SPACE_ROOT / "weights"
31
  CACHE_DIR.mkdir(parents=True, exist_ok=True)
32
 
 
33
  # ---------- 1) Pull weights from HF Hub ----------
34
  def get_weights_path() -> Path:
35
  return Path(
 
43
  )
44
  )
45
 
 
46
  # ---------- 2) Clone GitHub repo ----------
47
  def ensure_repo() -> Path:
48
  if not REPO_DIR.exists():
 
56
  sys.path.append(REPO_DIR.as_posix())
57
  return REPO_DIR
58
 
 
59
  # ---------- 3) Examples ----------
60
  def build_examples():
61
  repo = ensure_repo()
 
76
  return [[p.as_posix(), prompts[i] if i < len(prompts) else prompts[-1]]
77
  for i, p in enumerate(wav_paths[:10])]
78
 
 
79
  # ---------- 4) I/O helpers ----------
80
  def save_temp_wav(wav: np.ndarray, sr: int, path: Path):
81
  if wav.ndim == 2 and wav.shape[0] < wav.shape[1]:
 
86
  wav, sr = sf.read(path, always_2d=False)
87
  return wav.astype(np.float32) if wav.dtype == np.float64 else wav, sr
88
 
 
89
  # ---------- 5) Core inference ----------
90
  def run_sonicmaster_cli(input_wav_path: Path,
91
  prompt: str,
 
129
  continue
130
  return False
131
 
132
+ # ---------- 6) ZeroGPU real inference wrapper (ALWAYS defined) ----------
133
+ @spaces.GPU(duration=180)
134
+ def enhance_on_gpu(input_path: str, prompt: str, output_path: str) -> bool:
135
+ # Ensure CUDA initializes inside GPU context
136
+ try:
137
+ import torch # noqa: F401
138
+ except Exception:
139
+ pass
140
+ from pathlib import Path as _P
141
+ return run_sonicmaster_cli(_P(input_path), prompt, _P(output_path), _logs=[], progress=None)
142
 
143
  # ---------- 7) Gradio callback ----------
144
  def enhance_audio_ui(audio_path: str,
 
154
  except: pass
155
  save_temp_wav(wav, sr, tmp_in)
156
 
157
+ # Use GPU only when ZeroGPU is active; otherwise CPU fallback.
158
+ if progress: progress(0.3, desc="Starting inference")
159
  if USE_ZEROGPU:
160
  ok = enhance_on_gpu(tmp_in.as_posix(), prompt, tmp_out.as_posix())
161
  else:
 
167
  else:
168
  return (sr, wav)
169
 
 
170
  # ---------- 8) Gradio UI ----------
171
  with gr.Blocks(title="SonicMaster – Text-Guided Restoration & Mastering", fill_height=True) as demo:
172
  gr.Markdown("## 🎧 SonicMaster\nUpload or choose an example, write a text prompt, then click **Enhance**.")
 
183
  outputs=[out_audio],
184
  concurrency_limit=1)
185
 
 
186
  # ---------- 9) FastAPI mount ----------
187
  from fastapi import FastAPI, Request
188
  from starlette.responses import PlainTextResponse