cazyundee commited on
Commit
2781f74
·
verified ·
1 Parent(s): 242c5fa

Add SFX model support with model selector

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -14,7 +14,6 @@ if hf_token:
14
 
15
 
16
  # Required for ZeroGPU Spaces - must have at least one @spaces.GPU function
17
- # This is a minimal startup check that uses negligible quota
18
  @spaces.GPU(duration=1)
19
  def _gpu_startup_check():
20
  return "GPU check passed"
@@ -24,18 +23,29 @@ def _gpu_startup_check():
24
  _gpu_startup_check()
25
 
26
 
27
- # Load model once at startup (on CPU)
28
- print("Loading Stable Audio 3 Small model...")
29
- model = StableAudioModel.from_pretrained(
30
- "small-music",
31
- device="cpu"
32
- )
33
- print("Model loaded successfully!")
34
 
35
 
36
  @spaces.GPU(duration=1)
37
- def generate_audio(prompt, duration, steps, cfg_scale, seed):
38
- print(f"Generating: prompt='{prompt}', duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  audio = model.generate(
41
  prompt=prompt,
@@ -57,14 +67,19 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed):
57
 
58
 
59
  with gr.Blocks(title="Stable Audio 3 Small") as demo:
60
- gr.Markdown("# 🎵 Stable Audio 3 Small - Music Generation")
61
- gr.Markdown("Generate music using Stability AI's Stable Audio 3 Small model. Runs on CPU.")
62
 
63
  with gr.Row():
64
  with gr.Column():
 
 
 
 
 
65
  prompt = gr.Textbox(
66
  label="Prompt",
67
- placeholder="Describe the music you want to generate...",
68
  lines=2
69
  )
70
  duration = gr.Slider(
@@ -92,7 +107,7 @@ with gr.Blocks(title="Stable Audio 3 Small") as demo:
92
 
93
  btn.click(
94
  fn=generate_audio,
95
- inputs=[prompt, duration, steps, cfg_scale, seed],
96
  outputs=audio_output
97
  )
98
 
 
14
 
15
 
16
  # Required for ZeroGPU Spaces - must have at least one @spaces.GPU function
 
17
  @spaces.GPU(duration=1)
18
  def _gpu_startup_check():
19
  return "GPU check passed"
 
23
  _gpu_startup_check()
24
 
25
 
26
+ # Model cache
27
+ MODEL_CACHE = {}
 
 
 
 
 
28
 
29
 
30
  @spaces.GPU(duration=1)
31
+ def load_model(model_name):
32
+ """Load model on demand and cache it."""
33
+ if model_name not in MODEL_CACHE:
34
+ print(f"Loading {model_name} model...")
35
+ model = StableAudioModel.from_pretrained(
36
+ model_name,
37
+ device="cpu"
38
+ )
39
+ MODEL_CACHE[model_name] = model
40
+ print(f"{model_name} loaded successfully!")
41
+ return MODEL_CACHE[model_name]
42
+
43
+
44
+ @spaces.GPU(duration=1)
45
+ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
46
+ print(f"Generating with {model_name}: prompt='{prompt}', duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")
47
+
48
+ model = load_model(model_name)
49
 
50
  audio = model.generate(
51
  prompt=prompt,
 
67
 
68
 
69
  with gr.Blocks(title="Stable Audio 3 Small") as demo:
70
+ gr.Markdown("# 🎵 Stable Audio 3 Small - Music & SFX Generation")
71
+ gr.Markdown("Generate music and sound effects using Stability AI's Stable Audio 3 Small models. Runs on CPU.")
72
 
73
  with gr.Row():
74
  with gr.Column():
75
+ model_name = gr.Dropdown(
76
+ choices=["stabilityai/stable-audio-3-small-music", "stabilityai/stable-audio-3-small-sfx"],
77
+ value="stabilityai/stable-audio-3-small-music",
78
+ label="Model"
79
+ )
80
  prompt = gr.Textbox(
81
  label="Prompt",
82
+ placeholder="Describe the music or sound effect you want to generate...",
83
  lines=2
84
  )
85
  duration = gr.Slider(
 
107
 
108
  btn.click(
109
  fn=generate_audio,
110
+ inputs=[prompt, duration, steps, cfg_scale, seed, model_name],
111
  outputs=audio_output
112
  )
113