Spaces:
Sleeping
Sleeping
Vansh Chugh commited on
Commit ·
55cbfca
1
Parent(s): cca94cf
ask user only for the edit part, use non-deprecated autocast api
Browse files- app.py +9 -6
- src/models/instructmusicgenadapter_module.py +2 -3
app.py
CHANGED
|
@@ -104,10 +104,14 @@ def process_fn(input_audio_path: str, instruction: str) -> str:
|
|
| 104 |
if not instruction or not instruction.strip():
|
| 105 |
raise gr.Error("Instruction cannot be empty.")
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
input_audio, sample_rate = sf.read(input_audio_path)
|
| 108 |
input_audio_tensor = torch.tensor(input_audio).float()
|
| 109 |
if input_audio_tensor.ndim == 2:
|
| 110 |
-
# downmix to mono -- the original inference.py assumes a mono waveform
|
| 111 |
input_audio_tensor = input_audio_tensor.mean(dim=-1)
|
| 112 |
input_audio_tensor = input_audio_tensor.unsqueeze(0).unsqueeze(0)
|
| 113 |
if sample_rate != SAMPLE_RATE:
|
|
@@ -118,7 +122,7 @@ def process_fn(input_audio_path: str, instruction: str) -> str:
|
|
| 118 |
|
| 119 |
instruction_list = [instruction]
|
| 120 |
|
| 121 |
-
with
|
| 122 |
description, cond_code = model.model.musicgen._prepare_tokens_and_attributes(
|
| 123 |
instruction_list, input_audio_tensor
|
| 124 |
)
|
|
@@ -127,7 +131,7 @@ def process_fn(input_audio_path: str, instruction: str) -> str:
|
|
| 127 |
[cond_code, torch.ones_like(cond_code[:, :, 0:1]) * CODEBOOK_SIZE], dim=-1
|
| 128 |
)
|
| 129 |
|
| 130 |
-
with
|
| 131 |
audio_values = model.model.generate(
|
| 132 |
text_description=instruction_list,
|
| 133 |
condition_audio_code=cond_code,
|
|
@@ -154,9 +158,8 @@ with gr.Blocks() as demo:
|
|
| 154 |
gr.Audio(type="filepath", label="Input Audio").harp_required(True),
|
| 155 |
gr.Textbox(
|
| 156 |
label="Instruction",
|
| 157 |
-
value="
|
| 158 |
-
info="
|
| 159 |
-
"<edit> is add/only/no + an instrument, e.g. 'Only Drums', 'No Bass', "
|
| 160 |
"'Add Piano' (per the model's demo examples; piano/bass/drums/guitar work best)",
|
| 161 |
),
|
| 162 |
]
|
|
|
|
| 104 |
if not instruction or not instruction.strip():
|
| 105 |
raise gr.Error("Instruction cannot be empty.")
|
| 106 |
|
| 107 |
+
# model was trained on "Music piece. Instruct: <desired_edit>."
|
| 108 |
+
# only <desired_edit> is variable, so rest is hardcoded
|
| 109 |
+
instruction = f"Music piece. Instruct: {instruction.strip().rstrip('.')}."
|
| 110 |
+
|
| 111 |
input_audio, sample_rate = sf.read(input_audio_path)
|
| 112 |
input_audio_tensor = torch.tensor(input_audio).float()
|
| 113 |
if input_audio_tensor.ndim == 2:
|
| 114 |
+
# downmix to mono -- the original inference.py assumes a mono waveform!!
|
| 115 |
input_audio_tensor = input_audio_tensor.mean(dim=-1)
|
| 116 |
input_audio_tensor = input_audio_tensor.unsqueeze(0).unsqueeze(0)
|
| 117 |
if sample_rate != SAMPLE_RATE:
|
|
|
|
| 122 |
|
| 123 |
instruction_list = [instruction]
|
| 124 |
|
| 125 |
+
with torch.autocast("cuda", dtype=torch.float16):
|
| 126 |
description, cond_code = model.model.musicgen._prepare_tokens_and_attributes(
|
| 127 |
instruction_list, input_audio_tensor
|
| 128 |
)
|
|
|
|
| 131 |
[cond_code, torch.ones_like(cond_code[:, :, 0:1]) * CODEBOOK_SIZE], dim=-1
|
| 132 |
)
|
| 133 |
|
| 134 |
+
with torch.autocast("cuda", dtype=torch.float16):
|
| 135 |
audio_values = model.model.generate(
|
| 136 |
text_description=instruction_list,
|
| 137 |
condition_audio_code=cond_code,
|
|
|
|
| 158 |
gr.Audio(type="filepath", label="Input Audio").harp_required(True),
|
| 159 |
gr.Textbox(
|
| 160 |
label="Instruction",
|
| 161 |
+
value="Only Drums",
|
| 162 |
+
info="What to change: add/only/no + an instrument, e.g. 'Only Drums', 'No Bass', "
|
|
|
|
| 163 |
"'Add Piano' (per the model's demo examples; piano/bass/drums/guitar work best)",
|
| 164 |
),
|
| 165 |
]
|
src/models/instructmusicgenadapter_module.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
"""PyTorch Lightning module for InstructMusicGen model with adapter-based instruction tuning.
|
| 2 |
|
| 3 |
-
Trimmed to the inference path only
|
| 4 |
-
|
| 5 |
-
which load_from_checkpoint() needs to reconstruct the model for inference.
|
| 6 |
"""
|
| 7 |
import torch
|
| 8 |
from lightning import LightningModule
|
|
|
|
| 1 |
"""PyTorch Lightning module for InstructMusicGen model with adapter-based instruction tuning.
|
| 2 |
|
| 3 |
+
Trimmed to the inference path only i.e. removed training_step/ validation_step/test_step, torchmetrics/wandb
|
| 4 |
+
logging bcs not needed for inference; only kept what load_from_checkpoint() needs.
|
|
|
|
| 5 |
"""
|
| 6 |
import torch
|
| 7 |
from lightning import LightningModule
|