Download scripts/make_reference.py from matthiastl/gepard-onnx: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/make_reference.py
- Command line
-
hf download hf://matthiastl/gepard-onnx/scripts/make_reference.py
-
curl -L -o make_reference.py https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/make_reference.py
1.8 kB
| """RULE.md: the reference's frames, logits, stop values and sound for the 40 sentences (no voice), and its 3 synthetic | |
| reference clips' prefix vectors.""" | |
| import sys, time | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| import numpy as np, torch, soundfile as sf | |
| import codec, reference as R, texts | |
| OUT = Path(__file__).resolve().parents[1] / "runs" / "ref"; OUT.mkdir(parents=True, exist_ok=True) | |
| half = np.array([4, 3, 3, 3] * 8, np.float32) | |
| r, c = R.Reference(), codec.load() | |
| t0 = time.perf_counter(); total = 0 | |
| for i, t in enumerate(texts.TEXTS): | |
| trace = []; codes = r.generate(t, seed=1000 + i, trace=trace); total += len(codes) | |
| z = torch.from_numpy((codes.astype(np.float32) - half) / half).T[None] | |
| with torch.no_grad(): a, _ = c.decode_audio(inputs=z, input_len=torch.tensor([len(codes)])) | |
| np.savez(OUT / f"{i:02d}.npz", codes=codes, logits=np.stack([x[0] for x in trace]), stop=np.array([x[1] for x in trace], np.float32)) | |
| sf.write(OUT / f"{i:02d}.wav", a[0].numpy(), 22050) | |
| if i % 10 == 9: print(i + 1, "sentences,", total, "frames,", round(time.perf_counter() - t0), "s", flush=True) | |
| # synthetic reference clips: three of the sentences just made become reference voices | |
| for k, i in enumerate((12, 29, 36)): | |
| x, sr = sf.read(OUT / f"{i:02d}.wav", dtype="float32") | |
| with torch.no_grad(): | |
| tok, n = c.encode(audio=torch.from_numpy(x)[None], audio_len=torch.tensor([len(x)])) | |
| L4 = torch.tensor([8, 7, 6, 6]); base = torch.tensor([1, 8, 56, 336]) | |
| un = ((tok[0].T[:, :, None] // base) % L4).reshape(tok.shape[2], -1)[None] # [1, T, 32] | |
| pre = r.comp(un)[0] | |
| np.savez(OUT / f"voice{k}.npz", codes=un[0].numpy(), prefix=pre.numpy(), wav=x) | |
| print("voice", k, "frames", un.shape[1], flush=True) | |