Spaces:
Running on Zero
Running on Zero
Vansh Chugh commited on
Commit ·
88ae4dd
1
Parent(s): 6d3be73
cleanup
Browse files
app.py
CHANGED
|
@@ -46,9 +46,8 @@ models_on_device = False
|
|
| 46 |
model_loading = True
|
| 47 |
model_error: str | None = None
|
| 48 |
|
| 49 |
-
|
| 50 |
def build_params(encodec_weights: str, lm_weights: str) -> hp.MusicgenParams:
|
| 51 |
-
"""Assemble MusicgenParams with explicit weight paths."""
|
| 52 |
return hp.MusicgenParams(
|
| 53 |
encodec_params=hp.EncodecParams(
|
| 54 |
sample_rate=32_000,
|
|
@@ -74,9 +73,7 @@ def build_params(encodec_weights: str, lm_weights: str) -> hp.MusicgenParams:
|
|
| 74 |
|
| 75 |
|
| 76 |
def load_checkpoint(params: hp.MusicgenParams, ckp_path: str) -> LightningMusicgen:
|
| 77 |
-
|
| 78 |
-
model = LightningMusicgen(params) # direct instantiation bcs params.instantiate() uses
|
| 79 |
-
# pydoc.locate("stage.*") which fails after flattening
|
| 80 |
sft.load_model(model, ckp_path)
|
| 81 |
return model.cpu().eval()
|
| 82 |
|
|
@@ -128,7 +125,6 @@ def process_fn(
|
|
| 128 |
gen_seconds: int,
|
| 129 |
description: str,
|
| 130 |
) -> str:
|
| 131 |
-
"""Load context audio, run STAGE autoregressive generation, return the generated stem."""
|
| 132 |
global drums_model, bass_model, models_on_device
|
| 133 |
|
| 134 |
if model_loading:
|
|
@@ -136,16 +132,13 @@ def process_fn(
|
|
| 136 |
if model_error:
|
| 137 |
raise gr.Error(f"Model failed to load: {model_error}")
|
| 138 |
|
| 139 |
-
model = drums_model if instrument == "Drums" else bass_model
|
| 140 |
-
if model is None:
|
| 141 |
-
raise gr.Error(f"Model for {instrument} not available.")
|
| 142 |
-
|
| 143 |
if not models_on_device:
|
| 144 |
drums_model = drums_model.to(DEVICE) # type: ignore[union-attr]
|
| 145 |
bass_model = bass_model.to(DEVICE) # type: ignore[union-attr]
|
| 146 |
models_on_device = True
|
| 147 |
|
| 148 |
-
|
|
|
|
| 149 |
audio_np, orig_sr = sf.read(input_audio_path, always_2d=True)
|
| 150 |
context = torch.from_numpy(audio_np.T).float()
|
| 151 |
context = torchaudio.functional.resample(context, orig_sr, SAMPLE_RATE)
|
|
@@ -153,20 +146,18 @@ def process_fn(
|
|
| 153 |
context = context.mean(dim=0, keepdim=True)
|
| 154 |
context = context.reshape(1, 1, -1).to(DEVICE)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
out = model.generate(
|
| 159 |
n_samples=1,
|
| 160 |
gen_seconds=gen_seconds,
|
| 161 |
prompt=None,
|
| 162 |
context=context,
|
| 163 |
style=None,
|
| 164 |
beat=None,
|
| 165 |
-
description=
|
| 166 |
)
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
audio_out =
|
| 170 |
if audio_out.shape[0] == 1:
|
| 171 |
audio_out = audio_out.repeat(2, 1)
|
| 172 |
|
|
|
|
| 46 |
model_loading = True
|
| 47 |
model_error: str | None = None
|
| 48 |
|
| 49 |
+
# from hyperparameters.py
|
| 50 |
def build_params(encodec_weights: str, lm_weights: str) -> hp.MusicgenParams:
|
|
|
|
| 51 |
return hp.MusicgenParams(
|
| 52 |
encodec_params=hp.EncodecParams(
|
| 53 |
sample_rate=32_000,
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def load_checkpoint(params: hp.MusicgenParams, ckp_path: str) -> LightningMusicgen:
|
| 76 |
+
model = LightningMusicgen(params) # model_class strings in hyperparameters.py use the old "stage.*" prefix
|
|
|
|
|
|
|
| 77 |
sft.load_model(model, ckp_path)
|
| 78 |
return model.cpu().eval()
|
| 79 |
|
|
|
|
| 125 |
gen_seconds: int,
|
| 126 |
description: str,
|
| 127 |
) -> str:
|
|
|
|
| 128 |
global drums_model, bass_model, models_on_device
|
| 129 |
|
| 130 |
if model_loading:
|
|
|
|
| 132 |
if model_error:
|
| 133 |
raise gr.Error(f"Model failed to load: {model_error}")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
if not models_on_device:
|
| 136 |
drums_model = drums_model.to(DEVICE) # type: ignore[union-attr]
|
| 137 |
bass_model = bass_model.to(DEVICE) # type: ignore[union-attr]
|
| 138 |
models_on_device = True
|
| 139 |
|
| 140 |
+
model = drums_model if instrument == "Drums" else bass_model
|
| 141 |
+
|
| 142 |
audio_np, orig_sr = sf.read(input_audio_path, always_2d=True)
|
| 143 |
context = torch.from_numpy(audio_np.T).float()
|
| 144 |
context = torchaudio.functional.resample(context, orig_sr, SAMPLE_RATE)
|
|
|
|
| 146 |
context = context.mean(dim=0, keepdim=True)
|
| 147 |
context = context.reshape(1, 1, -1).to(DEVICE)
|
| 148 |
|
| 149 |
+
generated = model.generate(
|
|
|
|
|
|
|
| 150 |
n_samples=1,
|
| 151 |
gen_seconds=gen_seconds,
|
| 152 |
prompt=None,
|
| 153 |
context=context,
|
| 154 |
style=None,
|
| 155 |
beat=None,
|
| 156 |
+
description=[description.strip() or None],
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# generated: (1, 1, T) — squeeze to (1, T) and duplicate to stereo
|
| 160 |
+
audio_out = generated.squeeze(0).cpu().float()
|
| 161 |
if audio_out.shape[0] == 1:
|
| 162 |
audio_out = audio_out.repeat(2, 1)
|
| 163 |
|