-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_text_to_file_reader.py
65 lines (49 loc) · 2.56 KB
/
run_text_to_file_reader.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
65
import os
import torch
from InferenceInterfaces.InferenceFastSpeech2 import InferenceFastSpeech2
def read_texts(model_id, sentence, filename, device="cpu", language="en"):
tts = InferenceFastSpeech2(device=device, model_name=model_id)
tts.set_language(language)
if type(sentence) == str:
sentence = [sentence]
tts.read_to_file(text_list=sentence, file_location=filename)
del tts
def read_texts_as_ensemble(model_id, sentence, filename, device="cpu", language="en", amount=10):
"""
for this function, the filename should NOT contain the .wav ending, it's added automatically
"""
tts = InferenceFastSpeech2(device=device, model_name=model_id)
tts.set_language(language)
if type(sentence) == str:
sentence = [sentence]
for index in range(amount):
tts.default_utterance_embedding = torch.zeros(704).float().random_(-40, 40).to(device)
tts.read_to_file(text_list=sentence, file_location=filename + f"_{index}" + ".wav")
def read_harvard_sentences(model_id, device):
tts = InferenceFastSpeech2(device=device, model_name=model_id)
with open("Utility/test_sentences_combined_3.txt", "r", encoding="utf8") as f:
sents = f.read().split("\n")
output_dir = "audios/harvard_03_{}".format(model_id)
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
for index, sent in enumerate(sents):
tts.read_to_file(text_list=[sent], file_location=output_dir + "/{}.wav".format(index))
with open("Utility/test_sentences_combined_6.txt", "r", encoding="utf8") as f:
sents = f.read().split("\n")
output_dir = "audios/harvard_06_{}".format(model_id)
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
for index, sent in enumerate(sents):
tts.read_to_file(text_list=[sent], file_location=output_dir + "/{}.wav".format(index))
def read_contrastive_focus_sentences(model_id, device):
tts = InferenceFastSpeech2(device=device, model_name=model_id)
with open("Utility/contrastive_focus_test_sentences.txt", "r", encoding="utf8") as f:
sents = f.read().split("\n")
output_dir = "audios/focus_{}".format(model_id)
os.makedirs(output_dir, exist_ok=True)
for index, sent in enumerate(sents):
tts.read_to_file(text_list=[sent], file_location=output_dir + "/{}.wav".format(index))
if __name__ == '__main__':
exec_device = "cuda" if torch.cuda.is_available() else "cpu"
os.makedirs("audios", exist_ok=True)
read_texts(model_id="Meta", sentence="Hello World.", filename="audios/read_speech.wav", device="cpu", language="en")