Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
import soundfile as sf
|
7 |
+
from diffusers import DDPMScheduler
|
8 |
+
from pico_model import PicoDiffusion, build_pretrained_models
|
9 |
+
|
10 |
+
class dotdict(dict):
|
11 |
+
"""dot.notation access to dictionary attributes"""
|
12 |
+
__getattr__ = dict.get
|
13 |
+
__setattr__ = dict.__setitem__
|
14 |
+
__delattr__ = dict.__delitem__
|
15 |
+
|
16 |
+
class InferRunner:
|
17 |
+
def __init__(self):
|
18 |
+
self.vae, _ = build_pretrained_models("audioldm-s-full")
|
19 |
+
train_args = dotdict(json.loads(open("ckpts/pico_model/summary.jsonl").readlines()[0]))
|
20 |
+
self.pico_model = PicoDiffusion(
|
21 |
+
scheduler_name=train_args.scheduler_name,
|
22 |
+
unet_model_config_path=train_args.unet_model_config,
|
23 |
+
snr_gamma=train_args.snr_gamma,
|
24 |
+
freeze_text_encoder_ckpt="ckpts/laion_clap/630k-audioset-best.pt",
|
25 |
+
diffusion_pt="ckpts/pico_model/diffusion.pt",
|
26 |
+
).cuda().eval()
|
27 |
+
self.scheduler = DDPMScheduler.from_pretrained(train_args.scheduler_name, subfolder="scheduler")
|
28 |
+
|
29 |
+
def infer(caption, runner):
|
30 |
+
with torch.no_grad():
|
31 |
+
latents = runner.picomodel.demo_inference(caption, runner.scheduler, num_steps=200, guidance=3.0, num_samples=1, audio_len=16000*10, disable_progress=True)
|
32 |
+
mel = runner.vae.decode_first_stage(latents)
|
33 |
+
wave = runner.vae.decode_to_waveform(mel)[0][:audio_len]
|
34 |
+
sf.write(f"synthesized/{caption}.wav", wave, samplerate=16000, subtype='PCM_16')
|
35 |
+
|
36 |
+
infer_runner = InferRunner()
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
with gr.Row():
|
40 |
+
gr.Markdown("## PicoAudio")
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
with gr.Column():
|
44 |
+
prompt = gr.Textbox(label="Prompt: Input your caption formatted as 'event1 at onset1-offset1_onset2-offset2 and event2 at onset1-offset1.",
|
45 |
+
value="spraying at 0.38-1.176_3.06-3.856 and gunshot at 1.729-3.729_4.367-6.367_7.031-9.031.")
|
46 |
+
run_button = gr.Button()
|
47 |
+
|
48 |
+
with gr.Accordion("Advanced options", open=False):
|
49 |
+
num_steps = gr.Slider(label="num_steps", minimum=1,
|
50 |
+
maximum=300, value=200, step=1)
|
51 |
+
guidance = gr.Slider(
|
52 |
+
label="Guidance Scale:(Large => more relevant to text but the quality may drop)", minimum=0.1, maximum=8.0, value=3.0, step=0.1
|
53 |
+
)
|
54 |
+
|
55 |
+
with gr.Column():
|
56 |
+
outaudio = gr.Audio()
|
57 |
+
|
58 |
+
run_button.click(fn=infer, inputs=[
|
59 |
+
prompt, num_steps, guidance], outputs=[outaudio])
|
60 |
+
# with gr.Row():
|
61 |
+
# with gr.Column():
|
62 |
+
# gr.Examples(
|
63 |
+
# examples = [['An amateur recording features a steel drum playing in a higher register',25,5,55],
|
64 |
+
# ['An instrumental song with a caribbean feel, happy mood, and featuring steel pan music, programmed percussion, and bass',25,5,55],
|
65 |
+
# ['This musical piece features a playful and emotionally melodic male vocal accompanied by piano',25,5,55],
|
66 |
+
# ['A eerie yet calming experimental electronic track featuring haunting synthesizer strings and pads',25,5,55],
|
67 |
+
# ['A slow tempo pop instrumental piece featuring only acoustic guitar with fingerstyle and percussive strumming techniques',25,5,55]],
|
68 |
+
# inputs = [prompt, ddim_steps, scale, seed],
|
69 |
+
# outputs = [outaudio]
|
70 |
+
# )
|
71 |
+
# with gr.Column():
|
72 |
+
# pass
|
73 |
+
|
74 |
+
demo.launch()
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|