Sinhala Text-to-Speech — A
Coqui TTS VITS model that generates natural Sinhala speech from text, with
16 distinct voices to choose from.
1import torch
2from TTS.tts.configs.vits_config import VitsConfig
3from TTS.tts.models.vits import Vits
4from TTS.tts.utils.text import TTSTokenizer
5from TTS.tts.utils.speakers import SpeakerManager
6from TTS.utils.audio import AudioProcessor
7
8# Load config
9config = VitsConfig()
10config.load_json("config.json")
11
12# Initialize components
13ap = AudioProcessor.init_from_config(config)
14tokenizer, new_config = TTSTokenizer.init_from_config(config)
15speaker_manager = SpeakerManager()
16speaker_manager.load_ids_from_file("speakers.json")
17
18# Create and load model
19model = Vits(new_config, ap, tokenizer, speaker_manager)
20from safetensors.torch import load_file
21state_dict = load_file("sinhala_tts_vits_model.safetensors")
22model.load_state_dict(state_dict, strict=False)
23model.eval()
24
25# Synthesize
26text = "ආයුබෝවන්! ඔබට කොහොමද?"
27outputs = model.synthesize(text, config=new_config, speaker="mettananda")
28
29# Save audio
30import soundfile as sf
31sf.write("output.wav", outputs["wav"], 16000)
1# Start the server
2python server.py
3
4# Generate speech
5curl -X POST http://localhost:8081/tts \
6 -H "Content-Type: application/json" \
7 -d '{
8 "text": "ආයුබෝවන්!",
9 "speaker": "mettananda",
10 "emotion": "neutral"
11 }' \
12 --output output.wav
13
14# Health check
15curl http://localhost:8081/health
16
17# List speakers
18curl http://localhost:8081/speakers
1docker build -t sinhala-tts-server .
2docker run -p 8081:8081 sinhala-tts-server
For detailed instructions, see the
DEVELOPER_GUIDE.md which covers: