File size: 21,879 Bytes
a0bbc38
c10222e
9583919
 
c10222e
9583919
 
 
 
 
 
a0bbc38
 
9583919
 
 
c10222e
a0bbc38
 
 
 
c10222e
4c5d66a
 
 
 
a0bbc38
4c5d66a
a0bbc38
 
 
 
 
4c5d66a
a0bbc38
4c5d66a
 
 
a0bbc38
4c5d66a
 
a0bbc38
9583919
a0bbc38
c10222e
 
5f6b40b
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
 
749ffd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f6b40b
 
 
 
749ffd8
5f6b40b
 
 
 
 
749ffd8
 
5f6b40b
 
 
749ffd8
5f6b40b
 
749ffd8
5f6b40b
9583919
5f6b40b
 
9583919
5f6b40b
749ffd8
5f6b40b
749ffd8
5f6b40b
749ffd8
5f6b40b
 
9583919
749ffd8
5f6b40b
 
 
 
749ffd8
5f6b40b
749ffd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f6b40b
9583919
5f6b40b
749ffd8
5f6b40b
e823eac
5f6b40b
749ffd8
9583919
749ffd8
e823eac
 
 
 
749ffd8
5f6b40b
749ffd8
 
5f6b40b
 
e823eac
5f6b40b
e823eac
9583919
749ffd8
5f6b40b
749ffd8
e823eac
749ffd8
5f6b40b
 
 
 
 
 
e823eac
5f6b40b
 
 
 
 
 
 
 
 
 
 
9583919
749ffd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
749ffd8
5f6b40b
9583919
5f6b40b
749ffd8
e823eac
9583919
5f6b40b
 
 
 
 
 
 
 
 
e823eac
5f6b40b
 
 
 
 
 
 
 
 
 
 
e823eac
5f6b40b
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
e823eac
5f6b40b
 
e823eac
5f6b40b
 
 
 
e823eac
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
e823eac
 
5f6b40b
 
 
 
 
e823eac
 
5f6b40b
e823eac
5f6b40b
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
25c6bf6
5f6b40b
 
 
 
 
e823eac
5f6b40b
 
 
 
 
9583919
5f6b40b
 
 
9583919
5f6b40b
e823eac
5f6b40b
 
 
9583919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f6b40b
 
e823eac
5f6b40b
 
 
 
 
 
 
 
 
e823eac
5f6b40b
e823eac
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
e823eac
5f6b40b
 
 
 
 
 
 
e823eac
5f6b40b
e823eac
5f6b40b
 
 
e823eac
 
5f6b40b
 
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
9583919
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
e823eac
9583919
5f6b40b
e823eac
5f6b40b
 
9583919
5f6b40b
9583919
e823eac
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
 
9583919
5f6b40b
 
 
 
 
e823eac
9583919
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
 
 
9583919
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
 
 
 
 
 
 
e823eac
5f6b40b
e823eac
5f6b40b
 
 
 
 
25c6bf6
5f6b40b
 
 
e823eac
5f6b40b
9583919
5f6b40b
 
 
9583919
5f6b40b
 
e823eac
5f6b40b
 
 
9583919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e823eac
5f6b40b
 
 
 
e823eac
5f6b40b
 
 
 
9583919
5f6b40b
 
 
9583919
5f6b40b
 
 
 
 
9583919
5f6b40b
 
 
ffd7404
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import os
import sys
import uuid
from pathlib import Path

import argbind
import audiotools as at
import gradio as gr
import numpy as np
import torch
import yaml
from huggingface_hub import hf_hub_download
from torch_pitch_shift import pitch_shift

from vampnet import mask as pmask
from vampnet.interface import Interface

# 1. Setup paths and WorkDir
SCRIPT_DIR = Path(__file__).parent.absolute()
# This ensures relative paths like 'conf/interface.yml' work correctly
os.chdir(SCRIPT_DIR)

MODEL_DIR = SCRIPT_DIR / "models"


