reagvis commited on
Commit
9f0b456
·
verified ·
1 Parent(s): b9ec101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -47
app.py CHANGED
@@ -1,56 +1,110 @@
1
  import gradio as gr
2
  import torch
3
- import torchaudio
4
- from torchaudio.transforms import Resample
5
- from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
6
 
7
- # Load the HF feature extractor and model
8
- feature_extractor = AutoFeatureExtractor.from_pretrained(
9
- "MelodyMachine/Deepfake-audio-detection-V2"
10
- )
11
- model = AutoModelForAudioClassification.from_pretrained(
12
- "MelodyMachine/Deepfake-audio-detection-V2"
13
- )
14
 
15
- TARGET_SR = feature_extractor.sampling_rate # should be 16000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def detect_deepfake_audio(audio_path: str) -> str:
18
- # Load audio file
19
- waveform, orig_sr = torchaudio.load(audio_path)
20
-
21
- # Mix to mono if necessary
22
- if waveform.shape[0] > 1:
23
- waveform = torch.mean(waveform, dim=0, keepdim=True)
24
-
25
- # Resample if not already 16 kHz
26
- if orig_sr != TARGET_SR:
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
- with torch.no_grad():
35
- outputs = model(**inputs)
36
-
37
- # Compute probabilities
38
- probs = torch.softmax(outputs.logits, dim=-1)[0]
39
- idx = torch.argmax(probs).item()
40
- label = model.config.id2label[idx]
41
- confidence = probs[idx].item()
42
-
43
- return f"The audio is classified as **{label}** with confidence **{confidence:.2f}**"
44
-
45
- # Build the Gradio Blocks interface
46
- with gr.Blocks() as demo:
47
- gr.Markdown("# Audio Deepfake Detection")
48
- gr.Markdown("Upload an audio clip to check for deepfake content.")
49
- audio_in = gr.Audio(type="filepath", label="Select Audio File")
50
- txt_out = gr.Textbox(label="Result")
51
- gr.Button("Detect").click(
52
- fn=detect_deepfake_audio, inputs=audio_in, outputs=txt_out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
  if __name__ == "__main__":
56
- demo.launch()
 
 
 
 
 
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
+ )