liuhuadai commited on
Commit
31531e8
1 Parent(s): ef1f417

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -18
app.py CHANGED
@@ -24,6 +24,7 @@ from vocoder.bigvgan.models import VocoderBigVGAN
24
  import soundfile
25
  # from pytorch_memlab import LineProfiler,profile
26
  import gradio
 
27
 
28
  def load_model_from_config(config, ckpt = None, verbose=True):
29
  model = instantiate_from_config(config.model)
@@ -50,7 +51,7 @@ def load_model_from_config(config, ckpt = None, verbose=True):
50
 
51
 
52
  class GenSamples:
53
- def __init__(self,sampler,model,outpath,vocoder = None,save_mel = True,save_wav = True, original_inference_steps=None) -> None:
54
  self.sampler = sampler
55
  self.model = model
56
  self.outpath = outpath
@@ -61,29 +62,33 @@ class GenSamples:
61
  self.save_wav = save_wav
62
  self.channel_dim = self.model.channels
63
  self.original_inference_steps = original_inference_steps
 
 
 
64
 
65
  def gen_test_sample(self,prompt,mel_name = None,wav_name = None):# prompt is {'ori_caption':’xxx‘,'struct_caption':'xxx'}
66
  uc = None
67
  record_dicts = []
68
  # if os.path.exists(os.path.join(self.outpath,mel_name+f'_0.npy')):
69
  # return record_dicts
70
- emptycap = {'ori_caption':1*[""],'struct_caption':1*[""]}
71
- uc = self.model.get_learned_conditioning(emptycap)
 
72
 
73
  for n in range(1):# trange(self.opt.n_iter, desc="Sampling"):
74
  for k,v in prompt.items():
75
- prompt[k] = 1 * [v]
76
  c = self.model.get_learned_conditioning(prompt)# shape:[1,77,1280],即还没有变成句子embedding,仍是每个单词的embedding
77
  if self.channel_dim>0:
78
  shape = [self.channel_dim, 20, 312] # (z_dim, 80//2^x, 848//2^x)
79
  else:
80
  shape = [20, 312]
81
- samples_ddim, _ = self.sampler.sample(S=2,
82
  conditioning=c,
83
- batch_size=1,
84
  shape=shape,
85
  verbose=False,
86
- guidance_scale=5,
87
  original_inference_steps=self.original_inference_steps
88
  )
89
  x_samples_ddim = self.model.decode_first_stage(samples_ddim)
@@ -103,7 +108,9 @@ class GenSamples:
103
  return record_dicts
104
 
105
  @spaces.GPU(enable_queue=True)
106
- def infer(ori_prompt):
 
 
107
 
108
  prompt = dict(ori_caption=ori_prompt,struct_caption=f'<{ori_prompt}& all>')
109
 
@@ -124,7 +131,7 @@ def infer(ori_prompt):
124
  vocoder = VocoderBigVGAN("./model/vocoder",device)
125
 
126
 
127
- generator = GenSamples(sampler,model,"results/test",vocoder,save_mel = False,save_wav = True, original_inference_steps=config.model.params.num_ddim_timesteps)
128
  csv_dicts = []
129
 
130
  with torch.no_grad():
@@ -135,15 +142,61 @@ def infer(ori_prompt):
135
  print(f"Your samples are ready and waiting four you here: \nresults/test \nEnjoy.")
136
  return "results/test/"+wav_name+"_0.wav"
137
 
138
- def my_inference_function(text_prompt):
139
- file_path = infer(text_prompt)
140
  return file_path
141
 
142
 
143
-
144
- gradio_interface = gradio.Interface(
145
- fn = my_inference_function,
146
- inputs = "text",
147
- outputs = "audio"
148
- )
149
- gradio_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  import soundfile
25
  # from pytorch_memlab import LineProfiler,profile
26
  import gradio
27
+ import gradio as gr
28
 
29
  def load_model_from_config(config, ckpt = None, verbose=True):
30
  model = instantiate_from_config(config.model)
 
51
 
52
 
53
  class GenSamples:
54
+ def __init__(self,sampler,model,outpath,vocoder = None,save_mel = True,save_wav = True, original_inference_steps=None, ddim_steps=2, scale=5, num_samples=1) -> None:
55
  self.sampler = sampler
56
  self.model = model
57
  self.outpath = outpath
 
62
  self.save_wav = save_wav
63
  self.channel_dim = self.model.channels
64
  self.original_inference_steps = original_inference_steps
65
+ self.ddim_steps = ddim_steps
66
+ self.scale = scale
67
+ self.num_samples = num_samples
68
 
69
  def gen_test_sample(self,prompt,mel_name = None,wav_name = None):# prompt is {'ori_caption':’xxx‘,'struct_caption':'xxx'}
70
  uc = None
71
  record_dicts = []
72
  # if os.path.exists(os.path.join(self.outpath,mel_name+f'_0.npy')):
