Pepguy commited on
Commit
436a01d
·
verified ·
1 Parent(s): 759f0e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -2,19 +2,20 @@ import os
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):
@@ -69,9 +70,9 @@ def process_audio(payload: dict):
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)
@@ -88,7 +89,7 @@ def process_audio(payload: dict):
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",
@@ -117,9 +118,22 @@ def process_audio(payload: dict):
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
124
  })
125
 
@@ -155,6 +169,14 @@ def index():
155
  resultHTML += "<li>" + log + "</li>";
156
  }
157
  resultHTML += "</ul>";
 
 
 
 
 
 
 
 
158
  if(data.task_id) {
159
  resultHTML += `<p>Task ID: ${data.task_id}</p>`;
160
  }
 
2
  import uuid
3
  import subprocess
4
  import requests
5
+ import base64
6
  from urllib.parse import urlparse
7
  from fastapi import FastAPI, HTTPException
8
  from fastapi.responses import JSONResponse, HTMLResponse
9
 
10
  app = FastAPI()
11
 
12
+ # Base directory to store task files
13
  BASE_DIR = "tasks"
14
  os.makedirs(BASE_DIR, exist_ok=True)
15
 
16
  def add_status(log_list, message):
17
  log_list.append(message)
18
+ print(message) # Also log to console
19
 
20
  @app.post("/process")
21
  def process_audio(payload: dict):
 
70
  add_status(status_log, f"Error downloading file: {e}")
71
  return JSONResponse(status_code=400, content={"status": status_log})
72
 
73
+ # Clean filename: remove query parameters using urlparse
74
  parsed = urlparse(audio_url)
75
+ path = parsed.path
76
  ext = os.path.splitext(path)[1] or ".mp3"
77
  input_filename = f"input{ext}"
78
  input_filepath = os.path.join(task_dir, input_filename)
 
89
  add_status(status_log, "Error: Input file does not exist after download.")
90
  return JSONResponse(status_code=500, content={"status": status_log})
91
 
92
+ # Run Spleeter using the updated syntax:
93
  # spleeter separate -p spleeter:2stems -o <task_dir> <input_filepath>
94
  spleeter_cmd = [
95
  "spleeter", "separate",
 
118
 
119
  add_status(status_log, "Spleeter processing completed successfully.")
120
 
121
+ # Read output files and encode them in base64
122
+ try:
123
+ with open(vocals_file, "rb") as f:
124
+ vocals_data = f.read()
125
+ with open(accompaniment_file, "rb") as f:
126
+ accomp_data = f.read()
127
+ vocals_b64 = base64.b64encode(vocals_data).decode("utf-8")
128
+ accomp_b64 = base64.b64encode(accomp_data).decode("utf-8")
129
+ except Exception as e:
130
+ add_status(status_log, f"Error reading output files: {e}")
131
+ return JSONResponse(status_code=500, content={"status": status_log})
132
+
133
  return JSONResponse(content={
134
  "task_id": task_id,
135
+ "vocals": vocals_b64,
136
+ "accompaniment": accomp_b64,
137
  "status": status_log
138
  })
139
 
 
169
  resultHTML += "<li>" + log + "</li>";
170
  }
171
  resultHTML += "</ul>";
172
+ if (data.vocals && data.accompaniment) {
173
+ const vocalsDataURI = "data:audio/wav;base64," + data.vocals;
174
+ const accompDataURI = "data:audio/wav;base64," + data.accompaniment;
175
+ resultHTML += "<h3>Vocals</h3>";
176
+ resultHTML += "<audio controls src='" + vocalsDataURI + "'></audio>";
177
+ resultHTML += "<h3>Accompaniment</h3>";
178
+ resultHTML += "<audio controls src='" + accompDataURI + "'></audio>";
179
+ }
180
  if(data.task_id) {
181
  resultHTML += `<p>Task ID: ${data.task_id}</p>`;
182
  }