Update Main.py
Browse files
Main.py
CHANGED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Following pip packages need to be installed:
|
2 |
+
!pip install git+https://github.com/huggingface/transformers sentencepiece datasets
|
3 |
+
|
4 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
5 |
+
from datasets import load_dataset
|
6 |
+
import torch
|
7 |
+
import soundfile as sf
|
8 |
+
from datasets import load_dataset
|
9 |
+
|
10 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
11 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
12 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
13 |
+
|
14 |
+
inputs = processor(text="Hello, all this is a text to speech converter. Just change the embeddings_dataset number to try out different voices.", return_tensors="pt")
|
15 |
+
|
16 |
+
# load xvector containing speaker's voice characteristics from a dataset
|
17 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
18 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[5000]["xvector"]).unsqueeze(0)
|
19 |
+
|
20 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
21 |
+
|
22 |
+
sf.write("speech.wav", speech.numpy(), samplerate=16000)
|
23 |
+
|
24 |
+
from IPython.display import Audio
|
25 |
+
|
26 |
+
Audio(speech, rate=16000)
|