Repository navigation
Expand file tree
/
Copy pathintent_recognizer.py
More file actions
660 lines (559 loc) · 23.3 KB
/
Copy pathintent_recognizer.py
File metadata and controls
660 lines (559 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
"""Intent recognition module for Moonshine Voice.
This module provides intent recognition capabilities using semantic embeddings.
It can be used standalone or as a TranscriptEventListener to automatically
recognize intents from transcribed speech.
"""
import ctypes
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Dict, List, Optional
from moonshine_voice.moonshine_api import MoonshineIntentMatchC, _MoonshineLib
from moonshine_voice.errors import MoonshineError, check_error
from moonshine_voice.transcriber import (
TranscriptEventListener,
LineCompleted,
Error,
)
from moonshine_voice.download import EmbeddingModelArch
# Callback type for intent handlers
# Signature: (canonical_phrase: str, utterance: str, similarity: float) -> None
IntentHandler = Callable[[str, str, float], None]
@dataclass
class IntentMatch:
"""Represents a matched intent."""
canonical_phrase: str
utterance: str
similarity: float
@property
def trigger_phrase(self) -> str:
"""Alias for ``canonical_phrase`` (backward compatibility)."""
return self.canonical_phrase
class IntentRecognizer(TranscriptEventListener):
"""Intent recognizer that uses semantic embeddings to match utterances.
This class can be used standalone by calling process_utterance(), or as
a TranscriptEventListener to automatically process completed transcript
lines.
Example standalone usage:
>>> recognizer = IntentRecognizer("path/to/embedding-model")
>>> recognizer.register_intent("turn on the lights", lambda c, u, s: print(f"Lights on! ({s:.2f})"))
>>> recognizer.process_utterance("switch on the lights")
Lights on! (0.92)
Example as TranscriptEventListener:
>>> recognizer = IntentRecognizer("path/to/embedding-model")
>>> recognizer.register_intent("turn on the lights", lambda c, u, s: print(f"Lights on!"))
>>> transcriber.add_listener(recognizer) # Now intents are recognized automatically
"""
def __init__(
self,
model_path: str | Path,
model_arch: EmbeddingModelArch = EmbeddingModelArch.GEMMA_300M,
model_variant: str = "fp32",
threshold: float = 0.8,
):
"""
Initialize an intent recognizer.
Args:
model_path: Path to the directory containing the embedding model files
(ONNX model and tokenizer.bin).
model_arch: The embedding model architecture to use.
Currently only GEMMA_300M is supported.
model_variant: Model variant to load: "fp32", "fp16", "q8", "q4",
or "q4f16". Default is "q4" for efficiency.
threshold: The minimum similarity threshold used by process_utterance
and by get_closest_intents when no per-call threshold is
passed (default 0.8, range 0.0-1.0). Filtering is applied in
the native ``moonshine_get_closest_intents`` call, not at
construction.
"""
self._lib_wrapper = _MoonshineLib()
self._lib = self._lib_wrapper.lib
self._handle = None
self._handlers: Dict[str, IntentHandler] = {}
self._on_intent_callback: Optional[Callable[[IntentMatch], None]] = None
self._threshold = threshold
self._setup_function_signatures()
# Create the intent recognizer
model_path_bytes = str(model_path).encode("utf-8")
model_variant_bytes = model_variant.encode("utf-8") if model_variant else None
handle = self._lib.moonshine_create_intent_recognizer(
model_path_bytes,
model_arch.value,
model_variant_bytes,
)
if handle < 0:
raise MoonshineError(
f"Failed to create intent recognizer from {model_path}"
)
self._handle = handle
def _setup_function_signatures(self):
"""Setup ctypes function signatures for the intent recognizer C API."""
lib = self._lib
# Create intent recognizer
lib.moonshine_create_intent_recognizer.restype = ctypes.c_int32
lib.moonshine_create_intent_recognizer.argtypes = [
ctypes.c_char_p, # model_path
ctypes.c_uint32, # model_arch
ctypes.c_char_p, # model_variant
]
# Free intent recognizer
lib.moonshine_free_intent_recognizer.restype = None
lib.moonshine_free_intent_recognizer.argtypes = [ctypes.c_int32]
# Register intent
lib.moonshine_register_intent.restype = ctypes.c_int32
lib.moonshine_register_intent.argtypes = [
ctypes.c_int32, # intent_recognizer_handle
ctypes.c_char_p, # canonical_phrase
ctypes.POINTER(ctypes.c_float), # embedding (nullable)
ctypes.c_uint64, # embedding_size
ctypes.c_int32, # priority
]
# Unregister intent
lib.moonshine_unregister_intent.restype = ctypes.c_int32
lib.moonshine_unregister_intent.argtypes = [
ctypes.c_int32, # intent_recognizer_handle
ctypes.c_char_p, # canonical_phrase
]
lib.moonshine_get_closest_intents.restype = ctypes.c_int32
lib.moonshine_get_closest_intents.argtypes = [
ctypes.c_int32,
ctypes.c_char_p,
ctypes.c_float,
ctypes.POINTER(ctypes.POINTER(MoonshineIntentMatchC)),
ctypes.POINTER(ctypes.c_uint64),
]
lib.moonshine_free_intent_matches.restype = None
lib.moonshine_free_intent_matches.argtypes = [
ctypes.POINTER(MoonshineIntentMatchC),
ctypes.c_uint64,
]
# Get intent count
lib.moonshine_get_intent_count.restype = ctypes.c_int32
lib.moonshine_get_intent_count.argtypes = [ctypes.c_int32]
# Clear intents
lib.moonshine_clear_intents.restype = ctypes.c_int32
lib.moonshine_clear_intents.argtypes = [ctypes.c_int32]
# Calculate intent embedding
lib.moonshine_calculate_intent_embedding.restype = ctypes.c_int32
lib.moonshine_calculate_intent_embedding.argtypes = [
ctypes.c_int32, # intent_recognizer_handle
ctypes.c_char_p, # sentence
ctypes.POINTER(ctypes.POINTER(ctypes.c_float)), # out_embedding
ctypes.POINTER(ctypes.c_uint64), # out_embedding_size
ctypes.c_char_p, # model_name (nullable)
]
# Free intent embedding
lib.moonshine_free_intent_embedding.restype = None
lib.moonshine_free_intent_embedding.argtypes = [
ctypes.POINTER(ctypes.c_float),
]
# Calculate embedding distance (cosine similarity)
lib.moonshine_calculate_embedding_distance.restype = ctypes.c_int32
lib.moonshine_calculate_embedding_distance.argtypes = [
ctypes.c_int32, # intent_recognizer_handle
ctypes.POINTER(ctypes.c_float), # embedding_a
ctypes.POINTER(ctypes.c_float), # embedding_b
ctypes.c_uint64, # embedding_size
ctypes.POINTER(ctypes.c_float), # out_similarity
]
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit."""
self.close()
def close(self):
"""Free the intent recognizer resources."""
if self._handle is not None:
self._lib.moonshine_free_intent_recognizer(self._handle)
self._handle = None
self._handlers.clear()
def __del__(self):
"""Cleanup on deletion."""
if hasattr(self, "_handle"):
self.close()
def register_intent(
self,
trigger_phrase: str,
handler: Optional[IntentHandler] = None,
*,
embedding: Optional[List[float]] = None,
priority: int = 0,
) -> None:
"""
Register an intent with a canonical phrase.
Args:
trigger_phrase: The canonical phrase for this intent.
handler: Optional callable ``(canonical_phrase, utterance, similarity) -> None``
invoked by :meth:`process_utterance` for the best match.
embedding: Optional pre-computed embedding vector. When *None* the
native library computes the embedding from *trigger_phrase*.
priority: Higher-priority intents rank above lower-priority ones in
:meth:`get_closest_intents`, even when their similarity score
is lower. Defaults to ``0``.
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
if handler is not None:
self._handlers[trigger_phrase] = handler
trigger_bytes = trigger_phrase.encode("utf-8")
if embedding is not None:
c_arr = (ctypes.c_float * len(embedding))(*embedding)
c_ptr = ctypes.cast(c_arr, ctypes.POINTER(ctypes.c_float))
c_size = ctypes.c_uint64(len(embedding))
else:
c_ptr = None
c_size = ctypes.c_uint64(0)
error = self._lib.moonshine_register_intent(
self._handle, trigger_bytes, c_ptr, c_size, ctypes.c_int32(priority)
)
check_error(error)
def unregister_intent(self, trigger_phrase: str) -> bool:
"""
Remove a registered intent.
Args:
trigger_phrase: The trigger phrase of the intent to remove.
Returns:
True if the intent was found and removed, False otherwise.
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
trigger_bytes = trigger_phrase.encode("utf-8")
result = self._lib.moonshine_unregister_intent(self._handle, trigger_bytes)
if result == 0:
self._handlers.pop(trigger_phrase, None)
return True
return False
def get_closest_intents(
self,
utterance: str,
tolerance_threshold: Optional[float] = None,
) -> List[IntentMatch]:
"""
Rank registered intents against ``utterance`` synchronously.
Returns up to six matches at or above ``tolerance_threshold``, sorted by
descending similarity. Pass ``None`` for ``tolerance_threshold`` to use
this recognizer's ``threshold`` property.
The returned list is owned by Python (not backed by native memory).
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
thresh = (
float(tolerance_threshold)
if tolerance_threshold is not None
else self._threshold
)
utterance_bytes = utterance.encode("utf-8")
matches_ptr = ctypes.POINTER(MoonshineIntentMatchC)()
count = ctypes.c_uint64(0)
err = self._lib.moonshine_get_closest_intents(
self._handle,
utterance_bytes,
ctypes.c_float(thresh),
ctypes.byref(matches_ptr),
ctypes.byref(count),
)
if err != 0:
check_error(err)
n = int(count.value)
out: List[IntentMatch] = []
try:
for i in range(n):
row = matches_ptr[i]
phrase = (
row.canonical_phrase.decode("utf-8")
if row.canonical_phrase
else ""
)
out.append(
IntentMatch(
canonical_phrase=phrase,
utterance=utterance,
similarity=float(row.similarity),
)
)
finally:
self._lib.moonshine_free_intent_matches(matches_ptr, count.value)
return out
def process_utterance(self, utterance: str) -> bool:
"""
Process an utterance and invoke the handler of the most similar intent.
Args:
utterance: The utterance to process.
Returns:
True if an intent was recognized and handler invoked, False otherwise.
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
matches = self.get_closest_intents(utterance, self._threshold)
if not matches:
return False
top = matches[0]
canonical = top.canonical_phrase
if canonical in self._handlers:
self._handlers[canonical](canonical, utterance, top.similarity)
if self._on_intent_callback:
self._on_intent_callback(top)
return True
@property
def threshold(self) -> float:
"""Minimum similarity used by ``process_utterance`` / default for ``get_closest_intents``."""
return self._threshold
@threshold.setter
def threshold(self, value: float) -> None:
"""Set the default similarity threshold (Python-side; passed per call to native code)."""
self._threshold = float(value)
@property
def intent_count(self) -> int:
"""Get the number of registered intents."""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
count = self._lib.moonshine_get_intent_count(self._handle)
if count < 0:
check_error(count)
return count
def clear_intents(self) -> None:
"""Clear all registered intents."""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
error = self._lib.moonshine_clear_intents(self._handle)
check_error(error)
self._handlers.clear()
def calculate_embedding(
self, sentence: str, *, model_name: Optional[str] = None
) -> List[float]:
"""
Calculate the embedding vector for a sentence.
This is useful for pre-computing embeddings that can later be passed to
:meth:`register_intent` via the *embedding* parameter.
Args:
sentence: The input text to embed.
model_name: Reserved for future use; currently ignored by the native
library. Pass *None*.
Returns:
A list of floats representing the embedding vector.
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
out_ptr = ctypes.POINTER(ctypes.c_float)()
out_size = ctypes.c_uint64(0)
model_bytes = model_name.encode("utf-8") if model_name else None
error = self._lib.moonshine_calculate_intent_embedding(
self._handle,
sentence.encode("utf-8"),
ctypes.byref(out_ptr),
ctypes.byref(out_size),
model_bytes,
)
check_error(error)
n = int(out_size.value)
result = [float(out_ptr[i]) for i in range(n)]
self._lib.moonshine_free_intent_embedding(out_ptr)
return result
def distance(
self, embedding_a: List[float], embedding_b: List[float]
) -> float:
"""
Compute the cosine similarity between two embedding vectors.
Args:
embedding_a: The first embedding vector.
embedding_b: The second embedding vector. Must be the same length
as *embedding_a*.
Returns:
Cosine similarity in the range [-1, 1]. 1 means identical,
0 means orthogonal, -1 means opposite.
"""
if self._handle is None:
raise MoonshineError("Intent recognizer is not initialized")
if len(embedding_a) != len(embedding_b):
raise ValueError(
f"Embedding sizes differ: {len(embedding_a)} vs {len(embedding_b)}"
)
n = len(embedding_a)
arr_a = (ctypes.c_float * n)(*embedding_a)
arr_b = (ctypes.c_float * n)(*embedding_b)
out = ctypes.c_float(0.0)
error = self._lib.moonshine_calculate_embedding_distance(
self._handle, arr_a, arr_b, ctypes.c_uint64(n), ctypes.byref(out)
)
check_error(error)
return float(out.value)
def set_on_intent(self, callback: Optional[Callable[[IntentMatch], None]]) -> None:
"""
Set a callback that is invoked for any recognized intent.
This is useful when using the IntentRecognizer as a TranscriptEventListener,
as it allows you to handle all intents in one place rather than
registering individual handlers.
Args:
callback: A callable that takes an IntentMatch, or None to clear.
"""
self._on_intent_callback = callback
# TranscriptEventListener implementation
def on_line_completed(self, event: LineCompleted) -> None:
"""
Called when a transcription line is completed.
This implements the TranscriptEventListener interface, allowing the
IntentRecognizer to automatically process completed transcript lines.
Args:
event: The LineCompleted event containing the transcript line.
"""
if event.line and event.line.text:
# Strip whitespace and process non-empty utterances
utterance = event.line.text.strip()
if utterance:
self.process_utterance(utterance)
def on_error(self, event: Error) -> None:
"""
Called when an error occurs.
Args:
event: The Error event.
"""
# Log or handle errors as needed
pass
if __name__ == "__main__":
import argparse
import sys
from moonshine_voice import get_model_for_language, get_embedding_model
from moonshine_voice import (
MicTranscriber,
Transcriber,
load_wav_file,
TranscriptEventListener,
)
import time
parser = argparse.ArgumentParser(
description="Intent recognition with Moonshine Voice"
)
parser.add_argument(
"--language",
type=str,
default="en",
help="Language to use for transcription (default: en)",
)
parser.add_argument(
"--model-arch",
type=int,
default=None,
help="Model architecture to use for transcription",
)
parser.add_argument(
"--embedding-model",
type=str,
default="embeddinggemma-300m",
help="Embedding model name (default: embeddinggemma-300m)",
)
parser.add_argument(
"--threshold",
type=float,
default=0.8,
help="Similarity threshold for intent matching (default: 0.8)",
)
parser.add_argument(
"--intents",
type=str,
default="turn on the lights,turn off the lights,what is the weather,set a timer,play some music,stop the music",
help="Actions to handle, separated by commas",
)
parser.add_argument(
"--wav-file",
type=str,
default=None,
help="WAV file to process instead of using the microphone",
)
parser.add_argument(
"--quantization",
type=str,
default="fp32",
help="Quantization type for model: fp32, fp16, q8, q4, or q4f16. Default is fp32.",
)
args = parser.parse_args()
def on_intent_triggered_on(trigger: str, utterance: str, similarity: float):
"""Handler for when an intent is triggered."""
print(
f"\n'{trigger.upper()}' triggered by '{utterance}' with {similarity:.0%} confidence"
)
class TranscriptPrinter(TranscriptEventListener):
"""Listener that prints transcript updates to the terminal."""
def __init__(self):
self.last_line_text_length = 0
def update_last_terminal_line(self, new_text: str):
print(f"\r{new_text}", end="", flush=True)
if len(new_text) < self.last_line_text_length:
diff = self.last_line_text_length - len(new_text)
print(f"{' ' * diff}", end="", flush=True)
self.last_line_text_length = len(new_text)
def on_line_started(self, event):
self.last_line_text_length = 0
def on_line_text_changed(self, event):
self.update_last_terminal_line(f"📝 {event.line.text}")
def on_line_completed(self, event):
self.update_last_terminal_line(f"📝 {event.line.text}")
print() # New line after completion
# Load the transcription model
print("Loading transcription model...", file=sys.stderr)
model_path, model_arch = get_model_for_language(args.language, args.model_arch)
# Download and load the embedding model for intent recognition
print(
f"Loading embedding model ({args.embedding_model}, variant={args.quantization})...",
file=sys.stderr,
)
embedding_model_path, embedding_model_arch = get_embedding_model(
args.embedding_model, args.quantization
)
# Create the intent recognizer (implements TranscriptEventListener)
print(
f"Creating intent recognizer (threshold={args.threshold})...", file=sys.stderr
)
intent_recognizer = IntentRecognizer(
model_path=embedding_model_path,
model_arch=embedding_model_arch,
model_variant=args.quantization,
threshold=args.threshold,
)
# Register intents with their trigger phrases and handlers
intents = args.intents.split(",")
intents = [intent.strip() for intent in intents]
if len(intents) == 0:
raise ValueError("No intents provided")
for intent in intents:
intent_recognizer.register_intent(intent, on_intent_triggered_on)
print(f"Registered {intent_recognizer.intent_count} intents", file=sys.stderr)
# Create the microphone transcriber or use the WAV file if provided
if args.wav_file is None:
transcriber = MicTranscriber(model_path=model_path, model_arch=model_arch)
else:
transcriber = Transcriber(model_path=model_path, model_arch=model_arch)
audio_data, sample_rate = load_wav_file(args.wav_file)
# Add both the transcript printer and intent recognizer as listeners
# The intent recognizer will process completed lines and trigger handlers
transcript_printer = TranscriptPrinter()
transcriber.add_listener(transcript_printer)
transcriber.add_listener(intent_recognizer)
print("\n" + "=" * 60, file=sys.stderr)
print("🎤 Listening for voice commands...", file=sys.stderr)
print("Try saying phrases with the same meaning as these actions:", file=sys.stderr)
for intent in intents:
print(f" - '{intent}'", file=sys.stderr)
print(
"We're doing fuzzy matching of natural language, so phrases like 'Switch on the lights' or 'Lights on' or 'Let there be light' will trigger the 'turn on the lights' action, for example."
)
print("=" * 60, file=sys.stderr)
print("Press Ctrl+C to stop.\n", file=sys.stderr)
transcriber.start()
try:
# Use the microphone if no WAV file is provided
if args.wav_file is None:
while True:
time.sleep(0.1)
else:
# Process the WAV file in chunks
chunk_duration = 0.2
chunk_size = int(chunk_duration * sample_rate)
for i in range(0, len(audio_data), chunk_size):
chunk = audio_data[i : i + chunk_size]
transcriber.add_audio(chunk, sample_rate)
except KeyboardInterrupt:
print("\n\nStopping...", file=sys.stderr)
finally:
intent_recognizer.close()
transcriber.stop()
transcriber.close()