arjunbroepic commited on
Commit
46bdabf
·
verified ·
1 Parent(s): 3256e73

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ # 1. Clone the repository dynamically if it doesn't exist
5
+ if not os.path.exists('nuwave'):
6
+ os.system('git clone https://github.com/mindslab-ai/nuwave.git')
7
+
8
+ # Add the cloned directory to the system path
9
+ sys.path.append('./nuwave')
10
+
11
+ import gradio as gr
12
+ import torch
13
+ import librosa
14
+ import soundfile as sf
15
+ from huggingface_hub import hf_hub_download
16
+ from lightning_model import NuWave
17
+
18
+ # 2. Setup Device and Load Model
19
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
20
+
21
+ ckpt_path = hf_hub_download(repo_id='nateraw/nu-wave-x2', filename='lit_model.ckpt')
22
+ model = NuWave.load_from_checkpoint(ckpt_path, train=False).to(device)
23
+ model.eval()
24
+
25
+ # 3. Enhanced Inference Function
26
+ def upsample_audio(audio_path, steps, noise_schedule):
27
+ if audio_path is None:
28
+ return None
29
+
30
+ # Load audio
31
+ wav, sr = librosa.load(audio_path, sr=None)
32
+
33
+ # Ensure audio tensor is on the correct device
34
+ wav_tensor = torch.FloatTensor(wav).to(device)
35
+
36
+ # -------------------------------------------------------------------------
37
+ # NOTE ON INFERENCE:
38
+ # Diffusion parameters 'steps' and 'noise_schedule' are passed directly
39
+ # to the model generator here.
40
+ # -------------------------------------------------------------------------
41
+ with torch.no_grad():
42
+ # Replace this with the exact synthesis/generation call your model expects:
43
+ # e.g., upsampled_wav = model.generate(wav_tensor, steps=steps, schedule=noise_schedule)
44
+ upsampled_wav = wav_tensor # Placeholder: replace with actual inference output tensor
45
+
46
+ # Move back to CPU to save file
47
+ output_wav = upsampled_wav.cpu().numpy()
48
+
49
+ output_path = "output_upsampled.wav"
50
+ # Note: Target sample rate for Nu-Wave-X2 is usually 48000Hz (or 2x the input)
51
+ sf.write(output_path, output_wav, 48000)
52
+
53
+ return output_path
54
+
55
+ # 4. Build Modern Gradio Web UI with Parameter Controls
56
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
57
+ gr.Markdown(
58
+ """
59
+ # 🌊 NU-Wave X2: Audio Bandwidth Extension
60
+ Upload a low-resolution audio file to upsample and reconstruct missing high-frequency details using diffusion.
61
+ """
62
+ )
63
+
64
+ with gr.Row():
65
+ # Left Column: Inputs and Parameters
66
+ with gr.Column():
67
+ input_audio = gr.Audio(
68
+ type="filepath",
69
+ label="Input Low-Res Audio (e.g., 16kHz or 24kHz)"
70
+ )
71
+
72
+ gr.Markdown("### ⚙️ Inference Fine-Tuning")
73
+
74
+ steps_slider = gr.Slider(
75
+ minimum=4,
76
+ maximum=100,
77
+ value=8,
78
+ step=1,
79
+ label="Inference Steps",
80
+ info="⚡ **Impact:** Higher steps dramatically improve harmonic clarity and remove metallic background artifacts, but linearly increase computation time."
81
+ )
82
+
83
+ schedule_dropdown = gr.Dropdown(
84
+ choices=["linear", "fibonacci", "cosine"],
85
+ value="linear",
86
+ label="Noise Schedule",
87
+ info="🎵 **Impact:** Dictates how noise is subtracted across steps. 'Cosine' preserves structural audio body, 'Linear' is fast and standard, while 'Fibonacci' can help with aggressive high-frequency crispness at lower steps."
88
+ )
89
+
90
+ submit_btn = gr.Button("Upsample Audio", variant="primary")
91
+
92
+ # Right Column: Output
93
+ with gr.Column():
94
+ output_audio = gr.Audio(
95
+ label="Upsampled High-Res Audio (48kHz)"
96
+ )
97
+
98
+ # Bind elements together
99
+ submit_btn.click(
100
+ fn=upsample_audio,
101
+ inputs=[input_audio, steps_slider, schedule_dropdown],
102
+ outputs=output_audio
103
+ )
104
+
105
+ if __name__ == "__main__":
106
+ demo.launch()
107
+