Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save darshanjain-entrepreneur/5dec26cab3ebbe82d9573e40ba06f43f to your computer and use it in GitHub Desktop.
Code for ai vs human voice final
!pip install librosa scikit-learn pydub soundfile lightgbm xgboost imbalanced-learn
!apt-get install -y ffmpeg
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)
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 file in 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')
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)
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'):
X.append(extract_features(os.path.join(wav_human_folder, file)))
y.append("Human")
for file in os.listdir(wav_ai_folder):
if file.endswith('.wav'):
X.append(extract_features(os.path.join(wav_ai_folder, file)))
y.append("AI")
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
le = LabelEncoder()
y_encoded = le.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.3, stratify=y_encoded, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
smote = SMOTE(random_state=42)
X_train_resampled, y_train_resampled = smote.fit_resample(X_train_scaled, y_train)
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import cross_val_score
import lightgbm as lgb
import xgboost as xgb
import matplotlib.pyplot as plt
import pandas as pd
models = {
"SVM": SVC(kernel='rbf', C=10, gamma=0.01, probability=True),
"Random Forest": RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42),
"KNN": KNeighborsClassifier(n_neighbors=5),
"LightGBM": lgb.LGBMClassifier(n_estimators=100, random_state=42),
"XGBoost": xgb.XGBClassifier(eval_metric='logloss', random_state=42)
}
results = []
for name, model in models.items():
print(f"\n🔍 {name} Cross-Validation:")
model.fit(X_train_resampled, y_train_resampled)
y_pred = model.predict(X_test_scaled)
try:
cv_scores = cross_val_score(model, X_train_resampled, y_train_resampled, cv=5)
cv_accuracy = cv_scores.mean()
print(f" CV Accuracy: {cv_accuracy:.4f}")
except:
cv_accuracy = None
print(" CV Accuracy: N/A")
test_accuracy = accuracy_score(y_test, y_pred)
print(f" Test Accuracy: {test_accuracy:.4f}")
report = classification_report(y_test, y_pred, target_names=le.classes_, output_dict=True)
precision = report['AI']['precision']
recall = report['AI']['recall']
f1 = report['AI']['f1-score']
results.append({
'Model': name,
'CV Accuracy': cv_accuracy,
'Test Accuracy': test_accuracy,
'Precision': precision,
'Recall': recall,
'F1-Score': f1
})
cm = confusion_matrix(y_test, y_pred)
ConfusionMatrixDisplay(cm, display_labels=le.classes_).plot(cmap="Blues")
plt.title(f"{name} - Confusion Matrix")
plt.show()
df_results = pd.DataFrame(results)
print("\n📋 Model Comparison Table:")
print(df_results.sort_values(by="Test Accuracy", ascending=False))
plt.figure(figsize=(8, 5))
plt.bar(df_results['Model'], df_results['Test Accuracy'], color='skyblue')
plt.title("Model Test Accuracy Comparison")
plt.ylabel("Accuracy")
plt.ylim(0.9, 1.0)
plt.xticks(rotation=45)
plt.grid(axis='y')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment