Multi-language AI-generated voice detection model for Tamil, English, Hindi, Malayalam, and Telugu.
Wav2Vec2Model (facebook/wav2vec2-large-xlsr-53)
└── Dropout (0.1)
└── Linear (1024 → 2)
1 import torch
2 import torch . nn as nn
3 from transformers import Wav2Vec2Model
4 from pydub import AudioSegment
5 import librosa
6 import numpy as np
7
8 # Define the model architecture
9 class W2VBertDeepfakeDetector ( nn . Module ) :
10 def __init__ ( self , backbone , num_labels = 2 ) :
11 super ( ) . __init__ ( )
12 self . backbone = backbone
13 hidden_size = backbone . config . hidden_size
14 self . dropout = nn . Dropout ( 0.1 )
15 self . classifier = nn . Linear ( hidden_size , num_labels )
16
17 def forward ( self , input_values , attention_mask = None ) :
18 outputs = self . backbone ( input_values = input_values , attention_mask = attention_mask )
19 hidden_states = outputs . last_hidden_state
20 pooled = hidden_states . mean ( dim = 1 )
21 pooled = self . dropout ( pooled )
22 logits = self . classifier ( pooled )
23 return logits
24
25 # Load backbone
26 backbone = Wav2Vec2Model . from_pretrained ( "facebook/wav2vec2-large-xlsr-53" )
27
28 # Create model and load weights
29 model = W2VBertDeepfakeDetector ( backbone , num_labels = 2 )
30 model . load_state_dict ( torch . load ( "best_model.pt" , map_location = "cpu" ) )
31 model . eval ( )
1 def load_audio ( path , target_sr = 16000 ) :
2 audio_segment = AudioSegment . from_file ( path )
3 samples = np . array ( audio_segment . get_array_of_samples ( ) ) . astype ( np . float32 )
4 if audio_segment . channels > 1 :
5 samples = samples . reshape ( - 1 , audio_segment . channels ) . mean ( axis = 1 )
6 samples /= 32767.0
7 if audio_segment . frame_rate != target_sr :
8 samples = librosa . resample ( samples , orig_sr = audio_segment . frame_rate , target_sr = target_sr )
9 return torch . from_numpy ( samples ) . float ( )
10
11 # Load and classify audio
12 waveform = load_audio ( "your_audio.mp3" )
13 input_values = waveform . unsqueeze ( 0 )
14
15 with torch . no_grad ( ) :
16 logits = model ( input_values )
17 probs = torch . softmax ( logits , dim = - 1 )
18 prediction = torch . argmax ( probs , dim = - 1 ) . item ( )
19 confidence = probs [ 0 , prediction ] . item ( )
20
21 result = "AI_GENERATED" if prediction == 1 else "HUMAN"
22 print ( f"Classification: { result } (confidence: { confidence : .2% } )" )
1 @misc{ai-voice-detection-2024,
2 author = {Your Name},
3 title = {Multilingual AI Voice Detection using Wav2Vec2},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/kimnamjoon0007/lkht-v440}
7 }