run this https://huggingface.co/speechbrain/emotion-diarization-wavlm-large/tree/main…
- User
- Assistant
Certainly! You want to run the Emotion Diarization model from SpeechBrain with optimum's ONNX Runtime support. Let's break down the steps to achieve this:
Install Required Packages: First, ensure you have the necessary libraries installed. You'll need speechbrain, transformers, optimum, and onnxruntime.
!pip install speechbrain transformers optimum onnxruntime
Download and Convert the Model: The Emotion Diarization model isn't directly available as an ONNX model. You need to download the pre-trained model using SpeechBrain and convert it.
Run the Model: Once you have the model in ONNX format, you can use optimum to load and run inference.
Here's a complete example that demonstrates the process:
import torch
import torchaudio
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
import onnxruntime as ort
from optimum.onnxruntime import ORTModel
# Download the emotion diarization model from SpeechBrain
from speechbrain.pretrained import EncoderClassifier
pretrained_model = EncoderClassifier.from_hparams(source="speechbrain/emotion-diarization-wavlm-large")
# Download a sample audio file for testing
import requests
from scipy.io import wavfile
url = "https://huggingface.co/speechbrain/emotion-diarization-wavlm-large/blob/main/samples/sample1.wav"
response = requests.get(url)
with open("sample.wav", "wb") as f:
f.write(response.content)
# Load the audio file
def load_audio(file_name):
waveform, sample_rate = torchaudio.load(file_name)
return waveform, sample_rate
waveform, sample_rate = load_audio("sample.wav")
# Evaluate emotion using the pretrained SpeechBrain model
emotions = pretrained_model.classify_file("sample.wav")
print(emotions)
# Convert the model to ONNX format
input_names = ["input"]
output_names = ["output"]
# Placeholder for exporting the model to ONNX format
dummy_input = torch.randn(1, 1, 16000, requires_grad=True)
torch.onnx.export(pretrained_model.mods.embedding_model, dummy_input,
"emotion_diarization.onnx", verbose=True, input_names=input_names, output_names=output_names,
export_params=True, opset_version=12)
# Load the ONNX model
onnx_path = "emotion_diarization.onnx"
ort_model = ORTModel(onnx_path, "onnxruntime")
# Prepare input tensor
waveform = torch.unsqueeze(waveform, 0).numpy()
# Run inference
onnx_session = ort.InferenceSession(onnx_path)
inputs = {onnx_session.get_inputs()[0].name: waveform}
ort_outs = onnx_session.run(None, inputs)
print("ONNX model output:", ort_outs)
Explanation:
This code assumes you have an available environment where you can run the script. Ensure you have CUDA if you're running inference on a GPU.
Not yet included in a published release.