Skip to main content

Python bindings for k2

Project description

k2py

Python bindings for k2

Features

  • Efficient Streaming Decoding: OnlineDenseIntersecter for real-time forced alignment
  • Memory-Bounded Streaming: decode_and_flush() keeps memory at O(chunk) for arbitrarily long audio
  • Mixed Decode Modes: Combine decode() and decode_and_flush() for flush-every-N-chunks strategies
  • WebAssembly Support: Browser-side forced alignment via Emscripten (WASM32 + WASM64)
  • Cross-Platform: Supports Linux, macOS, and Windows
  • Python 3.10+: Compatible with modern Python versions
  • Built with pybind11: Fast C++ bindings with minimal overhead

Installation

From PyPI

pip install k2py

Coexistence with k2

k2py can coexist with the k2 package. Both can be imported in any order:

import k2
import k2py  # works fine

Usage

Basic Example

from k2py import OnlineDenseIntersecter, CreateFsaVecFromStr
import numpy as np

# Create FSA from string representation
fsa_str = "0 1 1 1.0\n1 2 2 1.0\n2"
result = CreateFsaVecFromStr(fsa_str, final_state=2)

# Initialize decoder
decoder = OnlineDenseIntersecter(
    result["fsa"],
    result["aux_labels"],
    search_beam=20.0,
    output_beam=8.0,
    min_active_states=30,
    max_active_states=10000
)

# Prepare acoustic scores (num_frames x vocab_size)
scores = np.random.randn(100, 50).astype(np.float32)

# Decode
lattice = decoder.DecodeWithArray(scores, return_lattice=True)

# Get final alignment result
alignment = decoder.Finish()
print(f"Token IDs: {alignment['token_ids']}")
print(f"Timestamps: {alignment['timestamps']}")
print(f"Durations: {alignment['durations']}")

Streaming with Memory-Bounded Flush

# For long audio: decode_and_flush() caps memory at O(chunk)
all_tokens = []
for chunk_scores in chunk_iterator(scores, chunk_size=1000):
    tokens, labels = decoder.decode_and_flush(chunk_scores)
    all_tokens.extend(tokens)

# Get any remaining tokens
final_tokens, final_labels = decoder.finish()
all_tokens.extend(final_tokens[0])

WebAssembly

k2py also compiles to WebAssembly for browser-side forced alignment. See wasm/README.md for build instructions, API reference, and benchmarks.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

k2py-0.4.0-cp314-cp314-win_amd64.whl (320.4 kB view details)

Uploaded CPython 3.14Windows x86-64

