Skip to content

Commit da36f83

Browse files
committed
feat(asr.py): add example for asr
1 parent 4248581 commit da36f83

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

examples/python/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ python embedder.py --model NexaAI/embeddinggemma-300m-npu --plugin-id npu --text
103103
```bash
104104
nexa pull NexaAI/paddleocr-npu
105105
106-
python cv_ocr.py --det-model NexaAI/paddleocr-npu --rec-model NexaAI/paddleocr-npu --image c:/Users/mengshengwu/workspace/nexa-sdk/temp/image.png
106+
python cv_ocr.py --det-model NexaAI/paddleocr-npu --rec-model NexaAI/paddleocr-npu --image path/to/image.png
107+
```
108+
109+
### ASR
110+
```bash
111+
nexa pull NexaAI/parakeet-npu
112+
113+
python asr.py --model NexaAI/parakeet-npu --audio path/to/audio.wav
107114
```
108115

109116
## Common Arguments

examples/python/asr.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
NexaAI ASR Example - Speech to Text (non-streaming)
3+
4+
This example demonstrates how to use the NexaAI SDK to transcribe an audio file.
5+
"""
6+
7+
import argparse
8+
import os
9+
10+
from nexaai.asr import ASR, ASRConfig
11+
12+
def main():
13+
parser = argparse.ArgumentParser(description="NexaAI ASR Example")
14+
parser.add_argument("--model",
15+
default="NexaAI/parakeet-npu",
16+
help="Model id or path")
17+
parser.add_argument("--audio",
18+
required=True,
19+
help="Path to the input audio file")
20+
parser.add_argument("--language", default="en",
21+
help="Language code (e.g., en, zh). Empty for auto-detect if supported")
22+
parser.add_argument("--beam-size", type=int, default=5,
23+
help="Beam size for decoding")
24+
parser.add_argument("--timestamps", default="segment",
25+
help="Timestamps granularity: none|segment|word (if supported)")
26+
parser.add_argument("--plugin-id", default="npu", help="Plugin ID to use")
27+
parser.add_argument("--device", default="npu", help="Device to run on (e.g., cpu, gpu, 0)")
28+
args = parser.parse_args()
29+
30+
model_path = os.path.expanduser(args.model)
31+
audio_path = os.path.expanduser(args.audio)
32+
33+
if not os.path.exists(audio_path):
34+
raise FileNotFoundError(f"Audio file not found: {audio_path}")
35+
36+
asr = ASR.from_(name_or_path=model_path, plugin_id=args.plugin_id, device_id=args.device)
37+
38+
cfg = ASRConfig(timestamps=args.timestamps, beam_size=args.beam_size, stream=False)
39+
result = asr.transcribe(audio_path=audio_path, language=args.language, config=cfg)
40+
print(result.transcript)
41+
42+
43+
if __name__ == "__main__":
44+
main()
45+
46+

0 commit comments

Comments
 (0)