Views
No views yet
pip install --upgrade pip
pip install --upgrade transformers sentencepiece datasets[audio]Text-to-Speech (TTS) pipeline. You can access the Arabic SPeechT5 model via the TTS pipeline in just a few lines of code!1from transformers import pipeline
2from datasets import load_dataset
3import soundfile as sf
4
5synthesiser = pipeline("text-to-speech", "("Messam174/speecht5_finetuned_essam2_ar")
6
7embeddings_dataset = load_dataset("herwoww/arabic_xvector_embeddings", split="validation")
8speaker_embedding = torch.tensor(embeddings_dataset[105]["speaker_embeddings"]).unsqueeze(0)
9# You can replace this embedding with your own as well.
10
11speech = synthesiser("السلام عليكم ورحمة الله وبركاته حياكم الله جميعا", forward_params={"speaker_embeddings": speaker_embedding})
12# ArTST is trained without diacritics.
13
14sf.write("speech.wav", speech["audio"], samplerate=speech["sampling_rate"])1from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
2from datasets import load_dataset
3import torch
4import soundfile as sf
5from pydub import AudioSegment
6
7# Check if GPU is available
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9print(f"Using device: {device}")
10
11# Load processor, model, and vocoder
12processor = SpeechT5Processor.from_pretrained("Messam174/speecht5_finetuned_essam2_ar")
13model = SpeechT5ForTextToSpeech.from_pretrained("Messam174/speecht5_finetuned_essam2_ar").to(device)
14vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
15
16# Prepare inputs
17inputs = processor(
18 text="السلام عليكم ورحمة الله وبركاته حياكم الله جميعا", return_tensors="pt"
19).to(device)
20
21# Load xvector containing speaker's voice characteristics from a dataset
22embeddings_dataset = load_dataset("herwoww/arabic_xvector_embeddings", split="validation")
23speaker_embedding = torch.tensor(embeddings_dataset[105]["speaker_embeddings"]).unsqueeze(0).to(device)
24
25# Generate speech
26with torch.no_grad(): # Disable gradient computation for inference
27 speech = model.generate_speech(inputs["input_ids"], speaker_embedding, vocoder=vocoder)
28
29# Save the output as WAV
30wav_file = "speech.wav"
31sf.write(wav_file, speech.cpu().numpy(), samplerate=16000)
32print(f"Speech saved to '{wav_file}'")
33
34
35| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 0.3806 | 0.3742 | 100 | 0.3452 |
| 0.3873 | 0.7484 | 200 | 0.3487 |
| 0.3788 | 1.1225 | 300 | 0.3441 |
| 0.3676 | 1.4967 | 400 | 0.3380 |
| 0.3668 | 1.8709 | 500 | 0.3333 |