| """ |
| π₯ TEST YOUR AUDIO MODEL FROM HUGGING FACE |
| Simma7/audio_model |
| """ |
|
|
| import torch |
| import librosa |
| from transformers import AutoProcessor, AutoModel |
| from huggingface_hub import snapshot_download |
|
|
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| |
| print("β¬οΈ Downloading model from Hugging Face...") |
|
|
| model_dir = snapshot_download("Simma7/audio_model") |
|
|
| print("β
Download complete") |
|
|
| |
| print("π§ Loading model...") |
|
|
| processor = AutoProcessor.from_pretrained(model_dir) |
| model = AutoModel.from_pretrained(model_dir).to(DEVICE) |
|
|
| print("β
Model loaded") |
|
|
| |
| def predict(audio_path): |
| audio, sr = librosa.load(audio_path, sr=16000) |
|
|
| inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True) |
|
|
| with torch.no_grad(): |
| outputs = model(**inputs.to(DEVICE)) |
|
|
| |
| embedding = outputs.last_hidden_state.mean(dim=1) |
| prob = torch.sigmoid(embedding.mean()).item() |
|
|
| return prob |
|
|
| |
| if __name__ == "__main__": |
| audio_path = "test.wav" |
|
|
| print("\nπ Analyzing audio...") |
|
|
| prob = predict(audio_path) |
|
|
| if prob > 0.5: |
| print("\nπ΄ FAKE AUDIO") |
| else: |
| print("\nπ’ REAL AUDIO") |
|
|
| print(f"π Confidence: {prob:.4f}") |