Views
No views yet
openai/whisper-small for single-label classification. It categorizes the perceived pitch level of an audio clip into one of three mutually exclusive classes.high-pitched: Relatively high perceived vocal pitch.medium-pitched: Moderate or neutral perceived vocal pitch.low-pitched: Relatively low perceived vocal pitch.WhisperProcessor for resampling and log-Mel feature extraction.openai/whisper-small1{
2 0: "high-pitched",
3 1: "medium-pitched",
4 2: "low-pitched",
5}1import torch
2import librosa
3import numpy as np
4from transformers import WhisperProcessor, WhisperForAudioClassification
5
6model_id = "Kang-Chieh/whisper-small-pitch-level"
7processor_id = "openai/whisper-small"
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10# Load the processor and model
11processor = WhisperProcessor.from_pretrained(processor_id)
12model = WhisperForAudioClassification.from_pretrained(model_id).to(device)
13
14def predict_pitch(audio_path):
15 # 1. Load audio and ensure 16kHz mono audio
16 audio, _ = librosa.load(audio_path, sr=16000, mono=True)
17
18 # 2. Preprocess
19 inputs = processor(audio, sampling_rate=16000, return_tensors="pt")
20 input_features = inputs.input_features.to(device)
21
22 # 3. Inference
23 with torch.no_grad():
24 logits = model(input_features=input_features).logits
25
26 # 4. Single-label logic (Softmax)
27 probs = torch.softmax(logits, dim=-1).squeeze().cpu().numpy()
28
29 # 5. Get the highest scoring label
30 id2label = {int(k): v for k, v in model.config.id2label.items()}
31 predicted_id = int(np.argmax(probs))
32
33 return {
34 "label": id2label[predicted_id],
35 "confidence": float(probs[predicted_id]),
36 "all_scores": {id2label[i]: float(probs[i]) for i in range(len(probs))},
37 }
38
39# Run example
40result = predict_pitch("audio_clip.wav")
41print(f"Detected Pitch: {result['label']} ({result['confidence']:.2%})")high-pitched: precision 0.85, recall 0.66, f1-score 0.74medium-pitched: precision 0.59, recall 0.69, f1-score 0.64low-pitched: precision 0.66, recall 0.69, f1-score 0.67