nii-yamagishilab commited on
Commit
74e0d15
·
verified ·
1 Parent(s): 502c8bb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +10 -5
README.md CHANGED
@@ -88,10 +88,15 @@ import torchaudio
88
  import fairseq
89
  from huggingface_hub import PyTorchModelHubMixin
90
 
91
- # === Detect all audio files in a specified folder ===
 
92
  folder_path = "/path/to/folder/contains/wavs/"
93
  audio_formats = (".mp3", ".wav", ".flac", ".m4a")
94
 
 
 
 
 
95
  # === Download Fairseq checkpoint if not present ===
96
  # The downloaded checkpoint is used for building front-end architecture,
97
  # its weights will be replaced by the model.safetensors file in this repo.
@@ -111,16 +116,15 @@ class SSLModel(torch.nn.Module):
111
  super().__init__()
112
  # The downloaded .pt file is used here
113
  model, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task([ssl_path])
114
- self.model = model[0]
115
 
116
  def extract_feat(self, input_data):
117
  # If input has shape (B, T, 1), squeeze the last dim
118
  if input_data.ndim == 3:
119
  input_data = input_data[:, :, 0]
120
-
121
  # Extract features
122
  with torch.no_grad():
123
- features = self.model(input_data, mask=False, features_only=True)['x']
124
  return features
125
 
126
  # === Function for reading and pre-processing waveforms ===
@@ -135,7 +139,7 @@ def load_wav_and_preprocess(wav_path, target_sr=16000):
135
  with torch.no_grad():
136
  wav = torch.nn.functional.layer_norm(wav, wav.shape)
137
  # Add batch dimension and return
138
- return wav.unsqueeze(0)
139
 
140
  # === The actual deepfake detection model using SSL frontend + FC backend ===
141
  class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
@@ -164,6 +168,7 @@ class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
164
 
165
  # === Load AntiDeepfake model from Hugging Face===
166
  model = DeepfakeDetector.from_pretrained("nii-yamagishilab/wav2vec-large-anti-deepfake")
 
167
  model.eval()
168
 
169
  # === Inference on a folder of audio files ===
 
88
  import fairseq
89
  from huggingface_hub import PyTorchModelHubMixin
90
 
91
+ # This is the only part of the script you need to modify.
92
+ # Set this to the path where your audio files are stored.
93
  folder_path = "/path/to/folder/contains/wavs/"
94
  audio_formats = (".mp3", ".wav", ".flac", ".m4a")
95
 
96
+ # === Set device (use GPU if available) ===
97
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
98
+ print(f"Using device: {device}")
99
+
100
  # === Download Fairseq checkpoint if not present ===
101
  # The downloaded checkpoint is used for building front-end architecture,
102
  # its weights will be replaced by the model.safetensors file in this repo.
 
116
  super().__init__()
117
  # The downloaded .pt file is used here
118
  model, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task([ssl_path])
119
+ self.model = model[0].to(device)
120
 
121
  def extract_feat(self, input_data):
122
  # If input has shape (B, T, 1), squeeze the last dim
123
  if input_data.ndim == 3:
124
  input_data = input_data[:, :, 0]
 
125
  # Extract features
126
  with torch.no_grad():
127
+ features = self.model(input_data.to(device), mask=False, features_only=True)['x']
128
  return features
129
 
130
  # === Function for reading and pre-processing waveforms ===
 
139
  with torch.no_grad():
140
  wav = torch.nn.functional.layer_norm(wav, wav.shape)
141
  # Add batch dimension and return
142
+ return wav.unsqueeze(0).to(device)
143
 
144
  # === The actual deepfake detection model using SSL frontend + FC backend ===
145
  class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
 
168
 
169
  # === Load AntiDeepfake model from Hugging Face===
170
  model = DeepfakeDetector.from_pretrained("nii-yamagishilab/wav2vec-large-anti-deepfake")
171
+ model.to(device)
172
  model.eval()
173
 
174
  # === Inference on a folder of audio files ===