Simma7 commited on
Commit
33442e6
·
verified ·
1 Parent(s): 3568c23

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +13 -49
model.py CHANGED
@@ -1,56 +1,20 @@
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}")
 
1
+ from transformers import AutoModelForAudioClassification, AutoProcessor
 
 
 
 
2
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ model_name = "Simma7/audio_model"
5
 
6
+ print("[INFO] Loading model...")
 
 
7
 
8
+ model = AutoModelForAudioClassification.from_pretrained(
9
+ model_name,
10
+ trust_remote_code=True
11
+ )
12
 
13
+ processor = AutoProcessor.from_pretrained(
14
+ model_name,
15
+ trust_remote_code=True
16
+ )
17
 
18
+ model.eval()
 
 
 
19
 
20
+ print("[SUCCESS] Model loaded successfully ✅")