Ilzhabimantara commited on
Commit
aebc6db
1 Parent(s): 8b05857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +391 -220
app.py CHANGED
@@ -27,24 +27,34 @@ from vc_infer_pipeline import VC
27
  from config import Config
28
  config = Config()
29
  logging.getLogger("numba").setLevel(logging.WARNING)
30
- limitation = os.getenv("SYSTEM") == "spaces"
 
 
 
 
 
 
31
 
32
  audio_mode = []
33
  f0method_mode = []
34
  f0method_info = ""
35
- if limitation is True:
36
- audio_mode = ["Upload audio", "TTS Audio"]
37
- f0method_mode = ["pm", "crepe", "harvest"]
38
- f0method_info = "PM is fast, rmvpe is middle, Crepe or harvest is good but it was extremely slow (Default: PM)"
 
 
 
 
39
  else:
40
- audio_mode = ["Upload audio", "Youtube", "TTS Audio"]
41
- f0method_mode = ["pm", "crepe", "harvest"]
42
- f0method_info = "PM is fast, rmvpe is middle. Crepe or harvest is good but it was extremely slow (Default: PM))"
43
 
44
  if os.path.isfile("rmvpe.pt"):
45
  f0method_mode.insert(2, "rmvpe")
46
 
47
- def create_vc_fn(model_title, tgt_sr, net_g, vc, if_f0, version, file_index):
48
  def vc_fn(
49
  vc_audio_mode,
50
  vc_input,
@@ -60,6 +70,10 @@ def create_vc_fn(model_title, tgt_sr, net_g, vc, if_f0, version, file_index):
60
  protect,
61
  ):
62
  try:
 
 
 
 
63
  if vc_audio_mode == "Input path" or "Youtube" and vc_input != "":
64
  audio, sr = librosa.load(vc_input, sr=16000, mono=True)
65
  elif vc_audio_mode == "Upload audio":
@@ -67,15 +81,15 @@ def create_vc_fn(model_title, tgt_sr, net_g, vc, if_f0, version, file_index):
67
  return "You need to upload an audio", None
68
  sampling_rate, audio = vc_upload
69
  duration = audio.shape[0] / sampling_rate
70
- if duration > 360 and limitation:
71
- return "Please upload an audio file that is less than 1 minute.", None
72
  audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
73
  if len(audio.shape) > 1:
74
  audio = librosa.to_mono(audio.transpose(1, 0))
75
  if sampling_rate != 16000:
76
  audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
77
  elif vc_audio_mode == "TTS Audio":
78
- if len(tts_text) > 600 and limitation:
79
  return "Text is too long", None
80
  if tts_text is None or tts_voice is None:
81
  return "You need to enter text and select a voice", None
@@ -106,118 +120,121 @@ def create_vc_fn(model_title, tgt_sr, net_g, vc, if_f0, version, file_index):
106
  f0_file=None,
107
  )
108
  info = f"[{datetime.now().strftime('%Y-%m-%d %H:%M')}]: npy: {times[0]}, f0: {times[1]}s, infer: {times[2]}s"
109
- print(f"{model_title} | {info}")
110
- return info, (tgt_sr, audio_opt)
 
111
  except:
112
  info = traceback.format_exc()
113
  print(info)
114
- return info, (None, None)
115
  return vc_fn
116
 
117
  def load_model():
118
  categories = []
119
- with open("weights/folder_info.json", "r", encoding="utf-8") as f:
120
- folder_info = json.load(f)
121
- for category_name, category_info in folder_info.items():
122
- if not category_info['enable']:
123
- continue
124
- category_title = category_info['title']
125
- category_folder = category_info['folder_path']
126
- models = []
127
- with open(f"weights/{category_folder}/model_info.json", "r", encoding="utf-8") as f:
128
- models_info = json.load(f)
129
- for character_name, info in models_info.items():
130
- if not info['enable']:
131
  continue
132
- model_title = info['title']
133
- model_name = info['model_path']
134
- model_author = info.get("author", None)
135
- model_cover = f"weights/{category_folder}/{character_name}/{info['cover']}"
136
- model_index = f"weights/{category_folder}/{character_name}/{info['feature_retrieval_library']}"
137
- cpt = torch.load(f"weights/{category_folder}/{character_name}/{model_name}", map_location="cpu")
138
- tgt_sr = cpt["config"][-1]
139
- cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
140
- if_f0 = cpt.get("f0", 1)
141
- version = cpt.get("version", "v1")
142
- if version == "v1":
143
- if if_f0 == 1:
144
- net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
145
- else:
146
- net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
147
- model_version = "V1"
148
- elif version == "v2":
149
- if if_f0 == 1:
150
- net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  else:
152
- net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
153
- model_version = "V2"
154
- del net_g.enc_q
155
- print(net_g.load_state_dict(cpt["weight"], strict=False))
156
- net_g.eval().to(config.device)
157
- if config.is_half:
158
- net_g = net_g.half()
159
- else:
160
- net_g = net_g.float()
161
- vc = VC(tgt_sr, config)
162
- print(f"Model loaded: {character_name} / {info['feature_retrieval_library']} | ({model_version})")
163
- models.append((character_name, model_title, model_author, model_cover, model_version, create_vc_fn(model_title, tgt_sr, net_g, vc, if_f0, version, model_index)))
164
- categories.append([category_title, category_folder, models])
165
  return categories
166
 
