ambujm22 commited on
Commit
c6d1d61
Β·
verified Β·
1 Parent(s): 5bce909

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -21
app.py CHANGED
@@ -2,7 +2,7 @@
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
5
- # Optional: 'spaces' is present on Spaces; harmless to try locally.
6
  try:
7
  import spaces
8
  except Exception:
@@ -25,14 +25,14 @@ def gpu_echo(x: str = "ok"):
25
  import sys
26
  import subprocess
27
  from pathlib import Path
28
- from typing import Tuple, Optional, List
29
 
30
  import gradio as gr
31
  import numpy as np
32
  import soundfile as sf
33
  from huggingface_hub import hf_hub_download
34
 
35
- # ZeroGPU runtime hint (still safe on CPU)
36
  USE_ZEROGPU = os.getenv("SPACE_RUNTIME", "").lower() == "zerogpu"
37
 
38
  SPACE_ROOT = Path(__file__).parent.resolve()
@@ -140,7 +140,7 @@ def run_sonicmaster_cli(
140
  return False, last_err or "All candidate commands failed."
141
 
142
  # ========== GPU path (called only if ZeroGPU/GPU available) ==========
143
- @spaces.GPU(duration=180)
144
  def enhance_on_gpu(input_path: str, prompt: str, output_path: str) -> Tuple[bool, str]:
145
  try:
146
  import torch # noqa: F401
@@ -156,6 +156,56 @@ def _has_cuda() -> bool:
156
  except Exception:
157
  return False
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  # ========== Gradio callback ==========
160
  def enhance_audio_ui(
161
  audio_path: str,
@@ -202,33 +252,59 @@ def enhance_audio_ui(
202
  return None, f"Unexpected error: {e}\n{traceback.format_exc()}"
203
 
204
  # ========== Gradio UI ==========
205
- PROMPT_EXAMPLES = [
206
- ["Increase the clarity of this song by emphasizing treble frequencies."],
207
- ["Make this song sound more boomy by amplifying the low end bass frequencies."],
208
- ["Make the audio smoother and less distorted."],
209
- ["Improve the balance in this song."],
210
- ["Reduce roominess/echo (dereverb)."],
211
- ["Raise the level of the vocals."],
212
- ["Give the song a wider stereo image."],
213
- ]
214
-
215
  with gr.Blocks(title="SonicMaster – Text-Guided Restoration & Mastering", fill_height=True) as _demo:
216
  gr.Markdown(
217
  "## 🎧 SonicMaster\n"
218
- "Upload or choose an example prompt, write your own instruction, then click **Enhance**.\n"
219
- "- First run downloads model weights & repo (progress will show).\n"
220
- "- On failure, the **Status** box shows the exact error (we won't echo the input audio)."
221
  )
222
  with gr.Row():
223
- with gr.Column():
 
 
 
 
 
 
224
  in_audio = gr.Audio(label="Input Audio", type="filepath")
225
  prompt = gr.Textbox(label="Text Prompt", placeholder="e.g., Reduce reverb and brighten vocals.")
226
  run_btn = gr.Button("πŸš€ Enhance", variant="primary")
227
- gr.Examples(examples=PROMPT_EXAMPLES, inputs=[prompt], label="Prompt Examples")
228
- with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
229
  out_audio = gr.Audio(label="Enhanced Audio (output)")
230
  status = gr.Textbox(label="Status / Messages", interactive=False, lines=8)
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  run_btn.click(
233
  fn=enhance_audio_ui,
234
  inputs=[in_audio, prompt],
@@ -243,4 +319,4 @@ app = demo
243
 
244
  # Local debugging only
245
  if __name__ == "__main__":
246
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
5
+ # Optional: 'spaces' present on Spaces; harmless to try locally.
6
  try:
7
  import spaces
8
  except Exception:
 
25
  import sys
26
  import subprocess
27
  from pathlib import Path
28
+ from typing import Tuple, Optional, List, Dict, Any
29
 
30
  import gradio as gr
31
  import numpy as np
32
  import soundfile as sf
33
  from huggingface_hub import hf_hub_download
34
 
35
+ # ZeroGPU runtime hint (safe on CPU)
36
  USE_ZEROGPU = os.getenv("SPACE_RUNTIME", "").lower() == "zerogpu"
37
 
38
  SPACE_ROOT = Path(__file__).parent.resolve()
 
140
  return False, last_err or "All candidate commands failed."
141
 
142
  # ========== GPU path (called only if ZeroGPU/GPU available) ==========
143
+ @spaces.GPU(duration=60)
144
  def enhance_on_gpu(input_path: str, prompt: str, output_path: str) -> Tuple[bool, str]:
145
  try:
146
  import torch # noqa: F401
 
156
  except Exception:
157
  return False
158
 
159
+ # ========== Examples (lazy) ==========
160
+ PROMPTS_10 = [
161
+ "Increase the clarity of this song by emphasizing treble frequencies.",
162
+ "Make this song sound more boomy by amplifying the low end bass frequencies.",
163
+ "Can you make this sound louder, please?",
164
+ "Make the audio smoother and less distorted.",
165
+ "Improve the balance in this song.",
166
+ "Disentangle the left and right channels to give this song a stereo feeling.",
167
+ "Correct the unnatural frequency emphasis. Reduce the roominess or echo.",
168
+ "Raise the level of the vocals, please.",
169
+ "Increase the clarity of this song by emphasizing treble frequencies.",
170
+ "Please, dereverb this audio.",
171
+ ]
172
+
173
+ def list_example_files(progress: Optional[gr.Progress] = None) -> List[str]:
174
+ """Return up to 10 .wav paths inside repo/samples/inputs (lazy clone)."""
175
+ repo = ensure_repo(progress=progress)
176
+ wav_dir = repo / "samples" / "inputs"
177
+ files = sorted(p for p in wav_dir.glob("*.wav") if p.is_file())
178
+ return [p.as_posix() for p in files[:10]]
179
+
180
+ def load_examples(_: Any = None, progress=gr.Progress()) -> Dict[str, Any]:
181
+ """Button/auto-load handler: populate dropdown choices and status text."""
182
+ paths = list_example_files(progress=progress)
183
+ if not paths:
184
+ return {
185
+ "choices": [],
186
+ "status": "No sample .wav files found in repo/samples/inputs.",
187
+ }
188
+ labels = [f"{i+1:02d} β€” {Path(p).name}" for i, p in enumerate(paths)]
189
+ return {
190
+ "choices": labels,
191
+ "paths": paths,
192
+ "status": f"Loaded {len(paths)} sample audios."
193
+ }
194
+
195
+ def set_example_selection(idx_label: str, paths: List[str]) -> Tuple[str, str]:
196
+ """When user picks an example, set the audio path + a suggested prompt."""
197
+ if not idx_label or not paths:
198
+ return "", ""
199
+ try:
200
+ # label "01 β€” file.wav" -> index 0
201
+ idx = int(idx_label.split()[0]) - 1
202
+ except Exception:
203
+ idx = 0
204
+ idx = max(0, min(idx, len(paths)-1))
205
+ audio_path = paths[idx]
206
+ prompt = PROMPTS_10[idx] if idx < len(PROMPTS_10) else PROMPTS_10[-1]
207
+ return audio_path, prompt
208
+
209
  # ========== Gradio callback ==========
210
  def enhance_audio_ui(
211
  audio_path: str,
 
252
  return None, f"Unexpected error: {e}\n{traceback.format_exc()}"
253
 
254
  # ========== Gradio UI ==========
 
 
 
 
 
 
 
 
 
 
255
  with gr.Blocks(title="SonicMaster – Text-Guided Restoration & Mastering", fill_height=True) as _demo:
256
  gr.Markdown(
257
  "## 🎧 SonicMaster\n"
258
+ "Upload audio or **load sample audios**, write a prompt, then click **Enhance**.\n"
259
+ "- On failure, the **Status** box shows the exact error "
 
260
  )
261
  with gr.Row():
262
+ with gr.Column(scale=1):
263
+ # Sample loader (lazy)
264
+ with gr.Accordion("Sample audios (10)", open=False):
265
+ load_btn = gr.Button("πŸ“₯ Load 10 sample audios")
266
+ samples_dropdown = gr.Dropdown(choices=[], label="Pick a sample", interactive=True)
267
+ samples_state = gr.State([]) # holds absolute paths
268
+
269
  in_audio = gr.Audio(label="Input Audio", type="filepath")
270
  prompt = gr.Textbox(label="Text Prompt", placeholder="e.g., Reduce reverb and brighten vocals.")
271
  run_btn = gr.Button("πŸš€ Enhance", variant="primary")
272
+
273
+ # Optional quick prompt examples (text-only)
274
+ gr.Examples(
275
+ examples=[[p] for p in [
276
+ "Reduce roominess/echo (dereverb).",
277
+ "Raise the level of the vocals.",
278
+ "Give the song a wider stereo image.",
279
+ ]],
280
+ inputs=[prompt],
281
+ label="Prompt Examples",
282
+ )
283
+
284
+ with gr.Column(scale=1):
285
  out_audio = gr.Audio(label="Enhanced Audio (output)")
286
  status = gr.Textbox(label="Status / Messages", interactive=False, lines=8)
287
 
288
+ # --- Wire up the sample loader ---
289
+ # 1) Load samples on button click (lazy clone)
290
+ load_result = load_btn.click(
291
+ fn=load_examples,
292
+ inputs=None,
293
+ outputs=None
294
+ )
295
+ # Manually map the dict result to components via .then (Gradio v5 API)
296
+ load_result.then(lambda d: d.get("choices", []), None, samples_dropdown)
297
+ load_result.then(lambda d: d.get("paths", []), None, samples_state)
298
+ load_result.then(lambda d: d.get("status", ""), None, status)
299
+
300
+ # 2) When a sample is chosen, set audio path + suggested prompt
301
+ samples_dropdown.change(
302
+ fn=set_example_selection,
303
+ inputs=[samples_dropdown, samples_state],
304
+ outputs=[in_audio, prompt],
305
+ )
306
+
307
+ # --- Enhance button ---
308
  run_btn.click(
309
  fn=enhance_audio_ui,
310
  inputs=[in_audio, prompt],
 
319
 
320
  # Local debugging only
321
  if __name__ == "__main__":
322
+ demo.launch(server_name="0.0.0.0", server_port=7860)