-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_text_to_file_reader.py
81 lines (66 loc) · 3.23 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import sys
import torch
from InferenceInterfaces.FastSpeech2Interface import InferenceFastSpeech2
def read_texts(model_id, sentence, filename, device="cpu", language="en", speaker_reference=None):
tts = InferenceFastSpeech2(device=device, model_name=model_id)
tts.set_language(language)
if speaker_reference is not None:
tts.set_utterance_embedding(speaker_reference)
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("test_audios", exist_ok=True)
name = input("\nName of the .wav file? (or 'exit')\n")
if name == "exit":
sys.exit()
read_texts(model_id="Quechua_only",
sentence="Wayqey yanapariway ama hina kaychu.",
filename=f"test_audios/{name}_quechua_only_test_cloned.wav",
device=exec_device,
speaker_reference=f"{name}.wav",
language="qu")
read_texts(model_id="Quechua",
sentence="Minchha paqarin wasiykita hamusaq.",
filename=f"test_audios/{name}_quechua_test_cloned.wav",
device=exec_device,
speaker_reference=f"{name}.wav",
language="qu")