File size: 872 Bytes
53826c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from transformers import pipeline
from datasets import load_dataset
import soundfile as sf
import torch

synthesiser = pipeline("text-to-speech", "microsoft/speecht5_tts")

def text_to_audio(text):
    # clean the response and max_size is 600
    text_clean = text.replace('\n', '').replace('*', '')
    text_550 = text_clean[:590]
    # get speaker embeddings
    embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
    speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
    # You can replace this embedding with your own as well.

    speech = synthesiser(text_550, forward_params={"speaker_embeddings": speaker_embedding})
    sf.write("output.wav", speech["audio"], samplerate=speech["sampling_rate"])
    audio_file = open("output.wav", "rb")
    audio_bytes = audio_file.read()
    return audio_bytes