TeraSpace commited on
Commit
d44113b
1 Parent(s): 552dea6

Delete infer_onnx.py

Browse files
Files changed (1) hide show
  1. infer_onnx.py +0 -97
infer_onnx.py DELETED
@@ -1,97 +0,0 @@
1
- import os
2
- import re
3
- import onnxruntime
4
- import numpy as np
5
- from huggingface_hub import snapshot_download
6
- from gruut import sentences
7
- import numpy as np
8
- import scipy.io.wavfile
9
-
10
-
11
-
12
- class TTS:
13
- def __init__(self, model_name: str, save_path: str = "./model", add_time_to_end: float = 0.8) -> None:
14
- if not os.path.exists(save_path):
15
- os.mkdir(save_path)
16
-
17
- model_dir = os.path.join(save_path, model_name)
18
-
19
- if not os.path.exists(model_dir):
20
- snapshot_download(repo_id=model_name,
21
- allow_patterns=["*.txt", "*.onnx"],
22
- local_dir=model_dir,
23
- local_dir_use_symlinks=False
24
- )
25
-
26
- sess_options = onnxruntime.SessionOptions()
27
- self.model = onnxruntime.InferenceSession(os.path.join(model_dir, "exported/model.onnx"), sess_options=sess_options)
28
-
29
- with open(os.path.join(model_dir, "exported/vocab.txt"), "r", encoding="utf-8") as vocab_file:
30
- self.symbols = vocab_file.read().split("\n")
31
- self.symbols = list(map(chr, list(map(int, self.symbols))))
32
-
33
- self.symbol_to_id = {s: i for i, s in enumerate(self.symbols)}
34
- self.add_time_to_end = add_time_to_end
35
-
36
-
37
- def _ru_phonems(self, text: str) -> str:
38
- text = text.lower()
39
- phonemes = ""
40
- for sent in sentences(text, lang="ru"):
41
- for word in sent:
42
- if word.phonemes:
43
- phonemes += "".join(word.phonemes)
44
- phonemes = re.sub(re.compile(r'\s+'), ' ', phonemes).lstrip().rstrip()
45
- return phonemes
46
-
47
-
48
- def _text_to_sequence(self, text: str) -> list[int]:
49
- '''convert text to seq'''
50
- sequence = []
51
- clean_text = self._ru_phonems(text)
52
- for symbol in clean_text:
53
- symbol_id = self.symbol_to_id[symbol]
54
- sequence += [symbol_id]
55
- return sequence
56
-
57
-
58
- def _intersperse(self, lst, item):
59
- result = [item] * (len(lst) * 2 + 1)
60
- result[1::2] = lst
61
- return result
62
-
63
-
64
- def _get_text(self, text: str) -> list[int]:
65
- text_norm = self._text_to_sequence(text)
66
- text_norm = self._intersperse(text_norm, 0)
67
- return text_norm
68
-
69
- def _add_silent(self, audio, silence_duration: float = 0.7, sample_rate: int = 22050):
70
- num_samples_silence = int(sample_rate * silence_duration)
71
- silence_array = np.zeros(num_samples_silence, dtype=np.float32)
72
- audio_with_silence = np.concatenate((audio, silence_array), axis=0)
73
- return audio_with_silence
74
-
75
- def save_wav(self, audio, path:str):
76
- '''save audio to wav'''
77
- scipy.io.wavfile.write(path, 22050, audio)
78
-
79
- def __call__(self, text: str, play = False):
80
- phoneme_ids = self._get_text(text)
81
- text = np.expand_dims(np.array(phoneme_ids, dtype=np.int64), 0)
82
- text_lengths = np.array([text.shape[1]], dtype=np.int64)
83
- scales = np.array(
84
- [0.667, 1, 0.8],
85
- dtype=np.float32,
86
- )
87
- audio = self.model.run(
88
- None,
89
- {
90
- "input": text,
91
- "input_lengths": text_lengths,
92
- "scales": scales,
93
- "sid": None,
94
- },
95
- )[0][0,0][0]
96
- audio = self._add_silent(audio)
97
- return audio