gere commited on
Commit
e9b314b
·
verified ·
1 Parent(s): 4c0ace6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -17
app.py CHANGED
@@ -18,11 +18,9 @@ from concurrent.futures import ThreadPoolExecutor
18
  app = Flask(__name__)
19
  CORS(app)
20
 
21
- # dynamic hardware detection
22
  has_gpu = torch.cuda.is_available()
23
  device_type = "cuda" if has_gpu else "cpu"
24
- # use lower sample rate on cpu for speed; higher on gpu for quality
25
- sr = 44100 if has_gpu else 22050
26
  target_loudness = -9.0
27
 
28
  def convert_to_wav(input_path):
@@ -35,9 +33,7 @@ def convert_to_wav(input_path):
35
  def load_mono(file_path):
36
  if not os.path.exists(file_path):
37
  return np.zeros(sr * 5)
38
- # limit to 60s on cpu to prevent hangs
39
- duration = None if has_gpu else 60
40
- y, _ = librosa.load(file_path, sr=sr, mono=True, duration=duration)
41
  return y
42
 
43
  def normalize_audio(y):
@@ -83,7 +79,7 @@ def separate_stems(input_file, job_id):
83
  input_file
84
  ]
85
  if not has_gpu:
86
- cmd.extend(["-j", "2"]) # limit threads on cpu to avoid oom
87
 
88
  subprocess.run(cmd, check=True)
89
  base = os.path.splitext(os.path.basename(input_file))[0]
@@ -113,16 +109,12 @@ def fuse_api():
113
  m_req.save(m_path)
114
  temp_files.extend([t_path, m_path])
115
 
116
- # parallel on gpu, sequential on cpu to manage resources
117
- if has_gpu:
118
- with ThreadPoolExecutor(max_workers=2) as executor:
119
- f_t = executor.submit(separate_stems, t_path, f"t_{job_id}")
120
- f_m = executor.submit(separate_stems, m_path, f"m_{job_id}")
121
- t_stems, t_dir = f_t.result()
122
- m_stems, m_dir = f_m.result()
123
- else:
124
- t_stems, t_dir = separate_stems(t_path, f"t_{job_id}")
125
- m_stems, m_dir = separate_stems(m_path, f"m_{job_id}")
126
 
127
  cleanup_dirs.extend([t_dir, m_dir])
128
 
 
18
  app = Flask(__name__)
19
  CORS(app)
20
 
 
21
  has_gpu = torch.cuda.is_available()
22
  device_type = "cuda" if has_gpu else "cpu"
23
+ sr = 44100
 
24
  target_loudness = -9.0
25
 
26
  def convert_to_wav(input_path):
 
33
  def load_mono(file_path):
34
  if not os.path.exists(file_path):
35
  return np.zeros(sr * 5)
36
+ y, _ = librosa.load(file_path, sr=sr, mono=True)
 
 
37
  return y
38
 
39
  def normalize_audio(y):
 
79
  input_file
80
  ]
81
  if not has_gpu:
82
+ cmd.extend(["-j", "1"])
83
 
84
  subprocess.run(cmd, check=True)
85
  base = os.path.splitext(os.path.basename(input_file))[0]
 
109
  m_req.save(m_path)
110
  temp_files.extend([t_path, m_path])
111
 
112
+ max_workers = 2 if has_gpu else 1
113
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
114
+ f_t = executor.submit(separate_stems, t_path, f"t_{job_id}")
115
+ f_m = executor.submit(separate_stems, m_path, f"m_{job_id}")
116
+ t_stems, t_dir = f_t.result()
117
+ m_stems, m_dir = f_m.result()
 
 
 
 
118
 
119
  cleanup_dirs.extend([t_dir, m_dir])
120