Skip to content

Commit cf70e66

Browse files
Vaibhavs10Wauplin
andauthored
add stable audio tools as a library + code snippets. (#741)
Adds inference snippets, download count support for stable audio tools. --------- Co-authored-by: Lucain <[email protected]>
1 parent ce8783c commit cf70e66

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

packages/tasks/src/model-libraries-snippets.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,43 @@ export const sklearn = (model: ModelData): string[] => {
326326
}
327327
};
328328

329+
export const stable_audio_tools = (model: ModelData): string[] => [
330+
`import torch
331+
import torchaudio
332+
from einops import rearrange
333+
from stable_audio_tools import get_pretrained_model
334+
from stable_audio_tools.inference.generation import generate_diffusion_cond
335+
336+
device = "cuda" if torch.cuda.is_available() else "cpu"
337+
338+
# Download model
339+
model, model_config = get_pretrained_model("${model.id}")
340+
sample_rate = model_config["sample_rate"]
341+
sample_size = model_config["sample_size"]
342+
343+
model = model.to(device)
344+
345+
# Set up text and timing conditioning
346+
conditioning = [{
347+
"prompt": "128 BPM tech house drum loop",
348+
}]
349+
350+
# Generate stereo audio
351+
output = generate_diffusion_cond(
352+
model,
353+
conditioning=conditioning,
354+
sample_size=sample_size,
355+
device=device
356+
)
357+
358+
# Rearrange audio batch to a single sequence
359+
output = rearrange(output, "b d n -> d (b n)")
360+
361+
# Peak normalize, clip, convert to int16, and save to file
362+
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
363+
torchaudio.save("output.wav", output, sample_rate)`,
364+
];
365+
329366
export const fastai = (model: ModelData): string[] => [
330367
`from huggingface_hub import from_pretrained_fastai
331368

packages/tasks/src/model-libraries.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
372372
term: { path: "hyperparams.yaml" },
373373
},
374374
},
375+
"stable-audio-tools": {
376+
prettyLabel: "Stable Audio Tools",
377+
repoName: "stable-audio-tools",
378+
repoUrl: "https://github.com/Stability-AI/stable-audio-tools.git",
379+
filter: false,
380+
countDownloads: { term: { path: "model.safetensors" } },
381+
snippets: snippets.stable_audio_tools,
382+
},
375383
"stable-baselines3": {
376384
prettyLabel: "stable-baselines3",
377385
repoName: "stable-baselines3",

0 commit comments

Comments
 (0)