k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (484.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl (324.2 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

k2py-0.4.0-cp313-cp313-win_amd64.whl (311.0 kB view details)

Uploaded CPython 3.13Windows x86-64

k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (484.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl (323.8 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

k2py-0.4.0-cp312-cp312-win_amd64.whl (311.1 kB view details)

Uploaded CPython 3.12Windows x86-64

k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (484.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl (323.8 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

k2py-0.4.0-cp311-cp311-win_amd64.whl (309.4 kB view details)

Uploaded CPython 3.11Windows x86-64

k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (481.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl (323.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

k2py-0.4.0-cp310-cp310-win_amd64.whl (308.5 kB view details)

Uploaded CPython 3.10Windows x86-64

k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (480.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl (321.9 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file k2py-0.4.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: k2py-0.4.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 320.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k2py-0.4.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 fca13fed91b88b458bef2396f09dca161bb72489bda09c6dbb2a5f10306bc0da
MD5 54aee6d39828b1eaabcb67089e5edaa8
BLAKE2b-256 29f960c0a5597253ca6f3567ba51b7d537e11794195ebee98cf2d2a2ee63a027

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp314-cp314-win_amd64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0bf51c68e796a14d7802136862ecf558cd4655cdcd163d8ffe8520ba7db367ad
MD5 1f33808d76e769c5a66d744dbc324090
BLAKE2b-256 aec03bbae886adcfbbb041a230df062573c7db0045217f8583ed12a59fdd50b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 920fb9d1673c446a7fb0dbfd04567f74ac761a1971e589c352c66bba8a61cd3c
MD5 b464ce83fab7158d3dc1d7569f99528f
BLAKE2b-256 0878988528a0199e06f2d969ea48b1c745239722d8a90c584a1c1df5ac92a500

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: k2py-0.4.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 311.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k2py-0.4.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 16d62b5370525066d6b99a960b702eab26396c4751b21606846a6e6136eee3c2
MD5 e1f2bf571a64ce5754cb98ec874be3ee
BLAKE2b-256 505dc7021eec2dd2262d59ac718787d765f3687646f5be11fe3ae19322324883

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp313-cp313-win_amd64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b039d6f4f708b05769cdfc62285db7c701e833e15825c5b1e09732545b844791
MD5 ee0bcb1ff68aa7aad86cbecb8ff7f0eb
BLAKE2b-256 18d7f0e52536b0296ad2b5978d681e49510ede0d1e92dbfcb1a1c9e5d0315d4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 cb54cc36d81693fca9c7f51236250bc5dac590bb706ae56f871c01ad34e84ab2
MD5 594fbb45169536496d711bba735ef1d9
BLAKE2b-256 50a1434eb63356d04d4b2eb228438c350d45c37a3ee1578471ef161a1e358ba8

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: k2py-0.4.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 311.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k2py-0.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d1eb564baf7f393350520f0ea53681e408dbef5fd858222a3e9c96438c0c6a94
MD5 ed478de2bbf26c4225a53156cd5f0f69
BLAKE2b-256 08b5888b46442aa42803f9182f78dc946696012c0c641d45f9aa5cb42d68cfb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp312-cp312-win_amd64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8c372eeb505b98fa1be83a4ec009492d4b25fc6292179335d8b79ca784efa496
MD5 2e873fd034b0318bbad162660dfe9f60
BLAKE2b-256 bf7118444361bae27c00713ee8f1f77ce0749d2d28e8b17dd1868fd43e7bf163

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6b6b3bc72d189303793a61763de77d157fb29445d5ef0895cc900f3b2176dfe4
MD5 271f1d6880d5cd822e1092102ff459e7
BLAKE2b-256 1d460723218bfd228395d9302edb8effd161bfa907d71c0df2b84aeb45208ebd

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: k2py-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 309.4 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k2py-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7b162b3264a5902a35b61d7d99476cb76374109ea480dc52822058c95635ff4e
MD5 1f7f5bb46c2eb03b7f809a5f9d1bfbbf
BLAKE2b-256 b6d26f7d340a0641b92fad4887bb05be5718e8ea8016fba3b8de2829290a1d89

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp311-cp311-win_amd64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 470e907d26708407015bc891520795bd445f3c08e983d7a364014dddcb214de7
MD5 c67aacdd49f03edd45dc8d55014a450f
BLAKE2b-256 899311808e3afbd5ddb80600605c3e2e52146fd10e80d2badac7d25e080aba5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 1f47cebc56533d476c9d8c24897689bd33583be964b6d6826db692f088aae986
MD5 310c52b973a20d6e7f0a1fd20a199906
BLAKE2b-256 e745a168b6b29b7122cee7d47b30b4135680119cbc0cda14a2f734da2d1f8cee

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: k2py-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 308.5 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k2py-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5c951919060aa533199a46717085c4ada73cd74d98bc265bba5365e4e9287036
MD5 103cab37f229e5575691dd70fd11e4b6
BLAKE2b-256 6aa92a61af0cc1768353dc143f996f391c2d5f5b5e5d1e191a5d4b0309c2c2aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp310-cp310-win_amd64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1ba7998871196efd6fdf280499116c37f73444b16c289580987d0140030bf827
MD5 146a62bb92b09f3b3dd77dd0ddd3a4a2
BLAKE2b-256 8702477b7151410cfd3a3450bf3144724ec5b890b6c0350303256dabd16b9a5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 34fdcb0389a8458465f10886719c1e3336650f927e0ae8e81a09b3ec1c81cf56
MD5 c9bd995374cb4d5445a54fc58cea35ce
BLAKE2b-256 99bbf64d96936648b998c6ee5fca68b2301e0a0a201f9976daf6f541f98cde9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl:

Publisher: publish-wheels.yml on lattifai/k2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page