Spaces:
Running
Running
File size: 9,000 Bytes
2e6f087 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch as T\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"import torchaudio\n",
"from utils import load_ckpt, print_colored\n",
"from tokenizer import make_tokenizer\n",
"from model import get_hertz_dev_config\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import Audio, display\n",
"\n",
"\n",
"# If you get an error like \"undefined symbol: __nvJitLinkComplete_12_4, version libnvJitLink.so.12\",\n",
"# you need to install PyTorch with the correct CUDA version. Run:\n",
"# `pip3 uninstall torch torchaudio && pip3 install torch torchaudio --index-url https://download.pytorch.org/whl/cu121`\n",
"\n",
"device = 'cuda' if T.cuda.is_available() else 'cpu'\n",
"T.cuda.set_device(0)\n",
"print_colored(f\"Using device: {device}\", \"grey\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This code will automatically download them if it can't find them.\n",
"audio_tokenizer = make_tokenizer(device)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# We have different checkpoints for the single-speaker and two-speaker models\n",
"# Set to True to load and run inference with the two-speaker model\n",
"TWO_SPEAKER = False\n",
"USE_PURE_AUDIO_ABLATION = False # We trained a base model with no text initialization at all. Toggle this to enable it.\n",
"assert not (USE_PURE_AUDIO_ABLATION and TWO_SPEAKER) # We only have a single-speaker version of this model.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_config = get_hertz_dev_config(is_split=TWO_SPEAKER, use_pure_audio_ablation=USE_PURE_AUDIO_ABLATION)\n",
"\n",
"generator = model_config()\n",
"generator = generator.eval().to(T.bfloat16).to(device)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_and_preprocess_audio(audio_path):\n",
" print_colored(\"Loading and preprocessing audio...\", \"blue\", bold=True)\n",
" # Load audio file\n",
" audio_tensor, sr = torchaudio.load(audio_path)\n",
" print_colored(f\"Loaded audio shape: {audio_tensor.shape}\", \"grey\")\n",
" \n",
" if TWO_SPEAKER:\n",
" if audio_tensor.shape[0] == 1:\n",
" print_colored(\"Converting mono to stereo...\", \"grey\")\n",
" audio_tensor = audio_tensor.repeat(2, 1)\n",
" print_colored(f\"Stereo audio shape: {audio_tensor.shape}\", \"grey\")\n",
" else:\n",
" if audio_tensor.shape[0] == 2:\n",
" print_colored(\"Converting stereo to mono...\", \"grey\")\n",
" audio_tensor = audio_tensor.mean(dim=0).unsqueeze(0)\n",
" print_colored(f\"Mono audio shape: {audio_tensor.shape}\", \"grey\")\n",
" \n",
" # Resample to 16kHz if needed\n",
" if sr != 16000:\n",
" print_colored(f\"Resampling from {sr}Hz to 16000Hz...\", \"grey\")\n",
" resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000)\n",
" audio_tensor = resampler(audio_tensor)\n",
" \n",
" # Clip to 5 minutes if needed\n",
" max_samples = 16000 * 60 * 5\n",
" if audio_tensor.shape[1] > max_samples:\n",
" print_colored(\"Clipping audio to 5 minutes...\", \"grey\")\n",
" audio_tensor = audio_tensor[:, :max_samples]\n",
"\n",
" \n",
" print_colored(\"Audio preprocessing complete!\", \"green\")\n",
" return audio_tensor.unsqueeze(0)\n",
"\n",
"def display_audio(audio_tensor):\n",
" audio_tensor = audio_tensor.cpu().squeeze()\n",
" if audio_tensor.ndim == 1:\n",
" audio_tensor = audio_tensor.unsqueeze(0)\n",
" audio_tensor = audio_tensor.float()\n",
"\n",
" # Make a waveform plot\n",
" plt.figure(figsize=(4, 1))\n",
" plt.plot(audio_tensor.numpy()[0], linewidth=0.5)\n",
" plt.axis('off')\n",
" plt.show()\n",
"\n",
" # Make an audio player\n",
" display(Audio(audio_tensor.numpy(), rate=16000))\n",
" print_colored(f\"Audio ready for playback ↑\", \"green\", bold=True)\n",
" \n",
" \n",
"\n",
"# Our model is very prompt-sensitive, so we recommend experimenting with a diverse set of prompts.\n",
"prompt_audio = load_and_preprocess_audio('./prompts/toaskanymore.wav')\n",
"display_audio(prompt_audio)\n",
"prompt_len_seconds = 3\n",
"prompt_len = prompt_len_seconds * 8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print_colored(\"Encoding prompt...\", \"blue\")\n",
"with T.autocast(device_type='cuda', dtype=T.bfloat16):\n",
" if TWO_SPEAKER:\n",
" encoded_prompt_audio_ch1 = audio_tokenizer.latent_from_data(prompt_audio[:, 0:1].to(device))\n",
" encoded_prompt_audio_ch2 = audio_tokenizer.latent_from_data(prompt_audio[:, 1:2].to(device))\n",
" encoded_prompt_audio = T.cat([encoded_prompt_audio_ch1, encoded_prompt_audio_ch2], dim=-1)\n",
" else:\n",
" encoded_prompt_audio = audio_tokenizer.latent_from_data(prompt_audio.to(device))\n",
"print_colored(f\"Encoded prompt shape: {encoded_prompt_audio.shape}\", \"grey\")\n",
"print_colored(\"Prompt encoded successfully!\", \"green\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_completion(encoded_prompt_audio, prompt_len, gen_len=None):\n",
" prompt_len_seconds = prompt_len / 8\n",
" print_colored(f\"Prompt length: {prompt_len_seconds:.2f}s\", \"grey\")\n",
" print_colored(\"Completing audio...\", \"blue\")\n",
" encoded_prompt_audio = encoded_prompt_audio[:, :prompt_len]\n",
" with T.autocast(device_type='cuda', dtype=T.bfloat16):\n",
" completed_audio_batch = generator.completion(\n",
" encoded_prompt_audio, \n",
" temps=(.8, (0.5, 0.1)), # (token_temp, (categorical_temp, gaussian_temp))\n",
" use_cache=True,\n",
" gen_len=gen_len)\n",
"\n",
" completed_audio = completed_audio_batch\n",
" print_colored(f\"Decoding completion...\", \"blue\")\n",
" if TWO_SPEAKER:\n",
" decoded_completion_ch1 = audio_tokenizer.data_from_latent(completed_audio[:, :, :32].bfloat16())\n",
" decoded_completion_ch2 = audio_tokenizer.data_from_latent(completed_audio[:, :, 32:].bfloat16())\n",
" decoded_completion = T.cat([decoded_completion_ch1, decoded_completion_ch2], dim=0)\n",
" else:\n",
" decoded_completion = audio_tokenizer.data_from_latent(completed_audio.bfloat16())\n",
" print_colored(f\"Decoded completion shape: {decoded_completion.shape}\", \"grey\")\n",
"\n",
" print_colored(\"Preparing audio for playback...\", \"blue\")\n",
"\n",
" audio_tensor = decoded_completion.cpu().squeeze()\n",
" if audio_tensor.ndim == 1:\n",
" audio_tensor = audio_tensor.unsqueeze(0)\n",
" audio_tensor = audio_tensor.float()\n",
"\n",
" if audio_tensor.abs().max() > 1:\n",
" audio_tensor = audio_tensor / audio_tensor.abs().max()\n",
"\n",
" return audio_tensor[:, max(prompt_len*2000 - 16000, 0):]\n",
"\n",
"num_completions = 10\n",
"print_colored(f\"Generating {num_completions} completions...\", \"blue\")\n",
"for _ in range(num_completions):\n",
" completion = get_completion(encoded_prompt_audio, prompt_len, gen_len=20*8) # 20 seconds of generation\n",
" display_audio(completion)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|