File size: 9,411 Bytes
f280f9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/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("""

<style>

    .main-header {

        background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);

        padding: 1rem;

        border-radius: 10px;

        color: white;

        text-align: center;

        margin-bottom: 2rem;

    }

    .result-box {

        padding: 1rem;

        border-radius: 10px;

        margin: 1rem 0;

        border-left: 5px solid;

    }

    .real-music {

        background-color: #d4edda;

        border-left-color: #28a745;

    }

    .fake-music {

        background-color: #f8d7da;

        border-left-color: #dc3545;

    }

</style>

""", 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("""

    <div class="main-header">

        <h1>Madverse Music: AI Music Detector</h1>

        <p>Detect AI-generated music vs human-created music using advanced AI technology</p>

    </div>

    """, unsafe_allow_html=True)
    
    # Sidebar
    with st.sidebar:
        st.markdown("### About")
        st.markdown("""

        This AI model can detect whether music is:

        - **Real**: Human-created music

        - **Fake**: AI-generated music (Suno, Udio, etc.)

        

        **Model**: SpecTTTra-α (120s)  

        **Accuracy**: 97% F1 score  

        **Max Duration**: 120 seconds

        """)
        
        st.markdown("### Supported Formats")
        st.markdown("- WAV (.wav)")
        st.markdown("- MP3 (.mp3)")
        st.markdown("- FLAC (.flac)")
        st.markdown("- M4A (.m4a)")
        st.markdown("- OGG (.ogg)")
        
        st.markdown("### Links")
        st.markdown("- [Madverse Website](https://madverse.co)")
        st.markdown("- [GitHub Repository](#)")
    
    # Load model
    model = load_model()
    
    if model is None:
        st.error("Model failed to load. Please refresh the page.")
        return
    
    st.success("AI model loaded successfully!")
    
    # File upload
    st.markdown("### Upload Audio File")
    uploaded_file = st.file_uploader(
        "Choose an audio file",
        type=['wav', 'mp3', 'flac', 'm4a', 'ogg'],
        help="Upload an audio file to analyze (max 120 seconds)"
    )
    
    if uploaded_file is not None:
        # Display file info
        st.markdown("### File Information")
        col1, col2, col3 = st.columns(3)
        
        with col1:
            st.metric("Filename", uploaded_file.name)
        with col2:
            st.metric("File Size", f"{uploaded_file.size / 1024:.1f} KB")
        with col3:
            st.metric("Format", uploaded_file.type)
        
        # Audio player
        st.markdown("### Preview")
        st.audio(uploaded_file)
        
        # Analysis button
        if st.button("Analyze Audio", type="primary", use_container_width=True):
            try:
                with st.spinner("Analyzing audio... This may take a few seconds..."):
                    # Reset file pointer
                    uploaded_file.seek(0)
                    
                    # Process audio
                    start_time = time.time()
                    result = process_audio(uploaded_file, model)
                    processing_time = time.time() - start_time
                
                if not result['success']:
                    st.error(f"Error processing audio: {result['error']}")
                    return
                
                # Display results
                st.markdown("### Analysis Results")
                
                classification = result['classification']
                confidence = result['confidence']
                
                # Result box
                if classification == "Real":
                    st.markdown(f"""

                    <div class="result-box real-music">

                        <h3>Result: Human-Created Music</h3>

                        <p><strong>Classification:</strong> {classification}</p>

                        <p><strong>Confidence:</strong> {confidence:.1%}</p>

                        <p><strong>Message:</strong> This appears to be human-created music!</p>

                    </div>

                    """, unsafe_allow_html=True)
                else:
                    st.markdown(f"""

                    <div class="result-box fake-music">

                        <h3>Result: AI-Generated Music</h3>

                        <p><strong>Classification:</strong> {classification}</p>

                        <p><strong>Confidence:</strong> {confidence:.1%}</p>

                        <p><strong>Message:</strong> This appears to be AI-generated music!</p>

                    </div>

                    """, unsafe_allow_html=True)
                
                # Detailed metrics
                with st.expander("Detailed Metrics"):
                    col1, col2, col3 = st.columns(3)
                    
                    with col1:
                        st.metric("Confidence", f"{confidence:.1%}")
                    with col2:
                        st.metric("Probability", f"{result['probability']:.3f}")
                    with col3:
                        st.metric("Processing Time", f"{processing_time:.2f}s")
                    
                    if result['duration'] > 0:
                        st.metric("Duration", f"{result['duration']:.1f}s")
                    
                    st.markdown("**Interpretation:**")
                    st.markdown("""

                    - **Probability < 0.5**: Classified as Real (human-created)

                    - **Probability ≥ 0.5**: Classified as Fake (AI-generated)

                    - **Confidence**: How certain the model is about its prediction

                    """)
                
            except Exception as e:
                st.error(f"Error processing audio: {str(e)}")
    
    # Footer
    st.markdown("---")
    st.markdown("""

    <div style="text-align: center; color: #666;">

        <p>Powered by <strong>Madverse Music</strong> | Built with Streamlit & PyTorch</p>

        <p>This tool is for research and educational purposes. Results may vary depending on audio quality.</p>

    </div>

    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()