Spaces:
Running
Running
Rename audio_generation.py to AudioGeneration.py
Browse files- AudioGeneration.py +9 -0
- audio_generation.py +0 -40
AudioGeneration.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from TTS.api import TTS
|
2 |
+
|
3 |
+
# Load the model once
|
4 |
+
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
5 |
+
|
6 |
+
def generate_audio(text, filename):
|
7 |
+
output_path = filename
|
8 |
+
tts_model.tts_to_file(text=text, file_path=output_path)
|
9 |
+
return output_path
|
audio_generation.py
DELETED
@@ -1,40 +0,0 @@
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|