Madverse Music: AI Music Detector
Detect AI-generated music vs human-created music using advanced AI technology
#!/usr/bin/env python3 """ Madverse Music - Hugging Face Spaces Version Streamlit app for HF Spaces deployment """ import streamlit as st import torch import librosa import tempfile import os import time import numpy as np # Import the sonics library for model loading try: from sonics import HFAudioClassifier except ImportError: st.error("Sonics library not found. Please install it first.") st.stop() # Global model variable model = None # Page configuration st.set_page_config( page_title="Madverse Music: AI Music Detector", page_icon="🎵", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS st.markdown(""" """, unsafe_allow_html=True) @st.cache_resource def load_model(): """Load the model with caching for HF Spaces""" try: with st.spinner("Loading AI model... This may take a moment..."): # Use the same loading method as the working API model = HFAudioClassifier.from_pretrained("awsaf49/sonics-spectttra-alpha-120s") model.eval() return model except Exception as e: st.error(f"Failed to load model: {str(e)}") return None def process_audio(audio_file, model): """Process audio file and return classification""" try: # Save uploaded file temporarily with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file: tmp_file.write(audio_file.read()) tmp_path = tmp_file.name # Load audio (model uses 16kHz sample rate) audio, sr = librosa.load(tmp_path, sr=16000) # Convert to tensor and add batch dimension audio_tensor = torch.FloatTensor(audio).unsqueeze(0) # Get prediction using the same pattern as working API with torch.no_grad(): output = model(audio_tensor) # Convert logit to probability using sigmoid probability = torch.sigmoid(output).item() # Classify: prob < 0.5 = Real, prob >= 0.5 = Fake if probability < 0.5: classification = "Real" confidence = (1 - probability) * 2 # Convert to 0-1 scale else: classification = "Fake" confidence = (probability - 0.5) * 2 # Convert to 0-1 scale # Calculate duration duration = len(audio) / sr # Clean up os.unlink(tmp_path) return { 'classification': classification, 'confidence': min(confidence, 1.0), # Cap at 1.0 'probability': probability, 'raw_score': output.item(), 'duration': duration, 'success': True } except Exception as e: # Clean up on error if 'tmp_path' in locals(): try: os.unlink(tmp_path) except: pass return { 'success': False, 'error': str(e) } def main(): # Header st.markdown("""
Detect AI-generated music vs human-created music using advanced AI technology
Classification: {classification}
Confidence: {confidence:.1%}
Message: This appears to be human-created music!
Classification: {classification}
Confidence: {confidence:.1%}
Message: This appears to be AI-generated music!
Powered by Madverse Music | Built with Streamlit & PyTorch
This tool is for research and educational purposes. Results may vary depending on audio quality.