ThomasR commited on
Commit
a2e0fc8
1 Parent(s): 784f78b

Inference script

Browse files
Files changed (1) hide show
  1. Inference.py +37 -0
Inference.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torchaudio
2
+ import soundfile as sf
3
+ import torch
4
+ from transformers import AutoProcessor, AutoModelForAudioClassification
5
+ from transformers import AutoFeatureExtractor
6
+
7
+ # Load model directly
8
+ feature_extractor = AutoFeatureExtractor.from_pretrained("ThomasR/facebook_wav2vec2-large_October_03_2023_05h34PM")
9
+ model = AutoModelForAudioClassification.from_pretrained("ThomasR/facebook_wav2vec2-large_October_03_2023_05h34PM")
10
+
11
+ # Label dict
12
+ label2id={'fake':0, 'real':1}
13
+ id2label = {v:k for k,v in label2id.items()}
14
+
15
+ #Inference function
16
+ def predict(audio_path):
17
+ wavform, sample_rate = sf.read(audio_path)
18
+
19
+ inputs = feature_extractor(
20
+ wavform, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt", max_length=16000, truncation=True, padding=True
21
+ )
22
+
23
+ with torch.no_grad():
24
+ logits = model(**inputs).logits
25
+
26
+ probabilities = torch.sigmoid(logits[0])
27
+ # labels is a one-hot array of shape (num_frames, num_speakers)
28
+ labels = (probabilities > 0.5).long()
29
+ pred_probs = list(probabilities.tolist())
30
+ LABELS=list(id2label.values())
31
+ pred_labels_and_probs = {LABELS[i]: round(float(pred_probs[i]),4) for i in range(len(LABELS))}
32
+
33
+ return pred_labels_and_probs
34
+
35
+
36
+ audio_path = 'fake_Elevenlabs_common_voice_en_36808626_Harry.wav'
37
+ predict(audio_path)