Simma7 commited on
Commit
c8e15e2
·
verified ·
1 Parent(s): 33cdab3

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +56 -0
model.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 🔥 TEST YOUR AUDIO MODEL FROM HUGGING FACE
3
+ Simma7/audio_model
4
+ """
5
+
6
+ import torch
7
+ import librosa
8
+ from transformers import AutoProcessor, AutoModel
9
+ from huggingface_hub import snapshot_download
10
+
11
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
12
+
13
+ # ================= DOWNLOAD MODEL =================
14
+ print("⬇️ Downloading model from Hugging Face...")
15
+
16
+ model_dir = snapshot_download("Simma7/audio_model")
17
+
18
+ print("✅ Download complete")
19
+
20
+ # ================= LOAD MODEL =================
21
+ print("🧠 Loading model...")
22
+
23
+ processor = AutoProcessor.from_pretrained(model_dir)
24
+ model = AutoModel.from_pretrained(model_dir).to(DEVICE)
25
+
26
+ print("✅ Model loaded")
27
+
28
+ # ================= PREDICT =================
29
+ def predict(audio_path):
30
+ audio, sr = librosa.load(audio_path, sr=16000)
31
+
32
+ inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
33
+
34
+ with torch.no_grad():
35
+ outputs = model(**inputs.to(DEVICE))
36
+
37
+ # embedding-based score
38
+ embedding = outputs.last_hidden_state.mean(dim=1)
39
+ prob = torch.sigmoid(embedding.mean()).item()
40
+
41
+ return prob
42
+
43
+ # ================= MAIN =================
44
+ if __name__ == "__main__":
45
+ audio_path = "test.wav" # 🔥 put your audio file
46
+
47
+ print("\n🔍 Analyzing audio...")
48
+
49
+ prob = predict(audio_path)
50
+
51
+ if prob > 0.5:
52
+ print("\n🔴 FAKE AUDIO")
53
+ else:
54
+ print("\n🟢 REAL AUDIO")
55
+
56
+ print(f"📊 Confidence: {prob:.4f}")