167
- def cut_vocal_and_inst(url, audio_provider, split_model):
168
- if url != "":
169
- if not os.path.exists("dl_audio"):
170
- os.mkdir("dl_audio")
171
- if audio_provider == "Youtube":
172
- ydl_opts = {
 
 
 
 
 
 
173
  'format': 'bestaudio/best',
174
  'postprocessors': [{
175
  'key': 'FFmpegExtractAudio',
176
  'preferredcodec': 'wav',
177
  }],
178
- "outtmpl": 'dl_audio/youtube_audio',
179
- }
180
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
181
- ydl.download([url])
182
- audio_path = "dl_audio/youtube_audio.wav"
183
- else:
184
- # Spotify doesnt work.
185
- # Need to find other solution soon.
186
- '''
187
- command = f"spotdl download {url} --output dl_audio/.wav"
188
- result = subprocess.run(command.split(), stdout=subprocess.PIPE)
189
- print(result.stdout.decode())
190
- audio_path = "dl_audio/spotify_audio.wav"
191
- '''
192
- if split_model == "htdemucs":
193
- command = f"demucs --two-stems=vocals {audio_path} -o output"
194
- result = subprocess.run(command.split(), stdout=subprocess.PIPE)
195
- print(result.stdout.decode())
196
- return "output/htdemucs/youtube_audio/vocals.wav", "output/htdemucs/youtube_audio/no_vocals.wav", audio_path, "output/htdemucs/youtube_audio/vocals.wav"
197
- else:
198
- command = f"demucs --two-stems=vocals -n mdx_extra_q {audio_path} -o output"
199
- result = subprocess.run(command.split(), stdout=subprocess.PIPE)
200
- print(result.stdout.decode())
201
- return "output/mdx_extra_q/youtube_audio/vocals.wav", "output/mdx_extra_q/youtube_audio/no_vocals.wav", audio_path, "output/mdx_extra_q/youtube_audio/vocals.wav"
202
- else:
203
- raise gr.Error("URL Required!")
204
- return None, None, None, None
205
 
206
- def combine_vocal_and_inst(audio_data, audio_volume, split_model):
207
  if not os.path.exists("output/result"):
208
  os.mkdir("output/result")
209
  vocal_path = "output/result/output.wav"
210
  output_path = "output/result/combine.mp3"
211
- if split_model == "htdemucs":
212
- inst_path = "output/htdemucs/youtube_audio/no_vocals.wav"
213
- else:
214
- inst_path = "output/mdx_extra_q/youtube_audio/no_vocals.wav"
215
  with wave.open(vocal_path, "w") as wave_file:
216
  wave_file.setnchannels(1)
217
  wave_file.setsampwidth(2)
218
  wave_file.setframerate(audio_data[0])
219
  wave_file.writeframes(audio_data[1].tobytes())
220
- command = f'ffmpeg -y -i {inst_path} -i {vocal_path} -filter_complex [1:a]volume={audio_volume}dB[v];[0:a][v]amix=inputs=2:duration=longest -b:a 320k -c:a libmp3lame {output_path}'
221
  result = subprocess.run(command.split(), stdout=subprocess.PIPE)
222
  print(result.stdout.decode())
223
  return output_path
