owiedotch commited on
Commit
7e97379
1 Parent(s): b3d6982

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -23
app.py CHANGED
@@ -1,32 +1,28 @@
1
  import gradio as gr
2
  import torch
3
- from datasets import load_dataset, Audio
4
- from transformers import EncodecModel, AutoProcessor
5
- import spaces
6
- from encodec import compress, decompress
7
  import io
8
 
9
- # Load the Encodec model and processor
10
  model = EncodecModel.from_pretrained("facebook/encodec_48khz")
11
- processor = AutoProcessor.from_pretrained("facebook/encodec_48khz")
12
 
13
  @spaces.GPU
14
  def encode(audio_file_path):
15
  try:
16
- # Open the audio file
17
- with open(audio_file_path, "rb") as audio_file:
18
- # Load and preprocess the audio
19
- audio_sample, sampling_rate = load_dataset("audiofolder", data_dir=audio_file_path, split="train")[0]["audio"]
20
- wav = torch.tensor(audio_sample).unsqueeze(0)
21
 
22
- # Compress to ecdc
23
- compressed_audio = compress(model, wav)
 
 
24
 
25
- # Save compressed audio to BytesIO
26
- output = io.BytesIO(compressed_audio)
27
- output.seek(0)
28
-
29
- return output
30
 
31
  except Exception as e:
32
  gr.Warning(f"An error occurred during encoding: {e}")
@@ -35,12 +31,9 @@ def encode(audio_file_path):
35
  @spaces.GPU
36
  def decode(compressed_audio_file):
37
  try:
38
- # Load compressed audio
39
- compressed_audio = compressed_audio_file.read()
40
-
41
  # Decompress audio
42
- wav, sr = decompress(compressed_audio)
43
-
44
  # Convert the decoded audio to a numpy array for Gradio output
45
  decoded_audio = wav.cpu().numpy()
46
 
 
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 import compress_to_file, decompress_from_file
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):
15
  try:
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
 
 
 
 
26
 
27
  except Exception as e:
28
  gr.Warning(f"An error occurred during encoding: {e}")
 
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()
39