Views
No views yet
SpeechT5 architecture, which integrates both speech recognition and synthesis into a unified framework. It is fine-tuned for Text-to-Speech (TTS) using a custom-trained tokenizer and an adapted configuration that accounts for the unique vocabulary of the Wolof language. The fine-tuning process was carried out using a dataset containing text in Wolof to help the model synthesize speech that captures the nuances of the language.!pip install transformers datasets1import torch
2from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
3from transformers import SpeechT5HifiGan
4
5def load_speech_model(checkpoint="bilalfaye/speecht5_tts-wolof", vocoder_checkpoint="microsoft/speecht5_hifigan"):
6 """
7 Load the SpeechT5 model, processor, and vocoder for text-to-speech.
8
9 Args:
10 checkpoint (str): The model checkpoint for SpeechT5 TTS.
11 vocoder_checkpoint (str): The checkpoint for the HiFi-GAN vocoder.
12
13 Returns:
14 processor: The processor for the model.
15 model: The loaded SpeechT5 model.
16 vocoder: The loaded HiFi-GAN vocoder.
17 device: The device (CPU or GPU) the model is loaded on.
18 """
19 # Check for GPU availability and set device accordingly
20 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
22 # Load the SpeechT5 processor and model
23 processor = SpeechT5Processor.from_pretrained(checkpoint)
24 model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint).to(device) # Move model to the correct device
25
26 # Load the HiFi-GAN vocoder
27 vocoder = SpeechT5HifiGan.from_pretrained(vocoder_checkpoint).to(device) # Move vocoder to the correct device
28
29 return processor, model, vocoder, device
30
31# Example usage
32processor, model, vocoder, device = load_speech_model()
33
34# Verify the device being used
35print(f"Model and vocoder loaded on device: {device}")
36
37from datasets import load_dataset
38# Load speaker embeddings (this dataset contains speaker-specific embeddings)
39embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
40speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
41
42import torch
43from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
44from IPython.display import Audio, display
45
46def generate_speech_from_text(text,
47 speaker_embedding=speaker_embedding,
48 processor=processor,
49 model=model,
50 vocoder=vocoder):
51 """
52 Generates speech from a given text using SpeechT5 and HiFi-GAN vocoder.
53
54 Args:
55 text (str): The input text to be converted to speech.
56 checkpoint (str): The model checkpoint for SpeechT5 TTS.
57 vocoder_checkpoint (str): The checkpoint for the HiFi-GAN vocoder.
58 speaker_embedding (torch.Tensor): The speaker embedding tensor.
59 processor (SpeechT5Processor): The processor for the model.
60 model (SpeechT5ForTextToSpeech): The loaded SpeechT5 model.
61 vocoder (SpeechT5HifiGan): The loaded HiFi-GAN vocoder.
62
63 Returns:
64 None
65 """
66 # Parameters for text-to-speech generation
67 max_text_positions = model.config.max_text_positions # Token limit
68 max_length = model.config.max_length * 1.2 # Slightly extended max_length
69 min_length = max_length // 3 # Adjust based on max_length
70 num_beams = 7 # Use beam search for better quality
71 temperature = 0.6 # Reduce temperature for stability
72
73 # Tokenize the input text and move input tensor to the correct device
74 inputs = processor(text=text, return_tensors="pt", padding=True, truncation=True, max_length=max_text_positions)
75 inputs = {key: value.to(model.device) for key, value in inputs.items()} # Move inputs to device
76
77 # Generate speech
78 speech = model.generate(
79 inputs["input_ids"],
80 speaker_embeddings=speaker_embedding.to(model.device), # Ensure speaker_embedding is also on the correct device
81 vocoder=vocoder,
82 max_length=int(max_length),
83 min_length=int(min_length),
84 num_beams=num_beams,
85 temperature=temperature,
86 no_repeat_ngram_size=3,
87 repetition_penalty=1.5,
88 eos_token_id=None,
89 use_cache=True
90 )
91
92 # Detach the speech from the computation graph and move it to CPU
93 speech = speech.detach().cpu().numpy()
94
95 # Play the generated speech using IPython Audio
96 display(Audio(speech, rate=16000))
97
98
99# Example usage
100text = "ñu ne ñoom ñooy nattukaay satélite yi"
101generate_speech_from_text(text)| Epoch | Training Loss | Validation Loss |
|---|---|---|
| 26 | 0.3894 | 0.3687 |
| 27 | 0.3858 | 0.3712 |
| 28 | 0.3874 | 0.3669 |
| 29 | 0.3887 | 0.3685 |
| 30 | 0.3854 | 0.3670 |
| 32 | 0.3856 | 0.3697 |