Views
No views yet
1import os
2import torch
3import librosa
4from transformers import logging
5from transformers import AutoTokenizer
6from models_xin import CLAP
7from utils import compute_similarity
8
9
10if __name__ == '__main__':
11 logging.set_verbosity_error()
12 ckpt = torch.hub.load_state_dict_from_url(
13 url="https://huggingface.co/KeiKinn/paraclap/resolve/main/best.pth.tar?download=true",
14 map_location="cpu",
15 check_hash=True,
16 )
17
18 text_model = 'bert-base-uncased'
19 audio_model = 'audeering/wav2vec2-large-robust-12-ft-emotion-msp-dim'
20
21 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
22
23 candidates = ['happy', 'sad', 'surprise', 'angry'] # free to adapt it to your need
24 wavpath = '[Waveform path]' # single channel wavform
25
26 waveform, sample_rate = librosa.load(wavpath, sr=16000)
27 x = torch.Tensor(waveform)
28
29 tokenizer = AutoTokenizer.from_pretrained(text_model)
30
31 candidate_tokens = tokenizer.batch_encode_plus(
32 candidates,
33 padding=True,
34 truncation=True,
35 return_tensors='pt'
36 )
37
38 model = CLAP(
39 speech_name=audio_model,
40 text_name=text_model,
41 embedding_dim=768,
42 )
43
44 model.load_state_dict(ckpt)
45 model.to(device)
46 print(f'Checkpoint is loaded')
47 model.eval()
48
49 with torch.no_grad():
50 z = model(
51 x.unsqueeze(0).to(device),
52 candidate_tokens
53 )
54
55 similarity = compute_similarity(z[2], z[0], z[1])
56 prediction = similarity.T.argmax(dim=1)
57
58 result = candidates[prediction]1@inproceedings{Jing24_PTA,
2 title = {ParaCLAP – Towards a general language-audio model for computational paralinguistic tasks},
3 author = {Xin Jing and Andreas Triantafyllopoulos and Björn Schuller},
4 year = {2024},
5 booktitle = {Interspeech 2024},
6 pages = {1155--1159},
7 doi = {10.21437/Interspeech.2024-1315},
8 issn = {2958-1796},
9}