File size: 1,498 Bytes
bd48fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# Copyright (c) 2026, CIIL Anuvani-LID. Apache-2.0.
"""Reference output of the binding tests, from the sherpa-onnx Python package. Every binding test prints
the same JSON lines for the same inputs:
  {"file": <clip>, "events": [[label, index, prob], ...]}   AudioTagging, all 23 languages, largest first
  {"pipeline_file": <recording>, "pipeline": {...}}           LidPipeline JSON of the long recording

    python3 tests/bindings/python_ref.py <lid model> <vad model> <long wav> <clip.wav> ...
"""
import json
import os
import sys

import sherpa_onnx
import soundfile as sf


def main():
    model, vad, long_wav, clips = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
    tagger = sherpa_onnx.AudioTagging(sherpa_onnx.AudioTaggingConfig(
        model=sherpa_onnx.AudioTaggingModelConfig(lid=model, num_threads=2), top_k=23))
    for path in clips:
        w, sr = sf.read(path, dtype="float32")
        s = tagger.create_stream()
        s.accept_waveform(sr, w)
        ev = tagger.compute(s)
        print(json.dumps({"file": os.path.basename(path), "events": [[e.name, e.index, e.prob] for e in ev]}))
    pipe = sherpa_onnx.LidPipeline(sherpa_onnx.LidPipelineConfig(
        model=sherpa_onnx.AudioTaggingModelConfig(lid=model, num_threads=2), vad_model=vad))
    w, sr = sf.read(long_wav, dtype="float32")
    print(json.dumps({"pipeline_file": os.path.basename(long_wav), "pipeline": json.loads(pipe.process(w, sr))}))


if __name__ == "__main__":
    main()