73
  # return record_dicts
74
+ if self.scale != 1.0:
75
+ emptycap = {'ori_caption':self.num_samples*[""],'struct_caption':self.num_samples*[""]}
76
+ uc = self.model.get_learned_conditioning(emptycap)
77
 
78
  for n in range(1):# trange(self.opt.n_iter, desc="Sampling"):
79
  for k,v in prompt.items():
80
+ prompt[k] = self.num_samples * [v]
81
  c = self.model.get_learned_conditioning(prompt)# shape:[1,77,1280],即还没有变成句子embedding,仍是每个单词的embedding
82
  if self.channel_dim>0:
83
  shape = [self.channel_dim, 20, 312] # (z_dim, 80//2^x, 848//2^x)
84
  else:
85
  shape = [20, 312]
86
+ samples_ddim, _ = self.sampler.sample(S=self.ddim_steps,
87
  conditioning=c,
88
+ batch_size=self.num_samples,
89
  shape=shape,
90
  verbose=False,
91
+ guidance_scale=self.scale,
92
  original_inference_steps=self.original_inference_steps
93
  )
94
  x_samples_ddim = self.model.decode_first_stage(samples_ddim)
 
108
  return record_dicts
109
 
110
  @spaces.GPU(enable_queue=True)
111
+ def infer(ori_prompt, ddim_steps, num_samples, scale, seed):
112
+ np.random.seed(seed)
113
+ torch.manual_seed(seed)
114
 
115
  prompt = dict(ori_caption=ori_prompt,struct_caption=f'<{ori_prompt}& all>')
116
 
 
131
  vocoder = VocoderBigVGAN("./model/vocoder",device)
132
 
133
 
134
+ generator = GenSamples(sampler,model,"results/test",vocoder,save_mel = False,save_wav = True, original_inference_steps=config.model.params.num_ddim_timesteps, ddim_steps=ddim_steps, scale=scale, num_samples=num_samples)
135
  csv_dicts = []
136
 
137
  with torch.no_grad():
 
142
  print(f"Your samples are ready and waiting four you here: \nresults/test \nEnjoy.")
143
  return "results/test/"+wav_name+"_0.wav"
144
 
145
+ def my_inference_function(text_prompt, ddim_steps, num_samples, scale, seed):
146
+ file_path = infer(text_prompt, ddim_steps, num_samples, scale, seed)
147
  return file_path
148
 
149
 
150
+ with gr.Blocks() as demo:
151
+ with gr.Row():
152
+ tgr.Markdown("## AudioLCM:Text-to-Audio Generation with Latent Consistency Models")
153
+
154
+ with gr.Row():
155
+ with gr.Column():
156
+ prompt = gr.Textbox(label="Prompt: Input your text here. ")
157
+ run_button = gr.Button(label="Run")
158
+
159
+ with gr.Accordion("Advanced options", open=False):
160
+ num_samples = gr.Slider(
161
+ label="Select from audios num.This number control the number of candidates \
162
+ (e.g., generate three audios and choose the best to show you). A Larger value usually lead to \
163
+ better quality with heavier computation", minimum=1, maximum=10, value=1, step=1)
164
+ # num_samples = 1
165
+ ddim_steps = gr.Slider(label="Steps", minimum=1,
166
+ maximum=150, value=2, step=1)
167
+ scale = gr.Slider(
168
+ label="Guidance Scale:(Large => more relevant to text but the quality may drop)", minimum=0.1, maximum=8.0, value=5.0, step=0.1
169
+ )
170
+ seed = gr.Slider(
171
+ label="Seed:Change this value (any integer number) will lead to a different generation result.",
172
+ minimum=0,
173
+ maximum=2147483647,
174
+ step=1,
175
+ value=44,
176
+ )
177
+
178
+ with gr.Column():
179
+ outaudio = gr.Audio()
180
+
181
+ run_button.click(fn=my_inference_function, inputs=[
182
+ prompt,ddim_steps, num_samples, scale, seed], outputs=[outaudio])
183
+ with gr.Row():
184
+ with gr.Column():
185
+ gr.Examples(
186
+ examples = [['a dog barking and a bird chirping',100,3,3,55],['Pigeons peck, coo, and flap their wings before a man speaks',100,3,3,55],
187
+ ['music of violin and piano',100,3,2,88],['wind thunder and rain falling',100,3,3,55],['music made by drum kit',100,3,3,55]],
188
+ inputs = [prompt,ddim_steps, num_samples, scale, seed],
189
+ outputs = [outaudio]
190
+ )
191
+ with gr.Column():
192
+ pass
193
+
194
+ demo.launch()
195
+
196
+
197
+ # gradio_interface = gradio.Interface(
198
+ # fn = my_inference_function,
199
+ # inputs = "text",
200
+ # outputs = "audio"
201
+ # )
202
+ # gradio_interface.launch()