vedmistry commited on
Commit
9a89e2a
·
1 Parent(s): 912d93c

add multiband ensemble control, clean up debug prints

Browse files
Files changed (1) hide show
  1. app.py +46 -21
app.py CHANGED
@@ -1,30 +1,20 @@
1
  import sys
2
- print("Script started", flush=True)
3
- print(f"Python version: {sys.version}", flush=True)
4
  import os
5
- print("os imported", flush=True)
6
  import tempfile
7
- print("tempfile imported", flush=True)
8
  import soundfile as sf
9
- print("soundfile imported", flush=True)
10
  import torch
11
- print("torch imported", flush=True)
12
  from audiosr import build_model, super_resolution_long_audio
13
- print("audiosr imported", flush=True)
14
  from pyharp import ModelCard, build_endpoint
15
- print("pyharp imported", flush=True)
16
  import gradio as gr
17
- print("gradio imported", flush=True)
18
  import random
19
- print("random imported", flush=True)
 
 
20
 
21
- print("Setting env vars", flush=True)
22
  os.environ["TOKENIZERS_PARALLELISM"] = "true"
23
  torch.set_float32_matmul_precision("high")
24
- print("Env vars set", flush=True)
25
 
26
  model = None
27
- print("Model variable initialized", flush=True)
28
 
29
  def get_model():
30
  global model
@@ -34,16 +24,38 @@ def get_model():
34
  print("Model loaded.", flush=True)
35
  return model
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  model_card = ModelCard(
38
  name="AudioSR",
39
  description="Upsample any audio to 48kHz using audio super-resolution.",
40
  author="Haohe Liu, Ke Chen, Qiao Tian, Wenwu Wang, Mark D. Plumbley",
41
  tags=["audio", "super-resolution", "upsampling", "48kHz"],
42
  )
43
- print("Model card created", flush=True)
44
 
45
  @torch.inference_mode()
46
- def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: str) -> str:
47
  try:
48
  seed_val = int(seed) if seed and seed.strip() not in ("0", "") else random.randint(1, 2**32 - 1)
49
  except (TypeError, ValueError):
@@ -59,17 +71,26 @@ def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, se
59
 
60
  output = waveform.cpu().numpy()
61
 
62
- if output.shape[0] == 1:
63
- output = output.squeeze(0)
 
 
 
 
 
 
 
64
  else:
65
- output = output.T
 
 
 
66
 
67
  output_path = tempfile.mktemp(suffix=".wav")
68
  sf.write(output_path, output, samplerate=48000)
69
  return output_path
70
 
71
  with gr.Blocks() as demo:
72
- print("Gradio blocks created", flush=True)
73
  input_components = [
74
  gr.Audio(type="filepath", label="Input Audio").harp_required(True),
75
  gr.Slider(minimum=10, maximum=500, step=10, value=50,
@@ -79,6 +100,11 @@ with gr.Blocks() as demo:
79
  label="Guidance Scale",
80
  info="Higher values follow the conditioning more closely"),
81
  gr.Textbox(value="0", label="Seed", info="0 = random seed"),
 
 
 
 
 
82
  ]
83
  output_components = [
84
  gr.Audio(type="filepath", label="Output Audio (48kHz)").set_info("Audio upsampled to 48kHz."),
@@ -89,7 +115,6 @@ with gr.Blocks() as demo:
89
  output_components=output_components,
90
  process_fn=process_fn,
91
  )
92
- print("Endpoint built", flush=True)
93
 
94
  print("Launching Gradio...", flush=True)
95
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_error=True, pwa=True)
 
1
  import sys
 
 
2
  import os
 
3
  import tempfile
 
4
  import soundfile as sf
 
5
  import torch
 
6
  from audiosr import build_model, super_resolution_long_audio
 
7
  from pyharp import ModelCard, build_endpoint
 
8
  import gradio as gr
 
9
  import random
10
+ import numpy as np
11
+ import librosa
12
+ from scipy import signal
13
 
 
14
  os.environ["TOKENIZERS_PARALLELISM"] = "true"
15
  torch.set_float32_matmul_precision("high")
 
16
 
17
  model = None
 
18
 
19
  def get_model():
20
  global model
 
24
  print("Model loaded.", flush=True)
25
  return model
