-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_creator.py
64 lines (54 loc) · 1.86 KB
/
demo_creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from model import TTS
from typing import List
from pathlib import Path
import soundfile as sf
from rich.console import Console
import yaml
class Demo:
name: str
query: List[str]
audio: List[str]
console: Console
def __init__(self, name, query, console=Console(), root="./demo", **kwargs) -> None:
self.name = name
self.query = query
self.console = console
self.root = Path(root)
self.audio = [
f"query_{str(i + 1).zfill(2)}.wav" for i in range(len(self.query))
]
if "audio" in kwargs:
self.audio = kwargs["audio"]
assert len(self.audio) == len(
self.query
), "The number of audio names does not equal to the query."
cfg = {
"model-id": "microsoft/speecht5_tts",
"vocoder": "microsoft/speecht5_hifigan",
"voice_path": "xvectors.json",
}
self.tts = TTS(cfg, False, console)
def create_dirtree(self):
demo_dir = self.root / self.name
demo_dir.mkdir(parents=True, exist_ok=True)
cfg = demo_dir / "config.yaml"
content = {"name": self.name, "query": self.query, "audio": self.audio}
with cfg.open("w") as f:
yaml.safe_dump(content, f)
def create_speech(self):
self.tts()
for q, a in zip(self.query, self.audio):
path = Path(self.root / self.name / a)
speech = self.tts.inference(q)
sf.write(str(path), speech, samplerate=16000)
print(f"Audio file {a} is written.")
def __call__(self):
self.create_dirtree()
self.create_speech()
if __name__ == "__main__":
query = [
"Tell me three sorting algorithms, just the names.",
"Which one has the least time complexity? Just tell me the name.",
]
d = Demo("intro_sorting", query)
d()