Spaces:
Configuration error
Configuration error
File size: 7,380 Bytes
42a0d43 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import joblib
from train_model import extract_features
import numpy as np
import os
import logging
import soundfile as sf
import librosa
import traceback
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def verify_audio_file(file_path):
"""Verify if the audio file is valid and can be processed."""
try:
print(f"[DEBUG] Starting audio file verification for: {file_path}")
logger.debug(f"Starting audio file verification for: {file_path}")
# Check basic file properties
if not os.path.exists(file_path):
print(f"[ERROR] File not found: {file_path}")
logger.error(f"File not found: {file_path}")
return False
file_size = os.path.getsize(file_path)
print(f"[DEBUG] File size: {file_size} bytes")
logger.debug(f"File size: {file_size} bytes")
if file_size == 0:
print("[ERROR] File is empty")
logger.error("File is empty")
return False
# Try reading with soundfile
try:
print("[DEBUG] Attempting to read with soundfile...")
logger.debug("Attempting to read with soundfile...")
with sf.SoundFile(file_path) as audio_file:
print(f"[DEBUG] SoundFile success - Sample rate: {audio_file.samplerate}Hz, "
f"Channels: {audio_file.channels}, Frames: {audio_file.frames}")
logger.debug(f"SoundFile success - Sample rate: {audio_file.samplerate}Hz, "
f"Channels: {audio_file.channels}, Frames: {audio_file.frames}")
if audio_file.frames == 0:
print("[ERROR] Audio file has no frames")
logger.error("Audio file has no frames")
return False
except Exception as e:
print(f"[WARNING] SoundFile read failed: {str(e)}")
logger.warning(f"SoundFile read failed with error: {str(e)}")
logger.debug(f"Full traceback: {traceback.format_exc()}")
# Don't return False here, try librosa as backup
# Try reading with librosa
try:
print("[DEBUG] Attempting to read with librosa...")
logger.debug("Attempting to read with librosa...")
y, sr = librosa.load(file_path, duration=1, sr=None)
print(f"[DEBUG] Librosa success - Sample rate: {sr}Hz, Length: {len(y)} samples")
logger.debug(f"Librosa success - Sample rate: {sr}Hz, Length: {len(y)} samples")
if len(y) == 0:
print("[ERROR] Librosa loaded empty audio data")
logger.error("Librosa loaded empty audio data")
return False
return True
except Exception as e:
print(f"[ERROR] Librosa read failed: {str(e)}")
logger.error(f"Librosa read failed with error: {str(e)}")
logger.debug(f"Full traceback: {traceback.format_exc()}")
return False
except Exception as e:
print(f"[ERROR] Error in verify_audio_file: {str(e)}")
logger.error(f"Error in verify_audio_file: {str(e)}")
logger.debug(f"Full traceback: {traceback.format_exc()}")
return False
def predict_healing_music(audio_path):
"""
Predict whether a music file is healing or not.
Returns: probability of being healing music (0-1)
"""
try:
# Step 1: Verify audio file
print(f"[DEBUG] Starting prediction process for: {audio_path}")
logger.info(f"Starting prediction process for: {audio_path}")
if not verify_audio_file(audio_path):
print("[ERROR] Audio file verification failed")
logger.error("Audio file verification failed")
return None
# Step 2: Load model and scaler
try:
print("[DEBUG] Loading model and scaler...")
logger.info("Loading model and scaler...")
if not os.path.exists('model.joblib') or not os.path.exists('scaler.joblib'):
print("[ERROR] Model or scaler file not found")
logger.error("Model or scaler file not found")
return None
model = joblib.load('model.joblib')
scaler = joblib.load('scaler.joblib')
print("[DEBUG] Model and scaler loaded successfully")
logger.info("Model and scaler loaded successfully")
except Exception as e:
print(f"[ERROR] Error loading model or scaler: {str(e)}")
logger.error(f"Error loading model or scaler: {str(e)}")
return None
# Step 3: Extract features
try:
print("[DEBUG] Starting feature extraction...")
logger.info("Starting feature extraction...")
features = extract_features(audio_path)
if features is None:
print("[ERROR] Feature extraction returned None")
logger.error("Feature extraction returned None")
return None
# Verify feature dimensions
expected_features = 38 # Updated to match our current feature extraction (26 MFCC + 12 Chroma)
if len(features) != expected_features:
print(f"[ERROR] Incorrect number of features. Expected {expected_features}, got {len(features)}")
logger.error(f"Incorrect number of features. Expected {expected_features}, got {len(features)}")
return None
print(f"[DEBUG] Successfully extracted {len(features)} features")
logger.info(f"Successfully extracted {len(features)} features")
logger.debug(f"Feature values: {features}")
except Exception as e:
print(f"[ERROR] Error during feature extraction: {str(e)}")
logger.error(f"Error during feature extraction: {str(e)}")
logger.debug(f"Full traceback: {traceback.format_exc()}")
return None
# Step 4: Scale features
try:
print("[DEBUG] Scaling features...")
logger.info("Scaling features...")
features_scaled = scaler.transform(features.reshape(1, -1))
print("[DEBUG] Features scaled successfully")
logger.info("Features scaled successfully")
except Exception as e:
print(f"[ERROR] Error scaling features: {str(e)}")
logger.error(f"Error scaling features: {str(e)}")
return None
# Step 5: Make prediction
try:
print("[DEBUG] Making prediction...")
logger.info("Making prediction...")
probability = model.predict_proba(features_scaled)[0][1]
print(f"[DEBUG] Prediction successful: {probability:.2f}")
logger.info(f"Prediction successful: {probability:.2f}")
return probability
except Exception as e:
print(f"[ERROR] Error making prediction: {str(e)}")
logger.error(f"Error making prediction: {str(e)}")
return None
except Exception as e:
print(f"[ERROR] Unexpected error during prediction: {str(e)}")
logger.error(f"Unexpected error during prediction: {str(e)}")
return None
|