Spaces:
Sleeping
Sleeping
Update audio_generator.py
Browse files- audio_generator.py +39 -33
audio_generator.py
CHANGED
@@ -1,34 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
raise
|
|
|
1 |
+
# audio_generation.py
|
2 |
+
|
3 |
+
from transformers import AutoProcessor, BarkModel
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
from scipy.io.wavfile import write as write_wav
|
7 |
+
from pydub import AudioSegment
|
8 |
import os
|
9 |
+
import uuid
|
10 |
+
|
11 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
12 |
+
model = BarkModel.from_pretrained("suno/bark")
|
13 |
+
|
14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
model.to(device)
|
16 |
+
|
17 |
+
def split_text(text, max_len=150):
|
18 |
+
return [text[i:i+max_len] for i in range(0, len(text), max_len)]
|
19 |
+
|
20 |
+
def generate_audio(text, output_dir="audios"):
|
21 |
+
os.makedirs(output_dir, exist_ok=True)
|
22 |
+
chunks = split_text(text)
|
23 |
+
final_audio = AudioSegment.empty()
|
24 |
+
|
25 |
+
for idx, chunk in enumerate(chunks):
|
26 |
+
inputs = processor(chunk, return_tensors="pt").to(device)
|
27 |
+
audio_array = model.generate(**inputs)
|
28 |
+
audio_array = audio_array.cpu().numpy().squeeze()
|
29 |
+
audio_array = audio_array / np.max(np.abs(audio_array))
|
30 |
+
|
31 |
+
temp_path = os.path.join(output_dir, f"{uuid.uuid4()}.wav")
|
32 |
+
write_wav(temp_path, rate=22050, data=audio_array)
|
33 |
+
|
34 |
+
segment = AudioSegment.from_wav(temp_path)
|
35 |
+
final_audio += segment
|
36 |
+
os.remove(temp_path)
|
37 |
+
|
38 |
+
final_filename = os.path.join(output_dir, f"{uuid.uuid4()}_final.wav")
|
39 |
+
final_audio.export(final_filename, format="wav")
|
40 |
+
return final_filename
|
|