webui.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import os
  2. import traceback,gradio as gr
  3. import logging
  4. from tools.i18n.i18n import I18nAuto
  5. i18n = I18nAuto()
  6. logger = logging.getLogger(__name__)
  7. import librosa,ffmpeg
  8. import soundfile as sf
  9. import torch
  10. import sys
  11. from mdxnet import MDXNetDereverb
  12. from vr import AudioPre, AudioPreDeEcho
  13. weight_uvr5_root = "tools/uvr5/uvr5_weights"
  14. uvr5_names = []
  15. for name in os.listdir(weight_uvr5_root):
  16. if name.endswith(".pth") or "onnx" in name:
  17. uvr5_names.append(name.replace(".pth", ""))
  18. device=sys.argv[1]
  19. is_half=eval(sys.argv[2])
  20. webui_port_uvr5=int(sys.argv[3])
  21. is_share=eval(sys.argv[4])
  22. def uvr(model_name, inp_root, save_root_vocal, paths, save_root_ins, agg, format0):
  23. infos = []
  24. try:
  25. inp_root = inp_root.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
  26. save_root_vocal = (
  27. save_root_vocal.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
  28. )
  29. save_root_ins = (
  30. save_root_ins.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
  31. )
  32. is_hp3 = "HP3" in model_name
  33. if model_name == "onnx_dereverb_By_FoxJoy":
  34. pre_fun = MDXNetDereverb(15)
  35. else:
  36. func = AudioPre if "DeEcho" not in model_name else AudioPreDeEcho
  37. pre_fun = func(
  38. agg=int(agg),
  39. model_path=os.path.join(weight_uvr5_root, model_name + ".pth"),
  40. device=device,
  41. is_half=is_half,
  42. )
  43. if inp_root != "":
  44. paths = [os.path.join(inp_root, name) for name in os.listdir(inp_root)]
  45. else:
  46. paths = [path.name for path in paths]
  47. for path in paths:
  48. inp_path = os.path.join(inp_root, path)
  49. if(os.path.isfile(inp_path)==False):continue
  50. need_reformat = 1
  51. done = 0
  52. try:
  53. info = ffmpeg.probe(inp_path, cmd="ffprobe")
  54. if (
  55. info["streams"][0]["channels"] == 2
  56. and info["streams"][0]["sample_rate"] == "44100"
  57. ):
  58. need_reformat = 0
  59. pre_fun._path_audio_(
  60. inp_path, save_root_ins, save_root_vocal, format0,is_hp3
  61. )
  62. done = 1
  63. except:
  64. need_reformat = 1
  65. traceback.print_exc()
  66. if need_reformat == 1:
  67. tmp_path = "%s/%s.reformatted.wav" % (
  68. os.path.join(os.environ["TEMP"]),
  69. os.path.basename(inp_path),
  70. )
  71. os.system(
  72. "ffmpeg -i %s -vn -acodec pcm_s16le -ac 2 -ar 44100 %s -y"
  73. % (inp_path, tmp_path)
  74. )
  75. inp_path = tmp_path
  76. try:
  77. if done == 0:
  78. pre_fun._path_audio_(
  79. inp_path, save_root_ins, save_root_vocal, format0,is_hp3
  80. )
  81. infos.append("%s->Success" % (os.path.basename(inp_path)))
  82. yield "\n".join(infos)
  83. except:
  84. infos.append(
  85. "%s->%s" % (os.path.basename(inp_path), traceback.format_exc())
  86. )
  87. yield "\n".join(infos)
  88. except:
  89. infos.append(traceback.format_exc())
  90. yield "\n".join(infos)
  91. finally:
  92. try:
  93. if model_name == "onnx_dereverb_By_FoxJoy":
  94. del pre_fun.pred.model
  95. del pre_fun.pred.model_
  96. else:
  97. del pre_fun.model
  98. del pre_fun
  99. except:
  100. traceback.print_exc()
  101. print("clean_empty_cache")
  102. if torch.cuda.is_available():
  103. torch.cuda.empty_cache()
  104. yield "\n".join(infos)
  105. with gr.Blocks(title="UVR5 WebUI") as app:
  106. gr.Markdown(
  107. value=
  108. i18n("本软件以MIT协议开源, 作者不对软件具备任何控制力, 使用软件者、传播软件导出的声音者自负全责. <br>如不认可该条款, 则不能使用或引用软件包内任何代码和文件. 详见根目录<b>LICENSE</b>.")
  109. )
  110. with gr.Tabs():
  111. with gr.TabItem(i18n("伴奏人声分离&去混响&去回声")):
  112. with gr.Group():
  113. gr.Markdown(
  114. value=i18n(
  115. "人声伴奏分离批量处理, 使用UVR5模型。 <br>合格的文件夹路径格式举例: E:\\codes\\py39\\vits_vc_gpu\\白鹭霜华测试样例(去文件管理器地址栏拷就行了)。 <br>模型分为三类: <br>1、保留人声:不带和声的音频选这个,对主人声保留比HP5更好。内置HP2和HP3两个模型,HP3可能轻微漏伴奏但对主人声保留比HP2稍微好一丁点; <br>2、仅保留主人声:带和声的音频选这个,对主人声可能有削弱。内置HP5一个模型; <br> 3、去混响、去延迟模型(by FoxJoy):<br>  (1)MDX-Net(onnx_dereverb):对于双通道混响是最好的选择,不能去除单通道混响;<br>&emsp;(234)DeEcho:去除延迟效果。Aggressive比Normal去除得更彻底,DeReverb额外去除混响,可去除单声道混响,但是对高频重的板式混响去不干净。<br>去混响/去延迟,附:<br>1、DeEcho-DeReverb模型的耗时是另外2个DeEcho模型的接近2倍;<br>2、MDX-Net-Dereverb模型挺慢的;<br>3、个人推荐的最干净的配置是先MDX-Net再DeEcho-Aggressive。"
  116. )
  117. )
  118. with gr.Row():
  119. with gr.Column():
  120. dir_wav_input = gr.Textbox(
  121. label=i18n("输入待处理音频文件夹路径"),
  122. placeholder="C:\\Users\\Desktop\\todo-songs",
  123. )
  124. wav_inputs = gr.File(
  125. file_count="multiple", label=i18n("也可批量输入音频文件, 二选一, 优先读文件夹")
  126. )
  127. with gr.Column():
  128. model_choose = gr.Dropdown(label=i18n("模型"), choices=uvr5_names)
  129. agg = gr.Slider(
  130. minimum=0,
  131. maximum=20,
  132. step=1,
  133. label=i18n("人声提取激进程度"),
  134. value=10,
  135. interactive=True,
  136. visible=False, # 先不开放调整
  137. )
  138. opt_vocal_root = gr.Textbox(
  139. label=i18n("指定输出主人声文件夹"), value="output/uvr5_opt"
  140. )
  141. opt_ins_root = gr.Textbox(
  142. label=i18n("指定输出非主人声文件夹"), value="output/uvr5_opt"
  143. )
  144. format0 = gr.Radio(
  145. label=i18n("导出文件格式"),
  146. choices=["wav", "flac", "mp3", "m4a"],
  147. value="flac",
  148. interactive=True,
  149. )
  150. but2 = gr.Button(i18n("转换"), variant="primary")
  151. vc_output4 = gr.Textbox(label=i18n("输出信息"))
  152. but2.click(
  153. uvr,
  154. [
  155. model_choose,
  156. dir_wav_input,
  157. opt_vocal_root,
  158. wav_inputs,
  159. opt_ins_root,
  160. agg,
  161. format0,
  162. ],
  163. [vc_output4],
  164. api_name="uvr_convert",
  165. )
  166. app.queue(concurrency_count=511, max_size=1022).launch(
  167. server_name="0.0.0.0",
  168. inbrowser=True,
  169. share=is_share,
  170. server_port=webui_port_uvr5,
  171. quiet=True,
  172. )