Spaces:
Sleeping
Sleeping
GVAmaresh
commited on
Commit
·
3743694
1
Parent(s):
7ef4b83
dev check working
Browse files
app.py
CHANGED
|
@@ -202,4 +202,66 @@ class UnifiedDeepfakeDetector:
|
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
print(f"Analysis error: {e}")
|
| 205 |
-
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
print(f"Analysis error: {e}")
|
| 205 |
+
return None, None, None
|
| 206 |
+
|
| 207 |
+
#--------------------------------------------------------------------------------------------------------------------
|
| 208 |
+
|
| 209 |
+
import torchaudio
|
| 210 |
+
import torch
|
| 211 |
+
import numpy as np
|
| 212 |
+
from scipy.stats import skew, kurtosis, median_abs_deviation
|
| 213 |
+
import os
|
| 214 |
+
import torch.nn.functional as F
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
import os
|
| 218 |
+
os.environ["TORCH_HOME"] = "/tmp/torch_cache"
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
from torchaudio.pipelines import WAV2VEC2_BASE
|
| 223 |
+
bundle = WAV2VEC2_BASE
|
| 224 |
+
|
| 225 |
+
model = bundle.get_model()
|
| 226 |
+
print("Model downloaded successfully!")
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def extract_features(file_path):
|
| 230 |
+
if os.path.exists(file_path):
|
| 231 |
+
print(f"File successfully written: {file_path}")
|
| 232 |
+
else:
|
| 233 |
+
print("File writing failed.")
|
| 234 |
+
waveform, sample_rate = torchaudio.load(file_path)
|
| 235 |
+
if sample_rate != bundle.sample_rate:
|
| 236 |
+
waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=bundle.sample_rate)(waveform)
|
| 237 |
+
|
| 238 |
+
with torch.inference_mode():
|
| 239 |
+
features, _ = model.extract_features(waveform)
|
| 240 |
+
|
| 241 |
+
pooled_features = []
|
| 242 |
+
for f in features:
|
| 243 |
+
if f.dim() == 3:
|
| 244 |
+
f = f.permute(0, 2, 1)
|
| 245 |
+
pooled_f = F.adaptive_avg_pool1d(f[0].unsqueeze(0), 1).squeeze(0)
|
| 246 |
+
pooled_features.append(pooled_f)
|
| 247 |
+
|
| 248 |
+
final_features = torch.cat(pooled_features, dim=0).numpy()
|
| 249 |
+
final_features = (final_features - np.mean(final_features)) / (np.std(final_features) + 1e-10)
|
| 250 |
+
|
| 251 |
+
return final_features
|
| 252 |
+
|
| 253 |
+
def additional_features(features):
|
| 254 |
+
mad = median_abs_deviation(features)
|
| 255 |
+
features_clipped = np.clip(features, 1e-10, None)
|
| 256 |
+
entropy = -np.sum(features_clipped * np.log(features_clipped))
|
| 257 |
+
return mad, entropy
|
| 258 |
+
|
| 259 |
+
def classify_audio(features):
|
| 260 |
+
|
| 261 |
+
_, entropy = additional_features(features)
|
| 262 |
+
print(entropy)
|
| 263 |
+
|
| 264 |
+
if entropy > 150:
|
| 265 |
+
return True, entropy
|
| 266 |
+
else:
|
| 267 |
+
return False, entropy
|