Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly display KaldiFatalError messages #22

Merged
merged 5 commits into from
Sep 30, 2024
Merged
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
11 changes: 4 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
include:
- os: ubuntu-latest
label: linux-64
prefix: /usr/share/miniconda3/envs/my-env

#- os: macos-latest
# label: osx-64
Expand All @@ -37,17 +36,15 @@ jobs:
fetch-depth: 0

- name: Install Conda environment with Micromamba
uses: mamba-org/setup-micromamba@v1
uses: conda-incubator/setup-miniconda@v3
with:
environment-file: environment.yml
environment-name: my-env
create-args: >-
python=3.10
cache-environment: true
activate-environment: kalpy_build
miniforge-version: latest

- name: Build kalpy
env:
KALDI_ROOT: ~/micromamba/envs/my-env
KALDI_ROOT: ~/miniconda3/envs/kalpy_build
shell: bash -l {0}
run: pip install .

Expand Down
17 changes: 17 additions & 0 deletions extensions/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,27 @@ void pybind_arg(py::module& m, Opt& opt) {
py::arg("name"), py::arg("arg"), py::arg("doc"));
}


} // namespace

void init_util(py::module &_m) {
py::module m = _m.def_submodule("util", "util pybind for Kaldi");


PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> exc_storage;

exc_storage.call_once_and_store_result(
[&]() { return py::exception<KaldiFatalError>(m, "KaldiFatalError"); });

py::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const KaldiFatalError &e) {
py::set_error(exc_storage.get_stored(), e.KaldiMessage());
}
});
//py::register_exception<KaldiFatalError>(m, "PyKaldiFatalError", PyExc_RuntimeError);
//py::register_local_exception<KaldiFatalError>(m, "PyKaldiFatalError", PyExc_RuntimeError);
pybind_basic_vector_holder<int32>(m, "IntVectorHolder");
py::class_<std::istream>(m, "istream");
{
Expand Down
2 changes: 1 addition & 1 deletion kalpy/fstext/lexicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def compute_disambiguation_symbols(self):
for pron in self.pronunciations:

phones = pron.pronunciation.split()
while phones:
while len(phones) > 0:
subsequences.add(" ".join(phones))
phones = phones[:-1]
last_used = collections.defaultdict(int)
Expand Down
28 changes: 19 additions & 9 deletions kalpy/gmm/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging
import pathlib
import sys
import traceback
import typing

from _kalpy.fstext import VectorFst
Expand Down Expand Up @@ -101,15 +103,21 @@ def align_utterances(
except KeyError:
logger.warning(f"Skipping {utterance_id} due to missing training graph")
continue
alignment = self.align_utterance(training_graph, feats, utterance_id)
if alignment is None:
yield utterance_id, None
num_error += 1
continue
yield alignment
total_likelihood += alignment.likelihood
total_frames += len(alignment.alignment)
num_done += 1
try:
alignment = self.align_utterance(training_graph, feats, utterance_id)
if alignment is None:
yield utterance_id, None
num_error += 1
continue
yield alignment
total_likelihood += alignment.likelihood
total_frames += len(alignment.alignment)
num_done += 1
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
logger.warning(f"Error on {utterance_id}: {e}")
traceback_lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
logger.debug("\n".join(traceback_lines))
if total_frames:
logger.info(
f"Overall log-likelihood per frame is {total_likelihood / total_frames} over {total_frames} frames."
Expand Down Expand Up @@ -153,6 +161,8 @@ def export_alignments(
likelihood_writer.Write(
str(alignment.utterance_id), alignment.per_frame_likelihoods
)
except Exception as e:
logger.error(e)
finally:
writer.Close()
if word_writer is not None:
Expand Down