patchy-planet commited on
Commit
3c42762
·
verified ·
1 Parent(s): 1cc1b05

Upload clap_embedder.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. clap_embedder.py +23 -0
clap_embedder.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch, librosa, laion_clap, functools, warnings
2
+ warnings.filterwarnings('ignore')
3
+
4
+ original_load = torch.load
5
+ torch.load = functools.partial(original_load, weights_only=False)
6
+ original_load_state_dict = torch.nn.Module.load_state_dict
7
+
8
+ def tolerant_load_state_dict(self, state_dict, strict=True, assign=False):
9
+ return original_load_state_dict(self, state_dict, strict=False, assign=assign)
10
+ torch.nn.Module.load_state_dict = tolerant_load_state_dict
11
+
12
+ model = laion_clap.CLAP_Module(enable_fusion=False)
13
+ model.load_ckpt()
14
+
15
+ torch.load = original_load
16
+ torch.nn.Module.load_state_dict = original_load_state_dict
17
+
18
+ def get_clap_embedding(path):
19
+ audio_data, _ = librosa.load(path, sr=48000)
20
+ audio_data = audio_data.reshape(1, -1)
21
+ with torch.no_grad():
22
+ audio_embed = model.get_audio_embedding_from_data(x=audio_data)
23
+ return audio_embed.flatten()