Views
No views yet
1pip3 install git+https://github.com/mesolitica/malaya-speech
2pip3 install malaya1from huggingface_hub import snapshot_download
2from malaya_speech.torch_model.vits.model_infer import SynthesizerTrn
3from malaya_speech.torch_model.vits.commons import intersperse
4from malaya_speech.utils.text import TTS_SYMBOLS
5from malaya_speech.tts import load_text_ids
6import torch
7import os
8import json
9
10try:
11 from malaya_boilerplate.hparams import HParams
12except BaseException:
13 from malaya_boilerplate.train.config import HParams
14
15speaker_id = {
16 'Husein': 0,
17 'Shafiqah Idayu': 1,
18 'Anwar Ibrahim': 2,
19}
20
21folder = snapshot_download(repo_id="malaysia-ai/malay-VITS-multispeaker")
22
23with open(os.path.join(folder, 'config.json')) as fopen:
24 hps = HParams(**json.load(fopen))
25
26model = SynthesizerTrn(
27 len(TTS_SYMBOLS),
28 hps.data.filter_length // 2 + 1,
29 hps.train.segment_size // hps.data.hop_length,
30 n_speakers=hps.data.n_speakers,
31 **hps.model,
32).eval()
33model.load_state_dict(torch.load(os.path.join(folder, 'model.pth'), map_location='cpu'))
34
35
36normalizer = load_text_ids(pad_to = None, understand_punct = True, is_lower = False)
37
38t, ids = normalizer.normalize('saya nak makan nasi ayam yang sedap, lagi lazat, dan hidup sangatlah susah kan.', add_fullstop = False)
39if hps.data.add_blank:
40 ids = intersperse(ids, 0)
41ids = torch.LongTensor(ids)
42ids_lengths = torch.LongTensor([ids.size(0)])
43ids = ids.unsqueeze(0)
44sid = torch.tensor([speaker_id['Husein']])
45
46with torch.no_grad():
47 audio = model.infer(
48 ids,
49 ids_lengths,
50 noise_scale=0.0,
51 noise_scale_w=0.0,
52 length_scale=1.0,
53 sid=sid,
54 )
55 y_ = audio[0].numpy()