nii-yamagishilab commited on
Commit
3e171ce
·
verified ·
1 Parent(s): b4b53dd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +10 -5
README.md CHANGED
@@ -83,10 +83,15 @@ import torchaudio
83
  import fairseq
84
  from huggingface_hub import PyTorchModelHubMixin
85
 
86
- # === Detect all audio files in a specified folder ===
 
87
  folder_path = "/path/to/folder/contains/wavs/"
88
  audio_formats = (".mp3", ".wav", ".flac", ".m4a")
89
 
 
 
 
 
90
  # === Download Fairseq checkpoint if not present ===
91
  # The downloaded checkpoint is used for building front-end architecture,
92
  # its weights will be replaced by the model.safetensors file in this repo.
@@ -106,16 +111,15 @@ class SSLModel(torch.nn.Module):
106
  super().__init__()
107
  # The downloaded .pt file is used here
108
  model, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task([ssl_path])
109
- self.model = model[0]
110
 
111
  def extract_feat(self, input_data):
112
  # If input has shape (B, T, 1), squeeze the last dim
113
  if input_data.ndim == 3:
114
  input_data = input_data[:, :, 0]
115
-
116
  # Extract features
117
  with torch.no_grad():
118
- features = self.model(input_data, mask=False, features_only=True)['x']
119
  return features
120
 
121
  # === Function for reading and pre-processing waveforms ===
@@ -130,7 +134,7 @@ def load_wav_and_preprocess(wav_path, target_sr=16000):
130
  with torch.no_grad():
131
  wav = torch.nn.functional.layer_norm(wav, wav.shape)
132
  # Add batch dimension and return
133
- return wav.unsqueeze(0)
134
 
135
  # === The actual deepfake detection model using SSL frontend + FC backend ===
136
  class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
@@ -159,6 +163,7 @@ class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
159
 
160
  # === Load AntiDeepfake model from Hugging Face===
161
  model = DeepfakeDetector.from_pretrained("nii-yamagishilab/xls-r-2b-anti-deepfake-nda")
 
162
  model.eval()
163
 
164
  # === Inference on a folder of audio files ===
 
83
  import fairseq
84
  from huggingface_hub import PyTorchModelHubMixin
85
 
86
+ # This is the only part of the script you need to modify.
87
+ # Set this to the path where your audio files are stored.
88
  folder_path = "/path/to/folder/contains/wavs/"
89
  audio_formats = (".mp3", ".wav", ".flac", ".m4a")
90
 
91
+ # === Set device (use GPU if available) ===
92
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
93
+ print(f"Using device: {device}")
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
  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].to(device)
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
  # Extract features
121
  with torch.no_grad():
122
+ features = self.model(input_data.to(device), mask=False, features_only=True)['x']
123
  return features
124
 
125
  # === Function for reading and pre-processing waveforms ===
 
134
  with torch.no_grad():
135
  wav = torch.nn.functional.layer_norm(wav, wav.shape)
136
  # Add batch dimension and return
137
+ return wav.unsqueeze(0).to(device)
138
 
139
  # === The actual deepfake detection model using SSL frontend + FC backend ===
140
  class DeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):
 
163
 
164
  # === Load AntiDeepfake model from Hugging Face===
165
  model = DeepfakeDetector.from_pretrained("nii-yamagishilab/xls-r-2b-anti-deepfake-nda")
166
+ model.to(device)
167
  model.eval()
168
 
169
  # === Inference on a folder of audio files ===