ThomasR's picture
Create gradioapp.py
4987836
import gradio as gr
import torch
import torchaudio
from transformers import AutoProcessor, AutoModelForAudioClassification
from transformers import AutoFeatureExtractor
# Load model directly
feature_extractor = AutoFeatureExtractor.from_pretrained("ThomasR/facebook_wav2vec2-large_October_03_2023_05h34PM")
model = AutoModelForAudioClassification.from_pretrained("ThomasR/facebook_wav2vec2-large_October_03_2023_05h34PM")
label2id={'fake':0, 'real':1}
id2label = {v:k for k,v in label2id.items()}
def predict(audio_path):
wavform, sample_rate = sf.read(audio_path)
inputs = feature_extractor(
wavform, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt", max_length=16000, truncation=True, padding=True
)
with torch.no_grad():
logits = model(**inputs).logits
probabilities = torch.sigmoid(logits[0])
# labels is a one-hot array of shape (num_frames, num_speakers)
labels = (probabilities > 0.5).long()
pred_probs = list(probabilities.tolist())
# index of the max score
idx = pred_probs.index(max(pred_probs))
LABELS=list(id2label.values())
#get labels corresponding to max score
max_label = LABELS[idx]
results = {LABELS[i]: round(float(pred_probs[i]),4) for i in range(len(LABELS))}
return results
demo = gr.Interface(fn=predict,
inputs=gr.Audio(type="filepath"),
outputs="label",
cache_examples=False
)
demo.launch(debug=False)