confit/cremad-parquet
Viewer • Updated • 7.44k • 469 • 2
How to use marshal-yash/SER_wav2vec with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("audio-classification", model="marshal-yash/SER_wav2vec") # Load model directly
from transformers import AutoProcessor, AutoModelForAudioClassification
processor = AutoProcessor.from_pretrained("marshal-yash/SER_wav2vec")
model = AutoModelForAudioClassification.from_pretrained("marshal-yash/SER_wav2vec")Wav2Vec2ForSequenceClassification).angry, calm, disgust, fear, happy, neutral, sad, surprise.facebook/wav2vec2-base-960h fine‑tuned for emotion classification.confit/cremad-parquet).import torch, numpy as np, soundfile as sf, librosa
from transformers import Wav2Vec2ForSequenceClassification, AutoFeatureExtractor
model_dir = "path/to/best_model" # local directory or Hub repo id
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_dir)
fe = AutoFeatureExtractor.from_pretrained(model_dir)
model.eval()
def load_audio(path, sr=fe.sampling_rate):
y, s = sf.read(path, always_2d=False)
if isinstance(y, np.ndarray):
if y.ndim > 1:
y = np.mean(y, axis=1)
if s != sr:
y = librosa.resample(y.astype(np.float32), orig_sr=s, target_sr=sr)
y = y.astype(np.float32)
else:
y = np.array(y, dtype=np.float32)
if y.size < sr // 10:
y = np.pad(y, (0, max(0, sr - y.size)))
return y
audio = load_audio("sample.wav")
inputs = fe(audio, sampling_rate=fe.sampling_rate, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)[0].cpu().numpy()
labels = [model.config.id2label[str(i)] if isinstance(list(model.config.id2label.keys())[0], str) else model.config.id2label[i] for i in range(len(probs))]
pairs = sorted(zip(labels, probs), key=lambda x: x[1], reverse=True)
print(pairs[:3])
This repository includes a FastAPI server that exposes POST /predict and returns sorted label probabilities and the dominant emotion.
wav, mp3, m4a, etc.). Internally converted to 16 kHz mono.{
"results": [{ "label": "happy", "score": 0.81 }, ...],
"dominant": { "label": "happy", "score": 0.81 }
}
If you use this model, please cite:
MIT
Base model
facebook/wav2vec2-base-960h