def ensure_models_exist():
    """Fallback check for weights. In Docker, these are already baked in."""
    repo_id = "ProjectCETI/wham"
    files = ["codec.pth", "coarse.pth", "c2f.pth", "wavebeat.pth"]

    for filename in files:
        if not (MODEL_DIR / filename).exists():
            print(f"Weight {filename} missing, downloading...")
            hf_hub_download(
                repo_id=repo_id, filename=filename, local_dir=str(MODEL_DIR)
            )


# Run the check
ensure_models_exist()

# 2. Hardware Setup
device = "cuda" if torch.cuda.is_available() else "cpu"
# Update sys.argv so argbind finds the config file correctly
sys.argv = ["app.py", "--args.load", "conf/interface.yml", "--Interface.device", device]


Interface = argbind.bind(Interface)

conf = argbind.parse_args()


def shift_pitch(signal, interval: int):
    signal.samples = pitch_shift(
        signal.samples, shift=interval, sample_rate=signal.sample_rate
    )
    return signal


def load_interface():
    with argbind.scope(conf):
        interface = Interface()
        # loader = AudioLoader()
        print(f"interface device is {interface.device}")
        return interface


interface = load_interface()


OUT_DIR = Path("gradio-outputs")
OUT_DIR.mkdir(exist_ok=True, parents=True)


def load_audio(file):
    print(file)
    filepath = file.name
    sig = at.AudioSignal.salient_excerpt(
        filepath, duration=interface.coarse.chunk_size_s
    )
    sig = interface.preprocess(sig)

    out_dir = OUT_DIR / "tmp" / str(uuid.uuid4())
    out_dir.mkdir(parents=True, exist_ok=True)
    sig.write(out_dir / "input.wav")
    return sig.path_to_file


def load_example_audio():
    return "./assets/example.wav"


