naptrap 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. naptrap-0.1.0/LICENSE +202 -0
  2. naptrap-0.1.0/MANIFEST.in +4 -0
  3. naptrap-0.1.0/NOTICE +19 -0
  4. naptrap-0.1.0/PKG-INFO +213 -0
  5. naptrap-0.1.0/README.md +181 -0
  6. naptrap-0.1.0/THIRD_PARTY_NOTICES.md +17 -0
  7. naptrap-0.1.0/docs/compatibility.md +24 -0
  8. naptrap-0.1.0/licenses/PhonMatchNet-BSD-3-Clause.txt +28 -0
  9. naptrap-0.1.0/pyproject.toml +46 -0
  10. naptrap-0.1.0/setup.cfg +4 -0
  11. naptrap-0.1.0/src/naptrap/__init__.py +15 -0
  12. naptrap-0.1.0/src/naptrap/__main__.py +5 -0
  13. naptrap-0.1.0/src/naptrap/artifacts.py +49 -0
  14. naptrap-0.1.0/src/naptrap/audio.py +24 -0
  15. naptrap-0.1.0/src/naptrap/cli.py +35 -0
  16. naptrap-0.1.0/src/naptrap/features.py +69 -0
  17. naptrap-0.1.0/src/naptrap/google.py +96 -0
  18. naptrap-0.1.0/src/naptrap/hub.py +21 -0
  19. naptrap-0.1.0/src/naptrap/models/__init__.py +5 -0
  20. naptrap-0.1.0/src/naptrap/models/network.py +61 -0
  21. naptrap-0.1.0/src/naptrap/models/phonmatchnet.py +128 -0
  22. naptrap-0.1.0/src/naptrap/registry.py +42 -0
  23. naptrap-0.1.0/src/naptrap/text.py +106 -0
  24. naptrap-0.1.0/src/naptrap/types.py +61 -0
  25. naptrap-0.1.0/src/naptrap.egg-info/PKG-INFO +213 -0
  26. naptrap-0.1.0/src/naptrap.egg-info/SOURCES.txt +32 -0
  27. naptrap-0.1.0/src/naptrap.egg-info/dependency_links.txt +1 -0
  28. naptrap-0.1.0/src/naptrap.egg-info/entry_points.txt +2 -0
  29. naptrap-0.1.0/src/naptrap.egg-info/requires.txt +9 -0
  30. naptrap-0.1.0/src/naptrap.egg-info/top_level.txt +1 -0
  31. naptrap-0.1.0/tests/conftest.py +60 -0
  32. naptrap-0.1.0/tests/test_inputs.py +74 -0
  33. naptrap-0.1.0/tests/test_loading.py +99 -0
  34. naptrap-0.1.0/tests/test_released_models.py +75 -0
