reagvis commited on
Commit
a9277e0
·
verified ·
1 Parent(s): 082c0e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -1,26 +1,32 @@
1
  import gradio as gr
2
  import torch
3
  import torchaudio
4
- from transformers import AutoProcessor, AutoModelForAudioClassification
5
 
6
- # Load the Hugging Face processor and model for audio deepfake detection.
7
- processor = AutoProcessor.from_pretrained("MelodyMachine/Deepfake-audio-detection-V2")
8
- model = AutoModelForAudioClassification.from_pretrained("MelodyMachine/Deepfake-audio-detection-V2")
 
 
 
 
9
 
10
  def detect_deepfake_audio(audio_path: str) -> str:
11
- # Load audio (supports WAV, MP3, FLAC, etc.)
12
  waveform, sample_rate = torchaudio.load(audio_path)
13
 
14
- # Convert to mono if necessary
15
  if waveform.shape[0] > 1:
16
  waveform = torch.mean(waveform, dim=0, keepdim=True)
17
 
18
  # Preprocess for model
19
- inputs = processor(waveform, sampling_rate=sample_rate, return_tensors="pt")
 
 
20
  with torch.no_grad():
21
  outputs = model(**inputs)
22
 
23
- # Get prediction
24
  probs = torch.softmax(outputs.logits, dim=-1)[0]
25
  idx = torch.argmax(probs).item()
26
  label = model.config.id2label[idx]
@@ -28,13 +34,15 @@ def detect_deepfake_audio(audio_path: str) -> str:
28
 
29
  return f"The audio is classified as **{label}** with confidence **{confidence:.2f}**"
30
 
31
- # Build Gradio interface
32
  with gr.Blocks() as demo:
33
- gr.Markdown("# Audio Deepfake Detection App")
34
- gr.Markdown("### Upload or record an audio clip to detect deepfake content.")
35
- audio_in = gr.Audio(source="upload", type="filepath", label="Upload Audio")
36
  txt_out = gr.Textbox(label="Result")
37
- gr.Button("Detect").click(fn=detect_deepfake_audio, inputs=audio_in, outputs=txt_out)
 
 
38
 
39
  if __name__ == "__main__":
40
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
  import torchaudio
4
+ from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
5
 
6
+ # Load the Hugging Face feature extractor and model
7
+ feature_extractor = AutoFeatureExtractor.from_pretrained(
8
+ "MelodyMachine/Deepfake-audio-detection-V2"
9
+ )
10
+ model = AutoModelForAudioClassification.from_pretrained(
11
+ "MelodyMachine/Deepfake-audio-detection-V2"
12
+ )
13
 
14
  def detect_deepfake_audio(audio_path: str) -> str:
15
+ # Load any audio format via torchaudio
16
  waveform, sample_rate = torchaudio.load(audio_path)
17
 
18
+ # Convert to mono if stereo
19
  if waveform.shape[0] > 1:
20
  waveform = torch.mean(waveform, dim=0, keepdim=True)
21
 
22
  # Preprocess for model
23
+ inputs = feature_extractor(
24
+ waveform, sampling_rate=sample_rate, return_tensors="pt"
25
+ )
26
  with torch.no_grad():
27
  outputs = model(**inputs)
28
 
29
+ # Softmax to get class probabilities
30
  probs = torch.softmax(outputs.logits, dim=-1)[0]
31
  idx = torch.argmax(probs).item()
32
  label = model.config.id2label[idx]
 
34
 
35
  return f"The audio is classified as **{label}** with confidence **{confidence:.2f}**"
36
 
37
+ # Gradio UI
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("# Audio Deepfake Detection")
40
+ gr.Markdown("Upload an audio clip to check for deepfake content.")
41
+ audio_in = gr.Audio(source="upload", type="filepath", label="Audio File")
42
  txt_out = gr.Textbox(label="Result")
43
+ gr.Button("Detect").click(
44
+ fn=detect_deepfake_audio, inputs=audio_in, outputs=txt_out
45
+ )
46
 
47
  if __name__ == "__main__":
48
  demo.launch()