26
 
27
+ def match_array_shapes(array_1: np.ndarray, array_2: np.ndarray):
28
+ if (len(array_1.shape) == 1) & (len(array_2.shape) == 1):
29
+ if array_1.shape[0] > array_2.shape[0]:
30
+ array_1 = array_1[:array_2.shape[0]]
31
+ elif array_1.shape[0] < array_2.shape[0]:
32
+ array_1 = np.pad(array_1, ((array_2.shape[0] - array_1.shape[0], 0)), 'constant', constant_values=0)
33
+ else:
34
+ if array_1.shape[1] > array_2.shape[1]:
35
+ array_1 = array_1[:,:array_2.shape[1]]
36
+ elif array_1.shape[1] < array_2.shape[1]:
37
+ padding = array_2.shape[1] - array_1.shape[1]
38
+ array_1 = np.pad(array_1, ((0,0), (0,padding)), 'constant', constant_values=0)
39
+ return array_1
40
+
41
+ def lr_filter(audio, cutoff, filter_type, order=12, sr=48000):
42
+ audio = audio.T
43
+ nyquist = 0.5 * sr
44
+ normal_cutoff = cutoff / nyquist
45
+ b, a = signal.butter(order//2, normal_cutoff, btype=filter_type, analog=False)
46
+ sos = signal.tf2sos(b, a)
47
+ filtered_audio = signal.sosfiltfilt(sos, audio)
48
+ return filtered_audio.T
49
+
50
  model_card = ModelCard(
51
  name="AudioSR",
52
  description="Upsample any audio to 48kHz using audio super-resolution.",
53
  author="Haohe Liu, Ke Chen, Qiao Tian, Wenwu Wang, Mark D. Plumbley",
54
  tags=["audio", "super-resolution", "upsampling", "48kHz"],
55
  )
 
56
 
57
  @torch.inference_mode()
58
+ def process_fn(input_audio_path: str, ddim_steps: int, guidance_scale: float, seed: str, multiband_ensemble: bool, input_cutoff: int) -> str:
59
  try:
60
  seed_val = int(seed) if seed and seed.strip() not in ("0", "") else random.randint(1, 2**32 - 1)
61
  except (TypeError, ValueError):
 
71
 
72
  output = waveform.cpu().numpy()
73
 
74
+ if multiband_ensemble:
75
+ crossover_freq = int(input_cutoff) - 1000
76
+ low, _ = librosa.load(input_audio_path, sr=48000, mono=True)
77
+ out = output.squeeze() if output.ndim > 1 else output
78
+ out = match_array_shapes(out, low)
79
+ low = lr_filter(low, crossover_freq, 'lowpass', order=10)
80
+ high = lr_filter(out, crossover_freq, 'highpass', order=10)
81
+ high = lr_filter(high, 23000, 'lowpass', order=2)
82
+ output = low + high
83
  else:
84
+ if output.shape[0] == 1:
85
+ output = output.squeeze(0)
86
+ else:
87
+ output = output.T
88
 
89
  output_path = tempfile.mktemp(suffix=".wav")
90
  sf.write(output_path, output, samplerate=48000)
91
  return output_path
92
 
93
  with gr.Blocks() as demo:
 
94
  input_components = [
95
  gr.Audio(type="filepath", label="Input Audio").harp_required(True),
96
  gr.Slider(minimum=10, maximum=500, step=10, value=50,
 
100
  label="Guidance Scale",
101
  info="Higher values follow the conditioning more closely"),
102
  gr.Textbox(value="0", label="Seed", info="0 = random seed"),
103
+ gr.Checkbox(value=False, label="Multiband Ensemble",
104
+ info="Blend original low frequencies with upsampled highs"),
105
+ gr.Slider(minimum=4000, maximum=20000, step=1000, value=12000,
106
+ label="Input Cutoff (Hz)",
107
+ info="Crossover frequency for multiband ensemble"),
108
  ]
109
  output_components = [
110
  gr.Audio(type="filepath", label="Output Audio (48kHz)").set_info("Audio upsampled to 48kHz."),
 
115
  output_components=output_components,
116
  process_fn=process_fn,
117
  )
 
118
 
119
  print("Launching Gradio...", flush=True)
120
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_error=True, pwa=True)