Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save darshanjain-entrepreneur/30a5f00f36cf6fca79def2acacb482c7 to your computer and use it in GitHub Desktop.

Select an option

Save darshanjain-entrepreneur/30a5f00f36cf6fca79def2acacb482c7 to your computer and use it in GitHub Desktop.
IT is the code for AI VS HUMAN VOICE DETECTION
# πŸ“¦ Step 1: Install dependencies
!pip install librosa scikit-learn pydub soundfile
!apt-get install -y ffmpeg
# πŸ“ Step 2: Mount Google Drive and define paths
from google.colab import drive
import os
from pydub import AudioSegment
drive.mount('/content/drive')
human_folder = '/content/drive/MyDrive/human_voice'
ai_folder = '/content/drive/MyDrive/ai_voice'
wav_human_folder = '/content/wav_human'
wav_ai_folder = '/content/wav_ai'
os.makedirs(wav_human_folder, exist_ok=True)
os.makedirs(wav_ai_folder, exist_ok=True)
# πŸ”„ Step 3: Convert MP3 to WAV and add noise to 20% files
import librosa
import numpy as np
import soundfile as sf
import random
def add_noise(y):
noise = np.random.normal(0, 0.005, y.shape)
return y + noise
def convert_and_process(folder, output_folder):
files = [f for f in os.listdir(folder) if f.endswith('.mp3')]
for idx, file in enumerate(files):
mp3_path = os.path.join(folder, file)
wav_path = os.path.join(output_folder, file.replace('.mp3', '.wav'))
audio = AudioSegment.from_mp3(mp3_path)
audio.export(wav_path, format='wav')
# Add noise to 20% of files
if random.random() < 0.2:
y, sr = librosa.load(wav_path, sr=None)
y_noisy = add_noise(y)
sf.write(wav_path, y_noisy, sr)
convert_and_process(human_folder, wav_human_folder)
convert_and_process(ai_folder, wav_ai_folder)
# 🎯 Step 4: Feature Extraction
import numpy as np
import librosa
def extract_features(file):
y, sr = librosa.load(file, sr=None)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
delta = librosa.feature.delta(mfcc)
features = np.mean(np.vstack((mfcc, delta)), axis=1)
return features
X, y = [], []
for file in os.listdir(wav_human_folder):
if file.endswith('.wav'):
features = extract_features(os.path.join(wav_human_folder, file))
X.append(features)
y.append("Human")
for file in os.listdir(wav_ai_folder):
if file.endswith('.wav'):
features = extract_features(os.path.join(wav_ai_folder, file))
X.append(features)
y.append("AI")
# πŸ“Š Step 5: Train-test split & scale
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# πŸ€– Step 6: Train models
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
models = {
"SVM": SVC(kernel='rbf', C=10, gamma=0.01), # tuned to perform best
"Random Forest": RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42),
"Logistic Regression": LogisticRegression(max_iter=1000, C=0.1, solver='liblinear') # add more regularization
}
for name, model in models.items():
model.fit(X_train_scaled, y_train)
y_pred = model.predict(X_test_scaled)
print(f"\nπŸ“Œ {name} Accuracy:", model.score(X_test_scaled, y_test))
print(classification_report(y_test, y_pred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment