Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- .gitignore +4 -0
- README.md +4 -4
- app.py +170 -0
- requirements.txt +20 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
./useful_ckpts
|
2 |
+
*.pyc
|
3 |
+
__pycache__
|
4 |
+
./not_finished
|
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
title: Make An Audio Inpaint
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
1 |
---
|
2 |
title: Make An Audio Inpaint
|
3 |
+
emoji: 🔥
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.17.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import matplotlib
|
6 |
+
from omegaconf import OmegaConf
|
7 |
+
from einops import repeat
|
8 |
+
import librosa
|
9 |
+
from ldm.models.diffusion.ddim import DDIMSampler
|
10 |
+
from vocoder.bigvgan.models import VocoderBigVGAN
|
11 |
+
from ldm.util import instantiate_from_config
|
12 |
+
from ldm.data.extract_mel_spectrogram import TRANSFORMS_16000
|
13 |
+
|
14 |
+
SAMPLE_RATE = 16000
|
15 |
+
cmap_transform = matplotlib.cm.viridis
|
16 |
+
torch.set_grad_enabled(False)
|
17 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
18 |
+
|
19 |
+
def initialize_model(config, ckpt):
|
20 |
+
config = OmegaConf.load(config)
|
21 |
+
model = instantiate_from_config(config.model)
|
22 |
+
model.load_state_dict(torch.load(ckpt,map_location='cpu')["state_dict"], strict=False)
|
23 |
+
|
24 |
+
model = model.to(device)
|
25 |
+
print(model.device,device,model.cond_stage_model.device)
|
26 |
+
sampler = DDIMSampler(model)
|
27 |
+
return sampler
|
28 |
+
|
29 |
+
|
30 |
+
def make_batch_sd(
|
31 |
+
mel,
|
32 |
+
mask,
|
33 |
+
device,
|
34 |
+
num_samples=1):
|
35 |
+
|
36 |
+
mel = torch.from_numpy(mel)[None,None,...].to(dtype=torch.float32)
|
37 |
+
mask = torch.from_numpy(mask)[None,None,...].to(dtype=torch.float32)
|
38 |
+
masked_mel = (1 - mask) * mel
|
39 |
+
|
40 |
+
mel = mel * 2 - 1
|
41 |
+
mask = mask * 2 - 1
|
42 |
+
masked_mel = masked_mel * 2 -1
|
43 |
+
|
44 |
+
batch = {
|
45 |
+
"mel": repeat(mel.to(device=device), "1 ... -> n ...", n=num_samples),
|
46 |
+
"mask": repeat(mask.to(device=device), "1 ... -> n ...", n=num_samples),
|
47 |
+
"masked_mel": repeat(masked_mel.to(device=device), "1 ... -> n ...", n=num_samples),
|
48 |
+
}
|
49 |
+
return batch
|
50 |
+
|
51 |
+
def gen_mel(input_audio):
|
52 |
+
sr,ori_wav = input_audio
|
53 |
+
print(sr,ori_wav.shape,ori_wav)
|
54 |
+
|
55 |
+
ori_wav = ori_wav.astype(np.float32, order='C') / 32768.0 # order='C'是以C语言格式存储,不用管
|
56 |
+
if len(ori_wav.shape)==2:# stereo
|
57 |
+
ori_wav = librosa.to_mono(ori_wav.T)# gradio load wav shape could be (wav_len,2) but librosa expects (2,wav_len)
|
58 |
+
print(sr,ori_wav.shape,ori_wav)
|
59 |
+
ori_wav = librosa.resample(ori_wav,orig_sr = sr,target_sr = SAMPLE_RATE)
|
60 |
+
|
61 |
+
mel_len,hop_size = 848,256
|
62 |
+
input_len = mel_len * hop_size
|
63 |
+
if len(ori_wav) < input_len:
|
64 |
+
input_wav = np.pad(ori_wav,(0,mel_len*hop_size),constant_values=0)
|
65 |
+
else:
|
66 |
+
input_wav = ori_wav[:input_len]
|
67 |
+
|
68 |
+
mel = TRANSFORMS_16000(input_wav)
|
69 |
+
return mel
|
70 |
+
|
71 |
+
def show_mel_fn(input_audio):
|
72 |
+
crop_len = 500 # the full mel cannot be showed due to gradio's Image bug when using tool='sketch'
|
73 |
+
crop_mel = gen_mel(input_audio)[:,:crop_len]
|
74 |
+
color_mel = cmap_transform(crop_mel)
|
75 |
+
return Image.fromarray((color_mel*255).astype(np.uint8))
|
76 |
+
|
77 |
+
|
78 |
+
def inpaint(sampler, batch, seed, ddim_steps, num_samples=1, W=512, H=512):
|
79 |
+
model = sampler.model
|
80 |
+
|
81 |
+
prng = np.random.RandomState(seed)
|
82 |
+
start_code = prng.randn(num_samples, model.first_stage_model.embed_dim, H // 8, W // 8)
|
83 |
+
start_code = torch.from_numpy(start_code).to(device=device, dtype=torch.float32)
|
84 |
+
|
85 |
+
c = model.get_first_stage_encoding(model.encode_first_stage(batch["masked_mel"]))
|
86 |
+
cc = torch.nn.functional.interpolate(batch["mask"],
|
87 |
+
size=c.shape[-2:])
|
88 |
+
c = torch.cat((c, cc), dim=1) # (b,c+1,h,w) 1 is mask
|
89 |
+
|
90 |
+
shape = (c.shape[1]-1,)+c.shape[2:]
|
91 |
+
samples_ddim, _ = sampler.sample(S=ddim_steps,
|
92 |
+
conditioning=c,
|
93 |
+
batch_size=c.shape[0],
|
94 |
+
shape=shape,
|
95 |
+
verbose=False)
|
96 |
+
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
97 |
+
|
98 |
+
|
99 |
+
mask = batch["mask"]# [-1,1]
|
100 |
+
mel = torch.clamp((batch["mel"]+1.0)/2.0,min=0.0, max=1.0)
|
101 |
+
mask = torch.clamp((batch["mask"]+1.0)/2.0,min=0.0, max=1.0)
|
102 |
+
predicted_mel = torch.clamp((x_samples_ddim+1.0)/2.0,min=0.0, max=1.0)
|
103 |
+
inpainted = (1-mask)*mel+mask*predicted_mel
|
104 |
+
inpainted = inpainted.cpu().numpy().squeeze()
|
105 |
+
inapint_wav = vocoder.vocode(inpainted)
|
106 |
+
|
107 |
+
return inpainted,inapint_wav
|
108 |
+
|
109 |
+
|
110 |
+
def predict(input_audio,mel_and_mask,ddim_steps,seed):
|
111 |
+
show_mel = np.array(mel_and_mask['image'].convert("L"))/255 # 由于展示的mel只展示了一部分,所以需要重新从音频生成mel
|
112 |
+
mask = np.array(mel_and_mask["mask"].convert("L"))/255
|
113 |
+
|
114 |
+
mel_bins,mel_len = 80,848
|
115 |
+
|
116 |
+
input_mel = gen_mel(input_audio)[:,:mel_len]# 由于展示的mel只展示了一部分,所以需要重新从音频生成mel
|
117 |
+
mask = np.pad(mask,((0,0),(0,mel_len-mask.shape[1])),mode='constant',constant_values=0)# 将mask填充到原来的mel的大小
|
118 |
+
print(mask.shape,input_mel.shape)
|
119 |
+
with torch.no_grad():
|
120 |
+
batch = make_batch_sd(input_mel,mask,device,num_samples=1)
|
121 |
+
inpainted,gen_wav = inpaint(
|
122 |
+
sampler=sampler,
|
123 |
+
batch=batch,
|
124 |
+
seed=seed,
|
125 |
+
ddim_steps=ddim_steps,
|
126 |
+
num_samples=1,
|
127 |
+
H=mel_bins, W=mel_len
|
128 |
+
)
|
129 |
+
inpainted = inpainted[:,:show_mel.shape[1]]
|
130 |
+
color_mel = cmap_transform(inpainted)
|
131 |
+
input_len = int(input_audio[1].shape[0] * SAMPLE_RATE / input_audio[0])
|
132 |
+
gen_wav = (gen_wav * 32768).astype(np.int16)[:input_len]
|
133 |
+
return Image.fromarray((color_mel*255).astype(np.uint8)),(SAMPLE_RATE,gen_wav)
|
134 |
+
|
135 |
+
|
136 |
+
sampler = initialize_model('./configs/inpaint/txt2audio_args.yaml', './useful_ckpts/inpaint7_epoch00047.ckpt')
|
137 |
+
vocoder = VocoderBigVGAN('./vocoder/logs/bigv16k53w',device=device)
|
138 |
+
|
139 |
+
block = gr.Blocks().queue()
|
140 |
+
with block:
|
141 |
+
with gr.Row():
|
142 |
+
gr.Markdown("## Make-An-Audio Inpainting")
|
143 |
+
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column():
|
146 |
+
input_audio = gr.inputs.Audio()
|
147 |
+
|
148 |
+
show_button = gr.Button("Show Mel")
|
149 |
+
|
150 |
+
run_button = gr.Button("Predict Masked Place")
|
151 |
+
with gr.Accordion("Advanced options", open=False):
|
152 |
+
ddim_steps = gr.Slider(label="Steps", minimum=1,
|
153 |
+
maximum=150, value=100, step=1)
|
154 |
+
seed = gr.Slider(
|
155 |
+
label="Seed",
|
156 |
+
minimum=0,
|
157 |
+
maximum=2147483647,
|
158 |
+
step=1,
|
159 |
+
randomize=True,
|
160 |
+
)
|
161 |
+
with gr.Column():
|
162 |
+
show_inpainted = gr.Image(type="pil").style(width=848,height=80)
|
163 |
+
outaudio = gr.Audio()
|
164 |
+
show_mel = gr.Image(type="pil",tool='sketch')#.style(width=848,height=80) # 加上这个没办法展示完全图片
|
165 |
+
show_button.click(fn=show_mel_fn, inputs=[input_audio], outputs=show_mel)
|
166 |
+
|
167 |
+
run_button.click(fn=predict, inputs=[input_audio,show_mel,ddim_steps,seed], outputs=[show_inpainted,outaudio])
|
168 |
+
|
169 |
+
|
170 |
+
block.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
2 |
+
torch
|
3 |
+
torch-fidelity==0.3.0
|
4 |
+
scipy
|
5 |
+
matplotlib
|
6 |
+
torchaudio>=0.13.0
|
7 |
+
torchvision>=0.14.0
|
8 |
+
tqdm
|
9 |
+
omegaconf
|
10 |
+
einops
|
11 |
+
numpy<=1.23.5
|
12 |
+
soundfile
|
13 |
+
librosa
|
14 |
+
pandas
|
15 |
+
# transformers
|
16 |
+
torchlibrosa
|
17 |
+
transformers
|
18 |
+
ftfy
|
19 |
+
pytorch-lightning==1.5.9
|
20 |
+
# -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
|