-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodel_utils.py
More file actions
149 lines (129 loc) · 5.3 KB
/
Copy pathmodel_utils.py
File metadata and controls
149 lines (129 loc) · 5.3 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# sherpa-onnx-tts-stt/model_utils.py
import os
import subprocess
import logging
import sherpa_onnx
import importlib
import sys
_LOGGER = logging.getLogger("sherpa_onnx_model_utils")
def _download_model(model_url, model_dir, model):
"""Downloads and extracts the model."""
if not os.path.exists(os.path.join(model_dir, model)):
_LOGGER.info(f"Downloading model: {model_url}")
os.makedirs(os.path.join(model_dir, model), exist_ok=True)
# Use curl (or wget) for download and extraction (more robust than Python libraries for large files)
try:
subprocess.check_call(
[
"curl",
"-L",
model_url,
"-o",
os.path.join(model_dir, model, f"{model}.tar.gz"),
]
)
_LOGGER.info(f"Downloaded model: {model_url}, Extracting...")
subprocess.check_call(
[
"tar",
"-xvf",
os.path.join(model_dir, model, f"{model}.tar.gz"),
"-C",
model_dir,
]
)
os.remove(os.path.join(model_dir, model, f"{model}.tar.gz")) # Clean up
_LOGGER.info(f"Download and extract Done. Cleaned up.")
except subprocess.CalledProcessError as e:
_LOGGER.error(f"Error downloading or extracting model: {e}")
raise # Re-raise to stop add-on startup on failure
else:
_LOGGER.info(f"{model} model already exists.")
def fetch_stt_model(stt_model_dir, model):
# --- STT Model ---
stt_model_url = f"https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/{model}.tar.bz2"
_download_model(stt_model_url, stt_model_dir, model)
def fetch_tts_model(tts_model_dir, model):
# --- TTS Model ---
tts_model_url = f"https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/{model}.tar.bz2"
_download_model(tts_model_url, tts_model_dir, model)
def fetch_vocoder_model(model_dir, model):
# --- Vocoder Model ---
model_url = f"https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/{model}"
if not os.path.exists(os.path.join(model_dir, model)):
_LOGGER.info("Downloading model: %s", model_url)
os.makedirs(model_dir, exist_ok=True)
# Use curl (or wget) for download and extraction (more robust than Python libraries for large files)
try:
subprocess.check_call(
[
"curl",
"-L",
model_url,
"-o",
os.path.join(model_dir, model),
]
)
_LOGGER.info("Downloaded model: %s", model_url)
except subprocess.CalledProcessError as e:
_LOGGER.error("Error downloading model: %s", e)
raise # Re-raise to stop add-on startup on failure
else:
_LOGGER.info("%s model already exists.", model)
def load_module(file):
spec = importlib.util.spec_from_file_location("model", file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def find_builtin_model(model, language, project_dir, model_type):
if model:
return os.path.join(
project_dir,
"models",
model_type,
f"{model}.py",
)
elif language:
model = os.path.join(project_dir, "models", model_type, "lang", language)
if os.path.exists(model):
return os.path.realpath(model)
return None
def initialize_models(cli_args):
"""Initializes STT and TTS models based on CLI arguments."""
stt_model_dir = "/stt-models"
tts_model_dir = "/tts-models"
os.environ.setdefault("STT_MODEL_DIR", "/stt-models")
os.environ.setdefault("TTS_MODEL_DIR", "/tts-models")
project_dir = os.path.dirname(os.path.realpath(__file__))
# STT Initialization (adjust paths as needed for extracted model)
try:
if cli_args.custom_stt_model_eval != "null":
if cli_args.stt_model:
fetch_stt_model(stt_model_dir, cli_args.stt_model)
stt_model = eval(cli_args.custom_stt_model_eval)
else:
model_file = find_builtin_model(
cli_args.stt_model, cli_args.language, project_dir, "stt"
)
stt_model = load_module(model_file).load(cli_args) if model_file else None
except Exception as e:
_LOGGER.critical("Failed to initialize custom STT model: %s", e)
raise
try:
# TTS Initialization
if cli_args.custom_tts_model_eval != "null":
if cli_args.tts_model:
fetch_tts_model(tts_model_dir, cli_args.tts_model)
tts_model = eval(cli_args.custom_tts_model_eval)
else:
model_file = find_builtin_model(
cli_args.tts_model, cli_args.language, project_dir, "tts"
)
tts_model = load_module(model_file).load(cli_args) if model_file else None
except Exception as e:
_LOGGER.critical("Failed to initialize custom TTS model: %s", e)
raise
if not (tts_model or stt_model):
_LOGGER.critical("No models loaded")
raise Exception("No models loaded")
return stt_model, tts_model