naptrap-0.1.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,4 @@
1
+ include README.md LICENSE NOTICE THIRD_PARTY_NOTICES.md
2
+ recursive-include docs *.md
3
+ recursive-include licenses *.txt
4
+ recursive-include tests *.py
naptrap-0.1.0/NOTICE ADDED
@@ -0,0 +1,19 @@
1
+ naptrap
2
+ Copyright 2026 Drew Bednar
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (LICENSE).
5
+
6
+ src/naptrap/models/network.py implements the PhonMatchNet architecture
7
+ (Yong-Hyeok Lee and Namhyun Cho, Interspeech 2023) after NCSOFT's
8
+ BSD-3-Clause PyTorch implementation, https://github.com/ncsoft/PhonMatchNet.
9
+ NCSOFT's copyright notice and license: licenses/PhonMatchNet-BSD-3-Clause.txt.
10
+ naptrap is not affiliated with or endorsed by NCSOFT or the PhonMatchNet authors.
11
+
12
+ src/naptrap/text.py reimplements the decoder-state "embedding" computation of
13
+ g2p_en by Kyubyong Park and Jongseok Kim (Apache-2.0),
14
+ https://github.com/Kyubyong/g2p.
15
+
16
+ src/naptrap/google.py reimplements Google's speech_embedding/1 model
17
+ (Apache-2.0) in PyTorch. naptrap is not affiliated with or endorsed by Google.
18
+
19
+ Model weights are not included in this package; see THIRD_PARTY_NOTICES.md.
naptrap-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,213 @@
1
+ Metadata-Version: 2.4
2
+ Name: naptrap
3
+ Version: 0.1.0
4
+ Summary: Local, text-defined wake word and keyword detection in PyTorch
5
+ Author-email: Drew Bednar <drew@runcible.io>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/androiddrew/naptrap
8
+ Project-URL: Issues, https://github.com/androiddrew/naptrap/issues
9
+ Project-URL: Models, https://huggingface.co/androiddrew/naptrap-phonmatchnet-en
10
+ Keywords: wake word,keyword spotting,speech,phonmatchnet,pytorch,kws
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
18
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
+ Requires-Python: >=3.9
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ License-File: NOTICE
23
+ License-File: licenses/PhonMatchNet-BSD-3-Clause.txt
24
+ Requires-Dist: torch>=1.13
25
+ Requires-Dist: safetensors>=0.4
26
+ Provides-Extra: hub
27
+ Requires-Dist: huggingface_hub>=0.20; extra == "hub"
28
+ Provides-Extra: test
29
+ Requires-Dist: pytest>=7; extra == "test"
30
+ Requires-Dist: numpy; extra == "test"
31
+ Dynamic: license-file
32
+
33
+ # naptrap
34
+
35
+ **Local, text-defined wake word detection in PyTorch.** Type a keyword, give
36
+ naptrap a short audio clip, and it tells you whether the keyword was spoken.
37
+ New wake words need no recordings or training.
38
+
39
+ ```python
40
+ from naptrap import load_model, load_wav
41
+
42
+ model = load_model("phonmatchnet") # downloads the model on first use
43
+ result = model.detect(load_wav("clip.wav"), "hey computer")
44
+
45
+ print(result.score) # keyword-match probability, 0-1
46
+ print(result.accepted) # score >= the model's calibrated threshold
47
+ ```
48
+
49
+ The first model is an independently trained
50
+ [PhonMatchNet](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en)
51
+ (Lee & Cho, Interspeech 2023) for English. Inference runs on the CPU with
52
+ **PyTorch as the only heavy dependency**. There is no TensorFlow, NumPy,
53
+ torchaudio or NLTK, and no cloud API.
54
+
55
+ ## Install
56
+
57
+ ```bash
58
+ pip install "naptrap[hub]"
59
+ ```
60
+
61
+ Python 3.9+ and PyTorch 1.13+ are required. If you want a CPU-only or
62
+ CUDA-specific PyTorch build, install it first; see
63
+ [pytorch.org](https://pytorch.org/get-started/locally/). The `hub` extra adds
64
+ `huggingface_hub` for automatic model downloads. Without it, `pip install
65
+ naptrap` works with model directories you download yourself.
66
+
67
+ ## Models
68
+
69
+ `load_model("phonmatchnet")` fetches three Hugging Face repositories (about
70
+ 6 MB in total), pinned to revision `v1`, and caches them locally:
71
+
72
+ | Repository | Role | License |
73
+ |---|---|---|
74
+ | [`androiddrew/naptrap-phonmatchnet-en`](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en) | keyword matcher | Apache-2.0 |
75
+ | [`androiddrew/google-speech-embedding-pytorch`](https://huggingface.co/androiddrew/google-speech-embedding-pytorch) | frozen audio encoder | Apache-2.0 |
76
+ | [`androiddrew/g2p-en-decoder-pytorch`](https://huggingface.co/androiddrew/g2p-en-decoder-pytorch) | frozen keyword text encoder | Apache-2.0 |
77
+
78
+ To work offline, download them once and pass the directories:
79
+
80
+ ```bash
81
+ hf download androiddrew/naptrap-phonmatchnet-en --revision v1 --local-dir models/phonmatchnet
82
+ hf download androiddrew/google-speech-embedding-pytorch --revision v1 --local-dir models/google
83
+ hf download androiddrew/g2p-en-decoder-pytorch --revision v1 --local-dir models/g2p
84
+ ```
85
+
86
+ ```python
87
+ model = load_model("phonmatchnet", model_dir="models/phonmatchnet",
88
+ google_dir="models/google", g2p_dir="models/g2p")
89
+ ```
90
+
91
+ Every weight file is checked against the SHA-256 recorded in its `config.json`.
92
+ The keyword model also pins the exact encoders it was trained with.
93
+
94
+ ## Usage
95
+
96
+ ### Audio
97
+
98
+ `detect()` scores **one mono 16 kHz clip of 0.5–2 seconds**, given as a 1-D
99
+ float32 tensor or array with values in [-1, 1]. `load_wav()` reads 16-bit PCM
100
+ WAV files using only the standard library. Convert other files first:
101
+
102
+ ```bash
103
+ ffmpeg -i input.m4a -ac 1 -ar 16000 -sample_fmt s16 clip.wav
104
+ ```
105
+
106
+ naptrap does not resample, trim or rescale audio for you.
107
+
108
+ ### Reusing a keyword
109
+
110
+ Converting the keyword text is cheap, but if you score many clips against the
111
+ same keyword you can do it once:
112
+
113
+ ```python
114
+ wake = model.keyword("hey computer")
115
+ for clip in clips:
116
+ if model.detect(clip, wake).accepted:
117
+ ...
118
+ ```
119
+
120
+ Keywords are English words; accents and punctuation are removed. Spell out
121
+ numbers ("route sixty six").
122
+
123
+ ### Operating points and thresholds
124
+
125
+ ```python
126
+ model.thresholds # {0.01: 0.7149..., 0.1: 0.0025...}
127
+ model.detect(clip, "hey computer", far=0.1) # more sensitive
128
+ ```
129
+
130
+ Each threshold was fitted on held-out development speakers to target a
131
+ per-clip false-accept rate (FAR) of 1% or 10%. **These targets do not hold on
132
+ other audio.** On phonetically confusable phrases, the 1% threshold accepted
133
+ 31% of negatives. For a real product, record some positive and negative clips
134
+ in your setting and choose your own threshold on `result.score`. See the
135
+ [model card](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en) for
136
+ the evaluation.
137
+
138
+ ### Command line
139
+
140
+ ```bash
141
+ naptrap detect clip.wav "hey computer"
142
+ naptrap detect clip.wav "hey computer" --far 0.1 --model-dir models/phonmatchnet \
143
+ --google-dir models/google --g2p-dir models/g2p
144
+ ```
145
+
146
+ The command prints the prediction as JSON. It exits with 0 if the keyword was
147
+ accepted, 1 if it was rejected and 2 on error. `python -m naptrap` works the
148
+ same way.
149
+
150
+ ### Precomputed features
151
+
152
+ `model.predict({"keyword_mel": ..., "google": ..., "text": ...})` scores
153
+ features you computed yourself: `[T, 40]`, `[T/8, 96]` and `[P, 256]` float32.
154
+ The encoders are exposed as `naptrap.GoogleSpeechEmbedding` and
155
+ `naptrap.G2PTextEncoder`.
156
+
157
+ ## Limitations
158
+
159
+ - **Single clips, not a streaming listener (yet).** To watch a microphone you
160
+ must window the audio yourself, e.g. 1.5 s windows every 0.25 s, and add
161
+ your own debouncing.
162
+ - **English only**, trained on read audiobook speech (LibriSpeech). Expect
163
+ lower accuracy with noise, far-field microphones, accents or children.
164
+ - **No speaker verification.** Anyone saying the keyword triggers it.
165
+ - Similar-sounding phrases are often confused.
166
+ - CPU inference is the supported path; other devices have not been validated.
167
+ - Not for security or safety-critical use.
168
+
169
+ ## Adding model families
170
+
171
+ Backends implement `keyword()`, `detect()` and `predict()`, and return a
172
+ `Prediction`. Register a loader under a new name:
173
+
174
+ ```python
175
+ from naptrap import register_model, load_model
176
+
177
+ register_model("my-model", MyModel.from_pretrained)
178
+ model = load_model("my-model", model_dir="...")
179
+ ```
180
+
181
+ ## Development
182
+
183
+ ```bash
184
+ pip install -e ".[test]"
185
+ pytest
186
+ # Also run the real-model tests against downloaded model directories:
187
+ NAPTRAP_TEST_MODELS=path/to/models pytest
188
+ ```
189
+
190
+ `NAPTRAP_TEST_MODELS` must point at a directory holding the three model
191
+ repositories in subdirectories named `naptrap-phonmatchnet-en`,
192
+ `google-speech-embedding-pytorch` and `g2p-en-decoder-pytorch`.
193
+
194
+ ## License and attribution
195
+
196
+ naptrap is licensed under the [Apache License 2.0](LICENSE). The network
197
+ follows the PhonMatchNet design and NCSOFT's BSD-3-Clause reference code (see
198
+ [NOTICE](NOTICE) and [licenses/](licenses/)). The PhonMatchNet weights were
199
+ trained independently. **naptrap is not affiliated with or endorsed by NCSOFT
200
+ or the PhonMatchNet authors.** See
201
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for every component.
202
+
203
+ If naptrap's PhonMatchNet model is useful in your research, please cite:
204
+
205
+ ```bibtex
206
+ @inproceedings{lee23phonmatchnet,
207
+ title = {PhonMatchNet: Phoneme-Guided Zero-Shot Keyword Spotting for User-Defined Keywords},
208
+ author = {Lee, Yong-Hyeok and Cho, Namhyun},
209
+ booktitle = {Proc. INTERSPEECH 2023},
210
+ pages = {3964--3968},
211
+ year = {2023}
212
+ }
213
+ ```
@@ -0,0 +1,181 @@
1
+ # naptrap
2
+
3
+ **Local, text-defined wake word detection in PyTorch.** Type a keyword, give
4
+ naptrap a short audio clip, and it tells you whether the keyword was spoken.
5
+ New wake words need no recordings or training.
6
+
7
+ ```python
8
+ from naptrap import load_model, load_wav
9
+
10
+ model = load_model("phonmatchnet") # downloads the model on first use
11
+ result = model.detect(load_wav("clip.wav"), "hey computer")
12
+
13
+ print(result.score) # keyword-match probability, 0-1
14
+ print(result.accepted) # score >= the model's calibrated threshold
15
+ ```
16
+
17
+ The first model is an independently trained
18
+ [PhonMatchNet](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en)
19
+ (Lee & Cho, Interspeech 2023) for English. Inference runs on the CPU with
20
+ **PyTorch as the only heavy dependency**. There is no TensorFlow, NumPy,
21
+ torchaudio or NLTK, and no cloud API.
22
+
23
+ ## Install
24
+
25
+ ```bash
26
+ pip install "naptrap[hub]"
27
+ ```
28
+
29
+ Python 3.9+ and PyTorch 1.13+ are required. If you want a CPU-only or
30
+ CUDA-specific PyTorch build, install it first; see
31
+ [pytorch.org](https://pytorch.org/get-started/locally/). The `hub` extra adds
32
+ `huggingface_hub` for automatic model downloads. Without it, `pip install
33
+ naptrap` works with model directories you download yourself.
34
+
35
+ ## Models
36
+
37
+ `load_model("phonmatchnet")` fetches three Hugging Face repositories (about
38
+ 6 MB in total), pinned to revision `v1`, and caches them locally:
39
+
40
+ | Repository | Role | License |
41
+ |---|---|---|
42
+ | [`androiddrew/naptrap-phonmatchnet-en`](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en) | keyword matcher | Apache-2.0 |
43
+ | [`androiddrew/google-speech-embedding-pytorch`](https://huggingface.co/androiddrew/google-speech-embedding-pytorch) | frozen audio encoder | Apache-2.0 |
44
+ | [`androiddrew/g2p-en-decoder-pytorch`](https://huggingface.co/androiddrew/g2p-en-decoder-pytorch) | frozen keyword text encoder | Apache-2.0 |
45
+
46
+ To work offline, download them once and pass the directories:
47
+
48
+ ```bash
49
+ hf download androiddrew/naptrap-phonmatchnet-en --revision v1 --local-dir models/phonmatchnet
50
+ hf download androiddrew/google-speech-embedding-pytorch --revision v1 --local-dir models/google
51
+ hf download androiddrew/g2p-en-decoder-pytorch --revision v1 --local-dir models/g2p
52
+ ```
53
+
54
+ ```python
55
+ model = load_model("phonmatchnet", model_dir="models/phonmatchnet",
56
+ google_dir="models/google", g2p_dir="models/g2p")
57
+ ```
58
+
59
+ Every weight file is checked against the SHA-256 recorded in its `config.json`.
60
+ The keyword model also pins the exact encoders it was trained with.
61
+
62
+ ## Usage
63
+
64
+ ### Audio
65
+
66
+ `detect()` scores **one mono 16 kHz clip of 0.5–2 seconds**, given as a 1-D
67
+ float32 tensor or array with values in [-1, 1]. `load_wav()` reads 16-bit PCM
68
+ WAV files using only the standard library. Convert other files first:
69
+
70
+ ```bash
71
+ ffmpeg -i input.m4a -ac 1 -ar 16000 -sample_fmt s16 clip.wav
72
+ ```
73
+
74
+ naptrap does not resample, trim or rescale audio for you.
75
+
76
+ ### Reusing a keyword
77
+
78
+ Converting the keyword text is cheap, but if you score many clips against the
79
+ same keyword you can do it once:
80
+
81
+ ```python
82
+ wake = model.keyword("hey computer")
83
+ for clip in clips:
84
+ if model.detect(clip, wake).accepted:
85
+ ...
86
+ ```
87
+
88
+ Keywords are English words; accents and punctuation are removed. Spell out
89
+ numbers ("route sixty six").
90
+
91
+ ### Operating points and thresholds
92
+
93
+ ```python
94
+ model.thresholds # {0.01: 0.7149..., 0.1: 0.0025...}
95
+ model.detect(clip, "hey computer", far=0.1) # more sensitive
96
+ ```
97
+
98
+ Each threshold was fitted on held-out development speakers to target a
99
+ per-clip false-accept rate (FAR) of 1% or 10%. **These targets do not hold on
100
+ other audio.** On phonetically confusable phrases, the 1% threshold accepted
101
+ 31% of negatives. For a real product, record some positive and negative clips
102
+ in your setting and choose your own threshold on `result.score`. See the
103
+ [model card](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en) for
104
+ the evaluation.
105
+
106
+ ### Command line
107
+
108
+ ```bash
109
+ naptrap detect clip.wav "hey computer"
110
+ naptrap detect clip.wav "hey computer" --far 0.1 --model-dir models/phonmatchnet \
111
+ --google-dir models/google --g2p-dir models/g2p
112
+ ```
113
+
114
+ The command prints the prediction as JSON. It exits with 0 if the keyword was
115
+ accepted, 1 if it was rejected and 2 on error. `python -m naptrap` works the
116
+ same way.
117
+
118
+ ### Precomputed features
119
+
120
+ `model.predict({"keyword_mel": ..., "google": ..., "text": ...})` scores
121
+ features you computed yourself: `[T, 40]`, `[T/8, 96]` and `[P, 256]` float32.
122
+ The encoders are exposed as `naptrap.GoogleSpeechEmbedding` and
123
+ `naptrap.G2PTextEncoder`.
124
+
125
+ ## Limitations
126
+
127
+ - **Single clips, not a streaming listener (yet).** To watch a microphone you
128
+ must window the audio yourself, e.g. 1.5 s windows every 0.25 s, and add
129
+ your own debouncing.
130
+ - **English only**, trained on read audiobook speech (LibriSpeech). Expect
131
+ lower accuracy with noise, far-field microphones, accents or children.
132
+ - **No speaker verification.** Anyone saying the keyword triggers it.
133
+ - Similar-sounding phrases are often confused.
134
+ - CPU inference is the supported path; other devices have not been validated.
135
+ - Not for security or safety-critical use.
136
+
137
+ ## Adding model families
138
+
139
+ Backends implement `keyword()`, `detect()` and `predict()`, and return a
140
+ `Prediction`. Register a loader under a new name:
141
+
142
+ ```python
143
+ from naptrap import register_model, load_model
144
+
145
+ register_model("my-model", MyModel.from_pretrained)
146
+ model = load_model("my-model", model_dir="...")
147
+ ```
148
+
149
+ ## Development
150
+
151
+ ```bash
152
+ pip install -e ".[test]"
153
+ pytest
154
+ # Also run the real-model tests against downloaded model directories:
155
+ NAPTRAP_TEST_MODELS=path/to/models pytest
156
+ ```
157
+
158
+ `NAPTRAP_TEST_MODELS` must point at a directory holding the three model
159
+ repositories in subdirectories named `naptrap-phonmatchnet-en`,
160
+ `google-speech-embedding-pytorch` and `g2p-en-decoder-pytorch`.
161
+
162
+ ## License and attribution
163
+
164
+ naptrap is licensed under the [Apache License 2.0](LICENSE). The network
165
+ follows the PhonMatchNet design and NCSOFT's BSD-3-Clause reference code (see
166
+ [NOTICE](NOTICE) and [licenses/](licenses/)). The PhonMatchNet weights were
167
+ trained independently. **naptrap is not affiliated with or endorsed by NCSOFT
168
+ or the PhonMatchNet authors.** See
169
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for every component.
170
+
171
+ If naptrap's PhonMatchNet model is useful in your research, please cite:
172
+
173
+ ```bibtex
174
+ @inproceedings{lee23phonmatchnet,
175
+ title = {PhonMatchNet: Phoneme-Guided Zero-Shot Keyword Spotting for User-Defined Keywords},
176
+ author = {Lee, Yong-Hyeok and Cho, Namhyun},
177
+ booktitle = {Proc. INTERSPEECH 2023},
178
+ pages = {3964--3968},
179
+ year = {2023}
180
+ }
181
+ ```
@@ -0,0 +1,17 @@
1
+ # Third-party notices
2
+
3
+ naptrap's own code is licensed under Apache-2.0 (see `LICENSE` and `NOTICE`).
4
+ The package bundles **no model weights**. Weights are downloaded separately
5
+ from Hugging Face, and each repository carries its own license and notices.
6
+
7
+ | Component | Used for | License | Where |
8
+ |---|---|---|---|
9
+ | PhonMatchNet architecture and training code, © NCSOFT Corporation | `naptrap/models/network.py` follows its network design | BSD-3-Clause | `licenses/PhonMatchNet-BSD-3-Clause.txt`, <https://github.com/ncsoft/PhonMatchNet> |
10
+ | PhonMatchNet weights, trained independently by Drew Bednar | default keyword model | Apache-2.0 | <https://huggingface.co/androiddrew/naptrap-phonmatchnet-en> |
11
+ | Google speech_embedding/1 | frozen audio features (`naptrap/google.py`) | Apache-2.0 | <https://huggingface.co/androiddrew/google-speech-embedding-pytorch> |
12
+ | g2p_en 2.1.0, Kyubyong Park & Jongseok Kim | frozen keyword text features (`naptrap/text.py`) | Apache-2.0 | <https://huggingface.co/androiddrew/g2p-en-decoder-pytorch> |
13
+ | LibriSpeech (Panayotov et al.) | training data for the PhonMatchNet weights | CC BY 4.0 | <https://www.openslr.org/12> |
14
+ | PyTorch, safetensors, huggingface_hub (optional) | runtime dependencies, installed separately | BSD-3-Clause / Apache-2.0 / Apache-2.0 | PyPI |
15
+
16
+ naptrap and these weights are not affiliated with or endorsed by NCSOFT, the
17
+ PhonMatchNet authors, Google or the g2p_en authors.
@@ -0,0 +1,24 @@
1
+ # Compatibility
2
+
3
+ naptrap requires Python 3.9+, PyTorch 1.13+ and safetensors 0.4+. There are
4
+ no upper version pins. The library uses eager PyTorch modules only (no
5
+ TorchScript) and standard convolution, GRU and FFT operations.
6
+
7
+ The full test suite, including the released-model checks, passes on CPU with:
8
+
9
+ | Python | PyTorch | safetensors |
10
+ |---|---|---|
11
+ | 3.9 | 1.13.1 | 0.7 |
12
+ | 3.12 | 2.14.1 | 0.8 |
13
+ | 3.13 | 2.14.1 | 0.8 |
14
+
15
+ Any Python version with a PyTorch wheel for your platform should work.
16
+ Inference runs on the CPU. Scoring a 1.5 s clip takes about 7 ms using two
17
+ threads of a desktop CPU; converting a keyword takes about 2 ms.
18
+
19
+ NumPy is not required. `load_wav()` uses the standard library, and NumPy
20
+ arrays are accepted as inputs if you already have them.
21
+
22
+ The test extra installs NumPy, which safetensors needs to write the test
23
+ fixtures. With PyTorch < 2.3, use NumPy 1.x to avoid a harmless NumPy-ABI
24
+ warning.