def _vamp(
    _input_audio,
    _num_steps,
    _masktemp,
    _sampletemp,
    _top_p,
    _prefix_s,
    _suffix_s,
    _rand_mask_intensity,
    _periodic_p,
    _periodic_w,
    _n_conditioning_codebooks,
    _dropout,
    _use_coarse2fine,
    _stretch_factor,
    _onset_mask_width,
    _typical_filtering,
    _typical_mass,
    _typical_min_tokens,
    _beat_mask_width,
    _beat_mask_downbeats,
    _seed,
    _n_mask_codebooks,
    _pitch_shift_amt,
    _sample_cutoff,
    return_mask=False,
):
    interface.to("cuda")

    out_dir = OUT_DIR / str(uuid.uuid4())
    out_dir.mkdir()
    sig = at.AudioSignal(_input_audio)
    sig = interface.preprocess(sig)

    loudness = sig.loudness()
    print(f"input loudness is {loudness}")

    if _pitch_shift_amt != 0:
        sig = shift_pitch(sig, _pitch_shift_amt)

    z = interface.encode(sig)

    ncc = _n_conditioning_codebooks

    # build the mask
    mask = pmask.linear_random(z, _rand_mask_intensity)
    mask = pmask.mask_and(
        mask, pmask.inpaint(z, interface.s2t(_prefix_s), interface.s2t(_suffix_s))
    )
    mask = pmask.mask_and(
        mask, pmask.periodic_mask(z, _periodic_p, _periodic_w, random_roll=True)
    )
    if _onset_mask_width > 0:
        mask = pmask.mask_or(
            mask, pmask.onset_mask(sig, z, interface, width=_onset_mask_width)
        )
    if _beat_mask_width > 0:
        beat_mask = interface.make_beat_mask(
            sig,
            after_beat_s=(_beat_mask_width / 1000),
            mask_upbeats=not _beat_mask_downbeats,
        )
        mask = pmask.mask_and(mask, beat_mask)

    # these should be the last two mask ops
    mask = pmask.dropout(mask, _dropout)
    mask = pmask.codebook_unmask(mask, ncc)
    mask = pmask.codebook_mask(mask, int(_n_mask_codebooks))

    print(f"dropout {_dropout}")
    print(f"masktemp {_masktemp}")
    print(f"sampletemp {_sampletemp}")
    print(f"top_p {_top_p}")
    print(f"prefix_s {_prefix_s}")
    print(f"suffix_s {_suffix_s}")
    print(f"rand_mask_intensity {_rand_mask_intensity}")
    print(f"num_steps {_num_steps}")
    print(f"periodic_p {_periodic_p}")
    print(f"periodic_w {_periodic_w}")
    print(f"n_conditioning_codebooks {_n_conditioning_codebooks}")
    print(f"use_coarse2fine {_use_coarse2fine}")
    print(f"onset_mask_width {_onset_mask_width}")
    print(f"beat_mask_width {_beat_mask_width}")
    print(f"beat_mask_downbeats {_beat_mask_downbeats}")
    print(f"stretch_factor {_stretch_factor}")
    print(f"seed {_seed}")
    print(f"pitch_shift_amt {_pitch_shift_amt}")
    print(f"sample_cutoff {_sample_cutoff}")

    _top_p_val = _top_p if _top_p > 0 else None
    # save the mask as a txt file
    np.savetxt(out_dir / "mask.txt", mask[:, 0, :].long().cpu().numpy())

    _seed_val = _seed if _seed > 0 else None
    zv, mask_z = interface.coarse_vamp(
        z,
        mask=mask,
        sampling_steps=_num_steps,
        mask_temperature=_masktemp * 10,
        sampling_temperature=_sampletemp,
        return_mask=True,
        typical_filtering=_typical_filtering,
        typical_mass=_typical_mass,
        typical_min_tokens=_typical_min_tokens,
        top_p=_top_p_val,
        gen_fn=interface.coarse.generate,
        seed=_seed_val,
        sample_cutoff=_sample_cutoff,
    )

    if _use_coarse2fine:
        zv = interface.coarse_to_fine(
            zv,
            mask_temperature=_masktemp * 10,
            sampling_temperature=_sampletemp,
            mask=mask,
            sampling_steps=_num_steps,
            sample_cutoff=_sample_cutoff,
            seed=_seed_val,
        )

    sig = interface.to_signal(zv).cpu()
    print("done")

    print(f"output loudness is {sig.loudness()}")
    sig = sig.normalize(loudness)
    print(f"normalized loudness is {sig.loudness()}")

    sig.write(out_dir / "output.wav")

    if return_mask:
        mask = interface.to_signal(mask_z).cpu()
        mask.write(out_dir / "mask.wav")
        return sig.path_to_file, mask.path_to_file
    else:
        return sig.path_to_file


def _extract_and_call_vamp(data, return_mask):
    return _vamp(
        _input_audio=data[input_audio],
        _num_steps=data[num_steps],
        _masktemp=data[masktemp],
        _sampletemp=data[sampletemp],
        _top_p=data[top_p],
        _prefix_s=data[prefix_s],
        _suffix_s=data[suffix_s],
        _rand_mask_intensity=data[rand_mask_intensity],
        _periodic_p=data[periodic_p],
        _periodic_w=data[periodic_w],
        _n_conditioning_codebooks=data[n_conditioning_codebooks],
        _dropout=data[dropout],
        _use_coarse2fine=data[use_coarse2fine],
        _stretch_factor=data[stretch_factor],
        _onset_mask_width=data[onset_mask_width],
        _typical_filtering=data[typical_filtering],
        _typical_mass=data[typical_mass],
        _typical_min_tokens=data[typical_min_tokens],
        _beat_mask_width=data[beat_mask_width],
        _beat_mask_downbeats=data[beat_mask_downbeats],
        _seed=data[seed],
        _n_mask_codebooks=data[n_mask_codebooks],
        _pitch_shift_amt=data[pitch_shift_amt],
        _sample_cutoff=data[sample_cutoff],
        return_mask=return_mask,
    )


def vamp(data):
    return _extract_and_call_vamp(data, return_mask=True)


def api_vamp(data):
    return _extract_and_call_vamp(data, return_mask=False)


