owiedotch commited on
Commit
c1782ed
1 Parent(s): fb74527

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import gradio as gr
2
  import torch
3
  import torchaudio
4
- from transformers import EncodecModel
5
  from encodec.utils import convert_audio
6
- from encodec.compress import compress_to_file, decompress_from_file # Import from the correct module
7
  import io
8
 
9
  # Load the Encodec model
10
- model = EncodecModel.from_pretrained("facebook/encodec_48khz")
11
- model.set_target_bandwidth(6.0) # Set the desired bandwidth (adjust as needed)
12
 
13
  @spaces.GPU
14
  def encode(audio_file_path):
@@ -16,10 +16,13 @@ def encode(audio_file_path):
16
  # Load and pre-process the audio waveform
17
  wav, sr = torchaudio.load(audio_file_path)
18
  wav = convert_audio(wav, sr, model.sample_rate, model.channels)
 
19
 
20
  # Compress to ecdc file in memory
21
- output = io.BytesIO()
22
- compress_to_file(model, wav, output)
 
 
23
  output.seek(0)
24
 
25
  return output
@@ -31,8 +34,11 @@ def encode(audio_file_path):
31
  @spaces.GPU
32
  def decode(compressed_audio_file):
33
  try:
 
 
 
34
  # Decompress audio
35
- wav, sr = decompress_from_file(compressed_audio_file)
36
 
37
  # Convert the decoded audio to a numpy array for Gradio output
38
  decoded_audio = wav.cpu().numpy()
 
1
  import gradio as gr
2
  import torch
3
  import torchaudio
4
+ from encodec import EncodecModel
5
  from encodec.utils import convert_audio
6
+ from encodec.compress import compress, decompress
7
  import io
8
 
9
  # Load the Encodec model
10
+ model = EncodecModel.encodec_model_48khz() # Use the encodec version of the model
11
+ model.set_target_bandwidth(6.0) # Set the desired bandwidth
12
 
13
  @spaces.GPU
14
  def encode(audio_file_path):
 
16
  # Load and pre-process the audio waveform
17
  wav, sr = torchaudio.load(audio_file_path)
18
  wav = convert_audio(wav, sr, model.sample_rate, model.channels)
19
+ wav = wav.unsqueeze(0)
20
 
21
  # Compress to ecdc file in memory
22
+ compressed_audio = compress(model, wav)
23
+
24
+ # Save compressed audio to BytesIO
25
+ output = io.BytesIO(compressed_audio)
26
  output.seek(0)
27
 
28
  return output
 
34
  @spaces.GPU
35
  def decode(compressed_audio_file):
36
  try:
37
+ # Load compressed audio
38
+ compressed_audio = compressed_audio_file.read()
39
+
40
  # Decompress audio
41
+ wav, sr = decompress(compressed_audio)
42
 
43
  # Convert the decoded audio to a numpy array for Gradio output
44
  decoded_audio = wav.cpu().numpy()