owiedotch commited on
Commit
5f18cf7
1 Parent(s): 9fbeded

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -43
app.py CHANGED
@@ -3,94 +3,60 @@ import jax
3
  import jax.numpy as jnp
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
16
- jax.tools.colab_tpu.setup_tpu()
17
- print("Connected to TPU")
18
- except:
19
- print("No TPU detected, using GPU or CPU.")
20
-
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 = 2.0 # Reduced win_duration for testing
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 temporary file
57
  with tempfile.NamedTemporaryFile(delete=False, suffix=".dac") as temp_file:
58
  dac_file.save(temp_file.name)
59
 
60
- # Check file size
61
- file_size = os.path.getsize(temp_file.name)
62
- if file_size == 0:
63
- print("WARNING: Compressed file size is 0 bytes!")
64
-
65
- # Return the temporary file path
66
  return temp_file.name
67
 
68
  except Exception as e:
69
- print(f"ERROR during encoding: {e}")
70
  gr.Warning(f"An error occurred during encoding: {e}")
71
  return None
72
 
73
  @spaces.GPU
74
  def decode(compressed_dac_file):
75
  try:
76
- # Create a temporary file to save the uploaded content
77
  with tempfile.NamedTemporaryFile(delete=False, suffix=".dac") as temp_file:
78
  temp_file.write(compressed_dac_file.file.read())
79
  temp_file_path = temp_file.name
80
 
81
- # Load the compressed DAC file
82
  dac_file = dac_jax.DACFile.load(temp_file_path)
83
-
84
- # Decompress using chunking
85
  y = model.decompress(decompress_chunk, dac_file)
86
-
87
- # Convert to numpy array and squeeze to remove extra dimensions
88
  decoded_audio = np.array(y).squeeze()
89
 
90
- # Delete the temporary file
91
  os.unlink(temp_file_path)
92
-
93
- return (44100, decoded_audio) # Return sample rate and audio data
94
 
95
  except Exception as e:
96
  gr.Warning(f"An error occurred during decoding: {e}")
 
3
  import jax.numpy as jnp
4
  import librosa
5
  import dac_jax
 
 
6
  import spaces
7
  import tempfile
8
  import os
9
  import numpy as np
10
 
 
 
 
 
 
 
 
 
11
  # Load the DAC model with padding set to False for chunking
12
  model, variables = dac_jax.load_model(model_type="44khz", padding=False)
13
 
14
+ # Jit-compile these functions because they're used inside a loop over chunks.
 
15
  @jax.jit
16
  def compress_chunk(x):
17
  return model.apply(variables, x, method='compress_chunk')
18
 
 
19
  @jax.jit
20
  def decompress_chunk(c):
21
  return model.apply(variables, c, method='decompress_chunk')
22
 
 
 
 
 
 
23
  @spaces.GPU
24
  def encode(audio_file_path):
25
  try:
26
+ # Load audio with librosa, specifying duration
27
+ signal, sample_rate = librosa.load(audio_file_path, sr=44100, mono=True, duration=.5) # Set duration as needed
 
28
 
29
  signal = jnp.array(signal, dtype=jnp.float32)
30
+ while signal.ndim < 3:
31
+ signal = jnp.expand_dims(signal, axis=0)
 
 
32
 
33
+ win_duration = 0.5 # Adjust based on your GPU's memory size
34
  dac_file = model.compress(compress_chunk, signal, sample_rate, win_duration=win_duration)
35
 
36
+ # Save to a temporary file
37
  with tempfile.NamedTemporaryFile(delete=False, suffix=".dac") as temp_file:
38
  dac_file.save(temp_file.name)
39
 
 
 
 
 
 
 
40
  return temp_file.name
41
 
42
  except Exception as e:
 
43
  gr.Warning(f"An error occurred during encoding: {e}")
44
  return None
45
 
46
  @spaces.GPU
47
  def decode(compressed_dac_file):
48
  try:
49
+ # Load from the uploaded file
50
  with tempfile.NamedTemporaryFile(delete=False, suffix=".dac") as temp_file:
51
  temp_file.write(compressed_dac_file.file.read())
52
  temp_file_path = temp_file.name
53
 
 
54
  dac_file = dac_jax.DACFile.load(temp_file_path)
 
 
55
  y = model.decompress(decompress_chunk, dac_file)
 
 
56
  decoded_audio = np.array(y).squeeze()
57
 
 
58
  os.unlink(temp_file_path)
59
+ return (44100, decoded_audio)
 
60
 
61
  except Exception as e:
62
  gr.Warning(f"An error occurred during decoding: {e}")