Commit ·
db83cda
1
Parent(s): fa8cbcf
initial commit
Browse files- app.py +70 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoFeatureExtractor,
|
| 6 |
+
AutoModelForAudioClassification,
|
| 7 |
+
)
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
MODEL_ID = "aitf-komdigi/KomdigiITS-86M-DFK-DeepfakeAudioClassification"
|
| 11 |
+
|
| 12 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(
|
| 13 |
+
MODEL_ID
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
model = AutoModelForAudioClassification.from_pretrained(
|
| 17 |
+
MODEL_ID
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def predict(audio):
|
| 24 |
+
if audio is None:
|
| 25 |
+
return "No audio uploaded"
|
| 26 |
+
|
| 27 |
+
sr, waveform = audio
|
| 28 |
+
|
| 29 |
+
waveform = waveform.astype(np.float32)
|
| 30 |
+
|
| 31 |
+
if waveform.ndim > 1:
|
| 32 |
+
waveform = waveform.mean(axis=1)
|
| 33 |
+
|
| 34 |
+
waveform = librosa.resample(
|
| 35 |
+
waveform,
|
| 36 |
+
orig_sr=sr,
|
| 37 |
+
target_sr=16000,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
inputs = feature_extractor(
|
| 41 |
+
waveform,
|
| 42 |
+
sampling_rate=16000,
|
| 43 |
+
return_tensors="pt",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
logits = model(**inputs).logits
|
| 48 |
+
|
| 49 |
+
score = torch.sigmoid(logits).item()
|
| 50 |
+
|
| 51 |
+
prediction = "Fake" if score >= 0.5 else "Real"
|
| 52 |
+
|
| 53 |
+
return {
|
| 54 |
+
"Real": round(1.0 - score, 4),
|
| 55 |
+
"Fake": round(score, 4),
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
demo = gr.Interface(
|
| 60 |
+
fn=predict,
|
| 61 |
+
inputs=gr.Audio(
|
| 62 |
+
sources=["upload", "microphone"],
|
| 63 |
+
type="numpy",
|
| 64 |
+
),
|
| 65 |
+
outputs=gr.Label(),
|
| 66 |
+
title="Audio Deepfake Detection",
|
| 67 |
+
description="Detect whether an audio clip is real or AI-generated.",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
librosa
|
| 5 |
+
soundfile
|
| 6 |
+
accelerate
|