Upload sample.py with huggingface_hub
Browse files
sample.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import soundfile as sf
|
| 9 |
+
import torch
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from diffusers.pipelines.deprecated.audio_diffusion.mel import Mel
|
| 12 |
+
from safetensors.torch import load_file
|
| 13 |
+
from transformers import AutoTokenizer, ClapModel
|
| 14 |
+
|
| 15 |
+
from audio_dit import AudioDiT
|
| 16 |
+
|
| 17 |
+
MAX_TOKENS = 32
|
| 18 |
+
|
| 19 |
+
@torch.no_grad()
|
| 20 |
+
def generate(model, seq, pool, null_seq, null_pool, steps, cfg, dev):
|
| 21 |
+
B = seq.shape[0]
|
| 22 |
+
x = torch.randn(B, 1, model.y_res, model.x_res, device=dev)
|
| 23 |
+
ns = null_seq.expand(B, -1, -1)
|
| 24 |
+
npool = null_pool.expand(B, -1)
|
| 25 |
+
dt = 1.0 / steps
|
| 26 |
+
for i in range(steps):
|
| 27 |
+
t = torch.full((B,), i * dt, device=dev)
|
| 28 |
+
with torch.autocast("cuda", dtype=torch.bfloat16):
|
| 29 |
+
vc = model(x, t, seq, pool)
|
| 30 |
+
vu = model(x, t, ns, npool)
|
| 31 |
+
x = x + (vu + cfg * (vc - vu)).float() * dt
|
| 32 |
+
return x
|
| 33 |
+
|
| 34 |
+
def image_from_tensor(row):
|
| 35 |
+
arr = ((row.clamp(-1, 1).float().cpu().numpy() + 1) * 127.5 + 0.5).astype(np.uint8)
|
| 36 |
+
return Image.fromarray(arr)
|
| 37 |
+
|
| 38 |
+
def load_model(weights, config, dev):
|
| 39 |
+
cfgd = json.load(open(config))
|
| 40 |
+
dit_cfg = cfgd["dit"]
|
| 41 |
+
model = AudioDiT(x_res=dit_cfg["x_res"], y_res=dit_cfg["y_res"],
|
| 42 |
+
text_seq_dim=dit_cfg["text_seq_dim"],
|
| 43 |
+
text_pool_dim=dit_cfg["text_pool_dim"]).to(dev).eval()
|
| 44 |
+
model.load_state_dict(load_file(weights))
|
| 45 |
+
mel = Mel(x_res=dit_cfg["x_res"], y_res=dit_cfg["y_res"], sample_rate=cfgd["mel"]["sample_rate"],
|
| 46 |
+
n_fft=cfgd["mel"]["n_fft"], hop_length=cfgd["mel"]["hop_length"], top_db=cfgd["mel"]["top_db"])
|
| 47 |
+
return model, mel, cfgd
|
| 48 |
+
|
| 49 |
+
def load_clap(name, dev):
|
| 50 |
+
tok = AutoTokenizer.from_pretrained(name)
|
| 51 |
+
clap = ClapModel.from_pretrained(name).to(dev).eval()
|
| 52 |
+
|
| 53 |
+
@torch.no_grad()
|
| 54 |
+
def enc(strings):
|
| 55 |
+
t = tok(strings, padding="max_length", max_length=MAX_TOKENS, truncation=True,
|
| 56 |
+
return_tensors="pt").to(dev)
|
| 57 |
+
out = clap.text_model(**t)
|
| 58 |
+
return out.last_hidden_state.float(), clap.text_projection(out.pooler_output).float()
|
| 59 |
+
|
| 60 |
+
return enc
|
| 61 |
+
|
| 62 |
+
def main():
|
| 63 |
+
ap = argparse.ArgumentParser()
|
| 64 |
+
ap.add_argument("prompts", nargs="+")
|
| 65 |
+
ap.add_argument("--out-dir", default="samples")
|
| 66 |
+
ap.add_argument("--cfg", type=float, default=4.0)
|
| 67 |
+
ap.add_argument("--steps", type=int, default=50)
|
| 68 |
+
ap.add_argument("--device", default="cuda")
|
| 69 |
+
ap.add_argument("--weights", default="/root/runs/audio_v1/model.safetensors")
|
| 70 |
+
ap.add_argument("--config", default="/root/runs/audio_v1/config.json")
|
| 71 |
+
ap.add_argument("--clap", default="laion/clap-htsat-unfused")
|
| 72 |
+
args = ap.parse_args()
|
| 73 |
+
dev = args.device
|
| 74 |
+
os.makedirs(args.out_dir, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
model, mel, _ = load_model(args.weights, args.config, dev)
|
| 77 |
+
enc = load_clap(args.clap, dev)
|
| 78 |
+
|
| 79 |
+
seq, pool = enc(args.prompts)
|
| 80 |
+
null_seq, null_pool = enc([""])
|
| 81 |
+
x = generate(model, seq, pool, null_seq, null_pool, args.steps, args.cfg, dev)
|
| 82 |
+
|
| 83 |
+
for prompt, row in zip(args.prompts, x[:, 0]):
|
| 84 |
+
img = image_from_tensor(row)
|
| 85 |
+
audio = mel.image_to_audio(img)
|
| 86 |
+
name = "_".join(prompt.lower().split())[:40]
|
| 87 |
+
sf.write(os.path.join(args.out_dir, f"{name}.wav"), audio, mel.get_sample_rate())
|
| 88 |
+
print(f" wrote {name}.wav ({len(audio)/mel.get_sample_rate():.1f}s) {prompt}", flush=True)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|