Update app.py
Browse files
app.py
CHANGED
|
@@ -2,35 +2,37 @@ import os
|
|
| 2 |
import uuid
|
| 3 |
import subprocess
|
| 4 |
import requests
|
|
|
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
BASE_DIR = "tasks"
|
| 12 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
def add_status(log_list, message):
|
| 15 |
log_list.append(message)
|
| 16 |
-
print(message) #
|
| 17 |
|
| 18 |
@app.post("/process")
|
| 19 |
def process_audio(payload: dict):
|
| 20 |
status_log = []
|
| 21 |
-
|
|
|
|
| 22 |
if "url" not in payload:
|
| 23 |
raise HTTPException(status_code=400, detail="Missing 'url' in payload")
|
| 24 |
|
| 25 |
audio_url = payload["url"]
|
| 26 |
add_status(status_log, "Received URL from payload.")
|
| 27 |
-
|
| 28 |
-
# Create a unique task
|
| 29 |
task_id = str(uuid.uuid4())
|
| 30 |
task_dir = os.path.join(BASE_DIR, task_id)
|
| 31 |
os.makedirs(task_dir, exist_ok=True)
|
| 32 |
add_status(status_log, f"Created task directory: {task_dir}")
|
| 33 |
-
|
| 34 |
# Check FFmpeg version
|
| 35 |
try:
|
| 36 |
ffmpeg_ver = subprocess.run(
|
|
@@ -44,7 +46,7 @@ def process_audio(payload: dict):
|
|
| 44 |
except Exception as e:
|
| 45 |
add_status(status_log, f"Failed to get FFmpeg version: {e}")
|
| 46 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 47 |
-
|
| 48 |
# Check Spleeter version
|
| 49 |
try:
|
| 50 |
spleeter_ver = subprocess.run(
|
|
@@ -57,8 +59,8 @@ def process_audio(payload: dict):
|
|
| 57 |
except Exception as e:
|
| 58 |
add_status(status_log, f"Failed to get Spleeter version: {e}")
|
| 59 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 60 |
-
|
| 61 |
-
# Download the audio file
|
| 62 |
try:
|
| 63 |
r = requests.get(audio_url)
|
| 64 |
r.raise_for_status()
|
|
@@ -66,13 +68,14 @@ def process_audio(payload: dict):
|
|
| 66 |
except Exception as e:
|
| 67 |
add_status(status_log, f"Error downloading file: {e}")
|
| 68 |
return JSONResponse(status_code=400, content={"status": status_log})
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
input_filename = f"input{ext}"
|
| 73 |
input_filepath = os.path.join(task_dir, input_filename)
|
| 74 |
-
|
| 75 |
-
# Save the downloaded file
|
| 76 |
try:
|
| 77 |
with open(input_filepath, "wb") as f:
|
| 78 |
f.write(r.content)
|
|
@@ -80,44 +83,41 @@ def process_audio(payload: dict):
|
|
| 80 |
except Exception as e:
|
| 81 |
add_status(status_log, f"Error saving file: {e}")
|
| 82 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 83 |
-
|
| 84 |
-
# Verify that the file exists
|
| 85 |
if not os.path.exists(input_filepath):
|
| 86 |
add_status(status_log, "Error: Input file does not exist after download.")
|
| 87 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 88 |
-
|
| 89 |
-
# Run Spleeter
|
| 90 |
-
|
| 91 |
spleeter_cmd = [
|
| 92 |
"spleeter", "separate",
|
| 93 |
-
"-i", input_filepath,
|
| 94 |
"-p", "spleeter:2stems",
|
| 95 |
-
"-o", task_dir
|
|
|
|
| 96 |
]
|
| 97 |
add_status(status_log, "Running Spleeter command: " + " ".join(spleeter_cmd))
|
|
|
|
| 98 |
try:
|
| 99 |
-
result = subprocess.run(
|
| 100 |
-
spleeter_cmd,
|
| 101 |
-
capture_output=True,
|
| 102 |
-
text=True,
|
| 103 |
-
check=True,
|
| 104 |
-
)
|
| 105 |
add_status(status_log, "Spleeter command output: " + result.stdout)
|
| 106 |
except subprocess.CalledProcessError as e:
|
| 107 |
add_status(status_log, "Spleeter processing failed: " + e.stderr)
|
| 108 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 109 |
-
|
| 110 |
-
#
|
|
|
|
| 111 |
output_folder = os.path.join(task_dir, base_name)
|
| 112 |
vocals_file = os.path.join(output_folder, "vocals.wav")
|
| 113 |
accompaniment_file = os.path.join(output_folder, "accompaniment.wav")
|
|
|
|
| 114 |
if not (os.path.exists(vocals_file) and os.path.exists(accompaniment_file)):
|
| 115 |
add_status(status_log, "Error: Output files not found after processing.")
|
| 116 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 117 |
|
| 118 |
add_status(status_log, "Spleeter processing completed successfully.")
|
| 119 |
-
|
| 120 |
-
# Return
|
| 121 |
return JSONResponse(content={
|
| 122 |
"task_id": task_id,
|
| 123 |
"status": status_log
|
|
@@ -125,7 +125,6 @@ def process_audio(payload: dict):
|
|
| 125 |
|
| 126 |
@app.get("/", response_class=HTMLResponse)
|
| 127 |
def index():
|
| 128 |
-
# Simple HTML page to test the API and display status logs.
|
| 129 |
html_content = """
|
| 130 |
<!DOCTYPE html>
|
| 131 |
<html>
|
|
|
|
| 2 |
import uuid
|
| 3 |
import subprocess
|
| 4 |
import requests
|
| 5 |
+
from urllib.parse import urlparse
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Create the base tasks directory if it doesn't exist.
|
| 12 |
BASE_DIR = "tasks"
|
| 13 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 14 |
|
| 15 |
def add_status(log_list, message):
|
| 16 |
log_list.append(message)
|
| 17 |
+
print(message) # Log to console for debugging
|
| 18 |
|
| 19 |
@app.post("/process")
|
| 20 |
def process_audio(payload: dict):
|
| 21 |
status_log = []
|
| 22 |
+
|
| 23 |
+
# Validate input
|
| 24 |
if "url" not in payload:
|
| 25 |
raise HTTPException(status_code=400, detail="Missing 'url' in payload")
|
| 26 |
|
| 27 |
audio_url = payload["url"]
|
| 28 |
add_status(status_log, "Received URL from payload.")
|
| 29 |
+
|
| 30 |
+
# Create a unique task directory
|
| 31 |
task_id = str(uuid.uuid4())
|
| 32 |
task_dir = os.path.join(BASE_DIR, task_id)
|
| 33 |
os.makedirs(task_dir, exist_ok=True)
|
| 34 |
add_status(status_log, f"Created task directory: {task_dir}")
|
| 35 |
+
|
| 36 |
# Check FFmpeg version
|
| 37 |
try:
|
| 38 |
ffmpeg_ver = subprocess.run(
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
add_status(status_log, f"Failed to get FFmpeg version: {e}")
|
| 48 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 49 |
+
|
| 50 |
# Check Spleeter version
|
| 51 |
try:
|
| 52 |
spleeter_ver = subprocess.run(
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
add_status(status_log, f"Failed to get Spleeter version: {e}")
|
| 61 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 62 |
+
|
| 63 |
+
# Download the audio file
|
| 64 |
try:
|
| 65 |
r = requests.get(audio_url)
|
| 66 |
r.raise_for_status()
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
add_status(status_log, f"Error downloading file: {e}")
|
| 70 |
return JSONResponse(status_code=400, content={"status": status_log})
|
| 71 |
+
|
| 72 |
+
# Remove any query parameters from the URL for a clean filename
|
| 73 |
+
parsed = urlparse(audio_url)
|
| 74 |
+
path = parsed.path # excludes query parameters
|
| 75 |
+
ext = os.path.splitext(path)[1] or ".mp3"
|
| 76 |
input_filename = f"input{ext}"
|
| 77 |
input_filepath = os.path.join(task_dir, input_filename)
|
| 78 |
+
|
|
|
|
| 79 |
try:
|
| 80 |
with open(input_filepath, "wb") as f:
|
| 81 |
f.write(r.content)
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
add_status(status_log, f"Error saving file: {e}")
|
| 85 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 86 |
+
|
|
|
|
| 87 |
if not os.path.exists(input_filepath):
|
| 88 |
add_status(status_log, "Error: Input file does not exist after download.")
|
| 89 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 90 |
+
|
| 91 |
+
# Run Spleeter using the latest command syntax:
|
| 92 |
+
# spleeter separate -p spleeter:2stems -o <task_dir> <input_filepath>
|
| 93 |
spleeter_cmd = [
|
| 94 |
"spleeter", "separate",
|
|
|
|
| 95 |
"-p", "spleeter:2stems",
|
| 96 |
+
"-o", task_dir,
|
| 97 |
+
input_filepath
|
| 98 |
]
|
| 99 |
add_status(status_log, "Running Spleeter command: " + " ".join(spleeter_cmd))
|
| 100 |
+
|
| 101 |
try:
|
| 102 |
+
result = subprocess.run(spleeter_cmd, capture_output=True, text=True, check=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
add_status(status_log, "Spleeter command output: " + result.stdout)
|
| 104 |
except subprocess.CalledProcessError as e:
|
| 105 |
add_status(status_log, "Spleeter processing failed: " + e.stderr)
|
| 106 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 107 |
+
|
| 108 |
+
# Spleeter creates an output folder with the base name of the input file.
|
| 109 |
+
base_name = os.path.splitext(input_filename)[0]
|
| 110 |
output_folder = os.path.join(task_dir, base_name)
|
| 111 |
vocals_file = os.path.join(output_folder, "vocals.wav")
|
| 112 |
accompaniment_file = os.path.join(output_folder, "accompaniment.wav")
|
| 113 |
+
|
| 114 |
if not (os.path.exists(vocals_file) and os.path.exists(accompaniment_file)):
|
| 115 |
add_status(status_log, "Error: Output files not found after processing.")
|
| 116 |
return JSONResponse(status_code=500, content={"status": status_log})
|
| 117 |
|
| 118 |
add_status(status_log, "Spleeter processing completed successfully.")
|
| 119 |
+
|
| 120 |
+
# Return task ID and status log (for debugging on the frontend)
|
| 121 |
return JSONResponse(content={
|
| 122 |
"task_id": task_id,
|
| 123 |
"status": status_log
|
|
|
|
| 125 |
|
| 126 |
@app.get("/", response_class=HTMLResponse)
|
| 127 |
def index():
|
|
|
|
| 128 |
html_content = """
|
| 129 |
<!DOCTYPE html>
|
| 130 |
<html>
|