ThomasR commited on
Commit
4987836
1 Parent(s): a2e0fc8

Create gradioapp.py

Browse files
Files changed (1) hide show
  1. gradioapp.py +45 -0
gradioapp.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchaudio
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
+ label2id={'fake':0, 'real':1}
12
+ id2label = {v:k for k,v in label2id.items()}
13
+
14
+ def predict(audio_path):
15
+ wavform, sample_rate = sf.read(audio_path)
16
+
17
+ inputs = feature_extractor(
18
+ wavform, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt", max_length=16000, truncation=True, padding=True
19
+ )
20
+
21
+ with torch.no_grad():
22
+ logits = model(**inputs).logits
23
+
24
+ probabilities = torch.sigmoid(logits[0])
25
+ # labels is a one-hot array of shape (num_frames, num_speakers)
26
+ labels = (probabilities > 0.5).long()
27
+
28
+ pred_probs = list(probabilities.tolist())
29
+ # index of the max score
30
+ idx = pred_probs.index(max(pred_probs))
31
+
32
+ LABELS=list(id2label.values())
33
+ #get labels corresponding to max score
34
+ max_label = LABELS[idx]
35
+ results = {LABELS[i]: round(float(pred_probs[i]),4) for i in range(len(LABELS))}
36
+
37
+ return results
38
+
39
+ demo = gr.Interface(fn=predict,
40
+ inputs=gr.Audio(type="filepath"),
41
+ outputs="label",
42
+ cache_examples=False
43
+ )
44
+
45
+ demo.launch(debug=False)