File size: 18,805 Bytes
869c0ac
 
 
 
02a7301
869c0ac
 
 
 
 
 
 
4033ea7
869c0ac
 
 
 
 
 
 
 
 
0954180
869c0ac
 
 
 
 
 
4033ea7
869c0ac
 
 
 
 
 
 
 
 
 
 
4033ea7
869c0ac
 
 
 
e66133f
869c0ac
e66133f
869c0ac
 
 
 
 
43ebb3b
 
 
 
 
 
 
d68360d
 
43ebb3b
 
 
 
 
 
 
4734cf5
43ebb3b
 
aedf71e
 
 
 
 
 
 
 
 
 
4033ea7
aedf71e
 
 
 
600e950
 
 
8741d10
600e950
8741d10
d787bde
aedf71e
 
869c0ac
 
4033ea7
869c0ac
 
 
 
f30235e
 
b7c9dfd
 
44050e3
 
 
 
 
 
 
 
869c0ac
 
4033ea7
869c0ac
 
4033ea7
869c0ac
e66133f
da20094
 
 
 
 
 
869c0ac
 
d533c9c
869c0ac
 
 
 
 
 
e66133f
 
 
 
 
 
 
 
 
 
 
 
 
072978d
e66133f
 
01c4a98
 
4033ea7
01c4a98
 
4033ea7
e66133f
d68360d
da20094
4033ea7
 
e66133f
 
 
 
 
 
 
 
 
 
 
 
 
a71fd34
9fca2a2
e66133f
 
4033ea7
 
a71fd34
e66133f
 
4033ea7
 
9fca2a2
 
 
ea68dfd
 
f67abdb
ea68dfd
 
 
 
 
 
 
 
f67abdb
ea68dfd
 
 
 
 
 
 
 
 
4033ea7
 
 
 
 
ea68dfd
 
 
 
 
 
 
9fca2a2
 
5e9c370
9fca2a2
 
 
 
 
 
 
5e9c370
9fca2a2
 
ea68dfd
9fca2a2
 
da472c4
 
ea68dfd
da472c4
 
 
 
 
 
 
 
 
b7c9dfd
da472c4
 
9fca2a2
 
f67abdb
5e9c370
9fca2a2
f67abdb
9fca2a2
f67abdb
9fca2a2
ffb86f0
0ff9228
ea68dfd
0ff9228
b7c9dfd
ea68dfd
d68360d
072978d
212ab44
ea68dfd
f67abdb
ea68dfd
9fca2a2
b7c9dfd
f67abdb
b7c9dfd
 
 
ea68dfd
 
 
 
 
f67abdb
 
 
 
5e9c370
 
7e864d5
 
f67abdb
7e864d5
 
 
 
 
 
 
 
f67abdb
7e864d5
 
 
 
e94fc5c
7e864d5
 
e94fc5c
7e864d5
43ebb3b
d68360d
7e864d5
 
 
 
869c0ac
 
43ebb3b
 
 
4734cf5
43ebb3b
 
 
 
 
 
 
 
 
f30235e
 
43ebb3b
 
 
 
 
869c0ac
 
43ebb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4734cf5
43ebb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869c0ac
 
 
 
 
 
 
 
 
43ebb3b
 
869c0ac
 
 
 
e66133f
43ebb3b
869c0ac
e66133f
869c0ac
43ebb3b
3ae9402
869c0ac
 
 
 
e66133f
43ebb3b
869c0ac
e66133f
869c0ac
e868325
43ebb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ae9402
43ebb3b
 
 
 
 
 
 
 
 
e94fc5c
43ebb3b
 
 
 
 
 
 
 
 
940551e
43ebb3b
 
 
3ae9402
 
43ebb3b
869c0ac
da20094
a71fd34
 
5ecf3c1
a71fd34
 
 
 
 
 
 
 
 
5ecf3c1
a71fd34
 
 
f30235e
a71fd34
 
 
 
 
5ecf3c1
a71fd34
 
 
f30235e
 
a71fd34
 
 
 
 
5ecf3c1
a71fd34
 
 
d68360d
a71fd34
 
 
 
 
 
 
 
 
 
5ecf3c1
a71fd34
 
 
d68360d
a71fd34
 
 
 
 
 
 
 
 
5ecf3c1
a71fd34
 
 
 
 
 
 
 
 
5ecf3c1
a71fd34
 
 
 
5ecf3c1
f30235e
 
 
 
5ecf3c1
a71fd34
 
 
 
 
5ecf3c1
a71fd34
 
 
 
