Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,26 @@ import gradio as gr
|
|
| 3 |
from audio_separator.separator import Separator
|
| 4 |
import tempfile
|
| 5 |
import shutil
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
ftp.connect(FTP_HOST, FTP_PORT)
|
| 27 |
-
ftp.login(FTP_USER, FTP_PASS)
|
| 28 |
-
ftp.set_pasv(True)
|
| 29 |
-
ftp.cwd(FTP_DIR)
|
| 30 |
-
|
| 31 |
-
with open(local_filepath, "rb") as f:
|
| 32 |
-
ftp.storbinary(f"STOR {filename}", f, blocksize=32768)
|
| 33 |
-
|
| 34 |
-
ftp.quit()
|
| 35 |
-
return f"{BASE_URL}/{filename}"
|
| 36 |
|
| 37 |
def separate_audio(input_file, model_name):
|
| 38 |
if input_file is None:
|
|
@@ -51,37 +41,31 @@ def separate_audio(input_file, model_name):
|
|
| 51 |
output_files = separator.separate(input_file)
|
| 52 |
|
| 53 |
stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
|
| 54 |
-
|
| 55 |
|
| 56 |
for stem in stems:
|
| 57 |
-
# Find the local file for this stem
|
| 58 |
match = next(
|
| 59 |
(os.path.join(temp_dir, f) for f in output_files if stem in f),
|
| 60 |
None,
|
| 61 |
)
|
| 62 |
-
|
| 63 |
-
if match:
|
| 64 |
-
# Upload to Hostinger and get URL [cite: 1, 2, 3]
|
| 65 |
-
url = upload_to_ftp(match)
|
| 66 |
-
results_urls.append(url)
|
| 67 |
-
else:
|
| 68 |
-
results_urls.append(None)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
return tuple(
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
if os.path.exists(temp_dir):
|
| 75 |
shutil.rmtree(temp_dir)
|
| 76 |
raise RuntimeError(f"Separation failed: {e}") from e
|
| 77 |
|
|
|
|
| 78 |
# ------------------ UI ------------------
|
| 79 |
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
gr.Markdown(
|
| 82 |
"""
|
| 83 |
-
# 🎵 AI 6-Stem Audio Separator
|
| 84 |
-
Split music into
|
| 85 |
"""
|
| 86 |
)
|
| 87 |
|
|
@@ -99,10 +83,10 @@ with gr.Blocks() as demo:
|
|
| 99 |
label="Select Model",
|
| 100 |
)
|
| 101 |
|
| 102 |
-
separate_btn = gr.Button("🚀 Separate
|
| 103 |
|
| 104 |
with gr.Column(scale=2):
|
| 105 |
-
gr.Markdown("### 🔊
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
output_vocals = gr.Audio(label="🎤 Vocals", type="filepath")
|
|
@@ -129,6 +113,11 @@ with gr.Blocks() as demo:
|
|
| 129 |
],
|
| 130 |
)
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
demo.launch(
|
| 134 |
ssr_mode=False,
|
|
|
|
| 3 |
from audio_separator.separator import Separator
|
| 4 |
import tempfile
|
| 5 |
import shutil
|
| 6 |
+
import threading
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def delayed_delete(path, delay=1800):
|
| 11 |
+
"""Delete a file or directory after a delay (default: 30 minutes)."""
|
| 12 |
+
|
| 13 |
+
def command():
|
| 14 |
+
time.sleep(delay)
|
| 15 |
+
try:
|
| 16 |
+
if os.path.isdir(path):
|
| 17 |
+
shutil.rmtree(path)
|
| 18 |
+
elif os.path.isfile(path):
|
| 19 |
+
os.remove(path)
|
| 20 |
+
print(f"Cleanup: Deleted {path}")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Cleanup error for {path}: {e}")
|
| 23 |
+
|
| 24 |
+
threading.Thread(target=command, daemon=True).start()
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def separate_audio(input_file, model_name):
|
| 28 |
if input_file is None:
|
|
|
|
| 41 |
output_files = separator.separate(input_file)
|
| 42 |
|
| 43 |
stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
|
| 44 |
+
results = []
|
| 45 |
|
| 46 |
for stem in stems:
|
|
|
|
| 47 |
match = next(
|
| 48 |
(os.path.join(temp_dir, f) for f in output_files if stem in f),
|
| 49 |
None,
|
| 50 |
)
|
| 51 |
+
results.append(match)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
delayed_delete(temp_dir, delay=1800)
|
| 54 |
+
return tuple(results)
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
if os.path.exists(temp_dir):
|
| 58 |
shutil.rmtree(temp_dir)
|
| 59 |
raise RuntimeError(f"Separation failed: {e}") from e
|
| 60 |
|
| 61 |
+
|
| 62 |
# ------------------ UI ------------------
|
| 63 |
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
| 67 |
+
# 🎵 AI 6-Stem Audio Separator
|
| 68 |
+
Split your music into high-quality individual tracks using **Demucs 6s**.
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
|
|
|
|
| 83 |
label="Select Model",
|
| 84 |
)
|
| 85 |
|
| 86 |
+
separate_btn = gr.Button("🚀 Separate Stems", variant="primary")
|
| 87 |
|
| 88 |
with gr.Column(scale=2):
|
| 89 |
+
gr.Markdown("### 🔊 Separation Results")
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
output_vocals = gr.Audio(label="🎤 Vocals", type="filepath")
|
|
|
|
| 113 |
],
|
| 114 |
)
|
| 115 |
|
| 116 |
+
gr.Markdown(
|
| 117 |
+
"⚠️ **Note:** Processing may take 1–2 minutes depending on file length and Space hardware."
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
if __name__ == "__main__":
|
| 122 |
demo.launch(
|
| 123 |
ssr_mode=False,
|