Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,110 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import
|
| 4 |
-
from torchaudio.transforms import Resample
|
| 5 |
-
from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
"MelodyMachine/Deepfake-audio-detection-V2"
|
| 10 |
-
)
|
| 11 |
-
model = AutoModelForAudioClassification.from_pretrained(
|
| 12 |
-
"MelodyMachine/Deepfake-audio-detection-V2"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
resampler = Resample(orig_sr, TARGET_SR)
|
| 28 |
-
waveform = resampler(waveform)
|
| 29 |
-
|
| 30 |
-
# Prepare inputs
|
| 31 |
-
inputs = feature_extractor(
|
| 32 |
-
waveform, sampling_rate=TARGET_SR, return_tensors="pt"
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import pipeline
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize the pipeline
|
| 6 |
+
pipe = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def detect_deepfake(audio_file):
|
| 9 |
+
"""
|
| 10 |
+
Detect if an audio file is deepfake or real
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
if audio_file is None:
|
| 14 |
+
return "Please upload an audio file"
|
| 15 |
+
|
| 16 |
+
# Run the classification
|
| 17 |
+
result = pipe(audio_file)
|
| 18 |
+
|
| 19 |
+
# Format the results
|
| 20 |
+
predictions = {}
|
| 21 |
+
confidence_text = ""
|
| 22 |
+
|
| 23 |
+
for item in result:
|
| 24 |
+
label = item['label']
|
| 25 |
+
score = item['score']
|
| 26 |
+
predictions[label] = score
|
| 27 |
+
confidence_text += f"{label}: {score:.4f} ({score*100:.2f}%)\n"
|
| 28 |
+
|
| 29 |
+
# Determine the prediction
|
| 30 |
+
top_prediction = max(predictions, key=predictions.get)
|
| 31 |
+
confidence = predictions[top_prediction]
|
| 32 |
+
|
| 33 |
+
# Create a more readable result
|
| 34 |
+
if 'fake' in top_prediction.lower() or 'deepfake' in top_prediction.lower():
|
| 35 |
+
main_result = f"⚠️ **DEEPFAKE DETECTED** (Confidence: {confidence*100:.1f}%)"
|
| 36 |
+
color = "red"
|
| 37 |
+
else:
|
| 38 |
+
main_result = f"✅ **REAL AUDIO** (Confidence: {confidence*100:.1f}%)"
|
| 39 |
+
color = "green"
|
| 40 |
+
|
| 41 |
+
detailed_results = f"**Detailed Results:**\n{confidence_text}"
|
| 42 |
+
|
| 43 |
+
return f"{main_result}\n\n{detailed_results}"
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error processing audio: {str(e)}"
|
| 47 |
|
| 48 |
+
# Create the Gradio interface
|
| 49 |
+
with gr.Blocks(title="Audio Deepfake Detection", theme=gr.themes.Soft()) as app:
|
| 50 |
+
gr.Markdown(
|
| 51 |
+
"""
|
| 52 |
+
# 🎵 Audio Deepfake Detection
|
| 53 |
+
|
| 54 |
+
Upload an audio file to detect if it's artificially generated (deepfake) or real.
|
| 55 |
+
|
| 56 |
+
**Supported formats:** WAV, MP3, FLAC, M4A
|
| 57 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
audio_input = gr.Audio(
|
| 63 |
+
label="Upload Audio File",
|
| 64 |
+
type="filepath",
|
| 65 |
+
sources=["upload"]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
detect_btn = gr.Button(
|
| 69 |
+
"🔍 Analyze Audio",
|
| 70 |
+
variant="primary",
|
| 71 |
+
size="lg"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Column():
|
| 75 |
+
output_text = gr.Textbox(
|
| 76 |
+
label="Detection Results",
|
| 77 |
+
lines=8,
|
| 78 |
+
max_lines=10,
|
| 79 |
+
interactive=False
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Set up the event handler
|
| 83 |
+
detect_btn.click(
|
| 84 |
+
fn=detect_deepfake,
|
| 85 |
+
inputs=audio_input,
|
| 86 |
+
outputs=output_text
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Also trigger on audio upload
|
| 90 |
+
audio_input.change(
|
| 91 |
+
fn=detect_deepfake,
|
| 92 |
+
inputs=audio_input,
|
| 93 |
+
outputs=output_text
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
gr.Markdown(
|
| 97 |
+
"""
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
**Note:** This model analyzes audio characteristics to detect artificial generation.
|
| 101 |
+
Results are probabilities, not definitive proof.
|
| 102 |
+
"""
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
app.launch(
|
| 107 |
+
server_name="0.0.0.0",
|
| 108 |
+
server_port=7860,
|
| 109 |
+
share=False
|
| 110 |
+
)
|