Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions openwakeword/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,26 @@ def tflite_predict(tflite_interpreter, input_index, output_index, x):
return tflite_interpreter.get_tensor(output_index)[None, ]

except ImportError:
logging.warning("Tried to import the tflite runtime, but it was not found. "
"Trying to switching to onnxruntime instead, if appropriate models are available.")
if wakeword_models != [] and all(['.onnx' in i for i in wakeword_models]):
inference_framework = "onnx"
elif wakeword_models != [] and all([os.path.exists(i.replace('.tflite', '.onnx')) for i in wakeword_models]):
inference_framework = "onnx"
wakeword_models = [i.replace('.tflite', '.onnx') for i in wakeword_models]
from importlib.util import find_spec
if find_spec("tensorflow") is not None and find_spec("tflite_runtime") is None:
logging.warning("Tried to import the tflite runtime, but it was not found. Using tensorflow instead.")
from tensorflow.lite.python import interpreter as tflite

def tflite_predict(tflite_interpreter, input_index, output_index, x):
tflite_interpreter.set_tensor(input_index, x)
tflite_interpreter.invoke()
return tflite_interpreter.get_tensor(output_index)[None, ]
else:
raise ValueError("Tried to import the tflite runtime for provided tflite models, but it was not found. "
"Please install it using `pip install tflite-runtime`")
logging.warning("Tried to import the tflite runtime, but it was not found. "
"Trying to switching to onnxruntime instead, if appropriate models are available.")
if wakeword_models != [] and all(['.onnx' in i for i in wakeword_models]):
inference_framework = "onnx"
elif wakeword_models != [] and all([os.path.exists(i.replace('.tflite', '.onnx')) for i in wakeword_models]):
inference_framework = "onnx"
wakeword_models = [i.replace('.tflite', '.onnx') for i in wakeword_models]
else:
raise ValueError("Tried to import the tflite runtime for provided tflite models, but it was not found. "
"Please install it using `pip install tflite-runtime`")

if inference_framework == "onnx":
try:
Expand Down
10 changes: 8 additions & 2 deletions openwakeword/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ def __init__(self,
try:
import tflite_runtime.interpreter as tflite
except ImportError:
raise ValueError("Tried to import the TFLite runtime, but it was not found."
"Please install it using `pip install tflite-runtime`")
from importlib.util import find_spec
if find_spec("tensorflow") is not None and find_spec("tflite_runtime") is None:
logging.warning("Tried to import the tflite runtime, but it was not found. Using tensorflow instead.")
from tensorflow.lite.python import interpreter as tflite
else:
raise ValueError("Tried to import the TFLite runtime, but it was not found."
"Neither was the TensorFlow interpreter."
"Please install TFLite runtime using `pip install tflite-runtime`")

if melspec_model_path == "":
melspec_model_path = os.path.join(pathlib.Path(__file__).parent.resolve(),
Expand Down
Loading