Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries and modules
|
| 2 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
import torch
|
| 5 |
+
from IPython.display import Audio
|
| 6 |
+
|
| 7 |
+
# Load the processor and model for text-to-speech
|
| 8 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 9 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 10 |
+
|
| 11 |
+
# Prepare the input text
|
| 12 |
+
text = "Don't count the days, make the days count."
|
| 13 |
+
inputs = processor(text=text, return_tensors="pt")
|
| 14 |
+
|
| 15 |
+
# Load the speaker embeddings dataset and select a specific speaker
|
| 16 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 17 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 18 |
+
|
| 19 |
+
# Generate the spectrogram for the speech
|
| 20 |
+
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
| 21 |
+
|
| 22 |
+
# Load the vocoder model to convert the spectrogram to speech waveform
|
| 23 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 24 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
| 25 |
+
|
| 26 |
+
# Play the generated speech
|
| 27 |
+
Audio(speech, rate=16000)
|