rianders commited on
Commit
b488939
·
verified ·
1 Parent(s): 2833121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import os
 
 
2
 
3
  import gradio as gr
4
  import numpy as np
@@ -9,35 +11,38 @@ from huggingface_hub import login
9
  from stable_audio_3 import StableAudioModel
10
 
11
 
12
- hf_token = os.environ.get("HF_TOKEN")
13
 
14
- if not hf_token:
15
  raise RuntimeError("HF_TOKEN Space secret is missing.")
16
 
17
- login(token=hf_token)
18
 
 
 
 
19
  print("PyTorch:", torch.__version__)
20
- print("PyTorch CUDA:", torch.version.cuda)
21
- print("Compiled architectures:", torch.cuda.get_arch_list())
22
 
 
 
23
  model = StableAudioModel.from_pretrained(
24
  "small-sfx",
25
  device="cuda",
26
  )
27
 
28
- def requested_gpu_duration(prompt: str, duration: float) -> int:
29
- """
30
- Reserve a modest amount of GPU time.
31
 
32
- Stable Audio 3 Small models are extremely fast on modern GPUs,
33
- but leave room for initial setup and audio decoding.
34
- """
35
  del prompt
36
 
37
  duration = float(duration)
38
 
39
  if duration <= 15:
40
  return 15
 
41
  if duration <= 60:
42
  return 20
43
 
@@ -66,7 +71,7 @@ def generate_sound(prompt: str, duration: float) -> str:
66
 
67
  audio = np.asarray(audio)
68
 
69
- # Remove batch dimension.
70
  if audio.ndim == 3:
71
  audio = audio[0]
72
 
@@ -74,6 +79,12 @@ def generate_sound(prompt: str, duration: float) -> str:
74
  if audio.ndim == 2 and audio.shape[0] <= 8:
75
  audio = audio.T
76
 
 
 
 
 
 
 
77
  with tempfile.NamedTemporaryFile(
78
  suffix=".wav",
79
  delete=False,
@@ -81,7 +92,7 @@ def generate_sound(prompt: str, duration: float) -> str:
81
  output_path = Path(output_file.name)
82
 
83
  sf.write(
84
- output_path,
85
  audio,
86
  samplerate=44_100,
87
  subtype="FLOAT",
 
1
  import os
2
+ import tempfile
3
+ from pathlib import Path
4
 
5
  import gradio as gr
6
  import numpy as np
 
11
  from stable_audio_3 import StableAudioModel
12
 
13
 
14
+ HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
16
+ if not HF_TOKEN:
17
  raise RuntimeError("HF_TOKEN Space secret is missing.")
18
 
19
+ login(token=HF_TOKEN)
20
 
21
+ # Safe diagnostics only. Do not call torch.cuda.get_arch_list(),
22
+ # torch.cuda.get_device_name(), or similar CUDA inspection functions
23
+ # during ZeroGPU startup.
24
  print("PyTorch:", torch.__version__)
25
+ print("PyTorch CUDA build:", torch.version.cuda)
26
+ print("Loading Stable Audio 3 Small SFX...")
27
 
28
+ # ZeroGPU emulates CUDA placement during startup and later transfers the
29
+ # packed tensors into the temporary GPU worker.
30
  model = StableAudioModel.from_pretrained(
31
  "small-sfx",
32
  device="cuda",
33
  )
34
 
35
+ print("Model loaded.")
36
+
 
37
 
38
+ def requested_gpu_duration(prompt: str, duration: float) -> int:
 
 
39
  del prompt
40
 
41
  duration = float(duration)
42
 
43
  if duration <= 15:
44
  return 15
45
+
46
  if duration <= 60:
47
  return 20
48
 
 
71
 
72
  audio = np.asarray(audio)
73
 
74
+ # Remove a batch dimension: [batch, channels, samples].
75
  if audio.ndim == 3:
76
  audio = audio[0]
77
 
 
79
  if audio.ndim == 2 and audio.shape[0] <= 8:
80
  audio = audio.T
81
 
82
+ # Avoid clipping if the model produces samples outside [-1, 1].
83
+ peak = float(np.max(np.abs(audio))) if audio.size else 0.0
84
+
85
+ if peak > 1.0:
86
+ audio = audio / peak
87
+
88
  with tempfile.NamedTemporaryFile(
89
  suffix=".wav",
90
  delete=False,
 
92
  output_path = Path(output_file.name)
93
 
94
  sf.write(
95
+ str(output_path),
96
  audio,
97
  samplerate=44_100,
98
  subtype="FLOAT",