Vansh Chugh commited on
Commit
17078a0
·
1 Parent(s): eec3cc2

increase timeout duration + timing inference

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -18,6 +18,7 @@ except ImportError:
18
 
19
  import functools
20
  import tempfile
 
21
  import numpy as np
22
  import scipy.signal
23
  import torch
@@ -225,12 +226,14 @@ model_card = ModelCard(
225
  )
226
 
227
 
228
- @spaces.GPU(duration=120)
229
  def process_fn(vocals_path, bass_path, drums_path, other_path):
 
230
  global model_ready
231
  if not model_ready:
232
  super_net.to(DEVICE)
233
  model_ready = True
 
234
 
235
  paths = {'vocals': vocals_path, 'bass': bass_path, 'drums': drums_path, 'other': other_path}
236
 
@@ -280,6 +283,7 @@ def process_fn(vocals_path, bass_path, drums_path, other_path):
280
  max_samples = min(max(samples), int(MAX_LENGTH * SR))
281
  max_samplingrate = max(samplingrate_list)
282
  max_subtype = max(subtype_list)
 
283
 
284
  # --- Build input tensor ---
285
  data = np.zeros((len(STEMS), 1, max_samples, N_CHANNELS), dtype=np.float32)
@@ -295,11 +299,13 @@ def process_fn(vocals_path, bass_path, drums_path, other_path):
295
  # round up to the next multiple of KERNEL_SIZE_ENCODER as required by the encoder
296
  new_samples = (1 + max_samples // KERNEL_SIZE_ENCODER) * KERNEL_SIZE_ENCODER
297
  data = np.pad(data, [(0, 0), (0, 0), (0, new_samples - max_samples), (0, 0)])
 
298
 
299
  # --- Effect normalization ---
300
  # changed from org. inference #512 because parallelization may not be safe on HF's CPU tier
301
  for k, stem in enumerate(STEMS):
302
  data[k][0] = normalize_audio_wave((data[k][0], EFFECTS, stem, features_mean))
 
303
 
304
  # --- Run model inference ---
305
  test_data = torch.from_numpy(data).to(DEVICE)
@@ -308,6 +314,7 @@ def process_fn(vocals_path, bass_path, drums_path, other_path):
308
  audio_out = test_out[DataType.TIME_SAMPLES].cpu().numpy()
309
  audio_out = audio_out[..., :max_samples] # trim encoder padding
310
  audio_out = audio_out[..., pad_samples:] # trim the 30s context pad added at load
 
311
 
312
  # first output stem is the mix
313
  audio_output = audio_out[0, 0, ...]
@@ -317,10 +324,12 @@ def process_fn(vocals_path, bass_path, drums_path, other_path):
317
  audio_output = librosa.resample(audio_output, orig_sr=SR,
318
  target_sr=max_samplingrate,
319
  res_type='kaiser_best')
 
320
 
321
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
322
  out_path = f.name
323
  save_wav(out_path, max_samplingrate, audio_output.T, subtype=max_subtype)
 
324
 
325
  return out_path
326
 
 
18
 
19
  import functools
20
  import tempfile
21
+ import time
22
  import numpy as np
23
  import scipy.signal
24
  import torch
 
226
  )
227
 
228
 
229
+ @spaces.GPU(duration=180)
230
  def process_fn(vocals_path, bass_path, drums_path, other_path):
231
+ t_start = time.time()
232
  global model_ready
233
  if not model_ready:
234
  super_net.to(DEVICE)
235
  model_ready = True
236
+ print(f'[timing] model-to-device: {time.time() - t_start:.2f}s')
237
 
238
  paths = {'vocals': vocals_path, 'bass': bass_path, 'drums': drums_path, 'other': other_path}
239
 
 
283
  max_samples = min(max(samples), int(MAX_LENGTH * SR))
284
  max_samplingrate = max(samplingrate_list)
285
  max_subtype = max(subtype_list)
286
+ print(f'[timing] load+resample stems: {time.time() - t_start:.2f}s')
287
 
288
  # --- Build input tensor ---
289
  data = np.zeros((len(STEMS), 1, max_samples, N_CHANNELS), dtype=np.float32)
 
299
  # round up to the next multiple of KERNEL_SIZE_ENCODER as required by the encoder
300
  new_samples = (1 + max_samples // KERNEL_SIZE_ENCODER) * KERNEL_SIZE_ENCODER
301
  data = np.pad(data, [(0, 0), (0, 0), (0, new_samples - max_samples), (0, 0)])
302
+ print(f'[timing] build input tensor: {time.time() - t_start:.2f}s')
303
 
304
  # --- Effect normalization ---
305
  # changed from org. inference #512 because parallelization may not be safe on HF's CPU tier
306
  for k, stem in enumerate(STEMS):
307
  data[k][0] = normalize_audio_wave((data[k][0], EFFECTS, stem, features_mean))
308
+ print(f'[timing] effect normalization: {time.time() - t_start:.2f}s')
309
 
310
  # --- Run model inference ---
311
  test_data = torch.from_numpy(data).to(DEVICE)
 
314
  audio_out = test_out[DataType.TIME_SAMPLES].cpu().numpy()
315
  audio_out = audio_out[..., :max_samples] # trim encoder padding
316
  audio_out = audio_out[..., pad_samples:] # trim the 30s context pad added at load
317
+ print(f'[timing] model inference: {time.time() - t_start:.2f}s')
318
 
319
  # first output stem is the mix
320
  audio_output = audio_out[0, 0, ...]
 
324
  audio_output = librosa.resample(audio_output, orig_sr=SR,
325
  target_sr=max_samplingrate,
326
  res_type='kaiser_best')
327
+ print(f'[timing] output resample: {time.time() - t_start:.2f}s')
328
 
329
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
330
  out_path = f.name
331
  save_wav(out_path, max_samplingrate, audio_output.T, subtype=max_subtype)
332
+ print(f'[timing] total: {time.time() - t_start:.2f}s')
333
 
334
  return out_path
335