gere commited on
Commit
3925eae
·
verified ·
1 Parent(s): bdba5d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -12,7 +12,6 @@ from scipy.signal import butter, lfilter
12
  from pedalboard import Pedalboard, Compressor, Limiter, HighpassFilter, LowpassFilter, Gain
13
  import subprocess
14
  from pydub import AudioSegment
15
- from concurrent.futures import ThreadPoolExecutor
16
  import torch
17
 
18
  app = Flask(__name__)
@@ -20,7 +19,7 @@ CORS(app)
20
 
21
  SR = 44100
22
  TARGET_LOUDNESS = -9.0
23
- device = "cuda" if torch.cuda.is_available() else "cpu"
24
 
25
  def convert_to_wav(input_path):
26
  if input_path.lower().endswith(".mp3"):
@@ -72,13 +71,17 @@ def beat_sync_warp(source, target):
72
 
73
  def separate_stems(input_file, job_id):
74
  out_dir = f"sep_{job_id}"
75
- cmd = ["demucs", "-n", "htdemucs", "--out", out_dir]
76
- if device == "cpu":
77
- cmd.extend(["-d", "cpu"])
78
- cmd.append(input_file)
 
 
 
 
79
  subprocess.run(cmd, check=True)
80
  base = os.path.splitext(os.path.basename(input_file))[0]
81
- stem_dir = os.path.join(out_dir, "htdemucs", base)
82
  stems = {
83
  "drums": os.path.join(stem_dir, "drums.wav"),
84
  "bass": os.path.join(stem_dir, "bass.wav"),
@@ -89,10 +92,8 @@ def separate_stems(input_file, job_id):
89
 
90
  @app.route('/', methods=['GET'])
91
  def health():
92
- response = make_response(jsonify({"status": "ready", "device": device}), 200)
93
  response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
94
- response.headers["Pragma"] = "no-cache"
95
- response.headers["Expires"] = "0"
96
  return response
97
 
98
  @app.route('/fuse', methods=['POST'])
@@ -114,12 +115,8 @@ def fuse_api():
114
  m_wav = convert_to_wav(m_path)
115
  if t_wav != t_path: temp_files.append(t_wav)
116
  if m_wav != m_path: temp_files.append(m_wav)
117
- max_workers = 1 if device == "cpu" else 2
118
- with ThreadPoolExecutor(max_workers=max_workers) as executor:
119
- future_t = executor.submit(separate_stems, t_wav, f"t_{job_id}")
120
- future_m = executor.submit(separate_stems, m_wav, f"m_{job_id}")
121
- t_stems, t_dir = future_t.result()
122
- m_stems, m_dir = future_m.result()
123
  cleanup_dirs.extend([t_dir, m_dir])
124
  t_other = load_mono(t_stems["other"])
125
  t_bass = load_mono(t_stems["bass"])
 
12
  from pedalboard import Pedalboard, Compressor, Limiter, HighpassFilter, LowpassFilter, Gain
13
  import subprocess
14
  from pydub import AudioSegment
 
15
  import torch
16
 
17
  app = Flask(__name__)
 
19
 
20
  SR = 44100
21
  TARGET_LOUDNESS = -9.0
22
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
23
 
24
  def convert_to_wav(input_path):
25
  if input_path.lower().endswith(".mp3"):
 
71
 
72
  def separate_stems(input_file, job_id):
73
  out_dir = f"sep_{job_id}"
74
+ cmd = [
75
+ "demucs",
76
+ "--identity",
77
+ "-n", "htdemucs_ft",
78
+ "--out", out_dir,
79
+ "-d", DEVICE,
80
+ input_file
81
+ ]
82
  subprocess.run(cmd, check=True)
83
  base = os.path.splitext(os.path.basename(input_file))[0]
84
+ stem_dir = os.path.join(out_dir, "htdemucs_ft", base)
85
  stems = {
86
  "drums": os.path.join(stem_dir, "drums.wav"),
87
  "bass": os.path.join(stem_dir, "bass.wav"),
 
92
 
93
  @app.route('/', methods=['GET'])
94
  def health():
95
+ response = make_response(jsonify({"status": "ready", "device": DEVICE}), 200)
96
  response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
 
 
97
  return response
98
 
99
  @app.route('/fuse', methods=['POST'])
 
115
  m_wav = convert_to_wav(m_path)
116
  if t_wav != t_path: temp_files.append(t_wav)
117
  if m_wav != m_path: temp_files.append(m_wav)
118
+ t_stems, t_dir = separate_stems(t_wav, f"t_{job_id}")
119
+ m_stems, m_dir = separate_stems(m_wav, f"m_{job_id}")
 
 
 
 
120
  cleanup_dirs.extend([t_dir, m_dir])
121
  t_other = load_mono(t_stems["other"])
122
  t_bass = load_mono(t_stems["bass"])