Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,12 @@ import jax.numpy as jnp
|
|
4 |
import librosa
|
5 |
import dac_jax
|
6 |
from dac_jax.audio_utils import volume_norm, db2linear
|
7 |
-
import io
|
8 |
import soundfile as sf
|
9 |
import spaces
|
10 |
import tempfile
|
11 |
import os
|
12 |
import numpy as np
|
13 |
|
14 |
-
# Global variable to store the temporary file path
|
15 |
-
temp_file_path = None
|
16 |
-
|
17 |
# Check for CUDA availability and set device
|
18 |
try:
|
19 |
import jax.tools.colab_tpu
|
@@ -25,41 +21,43 @@ except:
|
|
25 |
# Load the DAC model with padding set to False for chunking
|
26 |
model, variables = dac_jax.load_model(model_type="44khz", padding=False)
|
27 |
|
28 |
-
#
|
|
|
29 |
@jax.jit
|
30 |
def compress_chunk(x):
|
31 |
return model.apply(variables, x, method='compress_chunk')
|
32 |
|
|
|
33 |
@jax.jit
|
34 |
def decompress_chunk(c):
|
35 |
return model.apply(variables, c, method='decompress_chunk')
|
36 |
|
|
|
|
|
|
|
|
|
|
|
37 |
@spaces.GPU
|
38 |
def encode(audio_file_path):
|
39 |
-
global temp_file_path
|
40 |
try:
|
41 |
-
# Load
|
42 |
-
signal, sample_rate = librosa.load(audio_file_path, sr=44100
|
|
|
43 |
|
44 |
signal = jnp.array(signal, dtype=jnp.float32)
|
45 |
-
|
46 |
-
signal = jnp.expand_dims(signal, axis=0)
|
47 |
|
48 |
# Set chunk duration based on available GPU memory (adjust as needed)
|
49 |
-
win_duration =
|
50 |
|
51 |
# Compress using chunking
|
52 |
dac_file = model.compress(compress_chunk, signal, sample_rate, win_duration=win_duration)
|
53 |
|
54 |
-
# Save the compressed DAC file to a
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".dac") as temp_file:
|
59 |
-
dac_file.save(temp_file.name)
|
60 |
-
temp_file_path = temp_file.name
|
61 |
|
62 |
-
return
|
63 |
|
64 |
except Exception as e:
|
65 |
gr.Warning(f"An error occurred during encoding: {e}")
|
@@ -83,13 +81,6 @@ def decode(compressed_dac_file):
|
|
83 |
gr.Warning(f"An error occurred during decoding: {e}")
|
84 |
return None
|
85 |
|
86 |
-
def cleanup(audio_file_path):
|
87 |
-
global temp_file_path
|
88 |
-
if temp_file_path and os.path.exists(temp_file_path):
|
89 |
-
os.remove(temp_file_path)
|
90 |
-
temp_file_path = None
|
91 |
-
return None
|
92 |
-
|
93 |
# Gradio interface
|
94 |
with gr.Blocks() as demo:
|
95 |
gr.Markdown("<h1 style='text-align: center;'>Audio Compression with DAC-JAX</h1>")
|
@@ -102,7 +93,6 @@ with gr.Blocks() as demo:
|
|
102 |
encoded_output = gr.File(label="Compressed Audio (.dac)")
|
103 |
|
104 |
encode_button.click(encode, inputs=audio_input, outputs=encoded_output)
|
105 |
-
encoded_output.change(cleanup, inputs=[audio_input], outputs=None)
|
106 |
|
107 |
with gr.Tab("Decode"):
|
108 |
with gr.Row():
|
|
|
4 |
import librosa
|
5 |
import dac_jax
|
6 |
from dac_jax.audio_utils import volume_norm, db2linear
|
|
|
7 |
import soundfile as sf
|
8 |
import spaces
|
9 |
import tempfile
|
10 |
import os
|
11 |
import numpy as np
|
12 |
|
|
|
|
|
|
|
13 |
# Check for CUDA availability and set device
|
14 |
try:
|
15 |
import jax.tools.colab_tpu
|
|
|
21 |
# Load the DAC model with padding set to False for chunking
|
22 |
model, variables = dac_jax.load_model(model_type="44khz", padding=False)
|
23 |
|
24 |
+
# GPU-accelerated and jit-compiled chunk processing functions
|
25 |
+
@spaces.GPU
|
26 |
@jax.jit
|
27 |
def compress_chunk(x):
|
28 |
return model.apply(variables, x, method='compress_chunk')
|
29 |
|
30 |
+
@spaces.GPU
|
31 |
@jax.jit
|
32 |
def decompress_chunk(c):
|
33 |
return model.apply(variables, c, method='decompress_chunk')
|
34 |
|
35 |
+
def ensure_mono(audio, sr):
|
36 |
+
if audio.ndim > 1:
|
37 |
+
return librosa.to_mono(audio.T), sr
|
38 |
+
return audio, sr
|
39 |
+
|
40 |
@spaces.GPU
|
41 |
def encode(audio_file_path):
|
|
|
42 |
try:
|
43 |
+
# Load and ensure mono audio
|
44 |
+
signal, sample_rate = librosa.load(audio_file_path, sr=44100)
|
45 |
+
signal, sample_rate = ensure_mono(signal, sample_rate)
|
46 |
|
47 |
signal = jnp.array(signal, dtype=jnp.float32)
|
48 |
+
signal = jnp.expand_dims(signal, axis=(0, 1)) # Add batch and channel dimensions
|
|
|
49 |
|
50 |
# Set chunk duration based on available GPU memory (adjust as needed)
|
51 |
+
win_duration = 1.0
|
52 |
|
53 |
# Compress using chunking
|
54 |
dac_file = model.compress(compress_chunk, signal, sample_rate, win_duration=win_duration)
|
55 |
|
56 |
+
# Save the compressed DAC file to a file in the current directory
|
57 |
+
output_path = "compressed_audio.dac"
|
58 |
+
dac_file.save(output_path)
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
return output_path
|
61 |
|
62 |
except Exception as e:
|
63 |
gr.Warning(f"An error occurred during encoding: {e}")
|
|
|
81 |
gr.Warning(f"An error occurred during decoding: {e}")
|
82 |
return None
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
# Gradio interface
|
85 |
with gr.Blocks() as demo:
|
86 |
gr.Markdown("<h1 style='text-align: center;'>Audio Compression with DAC-JAX</h1>")
|
|
|
93 |
encoded_output = gr.File(label="Compressed Audio (.dac)")
|
94 |
|
95 |
encode_button.click(encode, inputs=audio_input, outputs=encoded_output)
|
|
|
96 |
|
97 |
with gr.Tab("Decode"):
|
98 |
with gr.Row():
|