Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import demucs.separate
|
3 |
+
import subprocess
|
4 |
+
import os
|
5 |
+
import uuid
|
6 |
+
import streamlit as st
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import shutil
|
9 |
+
|
10 |
+
st.title("Karaoke")
|
11 |
+
url = st.text_input("Enter spotify song URL")
|
12 |
+
button = st.button("Process")
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
if button:
|
16 |
+
with st.spinner("Processing"):
|
17 |
+
uuid_str = str(uuid.uuid4())
|
18 |
+
cmd = f"spotdl {url} --output audio{uuid_str}"
|
19 |
+
subprocess.run(cmd, shell=True)
|
20 |
+
files = os.listdir(f"audio{uuid_str}")
|
21 |
+
audio_files = [
|
22 |
+
file for file in files if file.endswith((".mp3", ".wav", ".ogg"))
|
23 |
+
]
|
24 |
+
old_path = os.path.join(f"audio{uuid_str}", audio_files[0])
|
25 |
+
new_path = os.path.join(f"audio{uuid_str}", "audio.mp3")
|
26 |
+
os.rename(old_path, new_path)
|
27 |
+
|
28 |
+
demucs.separate.main(
|
29 |
+
[
|
30 |
+
"--mp3",
|
31 |
+
"--two-stems",
|
32 |
+
"vocals",
|
33 |
+
"-n",
|
34 |
+
"htdemucs",
|
35 |
+
"-d",
|
36 |
+
"cpu",
|
37 |
+
new_path,
|
38 |
+
"-o",
|
39 |
+
f"output{uuid_str}",
|
40 |
+
]
|
41 |
+
)
|
42 |
+
|
43 |
+
sound = AudioSegment.from_mp3(f"./output{uuid_str}/htdemucs/audio/no_vocals.mp3")
|
44 |
+
sound.export("output.mp3", format="mp3", bitrate="192k")
|
45 |
+
|
46 |
+
shutil.rmtree(f"audio{uuid_str}")
|
47 |
+
shutil.rmtree(f"output{uuid_str}")
|
48 |
+
|
49 |
+
st.audio("output.mp3", format="audio/mp3")
|