Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
07b69d9
1
Parent(s): dae4d03
Activation link
Browse files- __pycache__/main.cpython-314.pyc +0 -0
- main.py +33 -11
__pycache__/main.cpython-314.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-314.pyc and b/__pycache__/main.cpython-314.pyc differ
|
|
|
main.py
CHANGED
|
@@ -547,7 +547,10 @@ def cleanup_unactivated_users():
|
|
| 547 |
|
| 548 |
# --- Core Logic ---
|
| 549 |
|
| 550 |
-
def run_demucs_and_extract(input_path: str, tmpdir: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 551 |
output_dir = os.path.join(tmpdir, "stems")
|
| 552 |
os.makedirs(output_dir, exist_ok=True)
|
| 553 |
|
|
@@ -569,6 +572,19 @@ def run_demucs_and_extract(input_path: str, tmpdir: str) -> str:
|
|
| 569 |
for root, dirs, files in os.walk(output_dir):
|
| 570 |
for f in files:
|
| 571 |
if f.endswith('.wav'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
src = os.path.join(root, f)
|
| 573 |
dst = os.path.join(mp3_dir, f.replace('.wav', '.mp3'))
|
| 574 |
# Convert WAV to MP3 (suppress ffmpeg output)
|
|
@@ -580,15 +596,17 @@ def run_demucs_and_extract(input_path: str, tmpdir: str) -> str:
|
|
| 580 |
if wav_count == 0:
|
| 581 |
raise Exception("Demucs produced no output files. Check if the model is installed and the input is valid.")
|
| 582 |
|
| 583 |
-
# Create instrumental mix
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
|
|
|
|
|
|
| 592 |
|
| 593 |
# Add original audio to the output
|
| 594 |
original_mp3 = os.path.join(mp3_dir, 'original.mp3')
|
|
@@ -606,6 +624,7 @@ def run_demucs_and_extract(input_path: str, tmpdir: str) -> str:
|
|
| 606 |
async def process_upload(
|
| 607 |
file: UploadFile = File(...),
|
| 608 |
tos_agreed: bool = Form(...),
|
|
|
|
| 609 |
recaptcha_token: str = Form(None),
|
| 610 |
authorization: str = Header(None),
|
| 611 |
background_tasks: BackgroundTasks = None
|
|
@@ -640,8 +659,11 @@ async def process_upload(
|
|
| 640 |
subprocess.run([FFMPEG_PATH, "-y", "-i", input_path, "-ac", "2", "-ar", "44100", audio_path],
|
| 641 |
check=True, capture_output=True, text=True)
|
| 642 |
|
|
|
|
|
|
|
|
|
|
| 643 |
# Process with Demucs
|
| 644 |
-
extracted_dir = run_demucs_and_extract(audio_path, tmpdir)
|
| 645 |
|
| 646 |
# If R2 is enabled and user is authenticated, upload to R2
|
| 647 |
if R2_ENABLED and uid:
|
|
|
|
| 547 |
|
| 548 |
# --- Core Logic ---
|
| 549 |
|
| 550 |
+
def run_demucs_and_extract(input_path: str, tmpdir: str, requested_stems: list = None) -> str:
|
| 551 |
+
if requested_stems is None:
|
| 552 |
+
requested_stems = ["vocals", "drums", "bass", "other", "instrumental"]
|
| 553 |
+
|
| 554 |
output_dir = os.path.join(tmpdir, "stems")
|
| 555 |
os.makedirs(output_dir, exist_ok=True)
|
| 556 |
|
|
|
|
| 572 |
for root, dirs, files in os.walk(output_dir):
|
| 573 |
for f in files:
|
| 574 |
if f.endswith('.wav'):
|
| 575 |
+
basename = f.replace('.wav', '').lower()
|
| 576 |
+
# Map Demucs names to our stem names
|
| 577 |
+
is_requested = False
|
| 578 |
+
if basename in requested_stems:
|
| 579 |
+
is_requested = True
|
| 580 |
+
elif basename == 'vocal' and 'vocals' in requested_stems:
|
| 581 |
+
is_requested = True
|
| 582 |
+
elif basename == 'drum' and 'drums' in requested_stems:
|
| 583 |
+
is_requested = True
|
| 584 |
+
|
| 585 |
+
if not is_requested:
|
| 586 |
+
continue
|
| 587 |
+
|
| 588 |
src = os.path.join(root, f)
|
| 589 |
dst = os.path.join(mp3_dir, f.replace('.wav', '.mp3'))
|
| 590 |
# Convert WAV to MP3 (suppress ffmpeg output)
|
|
|
|
| 596 |
if wav_count == 0:
|
| 597 |
raise Exception("Demucs produced no output files. Check if the model is installed and the input is valid.")
|
| 598 |
|
| 599 |
+
# Create instrumental mix if requested
|
| 600 |
+
if 'instrumental' in requested_stems:
|
| 601 |
+
stems = [os.path.join(mp3_dir, f) for f in os.listdir(mp3_dir) if 'vocal' not in f.lower() and f.endswith('.mp3')]
|
| 602 |
+
if stems:
|
| 603 |
+
inst_path = os.path.join(mp3_dir, 'instrumental.mp3')
|
| 604 |
+
n = len(stems)
|
| 605 |
+
labels = "".join(f"[{i}:a]" for i in range(n))
|
| 606 |
+
subprocess.run([FFMPEG_PATH, "-y"] + [i for s in stems for i in ("-i", s)] +
|
| 607 |
+
["-filter_complex", f"{labels}amix=inputs={n}:normalize=0", "-c:a", "libmp3lame", "-qscale:a", "2", inst_path],
|
| 608 |
+
check=True, capture_output=True, text=True)
|
| 609 |
+
# We don't increment wav_count here since it's an extra file
|
| 610 |
|
| 611 |
# Add original audio to the output
|
| 612 |
original_mp3 = os.path.join(mp3_dir, 'original.mp3')
|
|
|
|
| 624 |
async def process_upload(
|
| 625 |
file: UploadFile = File(...),
|
| 626 |
tos_agreed: bool = Form(...),
|
| 627 |
+
stems: str = Form("vocals,drums,bass,other,instrumental"),
|
| 628 |
recaptcha_token: str = Form(None),
|
| 629 |
authorization: str = Header(None),
|
| 630 |
background_tasks: BackgroundTasks = None
|
|
|
|
| 659 |
subprocess.run([FFMPEG_PATH, "-y", "-i", input_path, "-ac", "2", "-ar", "44100", audio_path],
|
| 660 |
check=True, capture_output=True, text=True)
|
| 661 |
|
| 662 |
+
# Parse stems list
|
| 663 |
+
requested_stems = [s.strip().lower() for s in stems.split(',') if s.strip()]
|
| 664 |
+
|
| 665 |
# Process with Demucs
|
| 666 |
+
extracted_dir = run_demucs_and_extract(audio_path, tmpdir, requested_stems)
|
| 667 |
|
| 668 |
# If R2 is enabled and user is authenticated, upload to R2
|
| 669 |
if R2_ENABLED and uid:
|