File size: 3,313 Bytes
516059f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pyharp import ModelCard, build_endpoint, save_and_return_filepath
from audiotools import AudioSignal
from audioldm import build_model, style_transfer
import gradio as gr
import soundfile as sf

audioldm = build_model(model_name="audioldm-m-full")

def save_wave(waveform, savepath, name="outwav"):
    if type(name) is not list:
        name = [name] * waveform.shape[0]

    for i in range(waveform.shape[0]):
        path = os.path.join(
            savepath,
            "%s_%s.wav"
            % (
                os.path.basename(name[i])
                if (not ".wav" in name[i])
                else os.path.basename(name[i]).split(".")[0],
                i,
            ),
        )
        print("Save audio to %s" % path)
        sf.write(path, waveform[i, 0], samplerate=16000)

def process_fn(input_audio_path, prompt, seed, guidance_scale, num_inference_steps, num_candidates, audio_length_in_s, transfer_strength):
    waveform = style_transfer(
        audioldm,
        prompt,
        audio_file,
        transfer_strength,
        int(seed),
        duration = audio_length_in_s,
        guidance_scale = guidance_scale,
        ddim_steps = int(num_inference_steps),
        batchsize = int(num_candidates),
        config=None,
    )
    waveform = waveform[:,None,:]
    sf.write("./output.wav", waveform[0, 0], samplerate=16000)
    
    #save_wave(waveform, "./", name="output.wav")  #broken, always appends _0.wav
    return "./output.wav"


card = ModelCard(
    name='AudioLDM Variations',
    description='AudioLDM Variation Generator, operates on region selected in track.',
    author='Team Audio',
    tags=['AudioLDM', 'Variations', 'audio-to-audio']
)

with gr.Blocks() as webapp:
    # Define your Gradio interface
    inputs = [
        gr.Audio(
            label="Audio Input", 
            type="filepath"
        ), 
        gr.Slider(
            label="seed",
            minimum="0",
            maximum="65535",
            value="43534",
            step="1"
        ),
        gr.Slider(
            minimum=0, maximum=10, 
            step=0.1, value=2.5, 
            label="Guidance Scale"
        ),
        gr.Slider(
            minimum=1, maximum=500, 
            step=1, value=200, 
            label="Inference Steps"
        ),
        gr.Slider(
            minimum=1, maximum=10, 
            step=1, value=1, 
            label="Candidates"
        ),
        gr.Slider(
            minimum=2.5, maximum=10.0, 
            step=2.5, value=5, 
            label="Duration"
        ),
    ]

    output = gr.Audio(label="Audio Output", type="filepath")

    ctrls_data, ctrls_button, process_button, cancel_button = build_endpoint(inputs, output, process_fn, card)

# queue the webapp: https://www.gradio.app/guides/setting-up-a-demo-for-maximum-performance
#webapp.queue()
webapp.launch(share=True)



for audio_file in input_files:
    waveform = style_transfer(
        audioldm,
        PROMPT,
        audio_file,
        TRANSFER_STRENGTH,
        SEED,
        duration = DURATION,
        guidance_scale = GUIDANCE_SCALE,
        ddim_steps = STEPS,
        batchsize = N_VARIATIONS,
        config=None,
    )
    waveform = waveform[:,None,:]
    save_wave(waveform, OUTPUT_DIRECTORY, name=f"{os.path.basename(audio_file)}_STYLE_{PROMPT}")