Skip to content

Commit 9b9ea1d

Browse files
authored
Merge pull request #1 from espnet/pr-fixes
Updates for new python versions
2 parents a30080a + 537404b commit 9b9ea1d

12 files changed

Lines changed: 92 additions & 56 deletions

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 2
2+
updates:
3+
# Maintain dependencies for GitHub Actions
4+
# Enable version updates for github-actions
5+
- package-ecosystem: "github-actions"
6+
directory: "/"
7+
schedule:
8+
interval: "weekly"

.github/workflows/python-package.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
python-version: ['3.7', '3.8', '3.9', '3.10']
18+
python-version: ['3.9', '3.10', '3.11', '3.12']
1919

2020
steps:
2121
- uses: actions/checkout@v2
@@ -27,7 +27,8 @@ jobs:
2727
run: |
2828
python -m pip install --upgrade pip
2929
python -m pip install .
30-
pip install flake8 pytest wheel torch
30+
pip install torch --index-url https://download.pytorch.org/whl/cpu
31+
pip install flake8 pytest wheel
3132
- name: Test with pytest
3233
run: |
3334
pytest

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v6.0.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
12+
- repo: https://github.com/psf/black
13+
rev: 25.1.0
14+
hooks:
15+
- id: black
16+
exclude: ^(doc)
17+
18+
- repo: https://github.com/pycqa/isort
19+
rev: 6.0.1
20+
hooks:
21+
- id: isort
22+
exclude: ^(doc)

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
include ctc_segmentation/ctc_segmentation_dyn.pyx
2-

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ clean:
1111

