reagvis commited on
Commit
824db7b
·
verified ·
1 Parent(s): a9277e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -3,7 +3,7 @@ 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
  )
@@ -12,21 +12,21 @@ model = AutoModelForAudioClassification.from_pretrained(
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,11 +34,11 @@ def detect_deepfake_audio(audio_path: str) -> str:
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
 
3
  import torchaudio
4
  from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
5
 
6
+ # Load the HF feature extractor and model
7
  feature_extractor = AutoFeatureExtractor.from_pretrained(
8
  "MelodyMachine/Deepfake-audio-detection-V2"
9
  )
 
12
  )
13
 
14
  def detect_deepfake_audio(audio_path: str) -> str:
15
+ # Load audio file
16
  waveform, sample_rate = torchaudio.load(audio_path)
17
 
18
+ # Mix to mono if necessary
19
  if waveform.shape[0] > 1:
20
  waveform = torch.mean(waveform, dim=0, keepdim=True)
21
 
22
+ # Prepare inputs
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
+ # Compute 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
+ # Build the Gradio Blocks interface
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(type="filepath", label="Select 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