LAP-DEV commited on
Commit
5de71b6
·
verified ·
1 Parent(s): 68f96cf

Delete modules/uvr/music_separator_old.py

Browse files
Files changed (1) hide show
  1. modules/uvr/music_separator_old.py +0 -183
modules/uvr/music_separator_old.py DELETED
@@ -1,183 +0,0 @@
1
- from typing import Optional, Union, List, Dict
2
- import numpy as np
3
- import torchaudio
4
- import soundfile as sf
5
- import os
6
- import torch
7
- import gc
8
- import gradio as gr
9
- from datetime import datetime
10
-
11
- from uvr.models import MDX, Demucs, VrNetwork, MDXC
12
- from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH
13
- from modules.utils.files_manager import load_yaml, save_yaml, is_video
14
- from modules.diarize.audio_loader import load_audio
15
-
16
- class MusicSeparator:
17
- def __init__(self,
18
- model_dir: Optional[str] = None,
19
- output_dir: Optional[str] = None):
20
- self.model = None
21
- self.device = self.get_device()
22
- self.available_devices = ["cpu", "cuda"]
23
- self.model_dir = model_dir
24
- self.output_dir = output_dir
25
- instrumental_output_dir = os.path.join(self.output_dir, "instrumental")
26
- vocals_output_dir = os.path.join(self.output_dir, "vocals")
27
- os.makedirs(instrumental_output_dir, exist_ok=True)
28
- os.makedirs(vocals_output_dir, exist_ok=True)
29
- self.audio_info = None
30
- self.available_models = ["UVR-MDX-NET-Inst_HQ_4", "UVR-MDX-NET-Inst_3"]
31
- self.default_model = self.available_models[0]
32
- self.current_model_size = self.default_model
33
- self.model_config = {
34
- "segment": 256,
35
- "split": True
36
- }
37
-
38
- def update_model(self,
39
- model_name: str = "UVR-MDX-NET-Inst_1",
40
- device: Optional[str] = None,
41
- segment_size: int = 256):
42
- """
43
- Update model with the given model name
44
-
45
- Args:
46
- model_name (str): Model name.
47
- device (str): Device to use for the model.
48
- segment_size (int): Segment size for the prediction.
49
- """
50
- if device is None:
51
- device = self.device
52
-
53
- self.device = device
54
- self.model_config = {
55
- "segment": segment_size,
56
- "split": True
57
- }
58
- self.model = MDX(name=model_name,
59
- other_metadata=self.model_config,
60
- device=self.device,
61
- logger=None,
62
- model_dir=self.model_dir)
63
-
64
- def separate(self,
65
- audio: Union[str, np.ndarray],
66
- model_name: str,
67
- device: Optional[str] = None,
68
- segment_size: int = 256,
69
- save_file: bool = False,
70
- progress: gr.Progress = gr.Progress()) -> tuple[np.ndarray, np.ndarray, List]:
71
- """
72
- Separate the background music from the audio.
73
-
74
- Args:
75
- audio (Union[str, np.ndarray]): Audio path or numpy array.
76
- model_name (str): Model name.
77
- device (str): Device to use for the model.
78
- segment_size (int): Segment size for the prediction.
79
- save_file (bool): Whether to save the separated audio to output path or not.
80
- progress (gr.Progress): Gradio progress indicator.
81
-
82
- Returns:
83
- A Tuple of
84
- np.ndarray: Instrumental numpy arrays.
85
- np.ndarray: Vocals numpy arrays.
86
- file_paths: List of file paths where the separated audio is saved. Return empty when save_file is False.
87
- """
88
- if isinstance(audio, str):
89
- output_filename, ext = os.path.basename(audio), ".wav"
90
- output_filename, orig_ext = os.path.splitext(output_filename)
91
-
92
- if is_video(audio):
93
- audio = load_audio(audio)
94
- sample_rate = 16000
95
- else:
96
- self.audio_info = torchaudio.info(audio)
97
- sample_rate = self.audio_info.sample_rate
98
- else:
99
- timestamp = datetime.now().strftime("%m%d%H%M%S")
100
- output_filename, ext = f"UVR-{timestamp}", ".wav"
101
- sample_rate = 16000
102
-
103
- model_config = {
104
- "segment": segment_size,
105
- "split": True
106
- }
107
-
108
- if (self.model is None or
109
- self.current_model_size != model_name or
110
- self.model_config != model_config or
111
- self.model.sample_rate != sample_rate or
112
- self.device != device):
113
- progress(0, desc="Initializing UVR Model...")
114
- self.update_model(
115
- model_name=model_name,
116
- device=device,
117
- segment_size=segment_size
118
- )
119
- self.model.sample_rate = sample_rate
120
-
121
- progress(0, desc="Separating background music from the audio...")
122
- result = self.model(audio)
123
- instrumental, vocals = result["instrumental"].T, result["vocals"].T
124
-
125
- file_paths = []
126
- if save_file:
127
- instrumental_output_path = os.path.join(self.output_dir, "instrumental", f"{output_filename}-instrumental{ext}")
128
- vocals_output_path = os.path.join(self.output_dir, "vocals", f"{output_filename}-vocals{ext}")
129
- sf.write(instrumental_output_path, instrumental, sample_rate, format="WAV")
130
- sf.write(vocals_output_path, vocals, sample_rate, format="WAV")
131
- file_paths += [instrumental_output_path, vocals_output_path]
132
-
133
- return instrumental, vocals, file_paths
134
-
135
- def separate_files(self,
136
- files: List,
137
- model_name: str,
138
- device: Optional[str] = None,
139
- segment_size: int = 256,
140
- save_file: bool = True,
141
- progress: gr.Progress = gr.Progress()) -> List[str]:
142
- """Separate the background music from the audio files. Returns only last Instrumental and vocals file paths
143
- to display into gr.Audio()"""
144
- self.cache_parameters(model_size=model_name, segment_size=segment_size)
145
-
146
- for file_path in files:
147
- instrumental, vocals, file_paths = self.separate(
148
- audio=file_path,
149
- model_name=model_name,
150
- device=device,
151
- segment_size=segment_size,
152
- save_file=save_file,
153
- progress=progress
154
- )
155
- return file_paths
156
-
157
- @staticmethod
158
- def get_device():
159
- """Get device for the model"""
160
- return "cuda" if torch.cuda.is_available() else "cpu"
161
-
162
- def offload(self):
163
- """Offload the model and free up the memory"""
164
- if self.model is not None:
165
- del self.model
166
- self.model = None
167
- if self.device == "cuda":
168
- torch.cuda.empty_cache()
169
- gc.collect()
170
- self.audio_info = None
171
-
172
- @staticmethod
173
- def cache_parameters(model_size: str,
174
- segment_size: int):
175
- cached_params = load_yaml(DEFAULT_PARAMETERS_CONFIG_PATH)
176
- cached_uvr_params = cached_params["bgm_separation"]
177
- uvr_params_to_cache = {
178
- "model_size": model_size,
179
- "segment_size": segment_size
180
- }
181
- cached_uvr_params = {**cached_uvr_params, **uvr_params_to_cache}
182
- cached_params["bgm_separation"] = cached_uvr_params
183
- save_yaml(cached_params, DEFAULT_PARAMETERS_CONFIG_PATH)