smjain commited on
Commit
ce66dde
1 Parent(s): 0d425b8

Upload infer_new.py

Browse files
Files changed (1) hide show
  1. infer_new.py +4 -188
infer_new.py CHANGED
@@ -318,126 +318,10 @@ def get_vc(sid, to_return_protect0):
318
  )
319
  )
320
 
321
- def find_audio_files(folder_path, extensions):
322
- audio_files = []
323
- for root, dirs, files in os.walk(folder_path):
324
- for file in files:
325
- if any(file.endswith(ext) for ext in extensions):
326
- audio_files.append(file)
327
- return audio_files
328
-
329
- def vc_multi(
330
- spk_item,
331
- vc_input,
332
- vc_output,
333
- vc_transform0,
334
- f0method0,
335
- file_index,
336
- index_rate,
337
- filter_radius,
338
- resample_sr,
339
- rms_mix_rate,
340
- protect,
341
- ):
342
- global tgt_sr, net_g, vc, hubert_model, version, cpt
343
- logs = []
344
- logs.append("Converting...")
345
- yield "\n".join(logs)
346
- print()
347
- try:
348
- if os.path.exists(vc_input):
349
- folder_path = vc_input
350
- extensions = [".mp3", ".wav", ".flac", ".ogg"]
351
- audio_files = find_audio_files(folder_path, extensions)
352
- for index, file in enumerate(audio_files, start=1):
353
- audio, sr = librosa.load(os.path.join(folder_path, file), sr=16000, mono=True)
354
- input_audio_path = folder_path, file
355
- f0_up_key = int(vc_transform0)
356
- times = [0, 0, 0]
357
- if hubert_model == None:
358
- load_hubert()
359
- if_f0 = cpt.get("f0", 1)
360
- audio_opt = vc.pipeline(
361
- hubert_model,
362
- net_g,
363
- spk_item,
364
- audio,
365
- input_audio_path,
366
- times,
367
- f0_up_key,
368
- f0method0,
369
- file_index,
370
- index_rate,
371
- if_f0,
372
- filter_radius,
373
- tgt_sr,
374
- resample_sr,
375
- rms_mix_rate,
376
- version,
377
- protect,
378
- f0_file=None
379
- )
380
- if resample_sr >= 16000 and tgt_sr != resample_sr:
381
- tgt_sr = resample_sr
382
- output_path = f"{os.path.join(vc_output, file)}"
383
- os.makedirs(os.path.join(vc_output), exist_ok=True)
384
- sf.write(
385
- output_path,
386
- audio_opt,
387
- tgt_sr,
388
- )
389
- info = f"{index} / {len(audio_files)} | {file}"
390
- print(info)
391
- logs.append(info)
392
- yield "\n".join(logs)
393
- else:
394
- logs.append("Folder not found or path doesn't exist.")
395
- yield "\n".join(logs)
396
- except:
397
- info = traceback.format_exc()
398
- print(info)
399
- logs.append(info)
400
- yield "\n".join(logs)
401
-
402
- def download_audio(url, audio_provider):
403
- logs = []
404
- os.makedirs("dl_audio", exist_ok=True)
405
- if url == "":
406
- logs.append("URL required!")
407
- yield None, "\n".join(logs)
408
- return None, "\n".join(logs)
409
- if audio_provider == "Youtube":
410
- logs.append("Downloading the audio...")
411
- yield None, "\n".join(logs)
412
- ydl_opts = {
413
- 'noplaylist': True,
414
- 'format': 'bestaudio/best',
415
- 'postprocessors': [{
416
- 'key': 'FFmpegExtractAudio',
417
- 'preferredcodec': 'wav',
418
- }],
419
- "outtmpl": 'result/dl_audio/audio',
420
- }
421
- audio_path = "result/dl_audio/audio.wav"
422
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
423
- ydl.download([url])
424
- logs.append("Download Complete.")
425
- yield audio_path, "\n".join(logs)
426
-
427
- def cut_vocal_and_inst_yt(split_model,spk_id):
428
- logs = []
429
- logs.append("Starting the audio splitting process...")
430
- yield "\n".join(logs), None, None, None
431
- command = f"demucs --two-stems=vocals -n {split_model} result/dl_audio/audio.wav -o output"
432
- result = subprocess.Popen(command.split(), stdout=subprocess.PIPE, text=True)
433
- for line in result.stdout:
434
- logs.append(line)
435
- yield "\n".join(logs), None, None, None
436
- print(result.stdout)
437
- vocal = f"output/{split_model}/{spk_id}_input_audio/vocals.wav"
438
- inst = f"output/{split_model}/{spk_id}_input_audio/no_vocals.wav"
439
- logs.append("Audio splitting complete.")
440
- yield "\n".join(logs), vocal, inst, vocal
441
 
