Views
No views yet
| Task | ID | Description |
|---|---|---|
| tags | 0 | General descriptions, can include genres and features. |
| genre | 1 | Estimated musical genres. |
| mood | 2 | Estimated emotional feeling. |
| movement | 3 | Estimated audio pace and expression. |
| theme | 4 | Estimated audio usage (not very accurate) |
1# Load model
2checkpoint = "DionTimmer/whisper-small-multitask-analyzer"
3model = WhisperForAudioCaptioning.from_pretrained(checkpoint)
4tokenizer = transformers.WhisperTokenizer.from_pretrained(checkpoint, language="en", task="transcribe")
5feature_extractor = transformers.WhisperFeatureExtractor.from_pretrained(checkpoint)
6
7# Load and preprocess audio
8input_file = "..."
9audio, sampling_rate = librosa.load(input_file, sr=feature_extractor.sampling_rate)
10features = feature_extractor(audio, sampling_rate=sampling_rate, return_tensors="pt").input_features
11
12# Mappings by ID
13print(model.task_mapping) # {0: 'tags', 1: 'genre', 2: 'mood', 3: 'movement', 4: 'theme'}
14
15# Inverted
16print(model.named_task_mapping) # {'tags': 0, 'genre': 1, 'mood': 2, 'movement': 3, 'theme': 4}
17
18# Prepare caption style
19style_prefix = f"{model.named_task_mapping['tags']}: "
20style_prefix_tokens = tokenizer("", text_target=style_prefix, return_tensors="pt", add_special_tokens=False).labels
21
22# Generate caption
23model.eval()
24outputs = model.generate(
25 inputs=features.to(model.device),
26 forced_ac_decoder_ids=style_prefix_tokens,
27 max_length=100,
28)
29
30print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])language="en" and task="transcribe".WhisperForAudioCaptioning can be found in the git repository or here on the HuggingFace Hub in the model repository. The class overrides default Whisper generate method to support forcing decoder prefix.