Skip to content

Commit

Permalink
Merge pull request #269 from jhj0517/feature/add-bgm-tab
Browse files Browse the repository at this point in the history
Add dedicated bgm separation tab
  • Loading branch information
jhj0517 authored Sep 18, 2024
2 parents 5f094f9 + 207096c commit 633c360
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
39 changes: 37 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def create_whisper_parameters(self):
nb_batch_size = gr.Number(label="Batch Size", value=whisper_params["batch_size"], precision=0)

with gr.Accordion("BGM Separation", open=False):
cb_bgm_separation = gr.Checkbox(label="Enable BGM separation", value=uvr_params["is_separate_bgm"],
cb_bgm_separation = gr.Checkbox(label="Enable BGM Separation Filter", value=uvr_params["is_separate_bgm"],
interactive=True)
dd_uvr_device = gr.Dropdown(label="Device", value=self.whisper_inf.music_separator.device,
choices=self.whisper_inf.music_separator.available_devices)
Expand Down Expand Up @@ -199,6 +199,7 @@ def launch(self):
translation_params = self.default_params["translation"]
deepl_params = translation_params["deepl"]
nllb_params = translation_params["nllb"]
uvr_params = self.default_params["bgm_separation"]

with self.app:
with gr.Row():
Expand Down Expand Up @@ -344,6 +345,39 @@ def launch(self):
inputs=None,
outputs=None)

with gr.TabItem("BGM Separation"):
files_audio = gr.Files(type="filepath", label="Upload Audio Files to separate background music")
dd_uvr_device = gr.Dropdown(label="Device", value=self.whisper_inf.music_separator.device,
choices=self.whisper_inf.music_separator.available_devices)
dd_uvr_model_size = gr.Dropdown(label="Model", value=uvr_params["model_size"],
choices=self.whisper_inf.music_separator.available_models)
nb_uvr_segment_size = gr.Number(label="Segment Size", value=uvr_params["segment_size"], precision=0)
cb_uvr_save_file = gr.Checkbox(label="Save separated files to output",
value=True, visible=False)
btn_run = gr.Button("SEPARATE BACKGROUND MUSIC", variant="primary")
with gr.Column():
with gr.Row():
ad_instrumental = gr.Audio(label="Instrumental", scale=8)
btn_open_instrumental_folder = gr.Button('📂', scale=1)
with gr.Row():
ad_vocals = gr.Audio(label="Vocals", scale=8)
btn_open_vocals_folder = gr.Button('📂', scale=1)

btn_run.click(fn=self.whisper_inf.music_separator.separate_files,
inputs=[files_audio, dd_uvr_model_size, dd_uvr_device, nb_uvr_segment_size,
cb_uvr_save_file],
outputs=[ad_instrumental, ad_vocals])
btn_open_instrumental_folder.click(inputs=None,
outputs=None,
fn=lambda: self.open_folder(os.path.join(
self.args.output_dir, "UVR", "instrumental"
)))
btn_open_vocals_folder.click(inputs=None,
outputs=None,
fn=lambda: self.open_folder(os.path.join(
self.args.output_dir, "UVR", "vocals"
)))

# Launch the app with optional gradio settings
args = self.args

Expand All @@ -363,7 +397,8 @@ def open_folder(folder_path: str):
if os.path.exists(folder_path):
os.system(f"start {folder_path}")
else:
print(f"The folder {folder_path} does not exist.")
os.makedirs(folder_path, exist_ok=True)
print(f"The directory path {folder_path} has newly created.")

@staticmethod
def on_change_models(model_size: str):
Expand Down
2 changes: 1 addition & 1 deletion configs/default_parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bgm_separation:
is_separate_bgm: false
model_size: "UVR-MDX-NET-Inst_HQ_4"
segment_size: 256
save_file: true
save_file: false

translation:
deepl:
Expand Down
55 changes: 48 additions & 7 deletions modules/uvr/music_separator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional, Union, List, Dict
import numpy as np
import torchaudio
import soundfile as sf
Expand All @@ -9,10 +9,10 @@
from datetime import datetime

