Working example

#3
by vrajshroff - opened

Code in model card no longer works. Here is a working example for people new to this like me:

Notes:

I hope this helps!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Speaker Diarization with Transformers.js</title>
</head>
<body>
<h1>Speaker Diarization Example</h1>

<script type="module">
import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]';


(async () => {
  try {
    // Load model and processor
    const model_id = 'onnx-community/pyannote-segmentation-3.0';
    const model = await AutoModelForAudioFrameClassification.from_pretrained(model_id);
    const processor = await AutoProcessor.from_pretrained(model_id);

    // Read and preprocess audio
    const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/mlk.wav';
    const audio = await read_audio(url, processor.feature_extractor.config.sampling_rate);
    const inputs = await processor(audio);

    // Run model with inputs
    const { logits } = await model(inputs);
    const result = processor.post_process_speaker_diarization(logits, audio.length);

    console.table(result[0], ['start', 'end', 'id', 'confidence']);
  } catch (err) {
    console.error("Error running the diarization:", err);
  }
})();
</script>

</body>
</html>

Sign up or log in to comment