442
  def cut_vocal_and_inst(audio_path,spk_id):
443
 
@@ -497,73 +381,5 @@ def combine_vocal_and_inst(vocal_path, inst_path):
497
  #print(result.stdout.decode())
498
  #return output_path
499
 
500
- def download_and_extract_models(urls):
501
- logs = []
502
- os.makedirs("zips", exist_ok=True)
503
- os.makedirs(os.path.join("zips", "extract"), exist_ok=True)
504
- os.makedirs(os.path.join(weight_root), exist_ok=True)
505
- os.makedirs(os.path.join(index_root), exist_ok=True)
506
- for link in urls.splitlines():
507
- url = link.strip()
508
- if not url:
509
- raise gr.Error("URL Required!")
510
- return "No URLs provided."
511
- model_zip = urlparse(url).path.split('/')[-2] + '.zip'
512
- model_zip_path = os.path.join('zips', model_zip)
513
- logs.append(f"Downloading...")
514
- yield "\n".join(logs)
515
- if "drive.google.com" in url:
516
- gdown.download(url, os.path.join("zips", "extract"), quiet=False)
517
- elif "mega.nz" in url:
518
- m = Mega()
519
- m.download_url(url, 'zips')
520
- else:
521
- os.system(f"wget {url} -O {model_zip_path}")
522
- logs.append(f"Extracting...")
523
- yield "\n".join(logs)
524
- for filename in os.listdir("zips"):
525
- archived_file = os.path.join("zips", filename)
526
- if filename.endswith(".zip"):
527
- shutil.unpack_archive(archived_file, os.path.join("zips", "extract"), 'zip')
528
- elif filename.endswith(".rar"):
529
- with rarfile.RarFile(archived_file, 'r') as rar:
530
- rar.extractall(os.path.join("zips", "extract"))
531
- for _, dirs, files in os.walk(os.path.join("zips", "extract")):
532
- logs.append(f"Searching Model and Index...")
533
- yield "\n".join(logs)
534
- model = False
535
- index = False
536
- if files:
537
- for file in files:
538
- if file.endswith(".pth"):
539
- basename = file[:-4]
540
- shutil.move(os.path.join("zips", "extract", file), os.path.join(weight_root, file))
541
- model = True
542
- if file.endswith('.index') and "trained" not in file:
543
- shutil.move(os.path.join("zips", "extract", file), os.path.join(index_root, file))
544
- index = True
545
- else:
546
- logs.append("No model in main folder.")
547
- yield "\n".join(logs)
548
- logs.append("Searching in subfolders...")
549
- yield "\n".join(logs)
550
- for sub_dir in dirs:
551
- for _, _, sub_files in os.walk(os.path.join("zips", "extract", sub_dir)):
552
- for file in sub_files:
553
- if file.endswith(".pth"):
554
- basename = file[:-4]
555
- shutil.move(os.path.join("zips", "extract", sub_dir, file), os.path.join(weight_root, file))
556
- model = True
557
- if file.endswith('.index') and "trained" not in file:
558
- shutil.move(os.path.join("zips", "extract", sub_dir, file), os.path.join(index_root, file))
559
- index = True
560
- shutil.rmtree(os.path.join("zips", "extract", sub_dir))
561
- if index is False:
562
- logs.append("Model only file, no Index file detected.")
563
- yield "\n".join(logs)
564
- logs.append("Download Completed!")
565
- yield "\n".join(logs)
566
- logs.append("Successfully download all models! Refresh your model list to load the model")
567
- yield "\n".join(logs)
568
  if __name__ == '__main__':
569
  app.run(debug=False, port=5000,host='0.0.0.0')
 
318
  )
319
  )
320
 
321
+
322
+
323
+
324
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
 
326
  def cut_vocal_and_inst(audio_path,spk_id):
327
 
 
381
  #print(result.stdout.decode())
382
  #return output_path
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  if __name__ == '__main__':
385
  app.run(debug=False, port=5000,host='0.0.0.0')