1212
upload:
1313
twine upload dist/*
14-
14+
1515
test:
1616
cd tests; python -c "import test_ctc_segmentation as test; test.test_ctc_segmentation()"
1717
cd tests; python -c "import test_ctc_segmentation as test; test.test_determine_utterance_segments()"
@@ -23,7 +23,7 @@ test:
2323
# To test the various installation methods:
2424
github:
2525
cd /; pip install git+https://github.com/lumaku/ctc-segmentation --user
26-
26+
2727
pip:
2828
cd /; pip install ctc-segmentation --user
2929

@@ -32,4 +32,3 @@ local:
3232

3333
rm:
3434
cd /; pip uninstall -y ctc-segmentation
35-

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CTC segmentation
22

33
<!-- Badges -->
4-
[![build status](https://github.com/lumaku/ctc-segmentation/actions/workflows/python-package.yml/badge.svg)](https://github.com/lumaku/ctc-segmentation/actions/workflows/python-package.yml)
4+
[![build status](https://github.com/espnet/ctc-segmentation/actions/workflows/python-package.yml/badge.svg)](https://github.com/espnet/ctc-segmentation/actions/workflows/python-package.yml)
55
[![version](https://img.shields.io/pypi/v/ctc-segmentation)](https://pypi.org/project/ctc-segmentation/)
66
[![AUR](https://img.shields.io/aur/version/python-ctc-segmentation-git)](https://aur.archlinux.org/packages/python-ctc-segmentation-git)
77
[![downloads](https://img.shields.io/pypi/dm/ctc-segmentation)](https://pypi.org/project/ctc-segmentation/)
@@ -19,7 +19,7 @@ The CTC segmentation package is not standalone, as it needs a neural network wit
1919
* In ESPnet 1 as corpus recipe: [Alignment script](https://github.com/espnet/espnet/blob/master/espnet/bin/asr_align.py), [Example recipe](https://github.com/espnet/espnet/tree/master/egs/tedlium2/align1), [Demo](https://github.com/espnet/espnet#ctc-segmentation-demo )
2020
* In ESPnet 2, as script or directly as python interface: [Alignment script](https://github.com/espnet/espnet/blob/master/espnet2/bin/asr_align.py), [Demo](https://github.com/espnet/espnet#ctc-segmentation-demo )
2121
* In Nvidia NeMo as dataset creation tool: [Documentation](https://docs.nvidia.com/deeplearning/nemo/user-guide/docs/en/main/tools/ctc_segmentation.html), [Example](https://github.com/NVIDIA/NeMo/blob/main/tutorials/tools/CTC_Segmentation_Tutorial.ipynb)
22-
* In Speechbrain, as python interface: [Alignment module](https://github.com/speechbrain/speechbrain/blob/develop/speechbrain/alignment/ctc_segmentation.py), [Examples](https://gist.github.com/lumaku/75eca1c86d9467a54888d149dc7b84f1)
22+
* In Speechbrain, as python interface: [Alignment module](https://github.com/speechbrain/speechbrain/blob/develop/speechbrain/alignment/ctc_segmentation.py), [Examples](https://gist.github.com/espnet/75eca1c86d9467a54888d149dc7b84f1)
2323

2424
It can also be used with other frameworks:
2525

@@ -59,29 +59,29 @@ def align_with_transcript(
5959
with torch.no_grad():
6060
logits = model(inputs.input_values).logits.cpu()[0]
6161
probs = torch.nn.functional.softmax(logits,dim=-1)
62-
62+
6363
# Tokenize transcripts
6464
vocab = tokenizer.get_vocab()
6565
inv_vocab = {v:k for k,v in vocab.items()}
6666
unk_id = vocab["<unk>"]
67-
67+
6868
tokens = []
6969
for transcript in transcripts:
7070
assert len(transcript) > 0
7171
tok_ids = tokenizer(transcript.replace("\n"," ").lower())['input_ids']
7272
tok_ids = np.array(tok_ids,dtype=np.int)
7373
tokens.append(tok_ids[tok_ids != unk_id])
74-
74+
7575
# Align
7676
char_list = [inv_vocab[i] for i in range(len(inv_vocab))]
7777
config = ctc_segmentation.CtcSegmentationParameters(char_list=char_list)
7878
config.index_duration = audio.shape[0] / probs.size()[0] / samplerate
79-
79+
8080
ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_token_list(config, tokens)
8181
timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
8282
segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, transcripts)
8383
return [{"text" : t, "start" : p[0], "end" : p[1], "conf" : p[2]} for t,p in zip(transcripts, segments)]
84-
84+
8585
def get_word_timestamps(
8686
audio : np.ndarray,
8787
samplerate : int = SAMPLERATE,
@@ -95,38 +95,38 @@ def get_word_timestamps(
9595
with torch.no_grad():
9696
logits = model(inputs.input_values).logits.cpu()[0]
9797
probs = torch.nn.functional.softmax(logits,dim=-1)
98-
98+
9999
predicted_ids = torch.argmax(logits, dim=-1)
100100
pred_transcript = processor.decode(predicted_ids)
101-
101+
102102
# Split the transcription into words
103103
words = pred_transcript.split(" ")
104-
104+
105105
# Align
106106
vocab = tokenizer.get_vocab()
107107
inv_vocab = {v:k for k,v in vocab.items()}
108108
char_list = [inv_vocab[i] for i in range(len(inv_vocab))]
109109
config = ctc_segmentation.CtcSegmentationParameters(char_list=char_list)
110110
config.index_duration = audio.shape[0] / probs.size()[0] / samplerate
111-
111+
112112
ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_text(config, words)
113113
timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
114114
segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, words)
115115
return [{"text" : w, "start" : p[0], "end" : p[1], "conf" : p[2]} for w,p in zip(words, segments)]
116116

117117
print(align_with_transcript(audio,transcripts))
118-
# [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
118+
# [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
119119
# {'text': 'SIR I EXIST', 'start': 2.3260775862068965, 'end': 4.078771551724138, 'conf': 0.0}]
120120

121121
print(get_word_timestamps(audio))
122-
# [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123-
# {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124-
# {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125-
# {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126-
# {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127-
# {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128-
# {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129-
# {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
122+
# [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123+
# {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124+
# {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125+
# {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126+
# {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127+
# {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128+
# {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129+
# {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
130130
# {'text': 'exist', 'start': 3.347198275862069, 'end': 4.078771551724138, 'conf': 0.0}]
131131
```
132132

@@ -145,7 +145,7 @@ pip install ctc-segmentation
145145

146146
* From source:
147147
```sh
148-
git clone https://github.com/lumaku/ctc-segmentation
148+
git clone https://github.com/espnet/ctc-segmentation
149149
cd ctc-segmentation
150150
cythonize -3 ctc_segmentation/ctc_segmentation_dyn.pyx
151151
python setup.py build
@@ -229,7 +229,7 @@ For examples, see the `prepare_*` functions in `ctc_segmentation.py`, or the exa
229229

230230
### Segments clean-up
231231

232-
Segments that were written to a `segments` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
232+
Segments that were written to a `segments` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
233233

234234
Utterances with a low confidence score are discarded in a data clean-up. This parameter may need adjustment depending on dataset, ASR model and used text conversion.
235235

ctc_segmentation/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""Import all functions of the CTC segmentation package."""
2-
from .ctc_segmentation import ctc_segmentation
3-
from .ctc_segmentation import CtcSegmentationParameters
4-
from .ctc_segmentation import determine_utterance_segments
5-
from .ctc_segmentation import prepare_text
6-
from .ctc_segmentation import prepare_tokenized_text
7-
from .ctc_segmentation import prepare_token_list
2+
3+
from .ctc_segmentation import (
4+
CtcSegmentationParameters,
5+
ctc_segmentation,
6+
determine_utterance_segments,
7+
prepare_text,
8+
prepare_token_list,
9+
prepare_tokenized_text,
10+
)
811
from .partitioning import get_partitions

ctc_segmentation/ctc_segmentation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import logging
21+
2122
import numpy as np
2223

2324
logger = logging.getLogger("ctc_segmentation")

ctc_segmentation/ctc_segmentation_dyn.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ For a description, see https://arxiv.org/abs/2007.09127
1313
"""
1414

1515
import logging
16+
1617
import numpy as np
18+
1719
cimport numpy as np
1820

1921

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[isort]
2+
profile = black

0 commit comments

Comments
 (0)