Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import onnxruntime as rt
|
6 |
+
from audio_separator import Separator
|
7 |
+
from pydub import AudioSegment
|
8 |
+
from pytube import YouTube
|
9 |
+
|
10 |
+
available_model = ["UVR-MDX-NET-Inst_HQ_3", "UVR-MDX-NET-Voc_FT", "UVR_MDXNET_KARA_2", "Kim_Vocal_2", "UVR_MDXNET_Main"]
|
11 |
+
base_path = Path(__file__).parent
|
12 |
+
|
13 |
+
def reduce_audio_size(audio_path):
|
14 |
+
s1 = AudioSegment.from_file(audio_path)
|
15 |
+
s1.export(audio_path, format="mp3", bitrate="64k")
|
16 |
+
|
17 |
+
|
18 |
+
def audio_sep(youtube_url, audio_path, separation_model, separation_mode, progress=gr.Progress()):
|
19 |
+
out_folder = base_path / "audio_filtered"
|
20 |
+
out_folder.mkdir(exist_ok=True)
|
21 |
+
temp_folder = base_path / "tmp"
|
22 |
+
temp_folder.mkdir(exist_ok=True)
|
23 |
+
|
24 |
+
print(youtube_url)
|
25 |
+
print(audio_path)
|
26 |
+
print(separation_model)
|
27 |
+
print(separation_mode)
|
28 |
+
|
29 |
+
youtube_url = youtube_url.strip()
|
30 |
+
if youtube_url is not None and youtube_url != "":
|
31 |
+
try:
|
32 |
+
print("Downloading YouTube audio...")
|
33 |
+
yt = YouTube(youtube_url)
|
34 |
+
video_id = yt.video_id
|
35 |
+
save_audio_path = temp_folder / f"{video_id}.mp3"
|
36 |
+
if yt.length > 5 * 60:
|
37 |
+
raise gr.Error("Video too long. Please use a video shorter than 5 minutes.")
|
38 |
+
stream = yt.streams.filter(only_audio=True).order_by("abr").desc().first()
|
39 |
+
stream.download(filename=str(save_audio_path))
|
40 |
+
audio_path = str(save_audio_path)
|
41 |
+
print("Downloaded YouTube audio")
|
42 |
+
except:
|
43 |
+
gr.Info("Something went wrong. Skipping to second input.")
|
44 |
+
|
45 |
+
if audio_path is None:
|
46 |
+
gr.Info("Please input an audio file or YouTube URL.")
|
47 |
+
return None, None
|
48 |
+
|
49 |
+
if len(separation_mode) == 1:
|
50 |
+
separation_mode = separation_mode[0]
|
51 |
+
elif len(separation_mode) == 0:
|
52 |
+
return None, None
|
53 |
+
else:
|
54 |
+
separation_mode = None
|
55 |
+
progress(0, desc="Starting...")
|
56 |
+
separator = Separator(
|
57 |
+
audio_path,
|
58 |
+
model_name=separation_model,
|
59 |
+
use_cuda=True if rt.get_device() == "GPU" else False,
|
60 |
+
output_dir=str(out_folder),
|
61 |
+
output_single_stem=separation_mode,
|
62 |
+
)
|
63 |
+
for i in progress.tqdm(range(50)):
|
64 |
+
time.sleep(0.01)
|
65 |
+
|
66 |
+
results = [out_folder / p for p in separator.separate()]
|
67 |
+
print(results)
|
68 |
+
|
69 |
+
for i in progress.tqdm(range(50, 100)):
|
70 |
+
time.sleep(0.01)
|
71 |
+
|
72 |
+
if separation_mode == "Instrument":
|
73 |
+
instrument_stem_path = str(results[0])
|
74 |
+
reduce_audio_size(instrument_stem_path)
|
75 |
+
vocal_stem_path = None
|
76 |
+
elif separation_mode == "Vocal":
|
77 |
+
instrument_stem_path = None
|
78 |
+
vocal_stem_path = str(results[0])
|
79 |
+
reduce_audio_size(vocal_stem_path)
|
80 |
+
else:
|
81 |
+
if "_(Instrumental)_" in str(results[0]):
|
82 |
+
instrument_stem_path = str(results[0])
|
83 |
+
reduce_audio_size(instrument_stem_path)
|
84 |
+
vocal_stem_path = str(results[1])
|
85 |
+
reduce_audio_size(vocal_stem_path)
|
86 |
+
else:
|
87 |
+
vocal_stem_path = str(results[0])
|
88 |
+
reduce_audio_size(vocal_stem_path)
|
89 |
+
instrument_stem_path = str(results[1])
|
90 |
+
reduce_audio_size(instrument_stem_path)
|
91 |
+
return instrument_stem_path, vocal_stem_path
|
92 |
+
|
93 |
+
|
94 |
+
gr.Interface(
|
95 |
+
audio_sep,
|
96 |
+
[
|
97 |
+
gr.Textbox(
|
98 |
+
label="YouTube video URL (No videos more than 5 mins)",
|
99 |
+
placeholder="https://www.youtube.com/watch?v=XXXXXXXXXXX",
|
100 |
+
),
|
101 |
+
gr.Audio(label="Audio Input", type="filepath"),
|
102 |
+
gr.Dropdown(available_model, label="Separation Model", value="UVR_MDXNET_KARA_2"),
|
103 |
+
gr.CheckboxGroup(choices=["Instrument", "Vocal"], label="Separation Mode", value=["Instrument", "Vocal"]),
|
104 |
+
],
|
105 |
+
[gr.Audio(label="Music/Instrument Output", type="filepath"), gr.Audio(label="Vocal Output", type="filepath")],
|
106 |
+
title="Audio Separator",
|
107 |
+
description="<center>Separate the music and vocal from the input audio</center>",
|
108 |
+
allow_flagging=False,
|
109 |
+
).queue().launch(share=False)
|