-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts_common.py
106 lines (84 loc) · 3.93 KB
/
tts_common.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import importlib.util
import pyttsx3
from helpers import replace_words_with_pronunciations
import time
def load_tts_apis(global_state):
global_state.tts_engines = []
tts_dir = os.path.join(os.path.dirname(__file__), "TTS")
for file_name in os.listdir(tts_dir):
if file_name.endswith(".py") and file_name != "__init__.py":
module_name = file_name[:-3]
spec = importlib.util.spec_from_file_location(module_name, os.path.join(tts_dir, file_name))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if hasattr(module, "get_name") and callable(module.get_name):
global_state.tts_apis.append(module.get_name())
def fallback_tts(text, event):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
event.set()
def say_something(text, global_state, event):
if global_state.custom_pronunciations:
text_parsed = replace_words_with_pronunciations(text,
global_state.custom_pronunciations['pronunciations'])
else:
text_parsed = text
# Check to see if the bot replied with their name, and if so, axe that.
if global_state.args.assistant:
assistant_name = global_state.args.assistant.lower()
else:
assistant_name = "assistant"
if text_parsed.lower().startswith(assistant_name + ":"):
text_parsed = text_parsed[len(assistant_name) + 1:]
# Determine which TTS engine to use based on user input
tts_dir = os.path.join(os.path.dirname(__file__), "TTS")
module_name = global_state.tts_engine
module_path = os.path.join(tts_dir, module_name + ".py")
if not os.path.exists(module_path):
print("TTS API module not found. Defaulting to pyttsx3.")
fallback_tts(text_parsed, event)
return
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
try:
# Call the appropriate function within the module
function_name = "say"
if hasattr(module, function_name):
function = getattr(module, function_name)
function(text_parsed, global_state, event)
else:
print("TTS API module does not have a 'say' function. Module name is {}".format(global_state.tts_engine))
fallback_tts(text_parsed, event)
except AttributeError:
print("TTS API module does not have a 'say' function. Module name is {}".format(global_state.tts_engine))
fallback_tts(text_parsed, event)
except Exception as e:
print("Error occurred while calling TTS API: {}".format(str(e)))
fallback_tts(text_parsed, event)
def update_model(model_name, global_state):
# Determine which TTS engine to use based on user input
tts_dir = os.path.join(os.path.dirname(__file__), "TTS")
module_name = global_state.tts_engine
module_path = os.path.join(tts_dir, module_name + ".py")
if not os.path.exists(module_path):
print("TTS API module not found. Unable to update model.")
return
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
try:
# Call the appropriate function within the module
function_name = "update_model"
if hasattr(module, function_name):
function = getattr(module, function_name)
function(model_name, global_state)
else:
print("TTS API module does not have a 'update_model' function. Module name is {}".format(global_state.tts_engine))
except AttributeError:
print("TTS API module does not have a 'update_model' function. Module name is {}".format(global_state.tts_engine))
except Exception as e:
print("Error occurred while calling TTS API: {}".format(str(e)))
return