Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
|
4 |
-
from transformers import EncodecModel
|
5 |
-
import
|
6 |
-
from encodec import
|
7 |
import io
|
8 |
|
9 |
-
# Load the Encodec model
|
10 |
model = EncodecModel.from_pretrained("facebook/encodec_48khz")
|
11 |
-
|
12 |
|
13 |
@spaces.GPU
|
14 |
def encode(audio_file_path):
|
15 |
try:
|
16 |
-
#
|
17 |
-
|
18 |
-
|
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 |
-
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
|
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 =
|
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 |
|