@@ -241,16 +258,22 @@ def change_audio_mode(vc_audio_mode):
241
  return (
242
  # Input & Upload
243
  gr.Textbox.update(visible=True),
 
244
  gr.Audio.update(visible=False),
245
  # Youtube
246
  gr.Dropdown.update(visible=False),
247
  gr.Textbox.update(visible=False),
 
 
 
248
  gr.Dropdown.update(visible=False),
 
249
  gr.Button.update(visible=False),
250
  gr.Audio.update(visible=False),
251
  gr.Audio.update(visible=False),
252
  gr.Audio.update(visible=False),
253
  gr.Slider.update(visible=False),
 
254
  gr.Audio.update(visible=False),
255
  gr.Button.update(visible=False),
256
  # TTS
@@ -261,16 +284,22 @@ def change_audio_mode(vc_audio_mode):
261
  return (
262
  # Input & Upload
263
  gr.Textbox.update(visible=False),
 
264
  gr.Audio.update(visible=True),
265
  # Youtube
266
  gr.Dropdown.update(visible=False),
267
  gr.Textbox.update(visible=False),
 
 
 
268
  gr.Dropdown.update(visible=False),
 
269
  gr.Button.update(visible=False),
270
  gr.Audio.update(visible=False),
271
  gr.Audio.update(visible=False),
272
  gr.Audio.update(visible=False),
273
  gr.Slider.update(visible=False),
 
274
  gr.Audio.update(visible=False),
275
  gr.Button.update(visible=False),
276
  # TTS
@@ -281,16 +310,22 @@ def change_audio_mode(vc_audio_mode):
281
  return (
282
  # Input & Upload
283
  gr.Textbox.update(visible=False),
 
284
  gr.Audio.update(visible=False),
285
  # Youtube
286
  gr.Dropdown.update(visible=True),
287
  gr.Textbox.update(visible=True),
 
 
 
288
  gr.Dropdown.update(visible=True),
 
289
  gr.Button.update(visible=True),
290
  gr.Audio.update(visible=True),
291
  gr.Audio.update(visible=True),
292
  gr.Audio.update(visible=True),
293
  gr.Slider.update(visible=True),
 
294
  gr.Audio.update(visible=True),
295
  gr.Button.update(visible=True),
296
  # TTS
@@ -301,61 +336,63 @@ def change_audio_mode(vc_audio_mode):
301
  return (
302
  # Input & Upload
303
  gr.Textbox.update(visible=False),
 
304
  gr.Audio.update(visible=False),
305
  # Youtube
306
  gr.Dropdown.update(visible=False),
307
  gr.Textbox.update(visible=False),
 
 
 
308
  gr.Dropdown.update(visible=False),
 
309
  gr.Button.update(visible=False),
310
  gr.Audio.update(visible=False),
311
  gr.Audio.update(visible=False),
312
  gr.Audio.update(visible=False),
313
  gr.Slider.update(visible=False),
 
314
  gr.Audio.update(visible=False),
315
  gr.Button.update(visible=False),
316
  # TTS
317
  gr.Textbox.update(visible=True),
318
  gr.Dropdown.update(visible=True)
319
  )
 
 
 
 
320
  else:
321
- return (
322
- # Input & Upload
323
- gr.Textbox.update(visible=False),
324
- gr.Audio.update(visible=True),
325
- # Youtube
326
- gr.Dropdown.update(visible=False),
327
- gr.Textbox.update(visible=False),
328
- gr.Dropdown.update(visible=False),
329
- gr.Button.update(visible=False),
330
- gr.Audio.update(visible=False),
331
- gr.Audio.update(visible=False),
332
- gr.Audio.update(visible=False),
333
- gr.Slider.update(visible=False),
334
- gr.Audio.update(visible=False),
335
- gr.Button.update(visible=False),
336
- # TTS
337
- gr.Textbox.update(visible=False),
338
- gr.Dropdown.update(visible=False)
339
- )
340
 
341
  if __name__ == '__main__':
342
  load_hubert()
343
  categories = load_model()
344
- tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
345
  voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
346
- with gr.Blocks(theme=gr.themes.Base()) as app:
347
  gr.Markdown(
348
- "# <center> RVC Models\n"
349
- "### <center> Recommended to use Google Colab to use other character and feature.\n"
350
- "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aziib/hololive-rvc-models-v2/blob/main/hololive_rvc_models_v2.ipynb)\n\n"
351
- "[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/megaaziib)\n\n"
 
 
352
  )
353
- for (folder_title, folder, models) in categories:
 
 
 
 
 
 
354
  with gr.TabItem(folder_title):
 
 
355
  with gr.Tabs():
356
  if not models:
357
  gr.Markdown("# <center> No Model Loaded.")
358
- gr.Markdown("## <center> Please add model or fix your model path.")
359
  continue
360
  for (name, title, author, cover, model_version, vc_fn) in models:
361
  with gr.TabItem(name):
@@ -369,95 +406,217 @@ if __name__ == '__main__':
369
  '</div>'
370
  )
371
  with gr.Row():
372
- with gr.Column():
373
- vc_audio_mode = gr.Dropdown(label="Input voice", choices=audio_mode, allow_custom_value=False, value="Upload audio")
374
- # Input and Upload
375
- vc_input = gr.Textbox(label="Input audio path", visible=False)
376
- vc_upload = gr.Audio(label="Upload audio file", visible=True, interactive=True)
377
- # Youtube
378
- vc_download_audio = gr.Dropdown(label="Provider", choices=["Youtube"], allow_custom_value=False, visible=False, value="Youtube", info="Select provider (Default: Youtube)")
379
- vc_link = gr.Textbox(label="Youtube URL", visible=False, info="Example: https://www.youtube.com/watch?v=Nc0sB1Bmf-A", placeholder="https://www.youtube.com/watch?v=...")
380
- vc_split_model = gr.Dropdown(label="Splitter Model", choices=["htdemucs", "mdx_extra_q"], allow_custom_value=False, visible=False, value="htdemucs", info="Select the splitter model (Default: htdemucs)")
381
- vc_split = gr.Button("Split Audio", variant="primary", visible=False)
382
- vc_vocal_preview = gr.Audio(label="Vocal Preview", visible=False)
383
- vc_inst_preview = gr.Audio(label="Instrumental Preview", visible=False)
384
- vc_audio_preview = gr.Audio(label="Audio Preview", visible=False)
385
- # TTS
386
- tts_text = gr.Textbox(visible=False, label="TTS text", info="Text to speech input")
387
- tts_voice = gr.Dropdown(label="Edge-tts speaker", choices=voices, visible=False, allow_custom_value=False, value="en-US-AnaNeural-Female")
388
- with gr.Column():
389
- vc_transform0 = gr.Number(label="Transpose", value=0, info='Type "12" to change from male to female voice. Type "-12" to change female to male voice')
390
- f0method0 = gr.Radio(
391
- label="Pitch extraction algorithm",
392
- info=f0method_info,
393
- choices=f0method_mode,
394
- value="pm",
395
- interactive=True
396
- )
397
- index_rate1 = gr.Slider(
398
- minimum=0,
399
- maximum=1,
400
- label="Retrieval feature ratio",
401
- info="Accents controling. Too high prob gonna sounds too robotic (Default: 0.4)",
402
- value=0.4,
403
- interactive=True,
404
- )
405
- filter_radius0 = gr.Slider(
406
- minimum=0,
407
- maximum=7,
408
- label="Apply Median Filtering",
409
- info="The value represents the filter radius and can reduce breathiness.",
410
- value=1,
411
- step=1,
412
- interactive=True,
413
- )
414
- resample_sr0 = gr.Slider(
415
- minimum=0,
416
- maximum=48000,
417
- label="Resample the output audio",
418
- info="Resample the output audio in post-processing to the final sample rate. Set to 0 for no resampling",
419
- value=0,
420
- step=1,
421
- interactive=True,
422
- )
423
- rms_mix_rate0 = gr.Slider(
424
- minimum=0,
425
- maximum=1,
426
- label="Volume Envelope",
427
- info="Use the volume envelope of the input to replace or mix with the volume envelope of the output. The closer the ratio is to 1, the more the output envelope is used",
428
- value=1,
429
- interactive=True,
430
- )
431
- protect0 = gr.Slider(
432
- minimum=0,
433
- maximum=0.5,
434
- label="Voice Protection",
435
- info="Protect voiceless consonants and breath sounds to prevent artifacts such as tearing in electronic music. Set to 0.5 to disable. Decrease the value to increase protection, but it may reduce indexing accuracy",
436
- value=0.23,
437
- step=0.01,
438
- interactive=True,
439
- )
440
- with gr.Column():
441
- vc_log = gr.Textbox(label="Output Information", interactive=False)
442
- vc_output = gr.Audio(label="Output Audio", interactive=False)
443
- vc_convert = gr.Button("Convert", variant="primary")
444
- vc_volume = gr.Slider(
445
- minimum=0,
446
- maximum=10,
447
- label="Vocal volume",
448
- value=4,
449
- interactive=True,
450
- step=1,
451
- info="Adjust vocal volume (Default: 4}",
452
- visible=False
453
- )
454
- vc_combined_output = gr.Audio(label="Output Combined Audio", visible=False)
455
- vc_combine = gr.Button("Combine",variant="primary", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
  vc_convert.click(
457
  fn=vc_fn,
458
  inputs=[
459
  vc_audio_mode,
460
- vc_input,
461
  vc_upload,
462
  tts_text,
463
  tts_voice,
@@ -471,37 +630,49 @@ if __name__ == '__main__':
471
  ],
472
  outputs=[vc_log ,vc_output]
473
  )
 
 
 
 
 
474
  vc_split.click(
475
  fn=cut_vocal_and_inst,
476
- inputs=[vc_link, vc_download_audio, vc_split_model],
477
- outputs=[vc_vocal_preview, vc_inst_preview, vc_audio_preview, vc_input]
478
  )
479
  vc_combine.click(
480
  fn=combine_vocal_and_inst,
481
- inputs=[vc_output, vc_volume, vc_split_model],
482
  outputs=[vc_combined_output]
483
  )
 
 
 
 
 
484
  vc_audio_mode.change(
485
  fn=change_audio_mode,
486
  inputs=[vc_audio_mode],
487
  outputs=[
488
- vc_input,
 
489
  vc_upload,
490
  vc_download_audio,
491
  vc_link,
 
 
492
  vc_split_model,
 
493
  vc_split,
 
494
  vc_vocal_preview,
495
  vc_inst_preview,
496
- vc_audio_preview,
497
- vc_volume,
498
  vc_combined_output,
499
  vc_combine,
500
  tts_text,
501
  tts_voice
502
  ]
503
  )
504
- if limitation is True:
505
- app.queue(concurrency_count=1, max_size=20, api_open=config.api).launch(share=config.colab)
506
- else:
507
- app.queue(concurrency_count=1, max_size=20, api_open=config.api).launch(share=True)
 
27
  from config import Config
28
  config = Config()
29
  logging.getLogger("numba").setLevel(logging.WARNING)
30
+ spaces = os.getenv("SYSTEM") == "spaces"
31
+ force_support = None
32
+ if config.unsupported is False:
33
+ if config.device == "mps" or config.device == "cpu":
34
+ force_support = False
35
+ else:
36
+ force_support = True
37
 
38
  audio_mode = []
39
  f0method_mode = []
40
  f0method_info = ""
41
+
42
+ if force_support is False or spaces is True:
43
+ if spaces is True:
44
+ audio_mode = ["Upload audio", "TTS Audio"]
45
+ else:
46
+ audio_mode = ["Input path", "Upload audio", "TTS Audio"]
47
+ f0method_mode = ["pm", "harvest"]
48
+ f0method_info = "PM is fast, Harvest is good but extremely slow, Rvmpe is alternative to harvest (might be better). (Default: PM)"
49
  else:
50
+ audio_mode = ["Input path", "Upload audio", "Youtube", "TTS Audio"]
51
+ f0method_mode = ["pm", "harvest", "crepe"]
52
+ f0method_info = "PM is fast, Harvest is good but extremely slow, Rvmpe is alternative to harvest (might be better), and Crepe effect is good but requires GPU (Default: PM)"
53
 
54
  if os.path.isfile("rmvpe.pt"):
55
  f0method_mode.insert(2, "rmvpe")
56
 
57
+ def create_vc_fn(model_name, tgt_sr, net_g, vc, if_f0, version, file_index):
58
  def vc_fn(
59
  vc_audio_mode,
60
  vc_input,
 
70
  protect,
71
  ):
72
  try:
73
+ logs = []
74
+ print(f"Converting using {model_name}...")
75
+ logs.append(f"Converting using {model_name}...")
76
+ yield "\n".join(logs), None
77
  if vc_audio_mode == "Input path" or "Youtube" and vc_input != "":
78
  audio, sr = librosa.load(vc_input, sr=16000, mono=True)
79
  elif vc_audio_mode == "Upload audio":
 
81
  return "You need to upload an audio", None
82
  sampling_rate, audio = vc_upload
83
  duration = audio.shape[0] / sampling_rate
84
+ if duration > 20 and spaces:
85
+ return "Please upload an audio file that is less than 20 seconds. If you need to generate a longer audio file, please use Colab.", None
86
  audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
87
  if len(audio.shape) > 1:
88
  audio = librosa.to_mono(audio.transpose(1, 0))
89
  if sampling_rate != 16000:
90
  audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
91
  elif vc_audio_mode == "TTS Audio":
92
+ if len(tts_text) > 100 and spaces:
93
  return "Text is too long", None
94
  if tts_text is None or tts_voice is None:
95
  return "You need to enter text and select a voice", None
 
120
  f0_file=None,
121
  )
122
  info = f"[{datetime.now().strftime('%Y-%m-%d %H:%M')}]: npy: {times[0]}, f0: {times[1]}s, infer: {times[2]}s"
123
+ print(f"{model_name} | {info}")
124
+ logs.append(f"Successfully Convert {model_name}\n{info}")
125
+ yield "\n".join(logs), (tgt_sr, audio_opt)
126
  except:
127
  info = traceback.format_exc()
128
  print(info)
129
+ yield info, None
130
  return vc_fn
131
 
132
  def load_model():
133
  categories = []
134
+ if os.path.isfile("weights/folder_info.json"):
135
+ with open("weights/folder_info.json", "r", encoding="utf-8") as f:
136
+ folder_info = json.load(f)
137
+ for category_name, category_info in folder_info.items():
138
+ if not category_info['enable']:
 
 
 
 
 
 
 
139
  continue
140
+ category_title = category_info['title']
141
+ category_folder = category_info['folder_path']
142
+ description = category_info['description']
143
+ models = []
144
+ with open(f"weights/{category_folder}/model_info.json", "r", encoding="utf-8") as f:
145
+ models_info = json.load(f)
146
+ for character_name, info in models_info.items():
147
+ if not info['enable']:
148
+ continue
149
+ model_title = info['title']
150
+ model_name = info['model_path']
151
+ model_author = info.get("author", None)
152
+ model_cover = f"weights/{category_folder}/{character_name}/{info['cover']}"
153
+ model_index = f"weights/{category_folder}/{character_name}/{info['feature_retrieval_library']}"
154
+ cpt = torch.load(f"weights/{category_folder}/{character_name}/{model_name}", map_location="cpu")
155
+ tgt_sr = cpt["config"][-1]
156
+ cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
157
+ if_f0 = cpt.get("f0", 1)
158
+ version = cpt.get("version", "v1")
159
+ if version == "v1":
160
+ if if_f0 == 1:
161
+ net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
162
+ else:
163
+ net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
164
+ model_version = "V1"
165
+ elif version == "v2":
166
+ if if_f0 == 1:
167
+ net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)
168
+ else:
169
+ net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
170
+ model_version = "V2"
171
+ del net_g.enc_q
172
+ print(net_g.load_state_dict(cpt["weight"], strict=False))
173
+ net_g.eval().to(config.device)
174
+ if config.is_half:
175
+ net_g = net_g.half()
176
  else:
177
+ net_g = net_g.float()
178
+ vc = VC(tgt_sr, config)
179
+ print(f"Model loaded: {character_name} / {info['feature_retrieval_library']} | ({model_version})")
180
+ models.append((character_name, model_title, model_author, model_cover, model_version, create_vc_fn(model_name, tgt_sr, net_g, vc, if_f0, version, model_index)))
181
+ categories.append([category_title, category_folder, description, models])
182
+ else:
183
+ categories = []
 
 
 
 
 
 
184
  return categories
185
 
186
+ def download_audio(url, audio_provider):
187
+ logs = []
188
+ if url == "":
189
+ raise gr.Error("URL Required!")
190
+ return "URL Required"
191
+ if not os.path.exists("dl_audio"):
192
+ os.mkdir("dl_audio")
193
+ if audio_provider == "Youtube":
194
+ logs.append("Downloading the audio...")
195
+ yield None, "\n".join(logs)
196
+ ydl_opts = {
197
+ 'noplaylist': True,
198
  'format': 'bestaudio/best',
199
  'postprocessors': [{
200
  'key': 'FFmpegExtractAudio',
201
  'preferredcodec': 'wav',
202
  }],
203
+ "outtmpl": 'dl_audio/audio',
204
+ }
205
+ audio_path = "dl_audio/audio.wav"
206
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
207
+ ydl.download([url])
208
+ logs.append("Download Complete.")
209
+ yield audio_path, "\n".join(logs)
210
+
211
+ def cut_vocal_and_inst(split_model):
212
+ logs = []
213
+ logs.append("Starting the audio splitting process...")
214
+ yield "\n".join(logs), None, None, None, None
215
+ command = f"demucs --two-stems=vocals -n {split_model} dl_audio/audio.wav -o output"
216
+ result = subprocess.Popen(command.split(), stdout=subprocess.PIPE, text=True)
217
+ for line in result.stdout:
218
+ logs.append(line)
219
+ yield "\n".join(logs), None, None, None, None
220
+ print(result.stdout)
221
+ vocal = f"output/{split_model}/audio/vocals.wav"
222
+ inst = f"output/{split_model}/audio/no_vocals.wav"
223
+ logs.append("Audio splitting complete.")
224
+ yield "\n".join(logs), vocal, inst, vocal
 
 
 
 
 
225
 
226
+ def combine_vocal_and_inst(audio_data, vocal_volume, inst_volume, split_model):
227
  if not os.path.exists("output/result"):
228
  os.mkdir("output/result")
229
  vocal_path = "output/result/output.wav"
230
  output_path = "output/result/combine.mp3"
231
+ inst_path = f"output/{split_model}/audio/no_vocals.wav"
 
 
 
232
  with wave.open(vocal_path, "w") as wave_file:
233
  wave_file.setnchannels(1)
234
  wave_file.setsampwidth(2)
235
  wave_file.setframerate(audio_data[0])
236
  wave_file.writeframes(audio_data[1].tobytes())
237
+ command = f'ffmpeg -y -i {inst_path} -i {vocal_path} -filter_complex [0:a]volume={inst_volume}[i];[1:a]volume={vocal_volume}[v];[i][v]amix=inputs=2:duration=longest[a] -map [a] -b:a 320k -c:a libmp3lame {output_path}'
238
  result = subprocess.run(command.split(), stdout=subprocess.PIPE)
239
  print(result.stdout.decode())
240
  return output_path
 
258
  return (
259
  # Input & Upload
260
  gr.Textbox.update(visible=True),
261
+ gr.Checkbox.update(visible=False),
262
  gr.Audio.update(visible=False),
263
  # Youtube
264
  gr.Dropdown.update(visible=False),
265
  gr.Textbox.update(visible=False),
266
+ gr.Textbox.update(visible=False),
267
+ gr.Button.update(visible=False),
268
+ # Splitter
269
  gr.Dropdown.update(visible=False),
270
+ gr.Textbox.update(visible=False),
271
  gr.Button.update(visible=False),
272
  gr.Audio.update(visible=False),
273
  gr.Audio.update(visible=False),
274
  gr.Audio.update(visible=False),
275
  gr.Slider.update(visible=False),
276
+ gr.Slider.update(visible=False),
277
  gr.Audio.update(visible=False),
278
  gr.Button.update(visible=False),
279
  # TTS
 
284
  return (
285
  # Input & Upload
286
  gr.Textbox.update(visible=False),
287
+ gr.Checkbox.update(visible=True),
288
  gr.Audio.update(visible=True),
289
  # Youtube
290
  gr.Dropdown.update(visible=False),
291
  gr.Textbox.update(visible=False),
292
+ gr.Textbox.update(visible=False),
293
+ gr.Button.update(visible=False),
294
+ # Splitter
295
  gr.Dropdown.update(visible=False),
296
+ gr.Textbox.update(visible=False),
297
  gr.Button.update(visible=False),
298
  gr.Audio.update(visible=False),
299
  gr.Audio.update(visible=False),
300
  gr.Audio.update(visible=False),
301
  gr.Slider.update(visible=False),
302
+ gr.Slider.update(visible=False),
303
  gr.Audio.update(visible=False),
304
  gr.Button.update(visible=False),
305
  # TTS
 
310
  return (
311
  # Input & Upload
312
  gr.Textbox.update(visible=False),
313
+ gr.Checkbox.update(visible=False),
314
  gr.Audio.update(visible=False),
315
  # Youtube
316
  gr.Dropdown.update(visible=True),
317
  gr.Textbox.update(visible=True),
318
+ gr.Textbox.update(visible=True),
319
+ gr.Button.update(visible=True),
320
+ # Splitter
321
  gr.Dropdown.update(visible=True),
322
+ gr.Textbox.update(visible=True),
323
  gr.Button.update(visible=True),
324
  gr.Audio.update(visible=True),
325
  gr.Audio.update(visible=True),
326
  gr.Audio.update(visible=True),
327
  gr.Slider.update(visible=True),
328
+ gr.Slider.update(visible=True),
329
  gr.Audio.update(visible=True),
330
  gr.Button.update(visible=True),
331
  # TTS
 
336
  return (
337
  # Input & Upload
338
  gr.Textbox.update(visible=False),
339
+ gr.Checkbox.update(visible=False),
340
  gr.Audio.update(visible=False),
341
  # Youtube
342
  gr.Dropdown.update(visible=False),
343
  gr.Textbox.update(visible=False),
344
+ gr.Textbox.update(visible=False),
345
+ gr.Button.update(visible=False),
346
+ # Splitter
347
  gr.Dropdown.update(visible=False),
348
+ gr.Textbox.update(visible=False),
349
  gr.Button.update(visible=False),
350
  gr.Audio.update(visible=False),
351
  gr.Audio.update(visible=False),
352
  gr.Audio.update(visible=False),
353
  gr.Slider.update(visible=False),
354
+ gr.Slider.update(visible=False),
355
  gr.Audio.update(visible=False),
356
  gr.Button.update(visible=False),
357
  # TTS
358
  gr.Textbox.update(visible=True),
359
  gr.Dropdown.update(visible=True)
360
  )
361
+
362
+ def use_microphone(microphone):
363
+ if microphone == True:
364
+ return gr.Audio.update(source="microphone")
365
  else:
366
+ return gr.Audio.update(source="upload")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
  if __name__ == '__main__':
369
  load_hubert()
370
  categories = load_model()
371
+ tts_voice_list = asyncio.new_event_loop().run_until_complete(edge_tts.list_voices())
372
  voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
373
+ with gr.Blocks() as app:
374
  gr.Markdown(
375
+ "<div align='center'>\n\n"+
376
+ "# RVC Models\n\n"+
377
+ "### Recommended to use Google Colab to use other character and feature.\n\n"+
378
+ "</div>\n\n"+
379
+ "[![Repository](https://img.shields.io/badge/Github-Multi%20Model%20RVC%20Inference-blue?style=for-the-badge&logo=github)](https://github.com/ArkanDash/Multi-Model-RVC-Inference)\n\n"+
380
+ "</div>"
381
  )
382
+ if categories == []:
383
+ gr.Markdown(
384
+ "<div align='center'>\n\n"+
385
+ "## No model found, please add the model into weights folder\n\n"+
386
+ "</div>"
387
+ )
388
+ for (folder_title, folder, description, models) in categories:
389
  with gr.TabItem(folder_title):
390
+ if description:
391
+ gr.Markdown(f"### <center> {description}")
392
  with gr.Tabs():
393
  if not models:
394
  gr.Markdown("# <center> No Model Loaded.")
395
+ gr.Markdown("## <center> Please add the model or fix your model path.")
396
  continue
397
  for (name, title, author, cover, model_version, vc_fn) in models:
398
  with gr.TabItem(name):
 
406
  '</div>'
407
  )
408
  with gr.Row():
409
+ if spaces is False:
410
+ with gr.TabItem("Input"):
411
+ with gr.Row():
412
+ with gr.Column():
413
+ vc_audio_mode = gr.Dropdown(label="Input voice", choices=audio_mode, allow_custom_value=False, value="Upload audio")
414
+ # Input
415
+ vc_input = gr.Textbox(label="Input audio path", visible=False)
416
+ # Upload
417
+ vc_microphone_mode = gr.Checkbox(label="Use Microphone", value=False, visible=True, interactive=True)
418
+ vc_upload = gr.Audio(label="Upload audio file", source="upload", visible=True, interactive=True)
419
+ # Youtube
420
+ vc_download_audio = gr.Dropdown(label="Provider", choices=["Youtube"], allow_custom_value=False, visible=False, value="Youtube", info="Select provider (Default: Youtube)")
421
+ vc_link = gr.Textbox(label="Youtube URL", visible=False, info="Example: https://www.youtube.com/watch?v=Nc0sB1Bmf-A", placeholder="https://www.youtube.com/watch?v=...")
422
+ vc_log_yt = gr.Textbox(label="Output Information", visible=False, interactive=False)
423
+ vc_download_button = gr.Button("Download Audio", variant="primary", visible=False)
424
+ vc_audio_preview = gr.Audio(label="Audio Preview", visible=False)
425
+ # TTS
426
+ tts_text = gr.Textbox(label="TTS text", info="Text to speech input", visible=False)
427
+ tts_voice = gr.Dropdown(label="Edge-tts speaker", choices=voices, visible=False, allow_custom_value=False, value="en-US-AnaNeural-Female")
428
+ with gr.Column():
429
+ vc_split_model = gr.Dropdown(label="Splitter Model", choices=["hdemucs_mmi", "htdemucs", "htdemucs_ft", "mdx", "mdx_q", "mdx_extra_q"], allow_custom_value=False, visible=False, value="htdemucs", info="Select the splitter model (Default: htdemucs)")
430
+ vc_split_log = gr.Textbox(label="Output Information", visible=False, interactive=False)
431
+ vc_split = gr.Button("Split Audio", variant="primary", visible=False)
432
+ vc_vocal_preview = gr.Audio(label="Vocal Preview", visible=False)
433
+ vc_inst_preview = gr.Audio(label="Instrumental Preview", visible=False)
434
+ with gr.TabItem("Convert"):
435
+ with gr.Row():
436
+ with gr.Column():
437
+ vc_transform0 = gr.Number(label="Transpose", value=0, info='Type "12" to change from male to female voice. Type "-12" to change female to male voice')
438
+ f0method0 = gr.Radio(
439
+ label="Pitch extraction algorithm",
440
+ info=f0method_info,
441
+ choices=f0method_mode,
442
+ value="pm",
443
+ interactive=True
444
+ )
445
+ index_rate1 = gr.Slider(
446
+ minimum=0,
447
+ maximum=1,
448
+ label="Retrieval feature ratio",
449
+ info="(Default: 0.7)",
450
+ value=0.7,
451
+ interactive=True,
452
+ )
453
+ filter_radius0 = gr.Slider(
454
+ minimum=0,
455
+ maximum=7,
456
+ label="Apply Median Filtering",
457
+ info="The value represents the filter radius and can reduce breathiness.",
458
+ value=3,
459
+ step=1,
460
+ interactive=True,
461
+ )
462
+ resample_sr0 = gr.Slider(
463
+ minimum=0,
464
+ maximum=48000,
465
+ label="Resample the output audio",
466
+ info="Resample the output audio in post-processing to the final sample rate. Set to 0 for no resampling",
467
+ value=0,
468
+ step=1,
469
+ interactive=True,
470
+ )
471
+ rms_mix_rate0 = gr.Slider(
472
+ minimum=0,
473
+ maximum=1,
474
+ label="Volume Envelope",
475
+ info="Use the volume envelope of the input to replace or mix with the volume envelope of the output. The closer the ratio is to 1, the more the output envelope is used",
476
+ value=1,
477
+ interactive=True,
478
+ )
479
+ protect0 = gr.Slider(
480
+ minimum=0,
481
+ maximum=0.5,
482
+ label="Voice Protection",
483
+ info="Protect voiceless consonants and breath sounds to prevent artifacts such as tearing in electronic music. Set to 0.5 to disable. Decrease the value to increase protection, but it may reduce indexing accuracy",
484
+ value=0.5,
485
+ step=0.01,
486
+ interactive=True,
487
+ )
488
+ with gr.Column():
489
+ vc_log = gr.Textbox(label="Output Information", interactive=False)
490
+ vc_output = gr.Audio(label="Output Audio", interactive=False)
491
+ vc_convert = gr.Button("Convert", variant="primary")
492
+ vc_vocal_volume = gr.Slider(
493
+ minimum=0,
494
+ maximum=10,
495
+ label="Vocal volume",
496
+ value=1,
497
+ interactive=True,
498
+ step=1,
499
+ info="Adjust vocal volume (Default: 1}",
500
+ visible=False
501
+ )
502
+ vc_inst_volume = gr.Slider(
503
+ minimum=0,
504
+ maximum=10,
505
+ label="Instrument volume",
506
+ value=1,
507
+ interactive=True,
508
+ step=1,
509
+ info="Adjust instrument volume (Default: 1}",
510
+ visible=False
511
+ )
512
+ vc_combined_output = gr.Audio(label="Output Combined Audio", visible=False)
513
+ vc_combine = gr.Button("Combine",variant="primary", visible=False)
514
+ else:
515
+ with gr.Column():
516
+ vc_audio_mode = gr.Dropdown(label="Input voice", choices=audio_mode, allow_custom_value=False, value="Upload audio")
517
+ # Input
518
+ vc_input = gr.Textbox(label="Input audio path", visible=False)
519
+ # Upload
520
+ vc_microphone_mode = gr.Checkbox(label="Use Microphone", value=False, visible=True, interactive=True)
521
+ vc_upload = gr.Audio(label="Upload audio file", source="upload", visible=True, interactive=True)
522
+ # Youtube
523
+ vc_download_audio = gr.Dropdown(label="Provider", choices=["Youtube"], allow_custom_value=False, visible=False, value="Youtube", info="Select provider (Default: Youtube)")
524
+ vc_link = gr.Textbox(label="Youtube URL", visible=False, info="Example: https://www.youtube.com/watch?v=Nc0sB1Bmf-A", placeholder="https://www.youtube.com/watch?v=...")
525
+ vc_log_yt = gr.Textbox(label="Output Information", visible=False, interactive=False)
526
+ vc_download_button = gr.Button("Download Audio", variant="primary", visible=False)
527
+ vc_audio_preview = gr.Audio(label="Audio Preview", visible=False)
528
+ # Splitter
529
+ vc_split_model = gr.Dropdown(label="Splitter Model", choices=["hdemucs_mmi", "htdemucs", "htdemucs_ft", "mdx", "mdx_q", "mdx_extra_q"], allow_custom_value=False, visible=False, value="htdemucs", info="Select the splitter model (Default: htdemucs)")
530
+ vc_split_log = gr.Textbox(label="Output Information", visible=False, interactive=False)
531
+ vc_split = gr.Button("Split Audio", variant="primary", visible=False)
532
+ vc_vocal_preview = gr.Audio(label="Vocal Preview", visible=False)
533
+ vc_inst_preview = gr.Audio(label="Instrumental Preview", visible=False)
534
+ # TTS
535
+ tts_text = gr.Textbox(label="TTS text", info="Text to speech input", visible=False)
536
+ tts_voice = gr.Dropdown(label="Edge-tts speaker", choices=voices, visible=False, allow_custom_value=False, value="en-US-AnaNeural-Female")
537
+ with gr.Column():
538
+ vc_transform0 = gr.Number(label="Transpose", value=0, info='Type "12" to change from male to female voice. Type "-12" to change female to male voice')
539
+ f0method0 = gr.Radio(
540
+ label="Pitch extraction algorithm",
541
+ info=f0method_info,
542
+ choices=f0method_mode,
543
+ value="pm",
544
+ interactive=True
545
+ )
546
+ index_rate1 = gr.Slider(
547
+ minimum=0,
548
+ maximum=1,
549
+ label="Retrieval feature ratio",
550
+ info="(Default: 0.7)",
551
+ value=0.7,
552
+ interactive=True,
553
+ )
554
+ filter_radius0 = gr.Slider(
555
+ minimum=0,
556
+ maximum=7,
557
+ label="Apply Median Filtering",
558
+ info="The value represents the filter radius and can reduce breathiness.",
559
+ value=3,
560
+ step=1,
561
+ interactive=True,
562
+ )
563
+ resample_sr0 = gr.Slider(
564
+ minimum=0,
565
+ maximum=48000,
566
+ label="Resample the output audio",
567
+ info="Resample the output audio in post-processing to the final sample rate. Set to 0 for no resampling",
568
+ value=0,
569
+ step=1,
570
+ interactive=True,
571
+ )
572
+ rms_mix_rate0 = gr.Slider(
573
+ minimum=0,
574
+ maximum=1,
575
+ label="Volume Envelope",
576
+ info="Use the volume envelope of the input to replace or mix with the volume envelope of the output. The closer the ratio is to 1, the more the output envelope is used",
577
+ value=1,
578
+ interactive=True,
579
+ )
580
+ protect0 = gr.Slider(
581
+ minimum=0,
582
+ maximum=0.5,
583
+ label="Voice Protection",
584
+ info="Protect voiceless consonants and breath sounds to prevent artifacts such as tearing in electronic music. Set to 0.5 to disable. Decrease the value to increase protection, but it may reduce indexing accuracy",
585
+ value=0.5,
586
+ step=0.01,
587
+ interactive=True,
588
+ )
589
+ with gr.Column():
590
+ vc_log = gr.Textbox(label="Output Information", interactive=False)
591
+ vc_output = gr.Audio(label="Output Audio", interactive=False)
592
+ vc_convert = gr.Button("Convert", variant="primary")
593
+ vc_vocal_volume = gr.Slider(
594
+ minimum=0,
595
+ maximum=10,
596
+ label="Vocal volume",
597
+ value=1,
598
+ interactive=True,
599
+ step=1,
600
+ info="Adjust vocal volume (Default: 1}",
601
+ visible=False
602
+ )
603
+ vc_inst_volume = gr.Slider(
604
+ minimum=0,
605
+ maximum=10,
606
+ label="Instrument volume",
607
+ value=1,
608
+ interactive=True,
609
+ step=1,
610
+ info="Adjust instrument volume (Default: 1}",
611
+ visible=False
612
+ )
613
+ vc_combined_output = gr.Audio(label="Output Combined Audio", visible=False)
614
+ vc_combine = gr.Button("Combine",variant="primary", visible=False)
615
  vc_convert.click(
616
  fn=vc_fn,
617
  inputs=[
618
  vc_audio_mode,
619
+ vc_input,
620
  vc_upload,
621
  tts_text,
622
  tts_voice,
 
630
  ],
631
  outputs=[vc_log ,vc_output]
632
  )
633
+ vc_download_button.click(
634
+ fn=download_audio,
635
+ inputs=[vc_link, vc_download_audio],
636
+ outputs=[vc_audio_preview, vc_log_yt]
637
+ )
638
  vc_split.click(
639
  fn=cut_vocal_and_inst,
640
+ inputs=[vc_split_model],
641
+ outputs=[vc_split_log, vc_vocal_preview, vc_inst_preview, vc_input]
642
  )
643
  vc_combine.click(
644
  fn=combine_vocal_and_inst,
645
+ inputs=[vc_output, vc_vocal_volume, vc_inst_volume, vc_split_model],
646
  outputs=[vc_combined_output]
647
  )
648
+ vc_microphone_mode.change(
649
+ fn=use_microphone,
650
+ inputs=vc_microphone_mode,
651
+ outputs=vc_upload
652
+ )
653
  vc_audio_mode.change(
654
  fn=change_audio_mode,
655
  inputs=[vc_audio_mode],
656
  outputs=[
657
+ vc_input,
658
+ vc_microphone_mode,
659
  vc_upload,
660
  vc_download_audio,
661
  vc_link,
662
+ vc_log_yt,
663
+ vc_download_button,
664
  vc_split_model,
665
+ vc_split_log,
666
  vc_split,
667
+ vc_audio_preview,
668
  vc_vocal_preview,
669
  vc_inst_preview,
670
+ vc_vocal_volume,
671
+ vc_inst_volume,
672
  vc_combined_output,
673
  vc_combine,
674
  tts_text,
675
  tts_voice
676
  ]
677
  )
678
+ app.queue(concurrency_count=1, max_size=20, api_open=config.api).launch(share=config.colab)