Views
No views yet
1import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers';
2
3// Load model and processor
4const model_id = 'onnx-community/pyannote-segmentation-3.0';
5const model = await AutoModelForAudioFrameClassification.from_pretrained(model_id);
6const processor = await AutoProcessor.from_pretrained(model_id);
7
8// Read and preprocess audio
9const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/mlk.wav';
10const audio = await read_audio(url, processor.feature_extractor.config.sampling_rate);
11const inputs = await processor(audio);
12
13// Run model with inputs
14const { logits } = await model(inputs);
15// {
16// logits: Tensor {
17// dims: [ 1, 767, 7 ], // [batch_size, num_frames, num_classes]
18// type: 'float32',
19// data: Float32Array(5369) [ ... ],
20// size: 5369
21// }
22// }
23
24const result = processor.post_process_speaker_diarization(logits, audio.length);
25// [
26// [
27// { id: 0, start: 0, end: 1.0512535626298245, confidence: 0.8220156481664611 },
28// { id: 2, start: 1.0512535626298245, end: 2.3398869619825127, confidence: 0.9008811707860472 },
29// ...
30// ]
31// ]
32
33// Display result
34console.table(result[0], ['start', 'end', 'id', 'confidence']);
35// ┌─────────┬────────────────────┬────────────────────┬────┬─────────────────────┐
36// │ (index) │ start │ end │ id │ confidence │
37// ├─────────┼────────────────────┼────────────────────┼────┼─────────────────────┤
38// │ 0 │ 0 │ 1.0512535626298245 │ 0 │ 0.8220156481664611 │
39// │ 1 │ 1.0512535626298245 │ 2.3398869619825127 │ 2 │ 0.9008811707860472 │
40// │ 2 │ 2.3398869619825127 │ 3.5946089560890773 │ 0 │ 0.7521651315796233 │
41// │ 3 │ 3.5946089560890773 │ 4.578039708226655 │ 2 │ 0.8491978128022479 │
42// │ 4 │ 4.578039708226655 │ 4.594995410849717 │ 0 │ 0.2935352600416393 │
43// │ 5 │ 4.594995410849717 │ 6.121008646925269 │ 3 │ 0.6788051309866024 │
44// │ 6 │ 6.121008646925269 │ 6.256654267909762 │ 0 │ 0.37125512393851134 │
45// │ 7 │ 6.256654267909762 │ 8.630452635138397 │ 2 │ 0.7467035186353542 │
46// │ 8 │ 8.630452635138397 │ 10.088643060721703 │ 0 │ 0.7689364814666032 │
47// │ 9 │ 10.088643060721703 │ 12.58113134631177 │ 2 │ 0.9123324509131324 │
48// │ 10 │ 12.58113134631177 │ 13.005023911888312 │ 0 │ 0.4828358177572041 │
49// └─────────┴────────────────────┴────────────────────┴────┴─────────────────────┘1# pip install torch onnx https://github.com/pyannote/pyannote-audio/archive/refs/heads/develop.zip
2import torch
3from pyannote.audio import Model
4
5model = Model.from_pretrained(
6 "pyannote/segmentation-3.0",
7 use_auth_token="hf_...", # <-- Set your HF token here
8).eval()
9
10dummy_input = torch.zeros(2, 1, 160000)
11torch.onnx.export(
12 model,
13 dummy_input,
14 'model.onnx',
15 do_constant_folding=True,
16 input_names=["input_values"],
17 output_names=["logits"],
18 dynamic_axes={
19 "input_values": {0: "batch_size", 1: "num_channels", 2: "num_samples"},
20 "logits": {0: "batch_size", 1: "num_frames"},
21 },
22)onnx).