from uvr.models import MDX, Demucs, VrNetwork, MDXC
from modules.utils.files_manager import is_video
from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH
from modules.utils.files_manager import load_yaml, save_yaml, is_video
from modules.diarize.audio_loader import load_audio


class MusicSeparator:
def __init__(self,
model_dir: Optional[str] = None,
Expand Down Expand Up @@ -67,7 +67,7 @@ def separate(self,
device: Optional[str] = None,
segment_size: int = 256,
save_file: bool = False,
progress: gr.Progress = gr.Progress()) -> tuple[np.ndarray, np.ndarray]:
progress: gr.Progress = gr.Progress()) -> tuple[np.ndarray, np.ndarray, List]:
"""
Separate the background music from the audio.
Expand All @@ -80,10 +80,14 @@ def separate(self,
progress (gr.Progress): Gradio progress indicator.
Returns:
tuple[np.ndarray, np.ndarray]: Instrumental and vocals numpy arrays.
A Tuple of
np.ndarray: Instrumental numpy arrays.
np.ndarray: Vocals numpy arrays.
file_paths: List of file paths where the separated audio is saved. Return empty when save_file is False.
"""
if isinstance(audio, str):
output_filename, ext = os.path.basename(audio), ".wav"
output_filename, orig_ext = os.path.splitext(output_filename)

if is_video(audio):
audio = load_audio(audio)
Expand Down Expand Up @@ -118,13 +122,37 @@ def separate(self,
result = self.model(audio)
instrumental, vocals = result["instrumental"].T, result["vocals"].T

file_paths = []
if save_file:
instrumental_output_path = os.path.join(self.output_dir, "instrumental", f"{output_filename}-instrumental{ext}")
vocals_output_path = os.path.join(self.output_dir, "vocals", f"{output_filename}-vocals{ext}")
sf.write(instrumental_output_path, instrumental, sample_rate, format="WAV")
sf.write(vocals_output_path, vocals, sample_rate, format="WAV")

return instrumental, vocals
file_paths += [instrumental_output_path, vocals_output_path]

return instrumental, vocals, file_paths

def separate_files(self,
files: List,
model_name: str,
device: Optional[str] = None,
segment_size: int = 256,
save_file: bool = True,
progress: gr.Progress = gr.Progress()) -> List[str]:
"""Separate the background music from the audio files. Returns only last Instrumental and vocals file paths
to display into gr.Audio()"""
self.cache_parameters(model_size=model_name, segment_size=segment_size)

for file_path in files:
instrumental, vocals, file_paths = self.separate(
audio=file_path,
model_name=model_name,
device=device,
segment_size=segment_size,
save_file=save_file,
progress=progress
)
return file_paths

@staticmethod
def get_device():
Expand All @@ -140,3 +168,16 @@ def offload(self):
torch.cuda.empty_cache()
gc.collect()
self.audio_info = None

@staticmethod
def cache_parameters(model_size: str,
segment_size: int):
cached_params = load_yaml(DEFAULT_PARAMETERS_CONFIG_PATH)
cached_uvr_params = cached_params["bgm_separation"]
uvr_params_to_cache = {
"model_size": model_size,
"segment_size": segment_size
}
cached_uvr_params = {**cached_uvr_params, **uvr_params_to_cache}
cached_params["bgm_separation"] = cached_uvr_params
save_yaml(cached_params, DEFAULT_PARAMETERS_CONFIG_PATH)
2 changes: 1 addition & 1 deletion modules/whisper/whisper_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def run(self,
params.lang = language_code_dict[params.lang]

if params.is_bgm_separate:
music, audio = self.music_separator.separate(
music, audio, _ = self.music_separator.separate(
audio=audio,
model_name=params.uvr_model_size,
device=params.uvr_device,
Expand Down

0 comments on commit 633c360

Please sign in to comment.