def save_vamp(data):
    out_dir = OUT_DIR / "saved" / str(uuid.uuid4())
    out_dir.mkdir(parents=True, exist_ok=True)

    sig_in = at.AudioSignal(data[input_audio])
    sig_out = at.AudioSignal(data[output_audio])

    sig_in.write(out_dir / "input.wav")
    sig_out.write(out_dir / "output.wav")

    _data = {
        "masktemp": data[masktemp],
        "sampletemp": data[sampletemp],
        "top_p": data[top_p],
        "prefix_s": data[prefix_s],
        "suffix_s": data[suffix_s],
        "rand_mask_intensity": data[rand_mask_intensity],
        "num_steps": data[num_steps],
        "notes": data[notes_text],
        "periodic_period": data[periodic_p],
        "periodic_width": data[periodic_w],
        "n_conditioning_codebooks": data[n_conditioning_codebooks],
        "use_coarse2fine": data[use_coarse2fine],
        "stretch_factor": data[stretch_factor],
        "seed": data[seed],
        "samplecutoff": data[sample_cutoff],
    }

    # save with yaml
    with open(out_dir / "data.yaml", "w") as f:
        yaml.dump(_data, f)

    import zipfile

    zip_path = str(out_dir.with_suffix(".zip"))
    with zipfile.ZipFile(zip_path, "w") as zf:
        for file in out_dir.iterdir():
            zf.write(file, file.name)

    return f"saved! your save code is {out_dir.stem}", zip_path


