Spaces:
Running
Running
Create text_to_audio.py
Browse files- text_to_audio.py +21 -0
text_to_audio.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from datasets import load_dataset
|
3 |
+
import soundfile as sf
|
4 |
+
import torch
|
5 |
+
|
6 |
+
synthesiser = pipeline("text-to-speech", "microsoft/speecht5_tts")
|
7 |
+
|
8 |
+
def text_to_audio(text):
|
9 |
+
# clean the response and max_size is 600
|
10 |
+
text_clean = text.replace('\n', '').replace('*', '')
|
11 |
+
text_550 = text_clean[:590]
|
12 |
+
# get speaker embeddings
|
13 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
14 |
+
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
15 |
+
# You can replace this embedding with your own as well.
|
16 |
+
|
17 |
+
speech = synthesiser(text_550, forward_params={"speaker_embeddings": speaker_embedding})
|
18 |
+
sf.write("output.wav", speech["audio"], samplerate=speech["sampling_rate"])
|
19 |
+
audio_file = open("output.wav", "rb")
|
20 |
+
audio_bytes = audio_file.read()
|
21 |
+
return audio_bytes
|