5ecf3c1
f30235e
 
 
 
5ecf3c1
a71fd34
 
 
 
 
5ecf3c1
a71fd34
 
 
 
 
5ecf3c1
a71fd34
3ae9402
 
a71fd34
 
 
da20094
 
 
5ecf3c1
da20094
 
 
869c0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a71fd34
869c0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "62c5865f",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/test_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c7800a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    # are we running on Google Colab?\n",
    "    import google.colab\n",
    "    !git clone -q https://github.com/teticio/audio-diffusion.git\n",
    "    %cd audio-diffusion\n",
    "    %pip install -q -r requirements.txt\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b447e2c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.insert(0, os.path.dirname(os.path.abspath(\"\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2fc0e7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import random\n",
    "import numpy as np\n",
    "from datasets import load_dataset\n",
    "from IPython.display import Audio\n",
    "from audiodiffusion import AudioDiffusion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b294a94a",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "generator = torch.Generator(device=device)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3feb265",
   "metadata": {},
   "source": [
    "## DDPM (De-noising Diffusion Probabilistic Models)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fd945bb",
   "metadata": {},
   "source": [
    "### Select model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97f24046",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@markdown teticio/audio-diffusion-256                     - trained on my Spotify \"liked\" playlist\n",
    "\n",
    "#@markdown teticio/audio-diffusion-breaks-256              - trained on samples used in music\n",
    "\n",
    "#@markdown teticio/audio-diffusion-instrumental-hiphop-256 - trained on instrumental hiphop\n",
    "\n",
    "model_id = \"teticio/audio-diffusion-256\"  #@param [\"teticio/audio-diffusion-256\", \"teticio/audio-diffusion-breaks-256\", \"audio-diffusion-instrumenal-hiphop-256\", \"teticio/audio-diffusion-ddim-256\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3d45c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_diffusion = AudioDiffusion(model_id=model_id)\n",
    "mel = audio_diffusion.pipe.mel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "011fb5a1",
   "metadata": {},
   "source": [
    "### Run model inference to generate mel spectrogram, audios and loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b809fed5",
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(10):\n",
    "    seed = generator.seed()\n",
    "    print(f'Seed = {seed}')\n",
    "    generator.manual_seed(seed)\n",
    "    image, (sample_rate,\n",
    "            audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "                generator=generator)\n",
    "    display(image)\n",
    "    display(Audio(audio, rate=sample_rate))\n",
    "    loop = AudioDiffusion.loop_it(audio, sample_rate)\n",
    "    if loop is not None:\n",
    "        display(Audio(loop, rate=sample_rate))\n",
    "    else:\n",
    "        print(\"Unable to determine loop points\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bb03e33",
   "metadata": {},
   "source": [
    "### Generate variations of audios"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80e5b5fa",
   "metadata": {},
   "source": [
    "Try playing around with `start_steps`. Values closer to zero will produce new samples, while values closer to 1,000 will produce samples more faithful to the original."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5074ec11",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = 2391504374279719  #@param {type:\"integer\"}\n",
    "generator.manual_seed(seed)\n",
    "image, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    generator=generator)\n",
    "display(image)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0fefe28",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "start_step = 500  #@param {type:\"slider\", min:0, max:1000, step:10}\n",
    "track = AudioDiffusion.loop_it(audio, sample_rate, loops=1)\n",
    "for variation in range(12):\n",
    "    image2, (\n",
    "        sample_rate,\n",
    "        audio2) = audio_diffusion.generate_spectrogram_and_audio_from_audio(\n",
    "            raw_audio=audio, start_step=start_step)\n",
    "    display(image2)\n",
    "    display(Audio(audio2, rate=sample_rate))\n",
    "    track = np.concatenate(\n",
    "        [track, AudioDiffusion.loop_it(audio2, sample_rate, loops=1)])\n",
    "display(Audio(track, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58a876c1",
   "metadata": {},
   "source": [
    "### Generate continuations (\"out-painting\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b95d5780",
   "metadata": {},
   "outputs": [],
   "source": [
    "overlap_secs = 2  #@param {type:\"integer\"}\n",
    "start_step = 0  #@param {type:\"slider\", min:0, max:1000, step:10}\n",
    "overlap_samples = overlap_secs * sample_rate\n",
    "track = audio\n",
    "for variation in range(12):\n",
    "    image2, (\n",
    "        sample_rate,\n",
    "        audio2) = audio_diffusion.generate_spectrogram_and_audio_from_audio(\n",
    "            raw_audio=audio[-overlap_samples:],\n",
    "            start_step=start_step,\n",
    "            mask_start_secs=overlap_secs)\n",
    "    display(image2)\n",
    "    display(Audio(audio2, rate=sample_rate))\n",
    "    track = np.concatenate([track, audio2[overlap_samples:]])\n",
    "    audio = audio2\n",
    "display(Audio(track, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6434d3f",
   "metadata": {},
   "source": [
    "### Remix (style transfer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0da030b2",
   "metadata": {},
   "source": [
    "Alternatively, you can start from another audio altogether, resulting in a kind of style transfer. Maintaining the same seed during generation fixes the style, while masking helps stitch consecutive segments together more smoothly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc620a80",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    # are we running on Google Colab?\n",
    "    from google.colab import files\n",
    "    audio_file = list(files.upload().keys())[0]\n",
    "except:\n",
    "    audio_file = \"/home/teticio/Music/liked/El Michels Affair - Glaciers Of Ice.mp3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a257e69",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "start_step = 500  #@param {type:\"slider\", min:0, max:1000, step:10}\n",
    "overlap_secs = 2  #@param {type:\"integer\"}\n",
    "mel.load_audio(audio_file)\n",
    "overlap_samples = overlap_secs * mel.get_sample_rate()\n",
    "slice_size = mel.x_res * mel.hop_length\n",
    "stride = slice_size - overlap_samples\n",
    "generator = torch.Generator(device=device)\n",
    "seed = generator.seed()\n",
    "print(f'Seed = {seed}')\n",
    "track = np.array([])\n",
    "not_first = 0\n",
    "for sample in range(len(mel.audio) // stride):\n",
    "    generator.manual_seed(seed)\n",
    "    audio = np.array(mel.audio[sample * stride:sample * stride + slice_size])\n",
    "    if not_first:\n",
    "        # Normalize and re-insert generated audio\n",
    "        audio[:overlap_samples] = audio2[-overlap_samples:] * np.max(\n",
    "            audio[:overlap_samples]) / np.max(audio2[-overlap_samples:])\n",
    "    _, (sample_rate,\n",
    "        audio2) = audio_diffusion.generate_spectrogram_and_audio_from_audio(\n",
    "            raw_audio=audio,\n",
    "            start_step=start_step,\n",
    "            generator=generator,\n",
    "            mask_start_secs=overlap_secs * not_first)\n",
    "    track = np.concatenate([track, audio2[overlap_samples * not_first:]])\n",
    "    not_first = 1\n",
    "    display(Audio(track, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "924ff9d5",
   "metadata": {},
   "source": [
    "### Fill the gap (\"in-painting\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0200264c",
   "metadata": {},
   "outputs": [],
   "source": [
    "slice = 3  #@param {type:\"integer\"}\n",
    "raw_audio = mel.get_audio_slice(slice)\n",
    "_, (sample_rate,\n",
    "    audio2) = audio_diffusion.generate_spectrogram_and_audio_from_audio(\n",
    "        raw_audio=raw_audio,\n",
    "        mask_start_secs=1,\n",
    "        mask_end_secs=1,\n",
    "        step_generator=torch.Generator(device=device))\n",
    "display(Audio(audio, rate=sample_rate))\n",
    "display(Audio(audio2, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efc32dae",
   "metadata": {},
   "source": [
    "## DDIM (De-noising Diffusion Implicit Models)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a021f78a",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_diffusion = AudioDiffusion(model_id='teticio/audio-diffusion-ddim-256')\n",
    "mel = audio_diffusion.pipe.mel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deb23339",
   "metadata": {},
   "source": [
    "### Generation can be done in many fewer steps with DDIMs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c105a497",
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(10):\n",
    "    seed = generator.seed()\n",
    "    print(f'Seed = {seed}')\n",
    "    generator.manual_seed(seed)\n",
    "    image, (sample_rate,\n",
    "            audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "                generator=generator)\n",
    "    display(image)\n",
    "    display(Audio(audio, rate=sample_rate))\n",
    "    loop = AudioDiffusion.loop_it(audio, sample_rate)\n",
    "    if loop is not None:\n",
    "        display(Audio(loop, rate=sample_rate))\n",
    "    else:\n",
    "        print(\"Unable to determine loop points\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cab4692c",
   "metadata": {},
   "source": [
    "The parameter eta controls the variance:\n",
    "* 0 - DDIM (deterministic)\n",
    "* 1 - DDPM (De-noising Diffusion Probabilistic Model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72bdd207",
   "metadata": {},
   "outputs": [],
   "source": [
    "image, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    steps=1000, generator=generator, eta=1)\n",
    "display(image)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d5442c",
   "metadata": {},
   "source": [
    "### DDIMs can be used as encoders..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "269ee816",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Doesn't have to be an audio from the train dataset, this is just for convenience\n",
    "ds = load_dataset('teticio/audio-diffusion-256')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "278d1d80",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ds['train'][264]['image']\n",
    "display(Audio(mel.image_to_audio(image), rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "912b54e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "noise = audio_diffusion.pipe.encode([image])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7b31f97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reconstruct original audio from noise\n",
    "_, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    noise=noise, generator=generator)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "998c776b",
   "metadata": {},
   "source": [
    "### ...or to interpolate between audios"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33f82367",
   "metadata": {},
   "outputs": [],
   "source": [
    "image2 = ds['train'][15978]['image']\n",
    "display(Audio(mel.image_to_audio(image2), rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f93fb6c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "noise2 = audio_diffusion.pipe.encode([image2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4190563",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.5  #@param {type:\"slider\", min:0, max:1, step:0.1}\n",
    "_, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    noise=audio_diffusion.pipe.slerp(noise, noise2, alpha),\n",
    "    generator=generator)\n",
    "display(Audio(mel.image_to_audio(image), rate=sample_rate))\n",
    "display(Audio(mel.image_to_audio(image2), rate=sample_rate))\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b244547",
   "metadata": {},
   "source": [
    "## Latent Audio Diffusion\n",
    "Instead of de-noising images directly in the pixel space, we can work in the latent space of a pre-trained VAE (Variational AutoEncoder). This is much faster to train and run inference on, although the quality suffers as there are now three stages involved in encoding / decoding: mel spectrogram, VAE and de-noising."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a88b3fbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_id = \"teticio/latent-audio-diffusion-ddim-256-new\"  #@param [\"teticio/latent-audio-diffusion-256\", \"teticio/latent-audio-diffusion-ddim-256\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15e353ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_diffusion = AudioDiffusion(model_id=model_id)\n",
    "mel = audio_diffusion.pipe.mel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa0f0c8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = 3412253600050855  #@param {type:\"integer\"}\n",
    "generator.manual_seed(seed)\n",
    "image, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    generator=generator)\n",
    "display(image)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73dc575d",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed2 = 7016114633369557  #@param {type:\"integer\"}\n",
    "generator.manual_seed(seed2)\n",
    "image2, (sample_rate, audio2) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    generator=generator)\n",
    "display(image2)\n",
    "display(Audio(audio2, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "428d2d67",
   "metadata": {},
   "source": [
    "### Interpolation in latent space\n",
    "As the VAE forces a more compact, lower dimensional representation for the spectrograms, interpolation in latent space can lead to meaningful combinations of audios. In combination with the (deterministic) DDIM from the previous section, the model can be used as an encoder / decoder to a lower dimensional space."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72211c2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "generator.manual_seed(seed)\n",
    "latents = torch.randn((1, audio_diffusion.pipe.unet.in_channels,\n",
    "                       audio_diffusion.pipe.unet.sample_size[0],\n",
    "                       audio_diffusion.pipe.unet.sample_size[1]),\n",
    "                      device=device,\n",
    "                      generator=generator)\n",
    "latents.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c732dbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "generator.manual_seed(seed2)\n",
    "latents2 = torch.randn((1, audio_diffusion.pipe.unet.in_channels,\n",
    "                        audio_diffusion.pipe.unet.sample_size[0],\n",
    "                        audio_diffusion.pipe.unet.sample_size[1]),\n",
    "                       device=device,\n",
    "                       generator=generator)\n",
    "latents2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "159bcfc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.5  #@param {type:\"slider\", min:0, max:1, step:0.1}\n",
    "_, (sample_rate, audio3) = audio_diffusion.generate_spectrogram_and_audio(\n",
    "    noise=audio_diffusion.pipe.slerp(latents, latents2, alpha),\n",
    "    generator=generator)\n",
    "display(Audio(audio, rate=sample_rate))\n",
    "display(Audio(audio2, rate=sample_rate))\n",
    "display(Audio(audio3, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce6c9cc1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "provenance": []
  },
  "gpuClass": "standard",
  "kernelspec": {
   "display_name": "huggingface",
   "language": "python",
   "name": "huggingface"
  },
  "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.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}