def harp_vamp(_input_audio, _beat_mask_width, _sampletemp):
    interface.to("cuda")

    out_dir = OUT_DIR / str(uuid.uuid4())
    out_dir.mkdir()
    sig = at.AudioSignal(_input_audio)
    sig = interface.preprocess(sig)

    z = interface.encode(sig)

    # build the mask
    mask = pmask.linear_random(z, 1.0)
    if _beat_mask_width > 0:
        beat_mask = interface.make_beat_mask(
            sig,
            after_beat_s=(_beat_mask_width / 1000),
        )
        mask = pmask.mask_and(mask, beat_mask)

    # save the mask as a txt file
    zv, mask_z = interface.coarse_vamp(
        z,
        mask=mask,
        sampling_temperature=_sampletemp,
        return_mask=True,
        gen_fn=interface.coarse.generate,
    )

    zv = interface.coarse_to_fine(
        zv,
        sampling_temperature=_sampletemp,
        mask=mask,
    )

    sig = interface.to_signal(zv).cpu()
    print("done")

    sig.write(out_dir / "output.wav")

    return sig.path_to_file


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            gr.Markdown("# VampNet Audio Vamping")
            gr.Markdown("""## Description:
            This is a demo of the VampNet, a generative audio model that transforms the input audio based on the chosen settings.
            You can control the extent and nature of variation with a set of manual controls and presets.
            Use this interface to experiment with different mask settings and explore the audio outputs.
            """)

            gr.Markdown("""
            ## Instructions:
            1. You can start by uploading some audio, or by loading the example audio.
            2. Choose a preset for the vamp operation, or manually adjust the controls to customize the mask settings.
            3. Click the "generate (vamp)!!!" button to apply the vamp operation. Listen to the output audio.
            4. Optionally, you can add some notes and save the result.
            5. You can also use the output as the new input and continue experimenting!
            """)
    with gr.Row():
        with gr.Column():
            manual_audio_upload = gr.File(
                label=f"upload some audio (will be randomly trimmed to max of {interface.coarse.chunk_size_s:.2f}s)",
                file_types=["audio"],
            )
            load_example_audio_button = gr.Button("or load example audio")

            input_audio = gr.Audio(
                label="input audio",
                interactive=True,
                type="filepath",
            )

            audio_mask = gr.Audio(
                label="audio mask (listen to this to hear the mask hints)",
                interactive=False,
                type="filepath",
            )

            # connect widgets
            load_example_audio_button.click(
                fn=load_example_audio, inputs=[], outputs=[input_audio]
            )

            manual_audio_upload.change(
                fn=load_audio, inputs=[manual_audio_upload], outputs=[input_audio]
            )

        # mask settings
        with gr.Column():
            presets = {
                "unconditional": {
                    "periodic_p": 0,
                    "onset_mask_width": 0,
                    "beat_mask_width": 0,
                    "beat_mask_downbeats": False,
                },
                "slight periodic variation": {
                    "periodic_p": 5,
                    "onset_mask_width": 5,
                    "beat_mask_width": 0,
                    "beat_mask_downbeats": False,
                },
                "moderate periodic variation": {
                    "periodic_p": 13,
                    "onset_mask_width": 5,
                    "beat_mask_width": 0,
                    "beat_mask_downbeats": False,
                },
                "strong periodic variation": {
                    "periodic_p": 17,
                    "onset_mask_width": 5,
                    "beat_mask_width": 0,
                    "beat_mask_downbeats": False,
                },
                "very strong periodic variation": {
                    "periodic_p": 21,
                    "onset_mask_width": 5,
                    "beat_mask_width": 0,
                    "beat_mask_downbeats": False,
                },
                "beat-driven variation": {
                    "periodic_p": 0,
                    "onset_mask_width": 0,
                    "beat_mask_width": 50,
                    "beat_mask_downbeats": False,
                },
                "beat-driven variation (downbeats only)": {
                    "periodic_p": 0,
                    "onset_mask_width": 0,
                    "beat_mask_width": 50,
                    "beat_mask_downbeats": True,
                },
                "beat-driven variation (downbeats only, strong)": {
                    "periodic_p": 0,
                    "onset_mask_width": 0,
                    "beat_mask_width": 20,
                    "beat_mask_downbeats": True,
                },
            }

            preset = gr.Dropdown(
                label="preset",
                choices=list(presets.keys()),
                value="strong periodic variation",
            )
            load_preset_button = gr.Button("load_preset")

            with gr.Accordion("manual controls", open=True):
                periodic_p = gr.Slider(
                    label="periodic prompt  (0 - unconditional, 2 - lots of hints, 8 - a couple of hints, 16 - occasional hint, 32 - very occasional hint, etc)",
                    minimum=0,
                    maximum=128,
                    step=1,
                    value=3,
                )

                onset_mask_width = gr.Slider(
                    label="onset mask width (multiplies with the periodic mask, 1 step ~= 10milliseconds) ",
                    minimum=0,
                    maximum=100,
                    step=1,
                    value=5,
                )

                beat_mask_width = gr.Slider(
                    label="beat prompt (ms)",
                    minimum=0,
                    maximum=200,
                    value=0,
                )
                beat_mask_downbeats = gr.Checkbox(
                    label="beat mask downbeats only?", value=False
                )

                n_mask_codebooks = gr.Number(
                    label="first upper codebook level to mask",
                    value=9,
                )

                with gr.Accordion("extras ", open=False):
                    pitch_shift_amt = gr.Slider(
                        label="pitch shift amount (semitones)",
                        minimum=-12,
                        maximum=12,
                        step=1,
                        value=0,
                    )

                    rand_mask_intensity = gr.Slider(
                        label="random mask intensity. (If this is less than 1, scatters prompts throughout the audio, should be between 0.9 and 1.0)",
                        minimum=0.0,
                        maximum=1.0,
                        value=1.0,
                    )

                    periodic_w = gr.Slider(
                        label="periodic prompt width (steps, 1 step ~= 10milliseconds)",
                        minimum=1,
                        maximum=20,
                        step=1,
                        value=1,
                    )
                    n_conditioning_codebooks = gr.Number(
                        label="number of conditioning codebooks. probably 0",
                        value=0,
                        precision=0,
                    )

                    stretch_factor = gr.Slider(
                        label="time stretch factor",
                        minimum=0,
                        maximum=64,
                        step=1,
                        value=1,
                    )

            preset_outputs = {
                periodic_p,
                onset_mask_width,
                beat_mask_width,
                beat_mask_downbeats,
            }

            def load_preset(_preset):
                return tuple(presets[_preset].values())

            load_preset_button.click(
                fn=load_preset, inputs=[preset], outputs=preset_outputs
            )

            with gr.Accordion("prefix/suffix prompts", open=False):
                prefix_s = gr.Slider(
                    label="prefix hint length (seconds)",
                    minimum=0.0,
                    maximum=10.0,
                    value=0.0,
                )
                suffix_s = gr.Slider(
                    label="suffix hint length (seconds)",
                    minimum=0.0,
                    maximum=10.0,
                    value=0.0,
                )

            masktemp = gr.Slider(
                label="mask temperature", minimum=0.0, maximum=100.0, value=1.5
            )
            sampletemp = gr.Slider(
                label="sample temperature",
                minimum=0.1,
                maximum=10.0,
                value=1.0,
                step=0.001,
            )

            with gr.Accordion("sampling settings", open=False):
                top_p = gr.Slider(
                    label="top p (0.0 = off)", minimum=0.0, maximum=1.0, value=0.0
                )
                typical_filtering = gr.Checkbox(label="typical filtering ", value=False)
                typical_mass = gr.Slider(
                    label="typical mass (should probably stay between 0.1 and 0.5)",
                    minimum=0.01,
                    maximum=0.99,
                    value=0.15,
                )
                typical_min_tokens = gr.Slider(
                    label="typical min tokens (should probably stay between 1 and 256)",
                    minimum=1,
                    maximum=256,
                    step=1,
                    value=64,
                )
                sample_cutoff = gr.Slider(
                    label="sample cutoff",
                    minimum=0.0,
                    maximum=1.0,
                    value=0.5,
                    step=0.01,
                )

            use_coarse2fine = gr.Checkbox(
                label="use coarse2fine", value=True, visible=False
            )

            num_steps = gr.Slider(
                label="number of steps (should normally be between 12 and 36)",
                minimum=1,
                maximum=128,
                step=1,
                value=36,
            )

            dropout = gr.Slider(
                label="mask dropout", minimum=0.0, maximum=1.0, step=0.01, value=0.0
            )

            seed = gr.Number(
                label="seed (0 for random)",
                value=0,
                precision=0,
            )

        # mask settings
        with gr.Column():
            # lora_choice = gr.Dropdown(
            #     label="lora choice",
            #     choices=list(loras.keys()),
            #     value=LORA_NONE,
            #     visible=False
            # )

            vamp_button = gr.Button("generate (vamp)!!!")
            output_audio = gr.Audio(
                label="output audio", interactive=True, type="filepath"
            )

            notes_text = gr.Textbox(
                label="type any notes about the generated audio here",
                value="",
                interactive=True,
            )
            save_button = gr.Button("save vamp")
            download_file = gr.File(
                label="vamp to download will appear here", interactive=False
            )
            use_as_input_button = gr.Button("use output as input")

            thank_you = gr.Markdown("")

    _inputs = {
        input_audio,
        num_steps,
        masktemp,
        sampletemp,
        top_p,
        prefix_s,
        suffix_s,
        rand_mask_intensity,
        periodic_p,
        periodic_w,
        n_conditioning_codebooks,
        dropout,
        use_coarse2fine,
        stretch_factor,
        onset_mask_width,
        typical_filtering,
        typical_mass,
        typical_min_tokens,
        beat_mask_width,
        beat_mask_downbeats,
        seed,
        # lora_choice,
        n_mask_codebooks,
        pitch_shift_amt,
        sample_cutoff,
    }

    # connect widgets
    vamp_button.click(
        fn=vamp,
        inputs=_inputs,
        outputs=[output_audio, audio_mask],
    )

    api_vamp_button = gr.Button("api vamp", visible=False)
    api_vamp_button.click(
        fn=api_vamp, inputs=_inputs, outputs=[output_audio], api_name="vamp"
    )

    use_as_input_button.click(
        fn=lambda x: x, inputs=[output_audio], outputs=[input_audio]
    )

    save_button.click(
        fn=save_vamp,
        inputs=_inputs | {notes_text, output_audio},
        outputs=[thank_you, download_file],
    )


demo.queue().launch(allowed_paths=[SCRIPT_DIR / "assets"])