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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import List
|
| 4 |
+
from loguru import logger as log
|
| 5 |
+
|
| 6 |
+
import requests
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from streamlit_option_menu import option_menu
|
| 9 |
+
|
| 10 |
+
from footer import footer
|
| 11 |
+
from header import header
|
| 12 |
+
from helpers import (
|
| 13 |
+
load_audio_segment,
|
| 14 |
+
load_list_of_songs,
|
| 15 |
+
plot_audio,
|
| 16 |
+
url_is_valid,
|
| 17 |
+
file_size_is_valid,
|
| 18 |
+
delete_old_files,
|
| 19 |
+
)
|
| 20 |
+
from service.demucs_runner import separator
|
| 21 |
+
from service.vocal_remover.runner import load_model, separate
|
| 22 |
+
from style import CSS_TABS
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
label_sources = {
|
| 26 |
+
"no_vocals.mp3": "🎶 Instrumental",
|
| 27 |
+
"vocals.mp3": "🎤 Vocals",
|
| 28 |
+
"drums.mp3": "🥁 Drums",
|
| 29 |
+
"bass.mp3": "🎸 Bass",
|
| 30 |
+
"guitar.mp3": "🎸 Guitar",
|
| 31 |
+
"piano.mp3": "🎹 Piano",
|
| 32 |
+
"other.mp3": "🎶 Other",
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
separation_mode_to_model = {
|
| 36 |
+
"Vocals & Instrumental (Low Quality, Faster)": (
|
| 37 |
+
"vocal_remover",
|
| 38 |
+
["vocals.mp3", "no_vocals.mp3"],
|
| 39 |
+
),
|
| 40 |
+
"Vocals & Instrumental (High Quality, Slower)": ("htdemucs", ["vocals.mp3", "no_vocals.mp3"]),
|
| 41 |
+
"Vocals, Drums, Bass & Other (Slower)": (
|
| 42 |
+
"htdemucs",
|
| 43 |
+
["vocals.mp3", "drums.mp3", "bass.mp3", "other.mp3"],
|
| 44 |
+
),
|
| 45 |
+
"Vocal, Drums, Bass, Guitar, Piano & Other (Slowest)": (
|
| 46 |
+
"htdemucs_6s",
|
| 47 |
+
["vocals.mp3", "drums.mp3", "bass.mp3", "guitar.mp3", "piano.mp3", "other.mp3"],
|
| 48 |
+
),
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
extensions = ["mp3", "wav", "ogg", "flac"]
|
| 52 |
+
|
| 53 |
+
out_path = Path("/tmp")
|
| 54 |
+
in_path = Path("/tmp")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@st.cache_data(show_spinner=False)
|
| 58 |
+
def get_sources(path, file_sources):
|
| 59 |
+
sources = {}
|
| 60 |
+
for file in file_sources:
|
| 61 |
+
fullpath = path / file
|
| 62 |
+
if fullpath.exists():
|
| 63 |
+
sources[file] = fullpath
|
| 64 |
+
return sources
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def reset_execution():
|
| 68 |
+
st.session_state.executed = False
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def show_results(model_name: str, dir_name_output: str, file_sources: List):
|
| 72 |
+
sources = get_sources(out_path / Path(model_name) / dir_name_output, file_sources)
|
| 73 |
+
tab_sources = st.tabs([f"**{label_sources.get(k)}**" for k in sources.keys()])
|
| 74 |
+
for i, (file, pathname) in enumerate(sources.items()):
|
| 75 |
+
with tab_sources[i]:
|
| 76 |
+
cols = st.columns(2)
|
| 77 |
+
with cols[0]:
|
| 78 |
+
auseg = load_audio_segment(pathname, "mp3")
|
| 79 |
+
st.image(
|
| 80 |
+
plot_audio(
|
| 81 |
+
auseg,
|
| 82 |
+
32767,
|
| 83 |
+
file=file,
|
| 84 |
+
model_name=model_name,
|
| 85 |
+
dir_name_output=dir_name_output,
|
| 86 |
+
),
|
| 87 |
+
use_column_width="always",
|
| 88 |
+
)
|
| 89 |
+
with cols[1]:
|
| 90 |
+
st.audio(str(pathname))
|
| 91 |
+
log.info(f"Displaying results for {dir_name_output} - {model_name}")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def body():
|
| 95 |
+
filename = None
|
| 96 |
+
name_song = None
|
| 97 |
+
st.markdown(
|
| 98 |
+
"<h4><center>Extract Vocals & Instrumental from any song</center></h4>",
|
| 99 |
+
unsafe_allow_html=True,
|
| 100 |
+
)
|
| 101 |
+
st.markdown(CSS_TABS, unsafe_allow_html=True)
|
| 102 |
+
|
| 103 |
+
cols = st.columns([1, 4, 1, 3, 1])
|
| 104 |
+
|
| 105 |
+
with cols[1]:
|
| 106 |
+
with st.columns([1, 8, 1])[1]:
|
| 107 |
+
option = option_menu(
|
| 108 |
+
menu_title=None,
|
| 109 |
+
options=["Examples", "Upload File", "From URL"],
|
| 110 |
+
icons=["cloud-upload-fill", "link-45deg", "music-note-list"],
|
| 111 |
+
orientation="horizontal",
|
| 112 |
+
styles={
|
| 113 |
+
"container": {
|
| 114 |
+
"width": "100%",
|
| 115 |
+
"height": "3.5rem",
|
| 116 |
+
"margin": "0px",
|
| 117 |
+
"padding": "0px",
|
| 118 |
+
},
|
| 119 |
+
"icon": {"font-size": "1rem"},
|
| 120 |
+
"nav-link": {
|
| 121 |
+
"display": "flex",
|
| 122 |
+
"height": "3rem",
|
| 123 |
+
"justify-content": "center",
|
| 124 |
+
"align-items": "center",
|
| 125 |
+
"text-align": "center",
|
| 126 |
+
"flex-direction": "column",
|
| 127 |
+
"font-size": "1rem",
|
| 128 |
+
"padding-left": "0px",
|
| 129 |
+
"padding-right": "0px",
|
| 130 |
+
},
|
| 131 |
+
},
|
| 132 |
+
key="option_separate",
|
| 133 |
+
)
|
| 134 |
+
if option == "Examples":
|
| 135 |
+
samples_song = load_list_of_songs(path="separate_songs.json")
|
| 136 |
+
if samples_song is not None:
|
| 137 |
+
name_song = st.selectbox(
|
| 138 |
+
label="Select a sample song and listen to sources separated",
|
| 139 |
+
options=list(samples_song.keys()) + [""],
|
| 140 |
+
format_func=lambda x: x.replace("_", " "),
|
| 141 |
+
index=len(samples_song),
|
| 142 |
+
key="select_example",
|
| 143 |
+
)
|
| 144 |
+
full_path = f"{in_path}/{name_song}"
|
| 145 |
+
if name_song != "" and Path(full_path).exists():
|
| 146 |
+
st.audio(full_path)
|
| 147 |
+
else:
|
| 148 |
+
name_song = None
|
| 149 |
+
|
| 150 |
+
elif option == "Upload File":
|
| 151 |
+
uploaded_file = st.file_uploader(
|
| 152 |
+
"Choose a file",
|
| 153 |
+
type=extensions,
|
| 154 |
+
key="file",
|
| 155 |
+
help="Supported formats: mp3, wav, ogg, flac.",
|
| 156 |
+
)
|
| 157 |
+
if uploaded_file is not None:
|
| 158 |
+
with st.spinner("Loading audio..."):
|
| 159 |
+
with open(in_path / uploaded_file.name, "wb") as f:
|
| 160 |
+
f.write(uploaded_file.getbuffer())
|
| 161 |
+
filename = uploaded_file.name
|
| 162 |
+
st.audio(uploaded_file)
|
| 163 |
+
|
| 164 |
+
elif option == "From URL":
|
| 165 |
+
url = st.text_input(
|
| 166 |
+
"Paste the URL of the audio file",
|
| 167 |
+
key="url_input",
|
| 168 |
+
help="Supported formats: mp3, wav, ogg, flac.",
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
if url != "" and url_is_valid(url):
|
| 172 |
+
with st.spinner("Downloading audio..."):
|
| 173 |
+
filename = url.split("/")[-1]
|
| 174 |
+
response = requests.get(url, stream=True)
|
| 175 |
+
if response.status_code == 200 and file_size_is_valid(
|
| 176 |
+
response.headers.get("Content-Length")
|
| 177 |
+
):
|
| 178 |
+
file_size = 0
|
| 179 |
+
with open(in_path / filename, "wb") as audio_file:
|
| 180 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 181 |
+
if chunk:
|
| 182 |
+
audio_file.write(chunk)
|
| 183 |
+
file_size += len(chunk)
|
| 184 |
+
if not file_size_is_valid(file_size):
|
| 185 |
+
audio_file.close()
|
| 186 |
+
os.remove(in_path / filename)
|
| 187 |
+
filename = None
|
| 188 |
+
return
|
| 189 |
+
st.audio(f"{in_path}/{filename}")
|
| 190 |
+
else:
|
| 191 |
+
st.error(
|
| 192 |
+
"Failed to download audio file. Try to download it manually and upload it."
|
| 193 |
+
)
|
| 194 |
+
filename = None
|
| 195 |
+
|
| 196 |
+
with cols[3]:
|
| 197 |
+
separation_mode = st.selectbox(
|
| 198 |
+
"Choose the separation mode",
|
| 199 |
+
[
|
| 200 |
+
"Vocals & Instrumental (Low Quality, Faster)",
|
| 201 |
+
"Vocals & Instrumental (High Quality, Slower)",
|
| 202 |
+
"Vocals, Drums, Bass & Other (Slower)",
|
| 203 |
+
"Vocal, Drums, Bass, Guitar, Piano & Other (Slowest)",
|
| 204 |
+
],
|
| 205 |
+
on_change=reset_execution(),
|
| 206 |
+
key="separation_mode",
|
| 207 |
+
)
|
| 208 |
+
if separation_mode == "Vocals & Instrumental (Low Quality, Faster)":
|
| 209 |
+
max_duration = 30
|
| 210 |
+
else:
|
| 211 |
+
max_duration = 15
|
| 212 |
+
model_name, file_sources = separation_mode_to_model[separation_mode]
|
| 213 |
+
|
| 214 |
+
if filename is not None:
|
| 215 |
+
song = load_audio_segment(in_path / filename, filename.split(".")[-1])
|
| 216 |
+
n_secs = round(len(song) / 1000)
|
| 217 |
+
if os.environ.get("ENV_LIMITATION", False):
|
| 218 |
+
with cols[3]:
|
| 219 |
+
start_time = st.number_input(
|
| 220 |
+
"Choose the start time",
|
| 221 |
+
min_value=0,
|
| 222 |
+
max_value=n_secs,
|
| 223 |
+
step=1,
|
| 224 |
+
value=0,
|
| 225 |
+
help=f"Maximum duration is {max_duration} seconds for this separation mode.\nDuplicate this space to [remove any limit](https://github.com/fabiogra/moseca#are-there-any-limitations).",
|
| 226 |
+
format="%d",
|
| 227 |
+
)
|
| 228 |
+
st.session_state.start_time = start_time
|
| 229 |
+
end_time = min(start_time + max_duration, n_secs)
|
| 230 |
+
song = song[start_time * 1000 : end_time * 1000]
|
| 231 |
+
st.info(
|
| 232 |
+
f"Audio source will be processed from {start_time} to {end_time} seconds.\nDuplicate this space to [remove any limit](https://github.com/fabiogra/moseca#are-there-any-limitations).",
|
| 233 |
+
icon="⏱",
|
| 234 |
+
)
|
| 235 |
+
else:
|
| 236 |
+
start_time = 0
|
| 237 |
+
end_time = n_secs
|
| 238 |
+
with st.columns([2, 1, 2])[1]:
|
| 239 |
+
execute = st.button(
|
| 240 |
+
"Separate Music Sources 🎶", type="primary", use_container_width=True
|
| 241 |
+
)
|
| 242 |
+
if execute or st.session_state.executed:
|
| 243 |
+
if execute:
|
| 244 |
+
st.session_state.executed = False
|
| 245 |
+
|
| 246 |
+
if not st.session_state.executed:
|
| 247 |
+
log.info(f"{option} - Separating {filename} with {separation_mode}...")
|
| 248 |
+
song.export(in_path / filename, format=filename.split(".")[-1])
|
| 249 |
+
with st.columns([1, 1, 1])[1]:
|
| 250 |
+
with st.spinner("Separating source audio, it will take a while..."):
|
| 251 |
+
if model_name == "vocal_remover":
|
| 252 |
+
model, device = load_model(pretrained_model="baseline.pth")
|
| 253 |
+
separate(
|
| 254 |
+
input=in_path / filename,
|
| 255 |
+
model=model,
|
| 256 |
+
device=device,
|
| 257 |
+
output_dir=out_path,
|
| 258 |
+
)
|
| 259 |
+
else:
|
| 260 |
+
stem = None
|
| 261 |
+
if separation_mode == "Vocals & Instrumental (High Quality, Slower)":
|
| 262 |
+
stem = "vocals"
|
| 263 |
+
|
| 264 |
+
separator(
|
| 265 |
+
tracks=[in_path / filename],
|
| 266 |
+
out=out_path,
|
| 267 |
+
model=model_name,
|
| 268 |
+
shifts=1,
|
| 269 |
+
overlap=0.5,
|
| 270 |
+
stem=stem,
|
| 271 |
+
int24=False,
|
| 272 |
+
float32=False,
|
| 273 |
+
clip_mode="rescale",
|
| 274 |
+
mp3=True,
|
| 275 |
+
mp3_bitrate=320,
|
| 276 |
+
verbose=True,
|
| 277 |
+
start_time=start_time,
|
| 278 |
+
end_time=end_time,
|
| 279 |
+
)
|
| 280 |
+
dir_name_output = ".".join(filename.split(".")[:-1])
|
| 281 |
+
filename = None
|
| 282 |
+
st.session_state.executed = True
|
| 283 |
+
show_results(model_name, dir_name_output, file_sources)
|
| 284 |
+
elif name_song is not None and option == "Examples":
|
| 285 |
+
show_results(model_name, name_song, file_sources)
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
if __name__ == "__main__":
|
| 289 |
+
header()
|
| 290 |
+
body()
|
| 291 |
+
footer()
|
| 292 |
+
delete_old_files("/tmp", 60 * 30)
|